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