-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
348 lines (288 loc) · 10.3 KB
/
CMakeLists.txt
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
project(sampgdk)
cmake_minimum_required(VERSION 2.8.12)
cmake_policy(SET CMP0022 NEW)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
option(SAMPGDK_STATIC "Build static library instead" OFF)
option(SAMPGDK_BUILD_PLUGINS "Build example plugins" OFF)
option(SAMPGDK_BUILD_AMALGAMATION "Build amalgamation" OFF)
option(SAMPGDK_BUILD_DOCS "Build Doxygen documentation" ON)
set(SAMPGDK_VERSION_MAJOR 4)
set(SAMPGDK_VERSION_MINOR 6)
set(SAMPGDK_VERSION_PATCH 3)
set(SAMPGDK_VERSION ${SAMPGDK_VERSION_MAJOR})
set(SAMPGDK_VERSION ${SAMPGDK_VERSION}.${SAMPGDK_VERSION_MINOR})
set(SAMPGDK_VERSION ${SAMPGDK_VERSION}.${SAMPGDK_VERSION_PATCH})
set(SAMPGDK_VERSION ${SAMPGDK_VERSION}${SAMPGDK_VERSION_SUFFIX})
math(EXPR SAMPGDK_VERSION_ID "${SAMPGDK_VERSION_MAJOR} << 24 |
${SAMPGDK_VERSION_MINOR} << 16 |
${SAMPGDK_VERSION_PATCH} << 8")
if(NOT (MSVC_IDE AND MSVC_VERSION LESS 1600))
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
endif()
find_package(SAMPSDK REQUIRED)
find_package(PythonInterp 2.7 REQUIRED)
include_directories(
${SAMPSDK_INCLUDE_DIR}
${SAMPSDK_INCLUDE_DIR}/amx
${PROJECT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}/include
${PROJECT_SOURCE_DIR}/lib/sampgdk
)
macro(sampgdk_add_subproject name)
add_subdirectory(${name})
file(RELATIVE_PATH folder ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TARGET ${name} PROPERTY FOLDER ${folder})
endmacro()
macro(sampgdk_get_property name var)
get_property(${var} DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY ${name})
endmacro()
macro(sampgdk_set_property name value)
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY ${name} ${value})
endmacro()
macro(sampgdk_append_property name value)
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} APPEND PROPERTY ${name} ${value})
endmacro()
macro(sampgdk_add_sources)
foreach(file ${ARGN})
if(NOT IS_ABSOLUTE ${file})
sampgdk_append_property(SAMPGDK_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/${file})
else()
sampgdk_append_property(SAMPGDK_SOURCES ${file})
endif()
endforeach()
endmacro()
macro(sampgdk_get_sources var)
sampgdk_get_property(SAMPGDK_SOURCES ${var})
endmacro()
macro(sampgdk_add_headers)
foreach(file ${ARGN})
if(NOT IS_ABSOLUTE ${file})
sampgdk_append_property(SAMPGDK_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/${file})
else()
sampgdk_append_property(SAMPGDK_HEADERS ${file})
endif()
endforeach()
endmacro()
macro(sampgdk_get_headers var)
sampgdk_get_property(SAMPGDK_HEADERS ${var})
endmacro()
macro(sampgdk_add_libraries)
foreach(library ${ARGN})
sampgdk_append_property(SAMPGDK_LIBRARIES ${library})
endforeach()
endmacro()
macro(sampgdk_get_libraries var)
sampgdk_get_property(SAMPGDK_LIBRARIES ${var})
endmacro()
macro(sampgdk_add_api_gen module_name idl_file api_file)
add_custom_command(
OUTPUT ${api_file}
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_BINARY_DIR}/scripts/python_wrapper.py
${PROJECT_SOURCE_DIR}/scripts/generate_code.py
--module=${module_name}
--idl=${idl_file}
--api=${api_file}
DEPENDS ${idl_file} ${PROJECT_SOURCE_DIR}/scripts/generate_code.py)
sampgdk_add_sources(${api_file})
endmacro()
macro(sampgdk_add_source_gen module_name idl_file source_file)
add_custom_command(
OUTPUT ${source_file}
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_BINARY_DIR}/scripts/python_wrapper.py
${PROJECT_SOURCE_DIR}/scripts/generate_code.py
--module=${module_name}
--idl=${idl_file}
--source=${source_file}
DEPENDS ${idl_file} ${PROJECT_SOURCE_DIR}/scripts/generate_code.py)
sampgdk_add_sources(${source_file})
endmacro()
macro(sampgdk_add_header_gen module_name idl_file header_file)
add_custom_command(
OUTPUT ${header_file}
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_BINARY_DIR}/scripts/python_wrapper.py
${PROJECT_SOURCE_DIR}/scripts/generate_code.py
--module=${module_name}
--idl=${idl_file}
--header=${header_file}
DEPENDS ${idl_file} ${PROJECT_SOURCE_DIR}/scripts/generate_code.py)
sampgdk_add_headers(${header_file})
endmacro()
macro(sampgdk_add_init_gen target list_file source_file)
add_custom_target(
${target}
COMMAND ${PYTHON_EXECUTABLE}
${PROJECT_SOURCE_DIR}/scripts/generate_init.py
--list=${list_file}
--source=${source_file}
DEPENDS ${list_file} ${PROJECT_SOURCE_DIR}/scripts/generate_init.py)
sampgdk_add_sources(${source_file})
endmacro()
macro(sampgdk_add_exports_gen format output_file)
add_custom_command(
OUTPUT ${output_file}
COMMAND ${PYTHON_EXECUTABLE}
${PROJECT_SOURCE_DIR}/scripts/generate_exports.py
--format=${format}
--output=${output_file}
${ARGN}
DEPENDS ${ARGN} ${PROJECT_SOURCE_DIR}/scripts/generate_exports.py)
endmacro()
add_subdirectory(cmake)
if(SAMPGDK_BUILD_DOCS)
add_subdirectory(doc)
endif()
add_subdirectory(include)
add_subdirectory(lib)
if(SAMPGDK_BUILD_PLUGINS)
add_subdirectory(plugins)
endif()
add_subdirectory(scripts)
sampgdk_get_sources(SAMPGDK_SOURCES)
foreach(file IN LISTS SAMPGDK_SOURCES)
get_filename_component(extension ${file} EXT)
if(extension STREQUAL ".idl")
file(RELATIVE_PATH path ${PROJECT_SOURCE_DIR}/lib/sampgdk ${file})
get_filename_component(dir ${path} PATH)
get_filename_component(name ${file} NAME_WE)
sampgdk_add_api_gen(${name} ${file}
${PROJECT_BINARY_DIR}/lib/sampgdk/${dir}/${name}.api)
sampgdk_add_source_gen(${name} ${file}
${PROJECT_BINARY_DIR}/lib/sampgdk/${dir}/${name}.c)
sampgdk_add_header_gen(${name} ${file}
${PROJECT_BINARY_DIR}/include/sampgdk/${dir}/${name}.h)
list(APPEND SAMPGDK_IDLS ${file})
endif()
endforeach()
sampgdk_get_sources(SAMPGDK_SOURCES)
foreach(file IN LISTS SAMPGDK_SOURCES)
get_filename_component(extension ${file} EXT)
if(extension STREQUAL ".api")
list(APPEND SAMPGDK_APIS ${file})
endif()
endforeach()
sampgdk_get_property(SAMPGDK_DEF_FILE SAMPGDK_DEF_FILE)
if(SAMPGDK_DEF_FILE)
sampgdk_add_exports_gen("def" ${SAMPGDK_DEF_FILE} ${SAMPGDK_APIS})
endif()
sampgdk_get_property(SAMPGDK_SYM_FILE SAMPGDK_SYM_FILE)
if(SAMPGDK_SYM_FILE)
sampgdk_add_exports_gen("list" ${SAMPGDK_SYM_FILE} ${SAMPGDK_APIS})
endif()
sampgdk_get_sources(SAMPGDK_SOURCES)
sampgdk_get_headers(SAMPGDK_HEADERS)
list(REMOVE_ITEM SAMPGDK_SOURCES ${SAMPGDK_IDLS})
if(SAMPGDK_STATIC)
add_library(sampgdk STATIC ${SAMPGDK_HEADERS} ${SAMPGDK_SOURCES})
else()
add_library(sampgdk SHARED ${SAMPGDK_HEADERS} ${SAMPGDK_SOURCES})
endif()
get_target_property(SAMPGDK_OUTPUT_NAME sampgdk OUTPUT_NAME)
if(NOT SAMPGDK_OUTPUT_NAME)
set(SAMPGDK_OUTPUT_NAME sampgdk)
endif()
if(WIN32)
set(SAMPGDK_OUTPUT_NAME ${SAMPGDK_OUTPUT_NAME}${SAMPGDK_VERSION_MAJOR})
if(SAMPGDK_STATIC)
set(SAMPGDK_OUTPUT_NAME ${SAMPGDK_OUTPUT_NAME}s)
endif()
endif()
set_target_properties(sampgdk PROPERTIES
VERSION ${SAMPGDK_VERSION}
SOVERSION ${SAMPGDK_VERSION_MAJOR}
DEFINE_SYMBOL IN_SAMPGDK
OUTPUT_NAME ${SAMPGDK_OUTPUT_NAME}
DEBUG_POSTFIX d
)
if(SAMPGDK_STATIC)
target_compile_definitions(sampgdk PUBLIC SAMPGDK_STATIC)
endif()
if(WIN32)
target_compile_definitions(sampgdk PRIVATE WIN32_LEAN_AND_MEAN)
elseif(UNIX)
target_compile_definitions(sampgdk PRIVATE _GNU_SOURCE)
endif()
if(MSVC)
target_compile_definitions(sampgdk PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()
if(UNIX)
target_compile_options(sampgdk PRIVATE -m32 -fvisibility=hidden)
if(NOT SAMPGDK_STATIC)
target_link_libraries(sampgdk PRIVATE
-m32 -Wl,--no-undefined,--retain-symbols-file=${SAMPGDK_SYM_FILE})
endif()
endif()
if(UNIX)
sampgdk_add_libraries(${CMAKE_DL_LIBS} rt)
endif()
sampgdk_get_libraries(SAMPGDK_LIBRARIES)
if(SAMPGDK_STATIC)
target_link_libraries(sampgdk INTERFACE ${SAMPGDK_LIBRARIES})
else()
target_link_libraries(sampgdk PRIVATE ${SAMPGDK_LIBRARIES})
endif()
if(SAMPGDK_BUILD_AMALGAMATION)
foreach(file IN LISTS SAMPGDK_SOURCES)
if (file MATCHES ".*\\.c" OR file MATCHES ".*\\.h")
list(APPEND SAMPGDK_AMALGAMATE_FLAGS --source "${file}")
endif()
endforeach()
foreach(file IN LISTS SAMPGDK_HEADERS)
if (file MATCHES ".*\\.h")
list(APPEND SAMPGDK_AMALGAMATE_FLAGS --header "${file}")
endif()
endforeach()
add_custom_target(sampgdk_amalgamate ALL
COMMAND ${PYTHON_EXECUTABLE}
${PROJECT_SOURCE_DIR}/scripts/amalgamate.py
--include-dir ${PROJECT_SOURCE_DIR}/include
--include-dir ${PROJECT_BINARY_DIR}/include
--include-dir ${PROJECT_SOURCE_DIR}/lib/sampgdk
--output-source ${PROJECT_BINARY_DIR}/lib/sampgdk/sampgdk.c
--output-header ${PROJECT_BINARY_DIR}/lib/sampgdk/sampgdk.h
--source-preamble ${PROJECT_SOURCE_DIR}/lib/sampgdk/sampgdk.c
--header-preamble ${PROJECT_SOURCE_DIR}/lib/sampgdk/sampgdk.h
${SAMPGDK_AMALGAMATE_FLAGS}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMENT "Generating amalgamation")
add_dependencies(sampgdk_amalgamate sampgdk)
add_custom_target(sampgdk_amalgamate_package ALL
COMMAND ${PYTHON_EXECUTABLE}
${PROJECT_SOURCE_DIR}/scripts/zip.py
${PROJECT_BINARY_DIR}/sampgdk-${SAMPGDK_VERSION}-amalgamation.zip
${PROJECT_BINARY_DIR}/lib/sampgdk/sampgdk.c
${PROJECT_BINARY_DIR}/lib/sampgdk/sampgdk.h
${PROJECT_SOURCE_DIR}/doc/how-to-use-amalgamation.txt
COMMENT "Packaging amalgamation files")
add_dependencies(sampgdk_amalgamate_package sampgdk_amalgamate)
endif()
add_dependencies(sampgdk sampgdk_init)
foreach(header IN LISTS SAMPGDK_HEADERS)
get_filename_component(path ${header} PATH)
get_filename_component(abs_path ${path} ABSOLUTE)
string(FIND ${abs_path} ${PROJECT_BINARY_DIR} pos)
if(pos LESS 0)
file(RELATIVE_PATH dir ${PROJECT_SOURCE_DIR} ${abs_path})
else()
file(RELATIVE_PATH dir ${PROJECT_BINARY_DIR} ${abs_path})
endif()
install(FILES ${header} DESTINATION ${dir} COMPONENT dev)
endforeach()
install(TARGETS sampgdk RUNTIME DESTINATION bin COMPONENT bin
ARCHIVE DESTINATION lib COMPONENT dev
LIBRARY DESTINATION lib COMPONENT bin)
if(WIN32)
set(CPACK_PACKAGE_FILE_NAME sampgdk-${SAMPGDK_VERSION}-win32)
set(CPACK_GENERATOR ZIP)
elseif(UNIX)
set(CPACK_PACKAGE_FILE_NAME sampgdk-${SAMPGDK_VERSION}-linux)
set(CPACK_GENERATOR TGZ)
endif()
if(SAMPGDK_STATIC)
set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_FILE_NAME}-static)
endif()
include(CPack)