Alex Barker
Alex Barker

Reputation: 4400

How do I use CMake on OS X, target the new Arm M1

I have some dependencies that use cmake I need to compile and I am not sure I am doing this correctly. I am currently using -march=x86-64 in my CFLAGS for amd64 which seems to work, but I'm not sure that's correct because it is not working when I try targeting the new M1. Is there something else I should be doing to target different architectures with cmake from the command line?

export CFLAGS="-O2 -march=aarch64 -fomit-frame-pointer -fno-stack-protector -pipe"

cmake -B ${{github.workspace}}/build \
    -G "Unix Makefiles" \
    -D CMAKE_INSTALL_PREFIX=${{github.workspace}}/dist/darwin/arm64 \
    -D CMAKE_VERBOSE_MAKEFILE=ON \
    -D BUILD_SHARED_LIBS=ON \
    -D BUILD_DEMO=ON

cmake --build ${{github.workspace}}/build \
    --parallel 2 \
    --config RelWithDebInfo \
    --clean-first

cmake --install ${{github.workspace}}/build --config RelWithDebInfo

Update: I am seeing this error.

CMake Error at /usr/local/Cellar/cmake/3.18.4/share/cmake/Modules/CMakeTestCCompiler.cmake:66 (message):
  The C compiler

"/Applications/Xcode_12.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"

  is not able to compile a simple test program.

  It fails with the following output:

-- Configuring incomplete, errors occurred!
See also "/Users/runner/work/project/project/build/CMakeFiles/CMakeOutput.log".

error: unknown target CPU 'aarch64'
note: valid target CPU values are: nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont, goldmont-plus, tremont, nehalem, corei7, westmere, sandybridge, corei7-avx, ivybridge, core-avx-i, haswell, core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake, cooperlake, cannonlake, icelake-client, icelake-server, tigerlake, knl, knm, k8, athlon64, athlon-fx, opteron, k8-sse3, athlon64-sse3, opteron-sse3, amdfam10, barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znver1, znver2, x86-64
make[1]: *** [CMakeFiles/cmTC_72653.dir/testCCompiler.c.o] Error 1
make: *** [cmTC_72653/fast] Error 2

Upvotes: 4

Views: 7603

Answers (2)

Alex Reinking
Alex Reinking

Reputation: 20026

Use the CMAKE_OSX_ARCHITECTURES variable, as I detail in this answer. Setting it to arm64 is sufficient.

Upvotes: 2

Alex Barker
Alex Barker

Reputation: 4400

It was as simple as adding the CFLAG -target arm64-apple-macos10.5.

Upvotes: 1

Related Questions