-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCMakeMacros.cmake
More file actions
204 lines (190 loc) · 9.46 KB
/
CMakeMacros.cmake
File metadata and controls
204 lines (190 loc) · 9.46 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
#
# Copyright(c) 2025 Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Minimal cmake version
cmake_minimum_required(VERSION 3.5)
function(sub_directory_list out_result target_dir)
file(GLOB children RELATIVE ${target_dir} ${target_dir}/*)
set(dirlist "")
foreach(child ${children})
if(IS_DIRECTORY ${target_dir}/${child})
list(APPEND dirlist ${child})
endif()
endforeach()
set("${out_result}" "${dirlist}" PARENT_SCOPE)
endfunction()
function(convert_to_absolute_paths out_list root_dir in_list)
set(lst)
foreach(item ${in_list})
list(APPEND lst "${root_dir}/${item}")
endforeach()
set("${out_list}" "${lst}" PARENT_SCOPE)
endfunction()
function(bacasable_glob_filter out_filter dir)
# Reset the var
set(filter)
# Generate the filter list
foreach(extension ${bacasable_source_extensions})
list(APPEND filter "${dir}/*${extension}")
endforeach()
# Override the output var
set("${out_filter}" "${filter}" PARENT_SCOPE)
endfunction()
function(bacasable_headers out_absolute_files root_dir specific_filter)
# Fetch the filters
bacasable_glob_filter(filter "${root_dir}")
# Fetch all the concerned files
file(GLOB_RECURSE relative_files RELATIVE "${root_dir}" ${filter})
# Convert them to absolute path
convert_to_absolute_paths(absolute_files "${root_dir}" "${relative_files}")
# Set filters
source_group("${specific_filter}\\Header Files" FILES ${absolute_files})
# Override the output var
set("${out_absolute_files}" "${absolute_files}" PARENT_SCOPE)
endfunction()
function(bacasable_sources out_absolute_files root_dir specific_filter)
# Fetch the filters
bacasable_glob_filter(filter "${root_dir}")
# Fetch all the concerned files
file(GLOB_RECURSE relative_files RELATIVE "${root_dir}" ${filter})
# Convert them to absolute path
convert_to_absolute_paths(absolute_files "${root_dir}" "${relative_files}")
# Set filters
source_group("${specific_filter}\\Source Files" FILES ${absolute_files})
# Override the output var
set("${out_absolute_files}" "${absolute_files}" PARENT_SCOPE)
endfunction()
function(bacasable_static_lib lib folder sources include_dirs)
# Add the library as static
add_library("${lib}" STATIC ${sources})
# Set properties
set_target_properties("${lib}" PROPERTIES COMPILE_PDB_NAME "${lib}")
set_target_properties("${lib}" PROPERTIES FOLDER "${folder}")
# Log the generation
message(STATUS "Static project ${lib} has been generated")
# Set include directories
target_include_directories("${lib}" PUBLIC ${include_dirs})
endfunction()
function(bacasable_dynamic_lib lib folder sources include_dirs)
# Add the library as static
add_library("${lib}" SHARED ${sources})
# Set properties
set_target_properties("${lib}" PROPERTIES COMPILE_PDB_NAME "${lib}")
set_target_properties("${lib}" PROPERTIES FOLDER "${folder}")
# Log the generation
message(STATUS "Shared project ${lib} has been generated")
# Set include directories
target_include_directories("${lib}" PUBLIC ${include_dirs})
endfunction()
function(bacasable_exe exe sourcegroup sources includes)
# Generate the exe
add_executable("${exe}" ${sources})
# Set properties
set_target_properties("${exe}" PROPERTIES COMPILE_PDB_NAME "${exe}")
set_target_properties("${exe}" PROPERTIES FOLDER "${sourcegroup}")
# Log the generation
message(STATUS "Executable ${sourcegroup} has been generated")
# Set includes
target_include_directories("${exe}" PRIVATE ${includes})
endfunction()
# Replace compilation flags, configuration type is optional
macro(replace_compile_flags pSearch pReplace)
set(MacroArgs "${ARGN}")
if( NOT MacroArgs )
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
foreach(MacroArg IN LISTS MacroArgs)
if( MacroArg STREQUAL "debug" )
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
elseif( MacroArg STREQUAL "dev" )
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_C_FLAGS_DEV "${CMAKE_C_FLAGS_DEV}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_CXX_FLAGS_DEV "${CMAKE_CXX_FLAGS_DEV}")
elseif( MacroArg STREQUAL "release" )
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
else()
message(FATAL_ERROR "Unknown configuration, cannot replace compile flags!")
endif()
endforeach()
endif()
endmacro()
# Add exe linker flags, configuration type is optional
macro(add_exe_linker_flags pFlags)
set(MacroArgs "${ARGN}")
if( NOT MacroArgs )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${pFlags}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${pFlags}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${pFlags}")
else()
foreach(MacroArg IN LISTS MacroArgs)
if( MacroArg STREQUAL "debug" )
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${pFlags}")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${pFlags}")
set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} ${pFlags}")
elseif( MacroArg STREQUAL "dev" )
set(CMAKE_EXE_LINKER_FLAGS_DEV "${CMAKE_EXE_LINKER_FLAGS_DEV} ${pFlags}")
set(CMAKE_SHARED_LINKER_FLAGS_DEV "${CMAKE_SHARED_LINKER_FLAGS_DEV} ${pFlags}")
set(CMAKE_MODULE_LINKER_FLAGS_DEV "${CMAKE_MODULE_LINKER_FLAGS_DEV} ${pFlags}")
elseif( MacroArg STREQUAL "release" )
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${pFlags}")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} ${pFlags}")
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE} ${pFlags}")
else()
message(FATAL_ERROR "Unknown configuration, cannot add linker flags!")
endif()
endforeach()
endif()
endmacro()
# Replace linker flags, configuration type is optional
macro(replace_linker_flags pSearch pReplace)
set(MacroArgs "${ARGN}")
if( NOT MacroArgs )
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}")
else()
foreach(MacroArg IN LISTS MacroArgs)
if( MacroArg STREQUAL "debug" )
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG}")
elseif( MacroArg STREQUAL "dev" )
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_EXE_LINKER_FLAGS_DEV "${CMAKE_EXE_LINKER_FLAGS_DEV}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_SHARED_LINKER_FLAGS_DEV "${CMAKE_SHARED_LINKER_FLAGS_DEV}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_MODULE_LINKER_FLAGS_DEV "${CMAKE_MODULE_LINKER_FLAGS_DEV}")
elseif( MacroArg STREQUAL "release" )
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
string(REGEX REPLACE "${pSearch}" "${pReplace}" CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE}")
else()
message(FATAL_ERROR "Unknown configuration, cannot replace linker flags!")
endif()
endforeach()
endif()
endmacro()
macro(copy_next_to_binary target_project target_asset)
add_custom_command(TARGET ${target_project} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${target_asset}" $<TARGET_FILE_DIR:${target_project}>)
endmacro()
macro(copy_dir_next_to_binary target_project target_dir dir_name)
add_custom_command(TARGET ${target_project} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${target_dir}" "$<TARGET_FILE_DIR:${target_project}>/${dir_name}")
endmacro()