Skip to content

Commit bbbb6e7

Browse files
committed
CMake: Move config.h generation to its own module.
This unclutters the top CMakeLists.txt and moves all the target settings needed for using config.h to the same place. Also check that all the needed variables are set before generating the file.
1 parent d153d43 commit bbbb6e7

File tree

5 files changed

+99
-36
lines changed

5 files changed

+99
-36
lines changed

prboom2/CMakeLists.txt

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,6 @@ set(PROJECT_TARNAME "dsda-doom")
8080
set(WAD_DATA "dsda-doom.wad")
8181
set(PROJECT_STRING "${PROJECT_NAME} ${PROJECT_VERSION}")
8282

83-
include(CheckSymbolExists)
84-
85-
check_symbol_exists(stricmp "string.h" HAVE_STRICMP)
86-
check_symbol_exists(strnicmp "string.h" HAVE_STRNICMP)
87-
check_symbol_exists(getopt "unistd.h" HAVE_GETOPT)
88-
check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
89-
check_symbol_exists(CreateFileMapping "windows.h" HAVE_CREATE_FILE_MAPPING)
90-
check_symbol_exists(strsignal "string.h" HAVE_STRSIGNAL)
91-
check_symbol_exists(mkstemp "stdlib.h" HAVE_MKSTEMP)
92-
check_symbol_exists(getpwuid "unistd.h;sys/types.h;pwd.h" HAVE_GETPWUID)
93-
94-
include(CheckIncludeFile)
95-
96-
check_include_file("sys/wait.h" HAVE_SYS_WAIT_H)
97-
check_include_file("unistd.h" HAVE_UNISTD_H)
98-
check_include_file("asm/byteorder.h" HAVE_ASM_BYTEORDER_H)
99-
check_include_file("dirent.h" HAVE_DIRENT_H)
100-
10183
include(DsdaTargetFeatures)
10284

10385
include(PkgConfigHelper)
@@ -178,7 +160,7 @@ option(SIMPLECHECKS "Enable checks which only impose significant overhead if a p
178160
# Debug options, disabled by default
179161
option(RANGECHECK "Enable internal range checking" OFF)
180162

181-
configure_file(cmake/config.h.cin config.h)
163+
include(DsdaConfigHeader)
182164

183165
set(DSDA_OUTPUT_PATH ${CMAKE_BINARY_DIR})
184166

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
include_guard()
2+
3+
include(DsdaHelpers)
4+
5+
set(DSDA_INTERNAL_GENERATED_CONFIG_DIR "${PROJECT_BINARY_DIR}/build-config")
6+
7+
# Internal functions, these should not be called from outside this module
8+
9+
function(dsda_internal_check_symbols)
10+
include(CheckSymbolExists)
11+
12+
check_symbol_exists(stricmp "string.h" HAVE_STRICMP)
13+
check_symbol_exists(strnicmp "string.h" HAVE_STRNICMP)
14+
check_symbol_exists(getopt "unistd.h" HAVE_GETOPT)
15+
check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
16+
check_symbol_exists(CreateFileMapping "windows.h" HAVE_CREATE_FILE_MAPPING)
17+
check_symbol_exists(strsignal "string.h" HAVE_STRSIGNAL)
18+
check_symbol_exists(mkstemp "stdlib.h" HAVE_MKSTEMP)
19+
check_symbol_exists(getpwuid "unistd.h;sys/types.h;pwd.h" HAVE_GETPWUID)
20+
endfunction()
21+
22+
function(dsda_internal_check_includes)
23+
include(CheckIncludeFile)
24+
25+
check_include_file("sys/wait.h" HAVE_SYS_WAIT_H)
26+
check_include_file("unistd.h" HAVE_UNISTD_H)
27+
check_include_file("asm/byteorder.h" HAVE_ASM_BYTEORDER_H)
28+
check_include_file("dirent.h" HAVE_DIRENT_H)
29+
endfunction()
30+
31+
function(dsda_internal_check_variables)
32+
set(expected_vars
33+
PROJECT_NAME
34+
PROJECT_TARNAME
35+
WAD_DATA
36+
PROJECT_VERSION
37+
PROJECT_STRING
38+
DOOMWADDIR
39+
DSDA_ABSOLUTE_PWAD_PATH
40+
WORDS_BIGENDIAN
41+
SIMPLECHECKS
42+
RANGECHECK
43+
)
44+
foreach(var IN LISTS expected_vars)
45+
if(NOT DEFINED ${var})
46+
message(FATAL_ERROR "config.h cannot be generated: \"${var}\" is not set")
47+
endif()
48+
endforeach()
49+
endfunction()
50+
51+
function(dsda_internal_generate_build_config)
52+
dsda_internal_check_symbols()
53+
dsda_internal_check_includes()
54+
dsda_internal_check_variables()
55+
56+
configure_file(
57+
"${CMAKE_CURRENT_LIST_DIR}/config.h.cin"
58+
"${DSDA_INTERNAL_GENERATED_CONFIG_DIR}/config.h"
59+
)
60+
endfunction()
61+
62+
dsda_internal_generate_build_config()
63+
64+
# Public functions
65+
66+
function(dsda_target_use_config_h tgt)
67+
dsda_fail_if_invalid_target(${tgt})
68+
69+
target_sources(${tgt}
70+
PRIVATE
71+
"${DSDA_INTERNAL_GENERATED_CONFIG_DIR}/config.h"
72+
)
73+
74+
target_include_directories(${tgt}
75+
PRIVATE
76+
$<BUILD_INTERFACE:${DSDA_INTERNAL_GENERATED_CONFIG_DIR}>
77+
)
78+
79+
target_compile_definitions(${TARGET}
80+
PRIVATE
81+
HAVE_CONFIG_H
82+
$<$<NOT:$<BOOL:${HAVE_STRICMP}>>:stricmp=strcasecmp>
83+
$<$<NOT:$<BOOL:${HAVE_STRNICMP}>>:strnicmp=strncasecmp>
84+
)
85+
endfunction()

prboom2/cmake/DsdaHelpers.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include_guard()
2+
3+
function(dsda_fail_if_invalid_target tgt)
4+
if(NOT TARGET ${tgt})
5+
message(FATAL_ERROR "${tgt} is not a valid CMake target.")
6+
endif()
7+
endfunction()

prboom2/cmake/DsdaTargetFeatures.cmake

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
include_guard()
22

3-
# Internal functions, these should not be called from outside this module
3+
include(DsdaHelpers)
44

5-
function(dsda_internal_fail_if_invalid_target tgt)
6-
if(NOT TARGET ${tgt})
7-
message(FATAL_ERROR "${tgt} is not a valid CMake target.")
8-
endif()
9-
endfunction()
5+
# Internal functions, these should not be called from outside this module
106

117
function(dsda_internal_setup_warnings_msvc result_var)
128
set(${result_var}
@@ -102,7 +98,7 @@ endfunction()
10298
# Public functions
10399

104100
function(dsda_target_set_warnings tgt)
105-
dsda_internal_fail_if_invalid_target(${tgt})
101+
dsda_fail_if_invalid_target(${tgt})
106102

107103
if(NOT DEFINED CACHE{DSDA_ENABLED_WARNINGS})
108104
dsda_internal_setup_warnings()
@@ -115,7 +111,7 @@ function(dsda_target_set_warnings tgt)
115111
endfunction()
116112

117113
function(dsda_target_silence_deprecation tgt)
118-
dsda_internal_fail_if_invalid_target(${tgt})
114+
dsda_fail_if_invalid_target(${tgt})
119115

120116
if(WIN32)
121117
target_compile_definitions(${tgt}
@@ -132,7 +128,7 @@ function(dsda_target_silence_deprecation tgt)
132128
endfunction()
133129

134130
function(dsda_target_enable_fast_math tgt)
135-
dsda_internal_fail_if_invalid_target(${tgt})
131+
dsda_fail_if_invalid_target(${tgt})
136132

137133
if(NOT DEFINED CACHE{DSDA_FAST_MATH_FLAG})
138134
dsda_internal_check_fast_math_flag()

prboom2/src/CMakeLists.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,17 +533,10 @@ function(AddGameExecutable TARGET SOURCES)
533533
dsda_target_set_warnings(${TARGET})
534534
dsda_target_silence_deprecation(${TARGET})
535535
dsda_target_enable_fast_math(${TARGET})
536-
537-
target_compile_definitions(${TARGET}
538-
PRIVATE
539-
HAVE_CONFIG_H
540-
$<$<NOT:$<BOOL:${HAVE_STRICMP}>>:stricmp=strcasecmp>
541-
$<$<NOT:$<BOOL:${HAVE_STRNICMP}>>:strnicmp=strncasecmp>
542-
)
536+
dsda_target_use_config_h(${TARGET})
543537

544538
target_include_directories(${TARGET} PRIVATE
545539
${SDL2_INCLUDE_DIRS}
546-
${CMAKE_BINARY_DIR}
547540
${CMAKE_CURRENT_SOURCE_DIR}
548541
)
549542

0 commit comments

Comments
 (0)