Skip to content

Commit e083793

Browse files
authored
Merge pull request #703 from FtZPetruska/cmake-config-h
CMake: Move config.h generation to its own module.
2 parents 765eac2 + 603638f commit e083793

File tree

5 files changed

+102
-39
lines changed

5 files changed

+102
-39
lines changed

prboom2/CMakeLists.txt

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ endif()
4949

5050
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
5151

52-
include(CheckBigEndian)
53-
check_big_endian(WORDS_BIGENDIAN)
54-
5552
include(GNUInstallDirs)
5653

5754
if(POLICY CMP0099)
@@ -80,24 +77,6 @@ set(PROJECT_TARNAME "dsda-doom")
8077
set(WAD_DATA "dsda-doom.wad")
8178
set(PROJECT_STRING "${PROJECT_NAME} ${PROJECT_VERSION}")
8279

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-
10180
include(DsdaTargetFeatures)
10281

10382
include(PkgConfigHelper)
@@ -178,7 +157,7 @@ option(SIMPLECHECKS "Enable checks which only impose significant overhead if a p
178157
# Debug options, disabled by default
179158
option(RANGECHECK "Enable internal range checking" OFF)
180159

181-
configure_file(cmake/config.h.cin config.h)
160+
include(DsdaConfigHeader)
182161

183162
set(DSDA_OUTPUT_PATH ${CMAKE_BINARY_DIR})
184163

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
include(CheckBigEndian)
53+
check_big_endian(WORDS_BIGENDIAN)
54+
55+
dsda_internal_check_symbols()
56+
dsda_internal_check_includes()
57+
dsda_internal_check_variables()
58+
59+
configure_file(
60+
"${CMAKE_CURRENT_LIST_DIR}/config.h.cin"
61+
"${DSDA_INTERNAL_GENERATED_CONFIG_DIR}/config.h"
62+
)
63+
endfunction()
64+
65+
dsda_internal_generate_build_config()
66+
67+
# Public functions
68+
69+
function(dsda_target_use_config_h tgt)
70+
dsda_fail_if_invalid_target(${tgt})
71+
72+
target_sources(${tgt}
73+
PRIVATE
74+
"${DSDA_INTERNAL_GENERATED_CONFIG_DIR}/config.h"
75+
)
76+
77+
target_include_directories(${tgt}
78+
PRIVATE
79+
$<BUILD_INTERFACE:${DSDA_INTERNAL_GENERATED_CONFIG_DIR}>
80+
)
81+
82+
target_compile_definitions(${TARGET}
83+
PRIVATE
84+
HAVE_CONFIG_H
85+
$<$<NOT:$<BOOL:${HAVE_STRICMP}>>:stricmp=strcasecmp>
86+
$<$<NOT:$<BOOL:${HAVE_STRNICMP}>>:strnicmp=strncasecmp>
87+
)
88+
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)