]> git.xonotic.org Git - xonotic/darkplaces.git/blob - buildsys/target/engine.cmake
cmake: Initial working implementation of cmake build system
[xonotic/darkplaces.git] / buildsys / target / engine.cmake
1 add_library(common INTERFACE)
2 #target_sources(common INTERFACE ${OBJ_COMMON})
3 target_compile_definitions(common INTERFACE -D_FILE_OFFSET_BITS=64 -D__KERNEL_STRICT_NAMES)
4
5 set(ENV{TZ} "UTC")
6
7 # Build a version string for the engine.
8 execute_process(
9         COMMAND git rev-parse --short HEAD
10         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
11         OUTPUT_VARIABLE revision
12         OUTPUT_STRIP_TRAILING_WHITESPACE
13 )
14
15 execute_process(
16         COMMAND "git show -s --format=%ad --date='format-local:%a %b %d %Y %H:%I:%S UTC'"
17         OUTPUT_VARIABLE timestamp
18         OUTPUT_STRIP_TRAILING_WHITESPACE
19 )
20 set(DP_BUILD_REVISION "${timestamp} - ${revision}")
21
22 if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x|i[36])86|AMD64")
23         option(ENGINE_CONFIG_SSE "Build with SSE support (x86 and x86_64 only)" ON)
24 else()
25         set(ENGINE_CONFIG_SSE OFF)
26 endif()
27
28 if(NOT MSVC)
29         # Flags for gcc, clang, tcc...
30
31         # NOTE: *never* *ever* use the -ffast-math or -funsafe-math-optimizations flag
32         # Also, since gcc 5, -ffinite-math-only makes NaN and zero compare equal inside engine code but not inside QC, which causes error spam for seemingly valid QC code like if (x != 0) return 1 / x;
33
34         target_compile_options(common INTERFACE
35                 -Wall -Winline -Werror=c++-compat -Wwrite-strings -Wshadow -Wold-style-definition -Wstrict-prototypes -Wsign-compare -Wdeclaration-after-statement -Wmissing-prototypes
36                 $<$<CONFIG:RELEASE>:-O3 -fno-strict-aliasing -fno-math-errno -fno-rounding-math -fno-trapping-math>
37                 $<$<CONFIG:DEBUG>:-ggdb>
38                 $<$<CONFIG:PROFILE>:-g -pg -ggdb -fprofile-arcs>
39                 $<$<CONFIG:PROFILERELEASE>:-fbranch-probabilities>
40         )
41
42         if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
43                 target_compile_options(common INTERFACE $<$<CONFIG:RELEASE>:-fno-signaling-nans>)
44         endif()
45
46         target_link_libraries(common INTERFACE -lm)
47
48         if(CMAKE_C_COMPILER_ID STREQUAL "TinyCC")
49                 target_compile_definitions(common INTERFACE -DSDL_DISABLE_IMMINTRIN_H)
50         endif()
51
52         if(ENGINE_CONFIG_PEDANTIC)
53                 target_compile_options(common INTERFACE -std=c11 -pedantic)
54                 target_compile_definitions(common INTERFACE -D_POSIX_C_SOURCE=200809L -DCONFIG_PEDANTIC)
55         endif()
56
57         if(ENGINE_CONFIG_SSE)
58                 target_compile_options(common INTERFACE -msse)
59         endif()
60
61         if(WIN32) # Windows MinGW
62                 if(CMAKE_SYSTEM_PROCESSOR MATCHES "i[36]86")
63                         target_link_options(common INTERFACE --large-address-aware)
64                 endif()
65                 target_link_libraries(common INTERFACE -lwinmm -limm32 -lversion -lwsock32 -lws2_32)
66         elseif(APPLE) # MacOS, iOS
67                 target_link_libraries(common INTERFACE -ldl -framework IOKit -framework CoreFoundation)
68         elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS") # SunOS, Solaris, OpenSolaris, illumos (OpenIndiana and friends)
69                 target_link_libraries(common INTERFACE -lrt -ldl -lsocket -lnsl)
70         else() # Linux, the BSDs, and probably everything else
71                 target_link_libraries(common INTERFACE -lrt -ldl)
72         endif()
73 else()
74         # Flags for Visual Studio
75         target_compile_options(common INTERFACE
76                 /W4 /Wd4706;4127;4100;4055;4054;4244;4305;4702;4201 /MP
77                 $<$<CONFIG:RELEASE>:/MD /Gy /O2 /Oi /Zi>
78                 $<$<CONFIG:DEBUG>:/Od /RTC1 /MDd /Gm>
79         )
80
81         target_link_options(common INTERFACE
82                 /SUBSYSTEM:WINDOWS
83                 $<$<CONFIG:RELEASE>:/OPT:REF /OPT:ICF>
84                 $<$<CONFIG:DEBUG>:/DEBUG /NODEFAULTLIB:msvcrt.lib>
85         )
86
87         if(CMAKE_SYSTEM_PROCESOR STREQUAL "AMD64")
88                 target_compile_options(client INTERFACE /Zi)
89         else()
90                 target_compile_options(client INTERFACE /ZI)
91                 target_link_options(common INTERFACE /LARGEADDRESSAWARE)
92         endif()
93 endif()
94
95 if(ENGINE_VERSION)
96         target_compile_definitions(common INTERFACE -DSVNREVISION=${ENGINE_VERSION})
97 endif()
98
99 if(CMAKE_BUILD_TYPE)
100         target_compile_definitions(common INTERFACE -DBUILDTYPE=${CMAKE_BUILD_TYPE})
101 endif()
102
103 if(ENGINE_NO_BUILD_TIMESTAMPS)
104         target_compile_definitions(common INTERFACE -DNO_BUILD_TIMESTAMPS)
105 endif()
106
107 if(ENGINE_BUILD_CLIENT)
108         include(buildsys/target/engine/client.cmake)
109 endif()
110
111 if(ENGINE_BUILD_SERVER)
112         include(buildsys/target/engine/server.cmake)
113 endif()