-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_utils.cmake
221 lines (205 loc) · 10.3 KB
/
internal_utils.cmake
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
214
215
216
217
218
219
220
# Defines functions and macros useful for building easily
set(GOOGLE_TEST_BUILD_DIRECTORY cmake-gtest-build)
if ( GTEST_BUILDING_IS_ACTIVATED )
##########################################
######## Setting up GOOGLE TEST ########
##########################################
set(GOOGLE_TEST_BUILD_OUTPUT ${PROJECT_SOURCE_DIR}/${GOOGLE_TEST_BUILD_DIRECTORY})
# https://stackoverflow.com/questions/3702115/creating-a-directory-in-cmake
file(MAKE_DIRECTORY ${PROJECT_SOURCE_DIR}/${GOOGLE_TEST_BUILD_DIRECTORY})
message("${ColourBold}Directory created at: ${GOOGLE_TEST_BUILD_OUTPUT}${ColourReset}")
set(GOOGLE_TEST_FOLDER_LOCATION ${PROJECT_SOURCE_DIR}/lib/gtest/)
set(GOOGLE_TEST_CMAKE_COMMANDS -Dgtest_build_samples=OFF -Dgtest_build_tests=OFF -Dgmock_build_tests=OFF -Dcxx_no_exception=ON -Dcxx_no_rtti=ON -DCMAKE_COMPILER_IS_GNUCXX=ON)
#########################################
######## Configure GOOGLE TEST ########
#########################################
execute_process(COMMAND ${CMAKE_COMMAND} ${GOOGLE_TEST_CMAKE_COMMANDS} -S ${GOOGLE_TEST_FOLDER_LOCATION} -B ${GOOGLE_TEST_BUILD_DIRECTORY})
#####################################
######## Build GOOGLE TEST ########
#####################################
add_custom_target(Building-google-test ALL COMMAND ;)
add_custom_command(TARGET Building-google-test PRE_BUILD
COMMAND ${CMAKE_COMMAND} --build ${GOOGLE_TEST_BUILD_OUTPUT}
)
endif ( GTEST_BUILDING_IS_ACTIVATED )
#####################################
######## Color definitions ########
#####################################
# https://stackoverflow.com/questions/18968979/how-to-get-colorized-output-with-cmake
if(NOT WIN32)
string(ASCII 27 Esc)
set(ColourReset "${Esc}[m")
set(ColourBold "${Esc}[1m")
set(Red "${Esc}[31m")
set(Green "${Esc}[32m")
set(Yellow "${Esc}[33m")
set(Blue "${Esc}[34m")
set(Magenta "${Esc}[35m")
set(Cyan "${Esc}[36m")
set(White "${Esc}[37m")
set(BoldRed "${Esc}[1;31m")
set(BoldGreen "${Esc}[1;32m")
set(BoldYellow "${Esc}[1;33m")
set(BoldBlue "${Esc}[1;34m")
set(BoldMagenta "${Esc}[1;35m")
set(BoldCyan "${Esc}[1;36m")
set(BoldWhite "${Esc}[1;37m")
set(Greenishbg "${Esc}[30;48;5;114m")
set(Orangeybg "${Esc}[30;48;5;130m")
set(Grayishbg "${Esc}[30;48;5;66m")
set(Pinkybg "${Esc}[30;48;5;168m")
endif()
#########################
######## C++98 ########
#########################
# Defines how the libraries will be created.
## Usage example: cxx_library_with_flags_cxx_98("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_library_with_flags_cxx_98 name type include_dirs cxx98_flags libs)
# message("${Magenta}name: ${name}${ColourReset}")
# message("${Magenta}type: ${type}${ColourReset}")
# message("${Magenta}include_dirs: ${include_dirs}${ColourReset}")
# message("${Magenta}cxx98_flags: ${cxx98_flags}${ColourReset}")
# message("${Magenta}libs: ${libs}${ColourReset}")
# message("${Magenta}ARGN: ${ARGN}${ColourReset}")
# type can be either STATIC or SHARED to denote a static or shared library.
# ARGN refers to additional arguments after 'cxx98_flags'.
add_library(${name} ${type} ${ARGN})
include_directories(${include_dirs})
# Set the output directory for build artifacts
set_target_properties(${name}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set_target_properties(${name}
PROPERTIES
COMPILE_FLAGS "${cxx98_flags} -std=gnu++98")
# To support mixing linking in static and dynamic libraries, link each
# library in with an extra call to target_link_libraries.
foreach (lib "${libs}")
target_link_libraries(${name} ${lib})
endforeach()
endfunction()
# Short form of cxx_library_with_flags_cxx98 for dinamic libraries.
## Usage example: cxx_shared_library_cxx_98("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_shared_library_cxx_98 name include_folders cxx98_flags libraries)
cxx_library_with_flags_cxx_98(${name} SHARED "${include_folders}" "${cxx98_flags}" "${libraries}" ${ARGN})
endfunction()
# Short form of cxx_library_with_flags_cxx98 for static libraries.
## Usage example: cxx_library_cxx_98("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_library_cxx_98 name include_folders cxx98_flags libraries)
cxx_library_with_flags_cxx_98(${name} "" "${include_folders}" "${cxx98_flags}" "${libraries}" ${ARGN})
endfunction()
#########################
######## C++11 ########
#########################
# Defines how the libraries will be created.
## Usage example: cxx_library_with_flags_cxx_11("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_library_with_flags_cxx_11 name type include_dirs cxx11_flags libs)
# Debugging by message
# message("${Magenta}name: ${name}${ColourReset}")
# message("${Magenta}type: ${type}${ColourReset}")
# message("${Magenta}include_dirs: ${include_dirs}${ColourReset}")
# message("${Magenta}cxx11_flags: ${cxx11_flags}${ColourReset}")
# message("${Magenta}libs: ${libs}${ColourReset}")
# message("${Magenta}ARGN: ${ARGN}${ColourReset}")
# type can be either STATIC or SHARED to denote a static or shared library.
# ARGN refers to additional arguments after 'cxx11_flags'.
add_library(${name} ${type} ${ARGN})
include_directories(${include_dirs})
# Set the output directory for build artifacts
set_target_properties(${name}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set_target_properties(${name}
PROPERTIES
COMPILE_FLAGS "${cxx11_flags} -std=gnu++11")
# To support mixing linking in static and dynamic libraries, link each
# library in with an extra call to target_link_libraries.
foreach (lib "${libs}")
target_link_libraries(${name} ${lib})
endforeach()
endfunction()
# Short form of cxx_library_with_flags_cxx11 for dinamic libraries.
## Usage example: cxx_shared_library_cxx_11("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_shared_library_cxx_11 name include_folders cxx11_flags libraries)
cxx_library_with_flags_cxx_11(${name} SHARED "${include_folders}" "${cxx11_flags}" "${libraries}" ${ARGN})
endfunction()
# Short form of cxx_library_with_flags_cxx11 for static libraries.
## Usage example: cxx_library_cxx_11("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_library_cxx_11 name include_folders cxx11_flags libraries)
set(NoType "")
cxx_library_with_flags_cxx_11(${name} "${NoType}" "${include_folders}" "${cxx11_flags}" "${libraries}" ${ARGN})
endfunction()
#######################
######## C99 ########
#######################
# Defines how the libraries will be created.
## Usage example: cxx_library_with_flags_c99("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_library_with_flags_c99 name type include_dirs c99_flags libs)
# type can be either STATIC or SHARED to denote a static or shared library.
# ARGN refers to additional arguments after 'c99_flags'.
add_library(${name} ${type} ${ARGN})
include_directories(${include_dirs})
# Set the output directory for build artifacts
set_target_properties(${name}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set_target_properties(${name}
PROPERTIES
COMPILE_FLAGS "${c99_flags} -std=c99")
# To support mixing linking in static and dynamic libraries, link each
# library in with an extra call to target_link_libraries.
foreach (lib "${libs}")
target_link_libraries(${name} ${lib})
endforeach()
endfunction()
# Short form of cxx_library_with_flags_c99 for dinamic libraries.
## Usage example: cxx_shared_library_cxx_99("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_shared_library_cxx_99 name include_folders c99_flags libraries)
cxx_library_with_flags_cxx_99(${name} SHARED "${include_folders}" "${c99_flags}" "${libraries}" ${ARGN})
endfunction()
# Short form of cxx_library_with_flags_c99 for static libraries.
## Usage example: cxx_library_cxx_99("${myLib}" "" "${include_routes}" "${cxx_coverage}" "${libraries_used}" ${src_files})
function(cxx_library_cxx_99 name include_folders c99_flags libraries)
cxx_library_with_flags_cxx_99(${name} "" "${include_folders}" "${c99_flags}" "${libraries}" ${ARGN})
endfunction()
###################################
######## Test Executable ########
###################################
# Defines how the executable will be created.
## Usage example: gtest_executable("${exeProgam}" "${include_routes}" "${libraries_used}")
function(gtest_executable name include_dirs exe_flags libs)
# Debugging by message
# message("${Magenta}name: ${name}${ColourReset}")
# message("${Magenta}include_dirs: ${include_dirs}${ColourReset}")
# message("${Magenta}exe_flags: ${exe_flags}${ColourReset}")
# message("${Magenta}libs: ${libs}${ColourReset}")
# message("${Magenta}ARGN: ${ARGN}${ColourReset}")
link_directories(${PROJECT_SOURCE_DIR}/${GOOGLE_TEST_BUILD_DIRECTORY}/lib)
message("${BoldBlue}Library: ${PROJECT_SOURCE_DIR}/${GOOGLE_TEST_BUILD_DIRECTORY}/lib${ColourReset}")
add_executable(${name} ${ARGN})
include_directories(${include_dirs})
# Set the output directory for build artifacts
set_target_properties(${name}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set_target_properties(${name}
PROPERTIES
COMPILE_FLAGS "${exe_flags} -std=gnu++11")
# To support mixing linking in static and dynamic libraries, link each
# library in with an extra call to target_link_libraries.
foreach (lib "${libs}")
target_link_libraries(${name} ${lib})
endforeach()
endfunction()