conan-io / cmake-conan

CMake wrapper for conan C and C++ package manager
MIT License
822 stars 250 forks source link

Incorrect invocation of AppleClang compiler for detecting libcxx. #296

Open DmitrySokolov opened 3 years ago

DmitrySokolov commented 3 years ago

The code

execute_process(
        COMMAND ${CMAKE_COMMAND} -E echo "#include <string>"
        COMMAND ${CMAKE_CXX_COMPILER} -x c++ ${compile_options} -E -dM -
        OUTPUT_VARIABLE string_defines
    )

causes the following output

-- Conan: Automatic detection of conan settings from cmake
In file included from <stdin>:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:504:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string_view:175:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__string:57:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:641:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstring:60:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string.h:60:15: fatal error: 'string.h' file not found
#include_next <string.h>
              ^~~~~~~~~~
1 error generated.
-- Conan: Settings= -s;build_type=Debug;-s;compiler=apple-clang;-s;compiler.version=12.0;-s;compiler.libcxx=libc++

-isysroot should be always specified for AppleClang, see also https://stackoverflow.com/a/63188875.

Fix:

    if(${CMAKE_${LANGUAGE}_COMPILER_ID} STREQUAL AppleClang)
        set(compile_options -isysroot ${CMAKE_OSX_SYSROOT} ${compile_options})
    endif()
    execute_process(
        COMMAND ${CMAKE_COMMAND} -E echo "#include <string>"
        COMMAND ${CMAKE_CXX_COMPILER} -x c++ ${compile_options} -E -dM -
        OUTPUT_VARIABLE string_defines
    )
DmitrySokolov commented 3 years ago

Patch for macOS and iOS:

--- conan.cmake
+++ conan.cmake
@@ -264,6 +264,13 @@
         endif()
     endforeach()

+    if(${CMAKE_${LANGUAGE}_COMPILER_ID} STREQUAL AppleClang)
+        set(compile_options -isysroot ${CMAKE_SYSROOT} ${compile_options})
+        if(${CMAKE_SYSTEM_NAME} STREQUAL iOS)
+            set(compile_options ${compile_options} -target arm64-apple-ios${CMAKE_OSX_DEPLOYMENT_TARGET})
+        endif()
+    endif()
+
     execute_process(
         COMMAND ${CMAKE_COMMAND} -E echo "#include <string>"
         COMMAND ${CMAKE_CXX_COMPILER} -x c++ ${compile_options} -E -dM -
czoido commented 3 years ago

Hi @DmitrySokolov, I think this should have already been fixed by https://github.com/conan-io/cmake-conan/pull/226 that is merged to develop and waiting to be release in 0.16, could you check if that PR fixed the issue? Thanks a lot

DmitrySokolov commented 3 years ago

Nope. #226 fixes only macOS platform. iOS is still broken. Moreover, CMAKE_OSX_SYSROOT=iphoneos for iOS, it's not a path to SDK.

1) The correct variable is CMAKE_SYSROOT, but this single fix is not enough 2) Another mandatory option is -target ...

DmitrySokolov commented 3 years ago

Hmm... macOS reguires CMAKE_OSX_SYSROOT, so the patch is

--- conan.cmake
+++ conan.cmake
@@ -264,6 +264,14 @@
         endif()
     endforeach()

+    if(${CMAKE_${LANGUAGE}_COMPILER_ID} STREQUAL AppleClang)
+        if(${CMAKE_SYSTEM_NAME} STREQUAL iOS)
+            set(compile_options -isysroot ${CMAKE_SYSROOT} -target arm64-apple-ios${CMAKE_OSX_DEPLOYMENT_TARGET} ${compile_options})
+        else()
+            set(compile_options -isysroot ${CMAKE_OSX_SYSROOT} ${compile_options})
+        endif()
+    endif()
+
     execute_process(
         COMMAND ${CMAKE_COMMAND} -E echo "#include <string>"
         COMMAND ${CMAKE_CXX_COMPILER} -x c++ ${compile_options} -E -dM -
ink-splatters commented 3 years ago

Hi, hasn't it been merged?