[CMake] Cross compilation and native executables

Brad King brad.king at kitware.com
Wed Jan 27 09:44:33 EST 2010


Michael Wild wrote:
> On 26. Jan, 2010, at 21:28 , Guillaume Duhamel wrote:
>> On the other hand, I've found out about the ExternalProject module in the
>> CMake documentation, could it be used to create a native-only CMake
>> sub-project?

Yes.

>> What I would really like is some way to have all compilation work done with
>> only one CMake + make invocation ; is there any way to achieve that?
>
> Although I don't have any experience with cross-compiling and toolchain files,
> I remember that there was a discussion some weeks ago where ExternalProject
> indeed was brought up as a possibility to achieve what you need.
> 
> The only tricky part is to infer the output location of the sub-projects
> executable and then do a
> 
> add_executable(XXX IMPORTED)
> set_target_properties(XXX PROPERTIES IMPORTED_LOCATION YYY)
> 
> The tricky part in determining the output location is that you need to
> consider CMAKE_EXECUTABLE_SUFFIX and CMAKE_CFG_INTDIR when the user uses a
> multi-config IDE such as Xcode or VisualStudio.

There are a few ways around this.  CMake has the export() and install(EXPORT)
commands capable of generating the imported target code automatically when
the subproject is configured.  The key is to find a way to get the subproject
to configure before/during the main project configuration.

Here are three approaches:

(1)
ExternalProject does not support this directly.  However, you can create one
"superproject" that builds *both* the native and cross-compiled projects as
external projects.  From the CMake->make workflow perspective it will look
similar to one build.

(2)
Use try_compile to build the entire host-only project during the CMake
configuration step of the main project.  This will work well if the host-only
project is not too big.

(3)
Use execute_process() to run the CMake command line tool and configure the
host-only project during the configuration step of the main project.  This
will make the results of the export() command from the subproject available
for loading by the main project.  Then add a custom target to drive the
subproject build during the main project build using "cmake --build".

-Brad


More information about the CMake mailing list