A method of writing that can handle file name changes.

The following code requires a cmake --build build command to build if the filename is changed. However, emptying and regenerating the build directory is cumbersome, so I’ve assigned a single command to a shortcut key for automation.

Problem: Each run takes over 5 seconds to build, slowing down development.

What I want to know: How can I automate the build process with a single, perfect command, even when changing filenames or other modifications?

"cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug && cmake --build build && ./build/app"

CMakeLists

cmake_minimum_required(VERSION 3.15)

# compiler
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_C_COMPILER "clang")

# project
set(PROJECT_NAME "app")


set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(PROJECT_VERSION "1.0.0")
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION})

#####################################################################
# test project
#####################################################################

set(EXE_NAME "app")
set(EXE_SOURCE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(EXE_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include")

file(GLOB_RECURSE EXE_SOURCE_LIST CONFIGURE_DEPENDS "${EXE_SOURCE_PATH}/*")


add_executable(${EXE_NAME} ${EXE_SOURCE_LIST})
target_include_directories(${EXE_NAME} PRIVATE "${EXE_INCLUDE_PATH}")

target_link_libraries(${EXE_NAME} PRIVATE "raylib")
target_link_libraries(${EXE_NAME} PRIVATE "box2d")
target_link_libraries(${EXE_NAME} PRIVATE "LDtkLoader")
target_link_libraries(${EXE_NAME} PRIVATE "GL")
target_link_libraries(${EXE_NAME} PRIVATE "m")
target_link_libraries(${EXE_NAME} PRIVATE "pthread")
target_link_libraries(${EXE_NAME} PRIVATE "dl")
target_link_libraries(${EXE_NAME} PRIVATE "rt")
target_link_libraries(${EXE_NAME} PRIVATE "X11")

Don’t glob the list of source files.

So what should we do?

You could use an IDE that supports CMake. Usually, renaming files and updating the target source file list in cmake files is also supported. OTOH, how often do you rename source code files? Strange work flow with enough overhead (other code files, vcs, etc.) that even also updating the file name in cmake files should not be much to require from developers.

Unless you are in a hostile environment or actually another build system is used instead? Sounds like a non-technical problem, then. But that’s just guessing from my side. Maybe you could clarify the problem that leads to this problem?

Globbing (at cmake configure time or once beforehand) should at best only be used for e.g. code generation with unpredictable file names. Or you have to live it’s downsides.