Cloud
Cloud

Reputation: 19333

CMake - Getting list of source/header files for various subprojects

Background

I have a large project that makes use of dozens of subprojects: some from in-house code bases, and some third-party projects which also use CMake.

To ensure common compiler options, I setup a macro in CMake called CreateDevFlags which is run in only the in-house sub-projects own CMakeLists file as the first line of code to execute. This makes sure that I don't break the compiler flags, output directory overrides, etc, for third-party projects, and all of the code I wrote myself is built with identical options.

Additionally, each sub project has a simple block of code along the lines of the following to define the source files to be compiled:

file(GLOB subproject_1A_SRC
    "src/*.c"
)

file(GLOB subproject_1A_INC
    "inc/*.h"
)

file(GLOB subproject_2B_SRC
    "src/*.c"
"extra_src/*.c"
)

file(GLOB subproject_2B_INC
    "inc/*.h"
"extra_details_inc/*.h"
)

Goal

I would like to add a sanity-check custom rule/function to the "master" CMakeLists file at the project root which runs all of the code for in-house subprojects through a code sanitizer (checks newlines, enforces style rules, etc).


Question

Is there a trivial way to have all "special" (ie: in-house) subprojects append their own source files to a "master" list of source (.c) and header (.h) files (possibly via the macro I created)? I realize I could manually create this list in the master CMakeLists file, but then I'd be duplicating efforts, and code maintainers would have to modify code in two places with this in effect.

Thank you.

Upvotes: 1

Views: 1458

Answers (3)

Cloud
Cloud

Reputation: 19333

In the end. the following approach solved my problem:

set(DIR1_SRCS "file1.cpp" PARENT_SCOPE)

and then in ./CMakeLists.txt

set(SRCS ${DIR1_SRCS} ${DIR2_SRCS})

Upvotes: 1

netskink
netskink

Reputation: 4539

I suggest you don't examine header files. Instead use include dirs for the paths to the header files. If you do this you will automatically get the depends working without having to track them yourself.

Your sanitizer should be able to parse the actual code to find and read the included headers.

Upvotes: 0

TheKitchenSink
TheKitchenSink

Reputation: 136

One possible implementation would be to have a list called FILE_TRACKER defined at top scope for your project. Then, you could do something like

# Create local list to append to
set(LOCAL_LIST ${FILE_TRACKER})

# Append all of your source files, from your local source
foreach(SRC_FILE ${subproject_1A_SRC})
    list(APPEND LOCAL_LIST ${SRC_FILE})
endforeach()

# Append to the upper macro (note: was initially set with FILE_TRACKER)
set(FILE_TRACKER ${LOCAL_LIST} PARENT_SCOPE)

The developers would only have to add their source to the one list, and the macro at the top level will be updated with the files.

Upvotes: 1

Related Questions