-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
214 lines (171 loc) · 8.55 KB
/
CMakeLists.txt
File metadata and controls
214 lines (171 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
################################################################################
# REQUIRED COMMAND-LINE INPUTS
# 1) -DBUILD_TESTING=[ON/OFF]
# 2) -DCMAKE_BUILD_TYPE=[Debug/Release/RelWithDebInfo/MinSizeRel]
# 3) -Dproject_config_file=[relative/path/to/config_settings_file]
# 4) -Dproject_source_main_exec=[source_file_name]
# OPTIONAL COMMAND-LINE INPUTS
# 1) -Dproject_main_exec_to_build=[executable_name]
# 2) -Dtesting_exec_to_build=[executable_name]
################################################################################
################################################################################
# MACROS / FUNCTIONS
################################################################################
function(include_dir_and_recurse_subdirs)
foreach(arg IN LISTS ARGN)
include_directories("${arg}")
file(GLOB_RECURSE subdir_list LIST_DIRECTORIES true RELATIVE "${PROJECT_SOURCE_DIR}/" CONFIGURE_DEPENDS "${arg}/*")
foreach(sdir ${subdir_list})
if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/${sdir}")
include_directories("${sdir}")
endif()
endforeach()
endforeach()
endfunction()
function(collect_source_files_recursively out_sources)
set(collected_sources "")
foreach(arg IN LISTS ARGN)
file(GLOB_RECURSE src CONFIGURE_DEPENDS "${arg}/*.c" "${arg}/*.cpp" "${arg}/*.hxx")
list(APPEND collected_sources ${src})
endforeach()
set(${out_sources} ${collected_sources} PARENT_SCOPE)
endfunction()
function(convert_lang_type_field out_lang_type)
if(${${out_lang_type}} STREQUAL "C")
set(out_lang_type "C" PARENT_SCOPE)
elseif(${${out_lang_type}} STREQUAL "CPP")
set(${out_lang_type} "CXX" PARENT_SCOPE)
else()
message(FATAL_ERROR "Reading from project config file: unable to read/convert project_language_type")
endif()
endfunction()
function(read_config_list_field out_field out_config_list)
list(REMOVE_AT ${out_config_list} 0)
list(GET ${out_config_list} 0 line)
list(FIND ARGN "field_is_list" index)
if(${index} GREATER -1)
string(REPLACE ":" ";" line "${line}")
endif()
list(FIND ARGN "field_is_optional" index)
if((${index} GREATER -1) AND ("${line}" STREQUAL "-"))
set(line "")
endif()
list(FIND ARGN "field_is_lang_type" index)
if(${index} GREATER -1)
convert_lang_type_field(line)
endif()
set(${out_field} "${line}" PARENT_SCOPE)
list(REMOVE_AT ${out_config_list} 0)
set(${out_config_list} ${${out_config_list}} PARENT_SCOPE)
endfunction()
function(skip_config_list_field out_config_list)
list(REMOVE_AT ${out_config_list} 0)
list(REMOVE_AT ${out_config_list} 0)
set(${out_config_list} ${${out_config_list}} PARENT_SCOPE)
endfunction()
################################################################################
# READ SETTINGS FROM PROJECT CONFIG FILE
################################################################################
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${project_config_file}")
file(STRINGS "${project_config_file}" project_config_list)
read_config_list_field(project_cmake_title project_config_list)
read_config_list_field(project_cmake_version_req project_config_list)
read_config_list_field(project_language_type project_config_list "field_is_lang_type")
read_config_list_field(project_language_standard project_config_list)
read_config_list_field(project_include_standards project_config_list "field_is_optional")
read_config_list_field(project_warning_levels project_config_list "field_is_optional")
read_config_list_field(project_source_dirs project_config_list "field_is_list")
skip_config_list_field(project_config_list)
skip_config_list_field(project_config_list)
read_config_list_field(testing_language_type project_config_list "field_is_lang_type")
read_config_list_field(testing_language_standard project_config_list)
read_config_list_field(testing_source_dirs project_config_list "field_is_list")
skip_config_list_field(project_config_list)
################################################################################
# CMAKE VERSION REQUIRED
################################################################################
cmake_minimum_required(VERSION "${project_cmake_version_req}")
################################################################################
# MAIN PROJECT DEF
################################################################################
project("${project_cmake_title}" NONE)
################################################################################
# CMAKE LANGUAGE SUPPORT CHECK
################################################################################
enable_language("${project_language_type}")
################################################################################
# SOURCE FILE GROUPS
################################################################################
# project sources
set(project_sources "")
collect_source_files_recursively(project_sources ${project_source_dirs})
################################################################################
# EXECUTABLES TO BUILD
################################################################################
if(NOT "${project_source_main_exec}" STREQUAL "-")
# project main entry point source
set(project_main_source "${project_source_main_exec}")
# executable file to produce for main source
add_executable("${project_main_exec_to_build}" "${project_source_main_exec}")
# remove main source from project_sources
list(REMOVE_ITEM project_sources "${CMAKE_CURRENT_SOURCE_DIR}/${project_source_main_exec}")
endif()
################################################################################
# USER LIB (ALL NON-MAIN SOURCES COLLECTED INTO ONE LIB)
################################################################################
add_library(user_lib ${project_sources})
################################################################################
# EXTERNAL LIBS
################################################################################
find_package(Threads REQUIRED)
################################################################################
# LINK LIBS --> EXECUTABLES
################################################################################
if(NOT "${project_source_main_exec}" STREQUAL "-")
target_link_libraries("${project_main_exec_to_build}" PRIVATE user_lib PRIVATE Threads::Threads)
endif()
################################################################################
# COMPILER OPTIONS
################################################################################
# warnings: enable most warnings
add_definitions("${project_warning_levels}")
################################################################################
# SOURCE CONFIG
################################################################################
# set project-wide language standards
if(NOT "${project_source_main_exec}" STREQUAL "-")
set_target_properties("${project_main_exec_to_build}" PROPERTIES ${project_language_type}_STANDARD ${project_language_standard} ${project_language_type}_STANDARD_REQUIRED ON)
endif()
set_target_properties(user_lib PROPERTIES ${project_language_type}_STANDARD ${project_language_standard} ${project_language_type}_STANDARD_REQUIRED ON)
# set project-wide system-include standards
add_definitions("${project_include_standards}")
# set project-wide include dirs
include_directories("${CMAKE_SOURCE_DIR}")
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
include_dir_and_recurse_subdirs(${project_source_dirs})
################################################################################
# TESTING (OPTIONAL ADD-ON TO BUILD)
################################################################################
# note BUILD_TESTING=ON must be passed to cmake (auto-calls enable_testing())
if(BUILD_TESTING)
# load CTest suite build tests
include(CTest)
# cmake language support check
enable_language("${testing_language_type}")
# source file lists
set(testing_sources "")
collect_source_files_recursively(testing_sources ${testing_source_dirs})
# test harness executable
add_executable("${testing_exec_to_build}" ${testing_sources})
# cmake ctests to run / linked libs
add_test(NAME "${testing_exec_to_build}" COMMAND "${testing_exec_to_build}")
# link libs --> executables
target_link_libraries("${testing_exec_to_build}" PRIVATE user_lib PRIVATE Threads::Threads)
# source config
# testing language standards
set_target_properties("${testing_exec_to_build}" PROPERTIES
${testing_language_type}_STANDARD "${testing_language_standard}"
${testing_language_type}_STANDARD_REQUIRED ON)
# testing include dirs
include_dir_and_recurse_subdirs(${testing_source_dirs})
endif()