Skip to content

Commit 0005e14

Browse files
jcfrKjell Hedström
authored andcommitted
cmake: Avoid extra recompilation updating generated_definitions.hpp only if needed (KjellKod#235)
This commit updates the build system to ensure the generated_definitions header is updated only if its content changed. This will avoid recompilation if the re-configured without changing any options.
1 parent b4be5a0 commit 0005e14

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ nbproject/*
66
build_clang
77
build_travis
88
gtest-1.7.0
9-
generated_definitions.hpp
109
*~

GenerateMacroDefinitionsFile.cmake

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,33 @@
2121
message( STATUS "" )
2222
message( STATUS "COMPILE_DEFINITIONS:\n\t[${G3_DEFINITIONS}]" )
2323
message( STATUS "" )
24+
2425
SET(GENERATED_G3_DEFINITIONS "${CMAKE_CURRENT_BINARY_DIR}/include/g3log/generated_definitions.hpp")
25-
file(REMOVE ${GENERATED_G3_DEFINITIONS} )
26-
FILE(WRITE ${GENERATED_G3_DEFINITIONS} "// AUTO GENERATED MACRO DEFINITIONS FOR G3LOG\n\n")
27-
FILE(APPEND ${GENERATED_G3_DEFINITIONS} "${HEADER}\n")
28-
FILE(APPEND ${GENERATED_G3_DEFINITIONS} "#pragma once\n\n")
29-
FILE(APPEND ${GENERATED_G3_DEFINITIONS} "// CMake induced definitions below. See g3log/Options.cmake for details.\n\n")
26+
27+
# If it exists, read existing file
28+
set(current_content "")
29+
if(EXISTS ${GENERATED_G3_DEFINITIONS})
30+
file(READ ${GENERATED_G3_DEFINITIONS} current_content)
31+
endif()
32+
33+
set(generated_content "// AUTO GENERATED MACRO DEFINITIONS FOR G3LOG\n\n")
34+
set(generated_content "${generated_content}\n${HEADER}\n")
35+
set(generated_content "${generated_content}\n#pragma once\n\n")
36+
set(generated_content "${generated_content}\n// CMake induced definitions below. See g3log/Options.cmake for details.\n\n")
3037

3138
FOREACH(definition ${G3_DEFINITIONS} )
32-
FILE(APPEND ${GENERATED_G3_DEFINITIONS} "#define ${definition}\n")
39+
set(generated_content "${generated_content}\n#define ${definition}\n")
3340
ENDFOREACH(definition)
3441

35-
message( STATUS "Generated ${GENERATED_G3_DEFINITIONS}" )
36-
file(READ ${GENERATED_G3_DEFINITIONS} generated_content)
42+
if(NOT "${current_content}" STREQUAL "${generated_content}")
43+
44+
message( STATUS "Generated ${GENERATED_G3_DEFINITIONS}" )
45+
46+
message( STATUS "******************** START *************************" )
47+
message(${generated_content})
48+
message( STATUS "******************** END *************************" )
49+
50+
file(WRITE ${GENERATED_G3_DEFINITIONS} ${generated_content})
51+
52+
endif()
3753

38-
message( STATUS "******************** START *************************" )
39-
MESSAGE(${generated_content})
40-
message( STATUS "******************** END *************************" )

0 commit comments

Comments
 (0)