dolik-rce / pegof

PEG grammar optimizer and formatter
Other
9 stars 2 forks source link

CMake: use aux_source_directory command to avoid writing the list of source files #24

Open leleliu008 opened 1 month ago

leleliu008 commented 1 month ago

Reference: https://cmake.org/cmake/help/latest/command/aux_source_directory.html

dolik-rce commented 1 month ago

I don't think this is correct use for aux_source_directory... Even the reference you linked mentions, that it causes trouble, when it's used to find files to compile:

It is tempting to use this command to avoid writing the list of source files for a library or executable target. While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added.

leleliu008 commented 1 month ago

when adding a new source file, you just need to rerun cmake -S . -B build.d to reconfigure, you don't need to add it to source list in CMakeLists.txt

dolik-rce commented 1 month ago

After some more digging I have found that file(GLOB ... CONFIGURE_DEPENDS) should work even without the need to manually rerun cmake -B when files are changed. It requires cmake 3.12, but I see no problem in upgrading the dependency. The docs say it is still not recommended to use this feature, but at it seems to work well, at least with common generators.

Docs: https://cmake.org/cmake/help/latest/command/file.html#glob

It is also simpler then using aux_source_directory multiple times, as it can gather all the files at once:

file(
    GLOB_RECURSE sources
    CONFIGURE_DEPENDS 
    LIST_DIRECTORIES false 
    RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} 
    src/*.c src/*.cc ${CMAKE_CURRENT_BINARY_DIR}/version.cc
)