leetal / ios-cmake

A CMake toolchain file for iOS/iPadOS, visionOS, macOS, watchOS & tvOS C/C++/Obj-C++ development
BSD 3-Clause "New" or "Revised" License
1.9k stars 450 forks source link

Error "invalid argument '-mmacosx-version-min=9.0'" #69

Closed snej closed 4 years ago

snej commented 4 years ago

I'm using CMake with ios-cmake (3.1.0) inside an Xcode Run Script build phase, to build a 3rd party static library my target links with. However, Clang immediately fails with:

clang: error: invalid argument '-mmacosx-version-min=9.0' not allowed with '-mios-simulator-version-min=9.0'

The problem appears to be caused by this statement in ios.toolchain.cmake:

https://github.com/leetal/ios-cmake/blob/8f7c16a462d17eba7124aba921153382605df288/ios.toolchain.cmake#L545-L546

If I comment out those two lines, everything builds fine. It seems wrong for the script to be forcing a macOS deployment target when it's not building for macOS, but somehow I'm the only one having trouble as a result...

Here's the full CMake output:

CMake options:                  -DCMAKE_TOOLCHAIN_FILE=vendor/ios-cmake/ios.toolchain.cmake                 -DPLATFORM=SIMULATOR64             -DENABLE_PROGRAMS=0             -DENABLE_TESTING=0 -DCMAKE_BUILD_TYPE=Debug -DLWS_WITH_ASAN=1
-- Building with Xcode version: 11.2.1
-- Configuring iphonesimulator build for platform: SIMULATOR64, architecture(s): x86_64
-- Using SDK: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.2.sdk for platform: SIMULATOR64
-- Using the default min-version since DEPLOYMENT_TARGET not provided!
-- Disabling bitcode support by default on simulators. ENABLE_BITCODE not provided for override!
-- Enabling ARC support by default. ENABLE_ARC not provided!
-- Hiding symbols visibility by default. ENABLE_VISIBILITY not provided!
-- Using NON-strict compiler checks by default. ENABLE_STRICT_TRY_COMPILE not provided!
-- Using C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- Using CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-- Using libtool: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool
-- Using install_name_tool: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool
-- Using a data_ptr size of 8
-- Building for minimum iphonesimulator version: 9.0 (SDK version: 13.2)
-- Version flags set to: -mios-simulator-version-min=9.0
-- Disabling bitcode support.
-- Enabling ARC support.
-- Hiding symbols (-fvisibility=hidden).
-- Building with Xcode version: 11.2.1
-- Configuring iphonesimulator build for platform: SIMULATOR64, architecture(s): x86_64
-- Using SDK: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.2.sdk for platform: SIMULATOR64 when checking compatibility
-- Using libtool: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool
-- Using a data_ptr size of 8
-- Building for minimum iphonesimulator version: 9.0 (SDK version: 13.2)
-- Version flags set to: -mios-simulator-version-min=9.0
-- Disabling bitcode support.
-- Enabling ARC support.
-- Hiding symbols (-fvisibility=hidden).
-- The C compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -- broken
CMake Error at /usr/local/Cellar/cmake/3.9.1/share/cmake/Modules/CMakeTestCCompiler.cmake:51 (message):
  The C compiler
  "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
  is not able to compile a simple test program.

  It fails with the following output:

   Change Dir: /Users/snej/Couchbase/CBL/couchbase-lite-ios/build/CouchbaseLite/Build/Intermediates.noindex/LiteCore.build/Debug-iphonesimulator/mbedTLS.build/Objects/mbedtls/CMakeFiles/CMakeTmp

  Run Build Command:"/Applications/Xcode.app/Contents/Developer/usr/bin/make"
  "cmTC_823d2/fast"

  /Applications/Xcode.app/Contents/Developer/usr/bin/make -f
  CMakeFiles/cmTC_823d2.dir/build.make CMakeFiles/cmTC_823d2.dir/build

  Building C object CMakeFiles/cmTC_823d2.dir/testCCompiler.c.o

  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
  -mios-simulator-version-min=9.0 -fobjc-abi-version=2 -fobjc-arc
  -mios-simulator-version-min=9.0 -fobjc-abi-version=2 -fobjc-arc -arch
  x86_64 -isysroot
  /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.2.sdk
  -mmacosx-version-min=9.0 -o CMakeFiles/cmTC_823d2.dir/testCCompiler.c.o -c
  /Users/snej/Couchbase/CBL/couchbase-lite-ios/build/CouchbaseLite/Build/Intermediates.noindex/LiteCore.build/Debug-iphonesimulator/mbedTLS.build/Objects/mbedtls/CMakeFiles/CMakeTmp/testCCompiler.c

  clang: error: invalid argument '-mmacosx-version-min=9.0' not allowed with
  '-mios-simulator-version-min=9.0'
snej commented 4 years ago

My colleague @borrrden has found that building our entire program for iOS with CMake doesn't produce these errors; they only happen when invoking CMake inside an Xcode build. So it may be that Clang is picking up an environment variable set by Xcode and getting mixed up...

snej commented 4 years ago

I think the actual problem may be here: https://github.com/leetal/ios-cmake/blob/8f7c16a462d17eba7124aba921153382605df288/ios.toolchain.cmake#L461-L464 We're on CMake 3.9.1, so we hit this line because MODERN_CMAKE is false. Setting CMAKE_SYSTEM_NAME to Darwin tells CMake this is a macOS build, hence CMake emits a -mmacosx-version-min flag.

It appears the logic for setting CMAKE_SYSTEM_NAME needs to happen on older versions of CMake too, at least back to 3.9.1. I've modified the script as follows and it works now:

diff --git a/ios.toolchain.cmake b/ios.toolchain.cmake
index 170f1fb..4842572 100644
--- a/ios.toolchain.cmake
+++ b/ios.toolchain.cmake
@@ -442,25 +442,22 @@ execute_process(COMMAND uname -r
   OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION
   ERROR_QUIET
   OUTPUT_STRIP_TRAILING_WHITESPACE)
-# CMake 3.14+ support building for iOS, watchOS and tvOS out of the box.
-if(MODERN_CMAKE)
-  if(SDK_NAME MATCHES "iphone")
-    set(CMAKE_SYSTEM_NAME iOS CACHE INTERNAL "" ${FORCE_CACHE})
-  elseif(SDK_NAME MATCHES "appletv")
-    set(CMAKE_SYSTEM_NAME tvOS CACHE INTERNAL "" ${FORCE_CACHE})
-  elseif(SDK_NAME MATCHES "watch")
-    set(CMAKE_SYSTEM_NAME watchOS CACHE INTERNAL "" ${FORCE_CACHE})
-  endif()

+if(SDK_NAME MATCHES "iphone")
+  set(CMAKE_SYSTEM_NAME iOS CACHE INTERNAL "" ${FORCE_CACHE})
+elseif(SDK_NAME MATCHES "appletv")
+  set(CMAKE_SYSTEM_NAME tvOS CACHE INTERNAL "" ${FORCE_CACHE})
+elseif(SDK_NAME MATCHES "watch")
+  set(CMAKE_SYSTEM_NAME watchOS CACHE INTERNAL "" ${FORCE_CACHE})
+endif()
+
+if(MODERN_CMAKE)
   # Provide flags for a combined FAT library build on newer CMake versions
   if(PLATFORM_INT MATCHES ".*COMBINED")
     set(CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH NO CACHE INTERNAL "" ${FORCE_CACHE})
     set(CMAKE_IOS_INSTALL_COMBINED YES CACHE INTERNAL "" ${FORCE_CACHE})
     message(STATUS "Will combine built (static) artifacts into FAT lib...")
   endif()
-else()
-  # Legacy code path prior to CMake 3.14
-  set(CMAKE_SYSTEM_NAME Darwin CACHE INTERNAL "" ${FORCE_CACHE})
 endif()
 # Standard settings.
 set(CMAKE_SYSTEM_VERSION ${SDK_VERSION} CACHE INTERNAL "")
leetal commented 4 years ago

Thanks for these findings! I'll see if I can reproduce this tomorrow :) I finally have some time over to give the toolchain some love again.

borrrden commented 4 years ago

One thing to note is that our project will set the CMAKE_OSX_SYSROOT to macosx if it is not set. I'm not sure if this will affect the toolchain or not (I thought that all of that was settled before starting the CMakeLists.txt parsing). The official iOS support for CMake seemed to choke on this and report to me that "macosx is not an iOS SDK" but this toolchain does not have the same issue.

leetal commented 4 years ago

I have now spent some time digging through old CMake docs and come to the conclusion that older (uncertain of HOW old), the "Darwin" option was sufficient, but at least on 3.8, specifying "iOS" indeed seems to be the correct way. I'll modify the toolchain with the changes provided since they seem to be more correct. Of course won't "appletv" or "watch" work on older CMake versions, but that error is up to the user to handle in that case. CMake will throw descriptive errors in those cases.

leetal commented 4 years ago

I have now pushed some changes to master. Please test with that and see if that resolves your issues.

leetal commented 4 years ago

Closing due to lack of activity