1

Is there a way to configure CMake such that a single make command will launch both host and target builds? I have a project that is meant to be run on a target platform but requires some generated data from a generator that needs to run on the host platform (PC). From CMake's documentation and looking around, I need a 2 pass build - first host to create the generator then the target crosscompiling build. My question is specifically how to launch the 2-pass build with a single make command through CMake configurations (not using an external bash or python script).

Things I know

  1. CMake doesn't allow changing toolchain within a single build
  2. Almost solutions like: How to instruct CMake to use the build architecture compiler? Which is fine except I need to skip the 2nd step of using an external script
  3. How to separate what's run during crosscompiling or not cmake - compile natively and crosscompile the same code
  4. It is supposedly possible to launch multiple CMake builds using CMake How to make CMake targeting multiple plattforms in a single build
Community
  • 1
  • 1
LemonPi
  • 1,026
  • 9
  • 22
  • You can always bend CMake and `make` to basically do whatever you want. I would just question if it is worth the effort and if anybody looking at your solution is still able to understand the inner workings and/or to find possible problems. For example just think about the `stdout`/`stderr` output in one of the possible scenarios where you always go for crosscompiling and inside do call `cmake` again for a host build via `add_custom_command()` calls. – Florian Feb 28 '17 at 11:42

1 Answers1

2

Turning my comment into an answer

Let's say you have an buildTool sub-directory for the host build tools:

buildTools\CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(BuildTools)

add_executable(genfoo genfoo.cpp)

Then you could have a main:

CMakeLists.txt

if (CMAKE_CROSSCOMPILING)
    set(_genfoo "bin/genfoo${CMAKE_EXECUTABLE_SUFFIX}")
    add_custom_command(
        OUTPUT ${_genfoo}
        COMMAND ${CMAKE_COMMAND} 
                    -DCMAKE_BUILD_TYPE:STRING="Release"
                    -DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE:PATH="${CMAKE_BINARY_DIR}/bin"
                    -B"bin"
                    -H"${CMAKE_SOURCE_DIR}/buildTools"
        COMMAND ${CMAKE_COMMAND} --build bin --config Release
    )
else()
    set(_genfoo genfoo)
    add_subdirectory(buildTools)
endif()

add_custom_command(
    OUTPUT foo.h
    COMMAND ${_genfoo}
    DEPENDS ${_genfoo}
)

add_executable(bar bar.cpp foo.h)
target_include_directories(bar PRIVATE ${CMAKE_BINARY_DIR})

References

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • Great! Could you elaborate on the `-B` and `-H` options and the `bin` param to `--build`? Can't find it in `cmake --help`. Where are they documented? – Flamefire Nov 06 '17 at 23:20
  • However I found a problem: `${CMAKE_EXECUTABLE_SUFFIX}"` is the current suffix. When compiling for windows on linux this will be (IMO) ".exe" but the generated file file not have that suffix as it is compiled on linux. – Flamefire Nov 06 '17 at 23:46