forked from swmpdg/Metamod-P-CMake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
405 lines (345 loc) · 11.5 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
###################################################
# #
# #
# Metamod-P CMake build file #
# Based on the Metamod-P Makefile #
# Written by Sam "Solokiller" Vanheer #
# #
# #
###################################################
cmake_minimum_required( VERSION 3.6 )
# function to collect all the sources from sub-directories
# into a single list
function(add_sources)
get_property(is_defined GLOBAL PROPERTY SRCS_LIST DEFINED)
if(NOT is_defined)
define_property(GLOBAL PROPERTY SRCS_LIST
BRIEF_DOCS "List of source files"
FULL_DOCS "List of source files to be compiled in one library")
endif()
# make absolute paths
set(SRCS)
foreach(s IN LISTS ARGN)
if(NOT IS_ABSOLUTE "${s}")
get_filename_component(s "${s}" ABSOLUTE)
endif()
list(APPEND SRCS "${s}")
endforeach()
# append to global list
set_property(GLOBAL APPEND PROPERTY SRCS_LIST "${SRCS}")
endfunction(add_sources)
# preprocess sources
function( preprocess_sources )
set(PREP_SRCS PARENT_SCOPE)
get_property(SRCS GLOBAL PROPERTY SRCS_LIST)
foreach(s IN LISTS SRCS)
file(RELATIVE_PATH rs "${CMAKE_CURRENT_SOURCE_DIR}" "${s}")
string(REGEX REPLACE "r$" "" o "${CMAKE_CURRENT_BINARY_DIR}/${rs}")
add_custom_command(
OUTPUT "${o}"
COMMAND ${CMAKE_COMMAND} -E copy "${s}" "${o}"
DEPENDS "${s}"
COMMENT "Creating ${o}"
VERBATIM
)
list(APPEND PREP_SRCS "${s}")
endforeach()
set(PREP_SRCS ${PREP_SRCS} PARENT_SCOPE)
endfunction( preprocess_sources )
function( create_source_groups _src_root_path )
get_property(SRCS GLOBAL PROPERTY SRCS_LIST)
foreach(_source IN ITEMS ${SRCS})
get_filename_component(_source_path "${_source}" PATH)
file(RELATIVE_PATH _source_path_rel "${_src_root_path}" "${_source_path}")
string(REPLACE "/" "\\" _group_path "${_source_path_rel}")
source_group("${_group_path}" FILES "${_source}")
endforeach()
endfunction( create_source_groups )
#Function to clear the sources list.
function( clear_sources )
set_property( GLOBAL PROPERTY SRCS_LIST "" )
set( PREP_SRCS PARENT_SCOPE )
endfunction( clear_sources )
# Function to add include files.
function( add_includes )
get_property(is_defined GLOBAL PROPERTY INCLUDES_LIST DEFINED)
if(NOT is_defined)
define_property(GLOBAL PROPERTY INCLUDES_LIST
BRIEF_DOCS "List of include files"
FULL_DOCS "List of include files to be compiled in one library")
endif()
# make absolute paths
set(INCLUDES)
foreach(s IN LISTS ARGN)
if(NOT IS_ABSOLUTE "${s}")
get_filename_component(s "${s}" ABSOLUTE)
endif()
list(APPEND INCLUDES "${s}")
endforeach()
# append to global list
set_property(GLOBAL APPEND PROPERTY INCLUDES_LIST "${INCLUDES}")
endfunction( add_includes )
# Function to install includes.
function( install_includes _include_root_path )
get_property(INCLUDES GLOBAL PROPERTY INCLUDES_LIST)
foreach(_include IN ITEMS ${INCLUDES})
get_filename_component(_include_path "${_include}" PATH)
file(RELATIVE_PATH _include_path_rel "${_include_root_path}" "${_include_path}")
string(REPLACE "/" "\\" _group_path "${_include_path_rel}")
install( FILES "${_include}" DESTINATION "include/${_group_path}" )
endforeach()
set_property( GLOBAL PROPERTY INCLUDES_LIST "" )
endfunction( install_includes )
macro(configure_msvc_runtime)
if(MSVC)
# Default to statically-linked runtime.
if("${MSVC_RUNTIME}" STREQUAL "")
set(MSVC_RUNTIME "static")
endif()
# Set compiler options.
set(variables
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
)
if(${MSVC_RUNTIME} STREQUAL "static")
message(STATUS
"MSVC -> forcing use of statically-linked runtime."
)
foreach(variable ${variables})
if(${variable} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}")
endif()
endforeach()
else()
message(STATUS
"MSVC -> forcing use of dynamically-linked runtime."
)
foreach(variable ${variables})
if(${variable} MATCHES "/MT")
string(REGEX REPLACE "/MT" "/MD" ${variable} "${${variable}}")
endif()
endforeach()
endif()
endif()
endmacro()
#
#Metamod-P project begin.
#
project( Metamod-P )
#Needs static CRT to avoid alloc issues.
set( MSVC_RUNTIME "static" )
configure_msvc_runtime()
set(variables
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
)
message(STATUS "Initial build flags:")
foreach(variable ${variables})
message(STATUS " '${variable}': ${${variable}}")
endforeach()
message(STATUS "")
#GoldSource is exclusively 32 bit so make sure it's always compatible.
if( UNIX )
set( LINUX_32BIT_FLAG "-m32" )
else()
set( LINUX_32BIT_FLAG "" )
endif()
set( DEPLOY_TO_GAME "0" CACHE BOOL "Whether to deploy libraries to the given game directory" )
set( GAME_BASE_DIRECTORY "" CACHE PATH "Path to the game directory. e.g. C:/steam/steamapps/common/Half-Life" )
if( NOT GAME_BASE_DIRECTORY )
MESSAGE( FATAL_ERROR "GAME_BASE_DIRECTORY is required to find needed libraries" )
endif()
set( MOD_NAME "" CACHE STRING "Name of the game. This is the name of the directory in GAME_BASE_DIRECTORY. Needed to deploy libraries to the correct mod directory" )
set( GAME_DIRECTORY ${GAME_BASE_DIRECTORY}/${MOD_NAME} )
#Can't deploy without a game directory.
if( NOT MOD_NAME AND DEPLOY_TO_GAME )
set( DEPLOY_TO_GAME "0" )
MESSAGE( WARNING "Can't deploy without a mod directory!" )
endif()
#The base directory for all Metamod data.
set( META_BASE_DIRECTORY ${GAME_DIRECTORY}/addons )
#Configurable by user.
set( SDK_DIRECTORY "${CMAKE_SOURCE_DIR}/hlsdk" CACHE PATH "Path to the Half-Life SDK directory" )
#Set global compiler settings.
if( MSVC )
#Warning level 4. Wall shows warnings in standard headers so we can't compile if that's enabled.
#Treat warnings as errors.
#When using the v140_xp toolset or newer, static local variables are initialized using thread safe initialization. This won't work on XP so we have to disable this behavior.
#See https://connect.microsoft.com/VisualStudio/Feedback/Details/1941836 for more information.
set( GLOBAL_COMPILER_FLAGS "/W4 /WX /Zc:threadSafeInit-" )
#Optimize for speed.
#Omit frame pointers.
#Precise floating point operations.
set( GLOBAL_COMPILER_FLAGS_RELEASE "/O2 /Oy /fp:precise" )
else()
#All warnings.
#No unknown pragma directives.
#No attributes.
set( GLOBAL_COMPILER_FLAGS "-Wall -Wno-unknown-pragmas -Wno-attributes" )
#Optimize for speed.
#Omit frame pointers.
#Precise floating point operations.
set( GLOBAL_COMPILER_FLAGS_RELEASE "-O2 -fomit-frame-pointer -ffast-math" )
#Cygwin uses GCC style settings.
if( CYGWIN )
set( GLOBAL_COMPILER_FLAGS
${GLOBAL_COMPILER_FLAGS}
-mno-cygwin
)
endif()
endif()
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GLOBAL_COMPILER_FLAGS}" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GLOBAL_COMPILER_FLAGS}" )
if( UNIX )
# debugging; halt on warnings
set( CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb3" )
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb3" )
endif()
set( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${GLOBAL_COMPILER_FLAGS_RELEASE}" )
set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${GLOBAL_COMPILER_FLAGS_RELEASE}" )
set( CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${GLOBAL_COMPILER_FLAGS_RELEASE}" )
set( CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${GLOBAL_COMPILER_FLAGS_RELEASE}" )
#C++14 support
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y" )
#Shared source files.
add_subdirectory( ${CMAKE_SOURCE_DIR}/shared shared )
preprocess_sources()
set( SHARED_SOURCES
${SDK_DIRECTORY}/public/interface.h
${SDK_DIRECTORY}/public/interface.cpp
${PREP_SRCS}
)
clear_sources()
#Shared sources for plugins.
add_subdirectory( ${CMAKE_SOURCE_DIR}/shared/plugin shared/plugin )
preprocess_sources()
set( SHARED_PLUGIN_SOURCES
${PREP_SRCS}
)
clear_sources()
#Shared include paths.
set( SHARED_INCLUDE_PATHS
${CMAKE_SOURCE_DIR}/metamod
${CMAKE_SOURCE_DIR}/shared
${SDK_DIRECTORY}/engine
${SDK_DIRECTORY}/common
${SDK_DIRECTORY}/public
${SDK_DIRECTORY}/pm_shared
${SDK_DIRECTORY}/dlls
${SDK_DIRECTORY}
${CMAKE_SOURCE_DIR}/external/SOURCE_SDK/public/steam
)
#The user can select an optimization setting.
#The Metamod-P library checks this to optimize internal calls.
set( OPT CACHE STRING "Optimization setting" )
set_property( CACHE OPT PROPERTY STRINGS debug opt opt-fast )
if( OPT STREQUAL "opt-fast" )
set( OPT_TYPE "OPT_TYPE=\"optimized+meta_debug-disabled\"" )
elseif( OPT STREQUAL "opt" )
set( OPT_TYPE "OPT_TYPE=\"optimized\"" )
elseif( NOT OPT STREQUAL "" )
#Any undefined option is treated as debug, except for an empty string because CMake won't fill in the var until it's selected.
set( OPT_TYPE "OPT_TYPE=\"debug\"" )
endif()
set( HLSDK_3_2_OLD_EIFACE "0" CACHE BOOL "Whether to use the old SDK interface for the Trace plugin" )
if( HLSDK_3_2_OLD_EIFACE )
set( HLSDK_3_2_OLD_EIFACE_FLAG "HLSDK_3_2_OLD_EIFACE" )
else()
set( HLSDK_3_2_OLD_EIFACE_FLAG )
endif()
#Shared preprocessor definitions.
#Use version safe interfaces.
set( SHARED_DEFINITIONS
${OPT_TYPE}
${HLSDK_3_2_OLD_EIFACE_FLAG}
VERSION_SAFE_STEAM_API_INTERFACES
)
if( OPT STREQUAL "opt-fast" )
#Disable debug output.
set( SHARED_DEFINITIONS
${SHARED_DEFINITIONS}
__BUILD_FAST_METAMOD__
)
endif()
if( WIN32 )
#Disable insecure CRT functions warning.
set( SHARED_DEFINITIONS
${SHARED_DEFINITIONS}
_CRT_SECURE_NO_WARNINGS
)
elseif( APPLE )
#Nothing so far.
elseif( UNIX )
set( SHARED_DEFINITIONS
${SHARED_DEFINITIONS}
linux
)
endif()
set( SHARED_PLUGIN_DEFS
)
#Shared library dependencies.
#Link with the Steamworks API. For non-Windows targets, prefer the library in the game over the local version.
if( WIN32 )
find_library( STEAM_API_LIB steam_api${CMAKE_IMPORT_LIBRARY_SUFFIX} PATHS ${CMAKE_SOURCE_DIR}/external/SOURCE_SDK/lib/public )
elseif( APPLE )
find_library( STEAM_API_LIB steam_api PATHS ${GAME_BASE_DIRECTORY} ${CMAKE_SOURCE_DIR}/external/SOURCE_SDK/lib/osx32 )
else()
find_library( STEAM_API_LIB steam_api PATHS ${GAME_BASE_DIRECTORY} ${CMAKE_SOURCE_DIR}/external/SOURCE_SDK/lib/linux32 )
endif()
set( SHARED_LIBRARY_DEPS
${STEAM_API_LIB}
)
if( NOT WIN32 )
#Link statically with the CRT and STL so it doesn't depend on the user's version.
#Note: STL dependency is hopefully temporary until its uses can be removed. - Solokiller
set( SHARED_LIBRARY_DEPS
${SHARED_LIBRARY_DEPS}
dl
m
-static-libgcc
-static-libstdc++
)
endif()
#Shared linker flags.
set( SHARED_LINKER_FLAGS
)
if( WIN32 )
#Alias stdcall for export.
if( NOT MSVC )
set( SHARED_LINKER_FLAGS
${SHARED_LINKER_FLAGS}
-Xlinker --add-stdcall-alias
)
if( CYGWIN )
set( SHARED_LINKER_FLAGS
${SHARED_LINKER_FLAGS}
-L/lib/w32api
)
endif()
endif()
else()
set( SHARED_LINKER_FLAGS
${SHARED_LINKER_FLAGS}
-fPIC
)
endif()
#Add subdirectories here.
add_subdirectory( metamod )
add_subdirectory( stub_plugin )
add_subdirectory( trace_plugin )
add_subdirectory( wdmisc_plugin )
#
#Metamod-P project end.
#