OSX+ExternalProject_Add(): passing compiler selection down to external projects
We have a cmake project that uses ExternalProject_Add() to build supporting software that uses a mix of cmake and autotools. When configuring this, we want the compiler selection (e.g. ${CMAKE_C_COMPILER}) to be passed down to all the external projects like this:
cmake_minimum_required (VERSION 3.0) project (testin C CXX) include (ExternalProject) set (COMPILER CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER}) ExternalProject_Add (libtool URL http://ftp.gnu.org/gnu/libtool/libtool-2.4.6.tar.gz CONFIGURE_COMMAND /configure ${COMPILER} --prefix=${CMAKE_INSTALL_PREFIX} UPDATE_COMMAND "")
Note the use of ${COMPILER} to configure CC and CXX with the autotool's "configure" script command line (for cmake-based projects we use ExternalProject_Add's CMAKE_CACHE_ARGS to pass down CMAKE_C_COMPILER in the cache variables). This works on bsd and linux, but not on OSX. Try the above on OSX and it will fail in the "configure" script.
The problem with OSX is that cmake is setting CMAKE_C_COMPILER to a compiler binary that does not have the correct implicit include paths and then internally adds a hidden "-isysroot" flag to fix that. This results in a CMAKE_C_COMPILER that cannot find stdio.h by itself (it needs that -isysroot flag). For example:
> cat conftest.c #include <stdin.h> int main() { } > /usr/bin/cc -c conftest.c > /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -c conftest.c conftest.c:1:10: fatal error: 'stdio.h' file not found #include <stdin.h> ^~~~~~~~~ 1 error generated. >
If I configure with "-DCMAKE_C_COMPILER=/usr/bin/cc" on OSX to override the default setting of "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc" then it works.
How can I pass down a working CC and CXX setting that matches CMAKE_C_COMPILER/CMAKE_CXX_COMPILER down to the autotools configure script on OSX?