Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit ca74647

Browse files
authored
Create CMakeLists.txt
1 parent 3e63048 commit ca74647

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed

CMakeLists.txt

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
# oh yes sir, the Linux support is gonna be here soon
2+
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
3+
4+
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
5+
6+
if (APPLE)
7+
# Secton: UNTESTED
8+
# NOTE: You will need to pass this as a command line parameter for the Xcode generator -DCMAKE_OSX_ARCHITECTURES=i386
9+
# Also note only Xcode 9.4.1 and earlier support i386
10+
set(CMAKE_OSX_ARCHITECTURES i386)
11+
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9)
12+
set(CMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT "dwarf-with-dsym")
13+
endif()
14+
15+
project(SourceDEFUN)
16+
17+
# For some reason, checking if CMAKE_BUILD_TYPE is defined is unreliable
18+
# So simply check if it's empty instead
19+
if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
20+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
21+
endif()
22+
23+
# Modern VS versions default to C++14 anyway, so make it consistent
24+
# But in the future we may want so support C++20
25+
set(CMAKE_CXX_STANDARD 14)
26+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
27+
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
28+
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
29+
30+
# This is a way to emulate groups.vgc
31+
set(BUILD_GROUP "game" CACHE STRING "Build Group")
32+
33+
# For the CMake GUIs that support a combobox list
34+
set_property(CACHE BUILD_GROUP PROPERTY STRINGS
35+
"everything"
36+
"game"
37+
"shaders"
38+
)
39+
40+
# Which game are we building?
41+
set(BUILD_GAME "hl2" CACHE STRING "Build Game")
42+
43+
set_property(
44+
CACHE BUILD_GAME PROPERTY STRINGS
45+
"hl2"
46+
"episodic"
47+
)
48+
49+
set(SRCDIR "${CMAKE_CURRENT_LIST_DIR}")
50+
set(GAMEDIR "${CMAKE_CURRENT_LIST_DIR}/game")
51+
set(THIRDPARTYDIR "${SRCDIR}/thirdparty")
52+
53+
# Compile options that are populated and set for each target depending on their type
54+
set(ADDITIONAL_COMPILE_OPTIONS_EXE)
55+
set(ADDITIONAL_COMPILE_OPTIONS_DLL)
56+
set(ADDITIONAL_COMPILE_OPTIONS_LIB)
57+
58+
# Libraries that are linked to for each target depending on their type
59+
set(ADDITIONAL_LINK_LIBRARIES_EXE)
60+
set(ADDITIONAL_LINK_LIBRARIES_DLL)
61+
62+
# Linker options that are populated and set for each target depending on their type
63+
set(ADDITIONAL_LINK_OPTIONS_EXE)
64+
set(ADDITIONAL_LINK_OPTIONS_DLL)
65+
set(ADDITIONAL_LINK_OPTIONS_LIB)
66+
67+
# Sources that are added to each target depending on their type
68+
set(ADDITIONAL_SOURCES_EXE)
69+
set(ADDITIONAL_SOURCES_DLL)
70+
set(ADDITIONAL_SOURCES_LIB)
71+
72+
# Compile definitions that are added to each target depending on their type
73+
set(ADDITIONAL_COMPILE_DEFINITIONS_EXE)
74+
set(ADDITIONAL_COMPILE_DEFINITIONS_DLL)
75+
set(ADDITIONAL_COMPILE_DEFINITIONS_LIB)
76+
77+
include("_cmake_scripts/pch_skip.cmake")
78+
include("_cmake_scripts/platform_dirs.cmake")
79+
include("_cmake_scripts/base.cmake")
80+
include("_cmake_scripts/video_base.cmake")
81+
include("_cmake_scripts/postbuild.cmake")
82+
83+
set(LIBPUBLIC "${SRCDIR}/lib/public${PLATSUBDIR}")
84+
set(LIBCOMMON "${SRCDIR}/lib/common${PLATSUBDIR}")
85+
86+
link_directories(
87+
${LIBPUBLIC}
88+
${LIBCOMMON}
89+
)
90+
91+
include_directories(
92+
"${SRCDIR}/common"
93+
"${SRCDIR}/public"
94+
"${SRCDIR}/public/tier0"
95+
"${SRCDIR}/public/tier1"
96+
)
97+
98+
add_compile_definitions($<$<CONFIG:Debug>:DEBUG> $<$<CONFIG:Debug>:_DEBUG>)
99+
add_compile_definitions($<$<CONFIG:Release>:NDEBUG>)
100+
101+
if (${IS_WINDOWS})
102+
include("_cmake_scripts/windows_base.cmake")
103+
elseif (${IS_LINUX} OR ${IS_OSX})
104+
include("_cmake_scripts/posix_base.cmake")
105+
endif()
106+
107+
include("_cmake_scripts/groups.cmake")
108+
109+
# Store all targets in a variable name ( See: https://stackoverflow.com/questions/37434946/how-do-i-iterate-over-all-cmake-targets-programmatically/62311397#62311397 )
110+
function(get_all_targets var)
111+
set(targets)
112+
get_all_targets_recursive(targets ${CMAKE_CURRENT_SOURCE_DIR})
113+
set(${var} ${targets} PARENT_SCOPE)
114+
endfunction()
115+
116+
macro(get_all_targets_recursive targets dir)
117+
get_property(subdirectories DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
118+
foreach(subdir ${subdirectories})
119+
get_all_targets_recursive(${targets} ${subdir})
120+
endforeach()
121+
122+
get_property(current_targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
123+
list(APPEND ${targets} ${current_targets})
124+
endmacro()
125+
126+
get_all_targets(ALL_TARGETS)
127+
128+
# Set of helper functions to add defintions/options/libs for each target in a filtered way
129+
function(add_compile_definitions_filtered target definitions)
130+
foreach(additional_definition IN LISTS ${definitions})
131+
set(SHOULD_EXCLUDE 0)
132+
# Exclude the compile definition if target defines an exclude list
133+
foreach(exclude IN LISTS "${target}_exclude_compile_definitions")
134+
if (${additional_definition} STREQUAL ${exclude})
135+
set(SHOULD_EXCLUDE 1)
136+
break()
137+
endif()
138+
endforeach()
139+
if (NOT ${SHOULD_EXCLUDE})
140+
target_compile_definitions(${target} PRIVATE ${additional_definition})
141+
endif()
142+
endforeach()
143+
endfunction()
144+
145+
146+
function(add_compile_options_filtered target options)
147+
foreach(additional_option IN LISTS ${options})
148+
set(SHOULD_EXCLUDE 0)
149+
# Exclude the compile options if target defines an exclude list
150+
foreach(exclude IN LISTS "${target}_exclude_compile_options")
151+
if (${additional_option} STREQUAL ${exclude})
152+
set(SHOULD_EXCLUDE 1)
153+
break()
154+
endif()
155+
endforeach()
156+
if (NOT ${SHOULD_EXCLUDE})
157+
target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:${additional_option}>")
158+
endif()
159+
endforeach()
160+
endfunction()
161+
162+
function(add_sources_filtered target sources)
163+
foreach(additional_source IN LISTS ${sources})
164+
set(SHOULD_EXCLUDE 0)
165+
# Exclude the source if target defines an exclude list
166+
foreach(exclude IN LISTS "${target}_exclude_source")
167+
if (${additional_source} STREQUAL ${exclude})
168+
set(SHOULD_EXCLUDE 1)
169+
break()
170+
endif()
171+
endforeach()
172+
if (NOT ${SHOULD_EXCLUDE})
173+
target_sources(${target} PRIVATE ${additional_source})
174+
endif()
175+
endforeach()
176+
endfunction()
177+
178+
function(add_libraries_filtered target libraries)
179+
foreach(additional_lib IN LISTS ${libraries})
180+
set(SHOULD_EXCLUDE 0)
181+
# Exclude the lib if target defines an exclude list
182+
foreach(exclude IN LISTS "${target}_exclude_lib")
183+
if (${additional_lib} STREQUAL ${exclude})
184+
set(SHOULD_EXCLUDE 1)
185+
break()
186+
endif()
187+
endforeach()
188+
if (NOT ${SHOULD_EXCLUDE})
189+
get_target_property(libraries ${target} LINK_LIBRARIES)
190+
# Don't bother adding it if the target already links it manually
191+
foreach(lib IN LISTS libraries)
192+
if (${additional_lib} STREQUAL ${lib})
193+
set(SHOULD_EXCLUDE 1)
194+
break()
195+
endif()
196+
endforeach()
197+
endif()
198+
if (NOT ${SHOULD_EXCLUDE})
199+
target_link_libraries(${target} PRIVATE ${additional_lib})
200+
endif()
201+
endforeach()
202+
endfunction()
203+
204+
# Iterates over all the targets and add necessary definitions/options/libs
205+
# This is an incredible hack, but it allows for targets to specify exclude lists
206+
# This allows us to emulate -$File and such from VPC
207+
foreach(target ${ALL_TARGETS})
208+
# Define an empty exclude list if one isn't defined
209+
if (NOT DEFINED "${target}_exclude_compile_options")
210+
set("${target}_exclude_compile_options")
211+
endif()
212+
213+
# Define an empty exclude list if one isn't defined
214+
if (NOT DEFINED "${target}_exclude_lib")
215+
set("${target}_exclude_lib")
216+
endif()
217+
218+
# Define an empty exclude list if one isn't defined
219+
if (NOT DEFINED "${target}_exclude_source")
220+
set("${target}_exclude_source")
221+
endif()
222+
223+
get_target_property(target_type ${target} TYPE)
224+
if (${target_type} STREQUAL "EXECUTABLE")
225+
add_compile_options_filtered(${target} ADDITIONAL_COMPILE_OPTIONS_EXE)
226+
add_libraries_filtered(${target} ADDITIONAL_LINK_LIBRARIES_EXE)
227+
add_sources_filtered(${target} ADDITIONAL_SOURCES_EXE)
228+
target_link_options(${target} PRIVATE ${ADDITIONAL_LINK_OPTIONS_EXE})
229+
target_compile_definitions(${target} PRIVATE MEMOVERRIDE_MODULE=$<TARGET_NAME_IF_EXISTS:${target}>)
230+
add_compile_definitions_filtered(${target} ADDITIONAL_COMPILE_DEFINITIONS_EXE)
231+
232+
# Only applies to Linux and OSX
233+
target_strip_symbols(${target})
234+
elseif((${target_type} STREQUAL "SHARED_LIBRARY") OR (${target_type} STREQUAL "MODULE_LIBRARY"))
235+
add_compile_options_filtered(${target} ADDITIONAL_COMPILE_OPTIONS_DLL)
236+
add_libraries_filtered(${target} ADDITIONAL_LINK_LIBRARIES_DLL)
237+
add_sources_filtered(${target} ADDITIONAL_SOURCES_DLL)
238+
target_link_options(${target} PRIVATE ${ADDITIONAL_LINK_OPTIONS_DLL})
239+
target_compile_definitions(${target} PRIVATE MEMOVERRIDE_MODULE=$<TARGET_NAME_IF_EXISTS:${target}> DLLNAME=$<TARGET_NAME_IF_EXISTS:${target}>)
240+
add_compile_definitions_filtered(${target} ADDITIONAL_COMPILE_DEFINITIONS_DLL)
241+
242+
# Only applies to Linux and OSX
243+
target_strip_symbols(${target})
244+
elseif(${target_type} STREQUAL "STATIC_LIBRARY")
245+
add_compile_options_filtered(${target} ADDITIONAL_COMPILE_OPTIONS_LIB)
246+
add_sources_filtered(${target} ADDITIONAL_SOURCES_LIB)
247+
target_link_options(${target} PRIVATE ${ADDITIONAL_LINK_OPTIONS_LIB})
248+
target_compile_definitions(${target} PRIVATE LIBNAME=$<TARGET_NAME_IF_EXISTS:${target}>)
249+
add_compile_definitions_filtered(${target} ADDITIONAL_COMPILE_DEFINITIONS_LIB)
250+
endif()
251+
252+
endforeach()

0 commit comments

Comments
 (0)