c383a1c937709271aecf72e57db0b362096ffe34
[taylor/freespace2.git] / CMakeLists.txt
1
2 project(freespace2)
3
4 cmake_minimum_required(VERSION 2.8)
5 cmake_policy(VERSION 2.8)
6
7 if(POLICY CMP0043)
8   cmake_policy(SET CMP0043 OLD)
9 endif()
10
11 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
12
13 include(CreateSourceGroups)
14
15
16 # ##############################################################################
17
18 option(FS1 "Build original FreeSpace" OFF)
19 option(DEMO "Create demo build" OFF)
20
21 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
22   message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
23   set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
24   # Set the possible values of build type for cmake-gui
25   set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "RelWithDebInfo")
26 endif()
27
28
29 if(FS1)
30   add_definitions(-DMAKE_FS1)
31
32   if(DEMO)
33         add_definitions(-DDEMO -DFS1_DEMO)
34   endif()
35 else()
36   if(DEMO)
37         add_definitions(-DFS2_DEMO)
38   endif()
39 endif()
40
41 # build everything by default
42 set(GAME_ONLY)
43
44 if(DEMO)
45   set(GAME_ONLY 1)
46 endif()
47
48 if(EMSCRIPTEN)
49   set(GAME_ONLY 1)
50   add_definitions(-s USE_SDL=2)
51   add_definitions(-s FULL_ES2=1)
52   #add_definitions(-s EXPORTED_FUNCTIONS="[_main]")
53
54   set(CMAKE_EXECUTABLE_SUFFIX ".html")
55 endif()
56
57 if(NOT EMSCRIPTEN)
58   add_definitions(-DLEGACY_GL)
59 endif()
60
61 if(NOT WIN32)
62   set(PLATFORM_LIBRARIES "")
63
64   add_definitions(-DPLAT_UNIX)
65   add_definitions(-Wall)
66   add_definitions(-Wshadow)
67 # add_definitions(-Wno-format-y2k)
68 # add_definitions(-Wno-deprecated)
69   add_definitions(-fsigned-char)
70
71   set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -DNDEBUG -g -O2")
72   set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -DNDEBUG -Os")
73   set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG -DRELEASE_REAL -O3")
74   set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3")
75 else()
76   set(SDL2DIR "${CMAKE_SOURCE_DIR}/../Support/SDL2" CACHE PATH "Path to SDL2")
77   set(OPENALDIR "${CMAKE_SOURCE_DIR}/../Support/OpenAL" CACHE PATH "Path to OpenAL")
78   set(LWSDIR "${CMAKE_SOURCE_DIR}/../Support/libwebsockets" CACHE PATH "Path to libwebsockets")
79   set(WXWIDGETSDIR "${CMAKE_SOURCE_DIR}/../Support/wxWidgets" CACHE PATH "Path to wxWidgets")
80   set(ANGLEDIR "${CMAKE_SOURCE_DIR}/../Support/ANGLE" CACHE PATH "Path to ANGLE")
81
82   set(PLATFORM_LIBRARIES
83         ws2_32.lib
84         ${ANGLEDIR}/lib/libEGL.lib
85         ${ANGLEDIR}/lib/libGLESv2.lib
86   )
87
88   add_definitions(/D _CRT_SECURE_NO_WARNINGS)
89   add_definitions(/W4)
90   add_definitions(/wd4100) # unreferenced parameter
91   add_definitions(/wd4127) # conditional expression is constant: do { } while (0)
92   add_definitions(/wd4996) # deprecated functions: fopen, fileno, ...
93 endif()
94
95 set(BIN_SUFFIX "")
96
97 if(NOT FS1)
98         set(BIN_SUFFIX "2")
99 endif()
100
101 if(DEMO)
102         set(BIN_SUFFIX "${BIN_SUFFIX}demo")
103 endif()
104
105 find_package(SDL2 "2.0.5" REQUIRED)
106 find_package(OpenGL REQUIRED)
107 find_package(OpenAL REQUIRED)
108
109 if(NOT EMSCRIPTEN)
110   find_package(LibWebSockets REQUIRED)
111
112   find_package(wxWidgets COMPONENTS core base gl net)
113   include(${wxWidgets_USE_FILE})
114 endif()
115
116 # ##############################################################################
117
118
119 include_directories(
120   ${PROJECT_SOURCE_DIR}/include
121   ${CMAKE_BINARY_DIR}/include
122   ${SDL2_INCLUDE_DIR}
123   ${OPENAL_INCLUDE_DIR}
124   ${LIBWEBSOCKETS_INCLUDE_DIR}
125   ${wxWidgets_INCLUDE_DIRS}
126 )
127
128
129 add_subdirectory(src)
130 add_subdirectory(include)
131
132
133 # ##############################################################################
134 #
135
136 #
137 # custom target for source tree version info
138 #
139
140 add_custom_target(buildver
141   COMMAND ${CMAKE_COMMAND}
142     -DSOURCE_DIR=${CMAKE_SOURCE_DIR}
143     -DBINARY_DIR=${CMAKE_BINARY_DIR}
144     -P ${CMAKE_SOURCE_DIR}/cmake/GitInfo.cmake
145   WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
146   SOURCES ${CMAKE_SOURCE_DIR}/cmake/GitInfo.cmake
147 )
148
149 #
150 # main code/game library
151 #
152
153 add_library(code
154   STATIC
155   ${code_SOURCE}
156   ${code_HEADERS}
157   ${platform_SOURCE}
158   ${platform_HEADERS}
159 )
160
161 CreateSourceGroups(${code_SOURCE} ${platform_SOURCE})
162
163 add_dependencies(code
164   buildver
165 )
166
167 #
168 # the game itself
169 #
170
171 set(FS_BINARY "fs${BIN_SUFFIX}")
172
173 add_executable(${FS_BINARY}
174   WIN32
175   ${freespace_SOURCE}
176   ${freespace_HEADERS}
177 )
178
179 CreateSourceGroups(${freespace_SOURCE})
180
181 target_link_libraries(
182   ${FS_BINARY}
183   code
184   ${SDL2_LIBRARY}
185   ${OPENGL_LIBRARIES}
186   ${OPENAL_LIBRARY}
187   ${LIBWEBSOCKETS_LIBRARIES}
188   ${PLATFORM_LIBRARIES}
189 )
190
191 if (EMSCRIPTEN)
192   set_target_properties(${FS_BINARY} PROPERTIES LINK_FLAGS "-s USE_SDL=2 -s FULL_ES2=1 -s TOTAL_MEMORY=184549376 --preload-file game@/")
193 endif()
194
195 if(FS1)
196   set_target_properties(${FS_BINARY} PROPERTIES PROJECT_LABEL "Freespace")
197 else()
198   set_target_properties(${FS_BINARY} PROPERTIES PROJECT_LABEL "Freespace2")
199 endif()
200
201 #
202 # launcher
203 #
204
205 if(NOT GAME_ONLY)
206   set(LAUNCHER_BINARY "freespace${BIN_SUFFIX}")
207
208   add_executable(${LAUNCHER_BINARY}
209   WIN32
210     ${launcher_SOURCE}
211     ${launcher_HEADERS}
212   )
213
214   CreateSourceGroups(${launcher_SOURCE})
215
216   target_link_libraries(
217     ${LAUNCHER_BINARY}
218     ${SDL2_LIBRARY}
219     ${OPENGL_LIBRARIES}
220     ${OPENAL_LIBRARY}
221     ${wxWidgets_LIBRARIES}
222     ${PLATFORM_LIBRARIES}
223   )
224
225   set_target_properties(${LAUNCHER_BINARY} PROPERTIES PROJECT_LABEL "Launcher")
226
227   # wxWidgets appears to need c++11 for one or more headers
228   if(NOT WIN32)
229   set_target_properties(${LAUNCHER_BINARY} PROPERTIES COMPILE_FLAGS -std=c++11)
230   endif()
231 endif(NOT GAME_ONLY)
232
233 #
234 # standalone server GUI
235 #
236
237 if(NOT GAME_ONLY)
238   # Not for FS1 Demo
239   if(NOT FS1 OR NOT DEMO)
240   if(FS1)
241     set(STANDALONE_BINARY fsstandalone)
242     else()
243     set(STANDALONE_BINARY fs2standalone)
244     endif()
245
246   add_executable(${STANDALONE_BINARY}
247     WIN32
248     ${standalone_SOURCE}
249     ${standalone_HEADERS}
250   )
251
252     CreateSourceGroups(${standalone_SOURCE})
253
254   target_link_libraries(
255     ${STANDALONE_BINARY}
256     ${SDL2_LIBRARY}
257     ${wxWidgets_LIBRARIES}
258     ${LIBWEBSOCKETS_LIBRARIES}
259     ${PLATFORM_LIBRARIES}
260   )
261
262     set_target_properties(${STANDALONE_BINARY} PROPERTIES PROJECT_LABEL "Standalone")
263
264   # wxWidgets appears to need c++11 for one or more headers
265   if(NOT WIN32)
266     set_target_properties(${STANDALONE_BINARY} PROPERTIES COMPILE_FLAGS -std=c++11)
267     endif()
268   endif()
269 endif(NOT GAME_ONLY)
270
271 #
272 # extra rule to make sure we cleanup all exe variants
273 #
274
275 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
276   # FS1 demo
277   fsdemo${CMAKE_EXECUTABLE_SUFFIX}
278   fsdemo${CMAKE_EXECUTABLE_SUFFIX}.mem
279   fsdemo.js
280   fsdemo.data
281   freespacedemo${CMAKE_EXECUTABLE_SUFFIX}
282   # FS1
283   fs${CMAKE_EXECUTABLE_SUFFIX}
284   fs${CMAKE_EXECUTABLE_SUFFIX}.mem
285   fs.js
286   fs.data
287   freespace${CMAKE_EXECUTABLE_SUFFIX}
288   # FS2 demo
289   fs2demo${CMAKE_EXECUTABLE_SUFFIX}
290   fs2demo${CMAKE_EXECUTABLE_SUFFIX}.mem
291   fs2demo.js
292   fs2demo.data
293   freespace2demo${CMAKE_EXECUTABLE_SUFFIX}
294   # FS2
295   fs2${CMAKE_EXECUTABLE_SUFFIX}
296   fs2${CMAKE_EXECUTABLE_SUFFIX}.mem
297   fs2.js
298   fs2.data
299   freespace2${CMAKE_EXECUTABLE_SUFFIX}
300   # standalone GUI
301   fsstandalone${CMAKE_EXECUTABLE_SUFFIX}
302   fs2standalone${CMAKE_EXECUTABLE_SUFFIX}
303 )
304
305 #
306 # ##############################################################################
307
308 # ##############################################################################
309 #
310 # toolset: targets for creating/modifying game assets
311 #
312
313 # don't build toolset unless it's a full build
314 if(NOT GAME_ONLY)
315
316   #
317   # AC: anim converter
318   #
319
320   add_executable(ac
321     EXCLUDE_FROM_ALL
322     ${ac_SOURCE}
323     ${ac_HEADERS}
324   )
325
326   CreateSourceGroups(${ac_SOURCE})
327
328   target_link_libraries(
329     ac
330     code
331     ${SDL2_LIBRARY}
332     ${OPENGL_LIBRARIES}
333     ${OPENAL_LIBRARY}
334     ${LIBWEBSOCKETS_LIBRARIES}
335     ${PLATFORM_LIBRARIES}
336   )
337
338   #
339   # CFILEARCHIVER: to create VP file archives
340   #
341
342   add_executable(cfilearchiver
343     EXCLUDE_FROM_ALL
344     ${cfilearchiver_SOURCE}
345   )
346
347   CreateSourceGroups(${cfilearchiver_SOURCE})
348
349   #
350   # CRYPTSTRING: string encryption (for embedded cheat codes)
351   #
352
353   add_executable(cryptstring
354     EXCLUDE_FROM_ALL
355     ${cryptstring_SOURCE}
356   )
357
358   CreateSourceGroups(${cryptstring_SOURCE})
359
360   #
361   # NEBEDIT: FS1 style nebula editor/creator
362   #
363
364   add_executable(nebedit
365     EXCLUDE_FROM_ALL
366     WIN32
367     ${nebedit_SOURCE}
368   )
369
370   CreateSourceGroups(${nebedit_SOURCE})
371
372   target_link_libraries(
373     nebedit
374     code
375     ${SDL2_LIBRARY}
376     ${OPENGL_LIBRARIES}
377     ${OPENAL_LIBRARY}
378     ${wxWidgets_LIBRARIES}
379     ${LIBWEBSOCKETS_LIBRARIES}
380     ${PLATFORM_LIBRARIES}
381   )
382
383   # wxWidgets appears to need c++11 for one or more headers
384   if(NOT WIN32)
385     set_target_properties(nebedit PROPERTIES COMPILE_FLAGS -std=c++11)
386   endif()
387
388   #
389   # POFVIEW: model viewer
390   #
391
392   add_executable(pofview
393     EXCLUDE_FROM_ALL
394     WIN32
395     ${pofview_SOURCE}
396     ${pofview_HEADERS}
397   )
398
399   CreateSourceGroups(${pofview_SOURCE})
400
401   target_link_libraries(
402     pofview
403     code
404     ${SDL2_LIBRARY}
405     ${OPENGL_LIBRARIES}
406     ${OPENAL_LIBRARY}
407     ${wxWidgets_LIBRARIES}
408     ${LIBWEBSOCKETS_LIBRARIES}
409     ${PLATFORM_LIBRARIES}
410   )
411
412   # wxWidgets appears to need c++11 for one or more headers
413   if(NOT WIN32)
414     set_target_properties(pofview PROPERTIES COMPILE_FLAGS -std=c++11)
415   endif()
416
417   #
418   # SCRAMBLE: file-based encryption for TBLs
419   #
420
421   add_executable(scramble
422     EXCLUDE_FROM_ALL
423     ${scramble_SOURCE}
424     ${scramble_HEADERS}
425   )
426
427   CreateSourceGroups(${scramble_SOURCE})
428
429   #
430   # FONTTOOL: create font files / edit kerning data
431   #
432
433   add_executable(fonttool
434     EXCLUDE_FROM_ALL
435     ${fonttool_SOURCE}
436     ${fonttool_HEADERS}
437   )
438
439   CreateSourceGroups(${fonttool_SOURCE})
440
441   # background setup, to allow running from build location without installing
442   add_custom_command(TARGET fonttool
443     POST_BUILD
444     COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/src/fonttool"
445     COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/src/fonttool/fonttool.pcx" "${CMAKE_CURRENT_BINARY_DIR}/src/fonttool"
446   )
447
448   target_link_libraries(
449     fonttool
450     code
451     ${SDL2_LIBRARY}
452     ${OPENGL_LIBRARIES}
453     ${OPENAL_LIBRARY}
454     ${LIBWEBSOCKETS_LIBRARIES}
455     ${PLATFORM_LIBRARIES}
456   )
457
458   #
459   # custom target to build all tools in one pass
460   #
461
462   add_custom_target(tools)
463
464   add_dependencies(tools
465     ac
466     cfilearchiver
467     cryptstring
468     nebedit
469     pofview
470     scramble
471     fonttool
472   )
473
474 endif(NOT GAME_ONLY)
475
476 #
477 # ##############################################################################
478
479 #
480 # optionally include any dev/user preferred build commands and/or options
481 #
482
483 include(custom.cmake OPTIONAL)