ObjectiveC: -arch flag missing for OBJC targets
While trying to build a Mac app with -DCMAKE_OSX_ARCHITECTURES='arm64;x86_64', I noticed the resulting binary wasn't a universal binary as desired. I've narrowed it down to missing -arch
flags when compiling OBJC source files (OBJCXX works fine however). Example:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(example OBJC OBJCXX)
add_executable(objc-example example.m)
add_executable(objcxx-example example.mm)
example.m and example.mm:
int main(void) {}
Commands and output:
$ cmake -DCMAKE_OSX_ARCHITECTURES='arm64;x86_64'
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.
-- The OBJC compiler identification is AppleClang 12.0.5.12050022
-- The OBJCXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting OBJC compiler ABI info
-- Detecting OBJC compiler ABI info - done
-- Check for working OBJC compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang - skipped
-- Detecting OBJCXX compiler ABI info
-- Detecting OBJCXX compiler ABI info - done
-- Check for working OBJCXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ - skipped
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/dostende/Dropbox/Code/cmake-bug
$ make VERBOSE=1
/Applications/CMake.app/Contents/bin/cmake -S/Users/dostende/Dropbox/Code/cmake-bug -B/Users/dostende/Dropbox/Code/cmake-bug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CMake.app/Contents/bin/cmake -E cmake_progress_start /Users/dostende/Dropbox/Code/cmake-bug/CMakeFiles /Users/dostende/Dropbox/Code/cmake-bug//CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/objcxx-example.dir/build.make CMakeFiles/objcxx-example.dir/depend
cd /Users/dostende/Dropbox/Code/cmake-bug && /Applications/CMake.app/Contents/bin/cmake -E cmake_depends "Unix Makefiles" /Users/dostende/Dropbox/Code/cmake-bug /Users/dostende/Dropbox/Code/cmake-bug /Users/dostende/Dropbox/Code/cmake-bug /Users/dostende/Dropbox/Code/cmake-bug /Users/dostende/Dropbox/Code/cmake-bug/CMakeFiles/objcxx-example.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/objcxx-example.dir/build.make CMakeFiles/objcxx-example.dir/build
[ 25%] Building OBJCXX object CMakeFiles/objcxx-example.dir/example.mm.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -x objective-c++ -arch arm64 -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -MD -MT CMakeFiles/objcxx-example.dir/example.mm.o -MF CMakeFiles/objcxx-example.dir/example.mm.o.d -o CMakeFiles/objcxx-example.dir/example.mm.o -c /Users/dostende/Dropbox/Code/cmake-bug/example.mm
[ 50%] Linking OBJCXX executable objcxx-example
/Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/objcxx-example.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch arm64 -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/objcxx-example.dir/example.mm.o -o objcxx-example
[ 50%] Built target objcxx-example
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/objc-example.dir/build.make CMakeFiles/objc-example.dir/depend
cd /Users/dostende/Dropbox/Code/cmake-bug && /Applications/CMake.app/Contents/bin/cmake -E cmake_depends "Unix Makefiles" /Users/dostende/Dropbox/Code/cmake-bug /Users/dostende/Dropbox/Code/cmake-bug /Users/dostende/Dropbox/Code/cmake-bug /Users/dostende/Dropbox/Code/cmake-bug /Users/dostende/Dropbox/Code/cmake-bug/CMakeFiles/objc-example.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/objc-example.dir/build.make CMakeFiles/objc-example.dir/build
[ 75%] Building OBJC object CMakeFiles/objc-example.dir/example.m.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -MD -MT CMakeFiles/objc-example.dir/example.m.o -MF CMakeFiles/objc-example.dir/example.m.o.d -o CMakeFiles/objc-example.dir/example.m.o -c /Users/dostende/Dropbox/Code/cmake-bug/example.m
[100%] Linking OBJC executable objc-example
/Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/objc-example.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/objc-example.dir/example.m.o -o objc-example
[100%] Built target objc-example
/Applications/CMake.app/Contents/bin/cmake -E cmake_progress_start /Users/dostende/Dropbox/Code/cmake-bug/CMakeFiles 0
$ file objc-example
objc-example: Mach-O 64-bit executable arm64
$ file objcxx-example
objcxx-example: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64:Mach-O 64-bit executable arm64]
objcxx-example (for architecture x86_64): Mach-O 64-bit executable x86_64
objcxx-example (for architecture arm64): Mach-O 64-bit executable arm64
I believe it's because of a typo here:
lang == "OBJ"
should be lang == "OBJC"
.
Edited by Brad King