3

Is it possible to have in CMakeLists.txt

add_subdirectory(foo)

combined with custom CMAKE_TOOLCHAIN_FILE which is to be used only for this foo subdirectory?

The idea is to use a different compiler / toolchain for the sources under foo directory regardless of which compiler is used for the rest of the project.

The point is to have some sort of solid solution which avoids wrapping cmake / make / etc invocation by a custom command (e.g. in order to avoid breaking IDE support, incremental builds, etc).

Possible use cases could be:

  • Cross-compiling modules for different target platforms as part of a single build system.
  • Cross-compilation of test binaries.
  • Testing this custom toolchain as a part of build process.
scrutari
  • 1,378
  • 2
  • 17
  • 33
  • You want to compile parts of code with different compilers? – KamilCuk Feb 19 '20 at 13:30
  • @KamilCuk, Yes, more specifically to build parts of code using specific toolchain and parameters of compiling, linking, etc. – scrutari Feb 19 '20 at 13:33
  • "Parameters of compiling and linking" are specified per target with `target_link_options` and `target_compile_options`. So... could you please clarify the "using specific toolchain" part? – KamilCuk Feb 19 '20 at 13:34
  • Sometimes it's better to keep code in subdirectories not modified. Plus the beauty of `CMAKE_TOOLCHAIN_FILE` is that the same code base can be used with different files with no alteration in sources. – scrutari Feb 19 '20 at 13:42
  • ? How does CMAKE_TOOLCHAIN_FILE is connected to altering source files? How is related to modifing the code in subdirectories? Why "sometimes"? In what circumstances related to `CMAKE_TOOLCHAIN_FILE` would you modify the code in subdirectories? I think you should modify the code only if you develop it - write a future, fix a bug, etc. How is it related to a toolchain file? – KamilCuk Feb 19 '20 at 13:45
  • I wouldn't modify sources in a subdirectory, but I want to build them with a specific `CMAKE_TOOLCHAIN_FILE` applied. – scrutari Feb 19 '20 at 13:55
  • What you want to achieve is certainly possible, but it's not as simple as you describe. See this [answer](https://stackoverflow.com/a/4898004/3987854), and these responses ([this](https://stackoverflow.com/a/36084786/3987854) and [that](https://stackoverflow.com/a/9545369/3987854)) may be useful. – Kevin Feb 19 '20 at 14:10
  • For those who has problems with (re)building of external projects upon update to their sources: https://stackoverflow.com/a/51237377/547270 (ans ugly but working solutions). – scrutari Feb 19 '20 at 16:07
  • 1
    This question is unanswered and it is not a duplicate as claimed in the close vote. It is a /totally different thing/ to change the compiler used and change the CMAKE_TOOLCHAIN_FILE variable. For example, the dependency project can define its own toolchain file implementations, while the solution provided in the close vote "duplicate" only works if you know the compiler location in the parent/dependent project. – BadZen Feb 21 '21 at 06:22

0 Answers0