ccmake

CMake add link_libraries and include_directories to single file based on platform target


I have a cmake based C project for which I compile both for host and for target. I have the following project structure:

root\
- app-cpu1\
-- src\
-- inc\
-- test\
- app-cpu2\
-- src\
-- inc\
-- test\
- target\
-- linux\
--- src\
---- runner.c
-- platform\
--- platform-toolchain-file.cmake
--- src\
---- runner.c

The idea behind this structure is that all target independent code wit within the app-cpu# directories and all target specific code is within the target/x directories.

Within the folder app-cpu1 I have a CMakeLists.txt file with something as follows:

# app-cpu1/CMakeLists.txt
add_executable(CPU1-app
  src/.. src files .ext
  ${CMAKE_SOURCE_DIR}/{TARGET_DIRECTORY}/src/runner.c
)

Depending on the configuration, either the linux or the platform runner.c is included. To run the application on the host or the platform target.

However, when I include the platform/src/runner.c I require additional link_libraries and INCLUDE_DIRECTORIES for my target to setup the HAL required for that target.

I have tried the set_source_files_properties() for this in the following way.

# target/platform/CMakeLists.txt

set_source_files_properties(src/runner.c INCLUDE_DIRECTORIES
    additional/include/directories)

set_source_files_properties(src/runner.c LINK_LIBRARIES
    additional/link/libraries)

However it appears this has no effect, the additional directories are not added to the compile instructions.

My questions:

  1. Is this the correct usage of set_source_files_properties() or am I missing something.
  2. Would this be the best approach for this goal within CMake, or would another approach be better suited.

Thanks in advance for any feedback.


Solution

  • In this case, the solution direction commented by @Tsyvarev was chosen.

    The following alterations have been made to the solution above.

    # app-cpu1/CMakeLists.txt
    add_executable(CPU1-app
      src/.. src files .ext
    )
    target_link_libraries(CPU1-app
        runnerLibrary
    )
    
    # target/platform/CMakeLists.txt
    
    add_library(runnerLibrary STATIC
        src/runner.c
    )
    target_include_directories(runnerLibrary PRIVATE
        additional/include/directories)
    target_link_libraries(runnerLibrary PRIVATE
        additional/link/libraries)
    

    This solution made more sense that the initial proposed solution in the question:

    1. Dependencies are more clear, that they are on a specific library rather than a file.

    2. Library dependencies can be made clear rather than single file dependencies.