Below is a small, self-contained recipe that
• reads the current content of somefile.id
• passes that content to the compiler as -DVAR=, but only for foo.c
• forces CMake to look at somefile.id every time you run cmake --build … so that a change in the file automatically triggers re-configuration and recompilation of foo.c only.
Tested with CMake ≥ 3.16 and Ninja, but it works with Makefiles just as well.
# ---- CMakeLists.txt -------------------------------------------------------
cmake_minimum_required(VERSION 3.16)
project(demo C)
set(ID_FILE "${CMAKE_SOURCE_DIR}/somefile.id") # 1) path to the data file
# 2) Tell CMake that this *data* file belongs to the "configure" step.
# When the timestamp of somefile.id changes, CMake will re-run its
# configure stage automatically the next time you do `cmake --build .`.
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${ID_FILE}")
# 3) Read the file and strip off newlines & surrounding whitespace.
file(READ "${ID_FILE}" _var_from_file)
string(STRIP "${_var_from_file}" _var_from_file)
# 4) Build target that contains foo.c (plus anything else you have)
add_executable(my_app
foo.c
bar.c # whatever other sources you have
)
# 5) Apply the compile definition only to foo.c
#
# COMPILE_DEFINITIONS set through a *source file property* affect that
# source file only, and do not “leak” to the others.
set_source_files_properties(
foo.c
PROPERTIES
COMPILE_DEFINITIONS "VAR=${_var_from_file}"
)
# ---------------------------------------------------------------------------
How it works
-
CMake’s build tool re-runs the “configure” step whenever any input file
listed in CMAKE_CONFIGURE_DEPENDS has a newer time-stamp.
Adding somefile.id to that list therefore guarantees it is re-read on every
build if you changed it.
-
During configuration, CMake reads the file (file(READ …)) and stores the
trimmed content in the variable _var_from_file.
-
set_source_files_properties(foo.c … COMPILE_DEFINITIONS …) attaches the
-DVAR=<value> definition solely to foo.c.
-
When you now edit somefile.id and run
cmake --build .
the sequence is
• CMake notices the altered time-stamp → reruns configuration
• new value is picked up → build files are regenerated
• Ninja recompiles only foo.c with the updated -DVAR=…
• Everything else is left untouched.
Alternative – generate a tiny header
If embedding the value as a compile definition is not a hard requirement,
a tiny header is often even simpler and avoids very long command lines:
# generate var.h at build time whenever somefile.id changes
add_custom_command(
OUTPUT "${CMAKE_BINARY_DIR}/var.h"
DEPENDS "${ID_FILE}"
COMMAND ${CMAKE_COMMAND} -E copy "${ID_FILE}" "${CMAKE_BINARY_DIR}/var.h"
COMMENT "Updating var.h from somefile.id"
)
add_custom_target(gen_header DEPENDS "${CMAKE_BINARY_DIR}/var.h")
add_dependencies(my_app gen_header)
target_include_directories(my_app PRIVATE "${CMAKE_BINARY_DIR}")
Then, inside foo.c:
#include "var.h" /* var.h just contains #define VAR <value> */
Either way you achieve the same goal: every cmake --build picks up the new
content of somefile.id and recompiles only what is necessary.