0

I would like to change the compile settings in a "subproject" in Cmake instantiated with ExternalProject_Add (ExternalProject — CMake Documentation). How could I do that?

As an example: in pico-sdk, I usually have include(pico_sdk_import.cmake) in my own project CMakeLists.txt, which eventually loads the file FindPioasm.cmake, which has this:

    set(PioasmBuild_TARGET PioasmBuild)
    set(Pioasm_TARGET Pioasm)

    if (NOT TARGET ${PioasmBuild_TARGET})
        pico_message_debug("PIOASM will need to be built")
#        message("Adding external project ${PioasmBuild_Target} in ${CMAKE_CURRENT_LIST_DIR}}")
        ExternalProject_Add(${PioasmBuild_TARGET}
                PREFIX pioasm SOURCE_DIR ${PIOASM_SOURCE_DIR}
                BINARY_DIR ${PIOASM_BINARY_DIR}
                BUILD_ALWAYS 1 # force dependency checking
                INSTALL_COMMAND ""
                CMAKE_CACHE_ARGS "-DPIOASM_EXTRA_SOURCE_FILES:STRING=${PIOASM_EXTRA_SOURCE_FILES}"
                )
    endif()

When I eventually run make VERBOSE=1 PioasmBuild, I eventually get the compile command line:

...
[ 10%] Building CXX object CMakeFiles/pioasm.dir/main.cpp.obj
/path/to/g++.exe   @CMakeFiles/pioasm.dir/includes_CXX.rsp -std=gnu++11 -o CMakeFiles/pioasm.dir/main.cpp.obj -c /path/to/pico-sdk/tools/pioasm/main.cpp
...

So, I would like to add options to this compiler command line, like -Wall or -DDEFINE, per source file - or if that is not possible, for all compiler invocations for files in the external project...

For the "normal"/actual CMake project, so far I have used set_source_files_properties(/path/to/file.cpp PROPERTIES COMPILE_FLAGS "-DDEFINE") with great success; however, probably because it is an external project, set_source_files_properties(${PICO_SDK_PATH}/tools/pioasm/main.cpp PROPERTIES COMPILE_FLAGS "-DDEFINE") does not seem to make any difference at all.

So, how can I change the compile options/settings/properties in CMake for files defined in a CMake ExternalProject? And would I do those changes before or after the include(pico_sdk_import.cmake) line (which ultimately "includes" the external project) in my project's CMakeLists.txt file?

starball
  • 20,030
  • 7
  • 43
  • 238
sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • 1
    Configuration step for `ExternalProject_Add` effectively calls `cmake` from the command line. So most solutions for [passing compiler parameters from CMake command line](https://stackoverflow.com/q/44284275/3440745) should be usable with `ExternalProject_Add` too (except, probably, approaches with setting environment variables). – Tsyvarev May 30 '23 at 22:48

1 Answers1

1

ExternalProject is for buildsystems that are separate from the current configuration. Even if the external project uses CMake, things are very much separate. Variables, targets, properties, etc. that are visible in the current configuration will not be automatically visible to the external project's configuration.

To pass configure compile flags for all source files of an external project, you can to use whichever of CONFIGURE_COMMAND, BUILD_COMMAND, CMAKE_ARGS, or CMAKE_CACHE_ARGS is appropriate. For CMake-driven external projects, you can use CMAKE_<LANG>_FLAGS.

I'm not aware of a nice way to set compile flags for only specific source files of external projects. I think you might have to modify the CMake configuration files of the external project itself. If something like https://gitlab.kitware.com/cmake/cmake/-/issues/21986 gets implemented in the future and the external project uses CMake, you might be able to use generator expressions in CMAKE_<LANG>_FLAGS to make a generator expression only get expanded for a particular source file based on its LOCATION property.

starball
  • 20,030
  • 7
  • 43
  • 238
  • Many thanks, this answer and @Tsyvarev's comment helped me a lot. Ultimately, I realized that after the end of the first `cmake` run (in an empty build folder), an empty subdirectory gets created for the external project - so without any CMake configuration files for the external project; one has to run the build step from the main Makefile to create them. So I settled for a message from the end of the main CMakeLists, which tells me to manually run that step, and then do `cmake -DCMAKE_CXX_FLAGS='-Wall` to change setting in the subdir of external project. – sdbbs May 31 '23 at 14:21