add additional emscripten files to cleanup list
[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.html.mem
343   fsdemo.binary
344   fsdemo.wast
345   fsdemo.wasm
346   fsdemo.js
347   fsdemo.data
348   freespacedemo${CMAKE_EXECUTABLE_SUFFIX}
349   # FS1
350   fs${CMAKE_EXECUTABLE_SUFFIX}
351   freespace${CMAKE_EXECUTABLE_SUFFIX}
352   # FS2 demo
353   fs2demo${CMAKE_EXECUTABLE_SUFFIX}
354   fs2demo.html.mem
355   fs2demo.binary
356   fs2demo.wast
357   fs2demo.wasm
358   fs2demo.js
359   fs2demo.data
360   freespace2demo${CMAKE_EXECUTABLE_SUFFIX}
361   # FS2
362   fs2${CMAKE_EXECUTABLE_SUFFIX}
363   freespace2${CMAKE_EXECUTABLE_SUFFIX}
364   # standalone GUI
365   fsstandalone${CMAKE_EXECUTABLE_SUFFIX}
366   fs2standalone${CMAKE_EXECUTABLE_SUFFIX}
367 )
368
369 #
370 # ##############################################################################
371
372 # ##############################################################################
373 #
374 # toolset: targets for creating/modifying game assets
375 #
376
377 # don't build toolset unless it's a full build
378 if(NOT GAME_ONLY)
379
380   #
381   # AC: anim converter
382   #
383
384   add_executable(ac
385     EXCLUDE_FROM_ALL
386     ${ac_SOURCE}
387     ${ac_HEADERS}
388   )
389
390   CreateSourceGroups(${ac_SOURCE})
391
392   target_link_libraries(
393     ac
394     code
395     ${SDL2_LIBRARY}
396     ${OPENGL_LIBRARIES}
397     ${OPENAL_LIBRARY}
398     ${LIBWEBSOCKETS_LIBRARIES}
399     ${PLATFORM_LIBRARIES}
400   )
401
402   #
403   # CFILEARCHIVER: to create VP file archives
404   #
405
406   add_executable(cfilearchiver
407     EXCLUDE_FROM_ALL
408     ${cfilearchiver_SOURCE}
409   )
410
411   CreateSourceGroups(${cfilearchiver_SOURCE})
412
413   #
414   # CRYPTSTRING: string encryption (for embedded cheat codes)
415   #
416
417   add_executable(cryptstring
418     EXCLUDE_FROM_ALL
419     ${cryptstring_SOURCE}
420   )
421
422   CreateSourceGroups(${cryptstring_SOURCE})
423
424   #
425   # NEBEDIT: FS1 style nebula editor/creator
426   #
427
428   add_executable(nebedit
429     EXCLUDE_FROM_ALL
430     WIN32
431     ${nebedit_SOURCE}
432   )
433
434   CreateSourceGroups(${nebedit_SOURCE})
435
436   target_link_libraries(
437     nebedit
438     code
439     ${SDL2_LIBRARY}
440     ${OPENGL_LIBRARIES}
441     ${OPENAL_LIBRARY}
442     ${wxWidgets_LIBRARIES}
443     ${LIBWEBSOCKETS_LIBRARIES}
444     ${PLATFORM_LIBRARIES}
445   )
446
447   # wxWidgets appears to need c++11 for one or more headers
448   if(NOT WIN32)
449     set_target_properties(nebedit PROPERTIES COMPILE_FLAGS -std=c++11)
450   endif()
451
452   #
453   # POFVIEW: model viewer
454   #
455
456   add_executable(pofview
457     EXCLUDE_FROM_ALL
458     WIN32
459     ${pofview_SOURCE}
460     ${pofview_HEADERS}
461   )
462
463   CreateSourceGroups(${pofview_SOURCE})
464
465   target_link_libraries(
466     pofview
467     code
468     ${SDL2_LIBRARY}
469     ${OPENGL_LIBRARIES}
470     ${OPENAL_LIBRARY}
471     ${wxWidgets_LIBRARIES}
472     ${LIBWEBSOCKETS_LIBRARIES}
473     ${PLATFORM_LIBRARIES}
474   )
475
476   # wxWidgets appears to need c++11 for one or more headers
477   if(NOT WIN32)
478     set_target_properties(pofview PROPERTIES COMPILE_FLAGS -std=c++11)
479   endif()
480
481   #
482   # SCRAMBLE: file-based encryption for TBLs
483   #
484
485   add_executable(scramble
486     EXCLUDE_FROM_ALL
487     ${scramble_SOURCE}
488     ${scramble_HEADERS}
489   )
490
491   CreateSourceGroups(${scramble_SOURCE})
492
493   #
494   # FONTTOOL: create font files / edit kerning data
495   #
496
497   add_executable(fonttool
498     EXCLUDE_FROM_ALL
499     ${fonttool_SOURCE}
500     ${fonttool_HEADERS}
501   )
502
503   CreateSourceGroups(${fonttool_SOURCE})
504
505   # background setup, to allow running from build location without installing
506   add_custom_command(TARGET fonttool
507     POST_BUILD
508     COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/src/fonttool"
509     COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/src/fonttool/fonttool.pcx" "${CMAKE_CURRENT_BINARY_DIR}/src/fonttool"
510   )
511
512   target_link_libraries(
513     fonttool
514     code
515     ${SDL2_LIBRARY}
516     ${OPENGL_LIBRARIES}
517     ${OPENAL_LIBRARY}
518     ${LIBWEBSOCKETS_LIBRARIES}
519     ${PLATFORM_LIBRARIES}
520   )
521
522   #
523   # custom target to build all tools in one pass
524   #
525
526   add_custom_target(tools)
527
528   add_dependencies(tools
529     ac
530     cfilearchiver
531     cryptstring
532     nebedit
533     pofview
534     scramble
535     fonttool
536   )
537
538 endif(NOT GAME_ONLY)
539
540 #
541 # ##############################################################################
542
543 #
544 # optionally include any dev/user preferred build commands and/or options
545 #
546
547 include(custom.cmake OPTIONAL)