]> icculus.org git repositories - taylor/freespace2.git/blob - CMakeLists.txt
make html shell generic, and CSP friendly
[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 "RelWithDebInfo" "Release" "Debug")
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 -O3")
77   set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG -DRELEASE_REAL -O3")
78   set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3")
79 else()
80   set(SDL2DIR "${CMAKE_SOURCE_DIR}/../Support/SDL2" CACHE PATH "Path to SDL2")
81   set(OPENALDIR "${CMAKE_SOURCE_DIR}/../Support/OpenAL" CACHE PATH "Path to OpenAL")
82   set(LWSDIR "${CMAKE_SOURCE_DIR}/../Support/libwebsockets" CACHE PATH "Path to libwebsockets")
83   set(WXWIDGETSDIR "${CMAKE_SOURCE_DIR}/../Support/wxWidgets" CACHE PATH "Path to wxWidgets")
84   set(ANGLEDIR "${CMAKE_SOURCE_DIR}/../Support/ANGLE" CACHE PATH "Path to ANGLE")
85
86   set(PLATFORM_LIBRARIES
87     ws2_32.lib
88     ${ANGLEDIR}/lib/libEGL.lib
89     ${ANGLEDIR}/lib/libGLESv2.lib
90   )
91
92   add_definitions(/D _CRT_SECURE_NO_WARNINGS)
93   add_definitions(/W4)
94   add_definitions(/wd4100) # unreferenced parameter
95   add_definitions(/wd4127) # conditional expression is constant: do { } while (0)
96   add_definitions(/wd4996) # deprecated functions: fopen, fileno, ...
97 endif()
98
99 set(BIN_SUFFIX "")
100
101 if(NOT FS1)
102         set(BIN_SUFFIX "2")
103 endif()
104
105 if(DEMO)
106         set(BIN_SUFFIX "${BIN_SUFFIX}demo")
107 endif()
108
109 find_package(SDL2 "2.0.5" REQUIRED)
110 find_package(OpenGL REQUIRED)
111 find_package(OpenAL REQUIRED)
112
113 if(NOT EMSCRIPTEN)
114   find_package(LibWebSockets REQUIRED)
115
116   find_package(wxWidgets COMPONENTS core base gl net)
117   include(${wxWidgets_USE_FILE})
118 endif()
119
120 # ##############################################################################
121
122
123 include_directories(
124   ${PROJECT_SOURCE_DIR}/include
125   ${CMAKE_BINARY_DIR}/include
126   ${SDL2_INCLUDE_DIR}
127   ${OPENAL_INCLUDE_DIR}
128   ${LIBWEBSOCKETS_INCLUDE_DIR}
129   ${wxWidgets_INCLUDE_DIRS}
130 )
131
132
133 add_subdirectory(src)
134 add_subdirectory(include)
135
136
137 # ##############################################################################
138 #
139
140 #
141 # custom target for source tree version info
142 #
143
144 add_custom_target(buildver
145   COMMAND ${CMAKE_COMMAND}
146     -DSOURCE_DIR=${CMAKE_SOURCE_DIR}
147     -DBINARY_DIR=${CMAKE_BINARY_DIR}
148     -P ${CMAKE_SOURCE_DIR}/cmake/GitInfo.cmake
149   WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
150   SOURCES ${CMAKE_SOURCE_DIR}/cmake/GitInfo.cmake
151 )
152
153 #
154 # main code/game library
155 #
156
157 add_library(code
158   STATIC
159   ${code_SOURCE}
160   ${code_HEADERS}
161   ${platform_SOURCE}
162   ${platform_HEADERS}
163 )
164
165 CreateSourceGroups(${code_SOURCE} ${platform_SOURCE})
166
167 add_dependencies(code
168   buildver
169 )
170
171 #
172 # the game itself
173 #
174
175 set(FS_BINARY "fs${BIN_SUFFIX}")
176
177 add_executable(${FS_BINARY}
178   WIN32
179   ${freespace_SOURCE}
180   ${freespace_HEADERS}
181 )
182
183 CreateSourceGroups(${freespace_SOURCE})
184
185 target_link_libraries(
186   ${FS_BINARY}
187   code
188   ${SDL2_LIBRARY}
189   ${OPENGL_LIBRARIES}
190   ${OPENAL_LIBRARY}
191   ${LIBWEBSOCKETS_LIBRARIES}
192   ${PLATFORM_LIBRARIES}
193 )
194
195 if(EMSCRIPTEN)
196   set(ASYNC_FUNCTIONS
197         # common
198     main
199     game_main
200     game_init
201     # fs2 title screen
202     display_title_screen
203     # loading screen
204     game_loop_caller
205     gameseq_process_events
206     game_process_event
207     gameseq_set_state
208     game_enter_state
209     game_start_mission
210     freespace_mission_load_stuff
211     game_busy_callback
212     game_loading_callback
213     game_loading_callback_close
214     level_page_in
215     bm_page_in_stop
216     gamesnd_preload_common_sounds
217     game_busy
218   )
219
220   list(GET ASYNC_FUNCTIONS -1 LastOne)
221
222   set(EMTERPRETIFY_WHITELIST "[")
223
224   foreach(func ${ASYNC_FUNCTIONS})
225     if (${func} STREQUAL ${LastOne})
226       set(EMTERPRETIFY_WHITELIST "${EMTERPRETIFY_WHITELIST}\"_${func}\"]")
227     else()
228       set(EMTERPRETIFY_WHITELIST "${EMTERPRETIFY_WHITELIST}\"_${func}\",")
229     endif()
230   endforeach()
231
232   set(LINK_FLAGS
233     "-O3"
234     "-s USE_SDL=2"
235     "-s FULL_ES2=1"
236     "-s WASM=1"
237     "-s TOTAL_MEMORY=184549376"
238     "--shell-file ${CMAKE_BINARY_DIR}/dist/demo_shell.html"
239     "--pre-js ${FS_BINARY}_preload.js"
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
254   if(FS1)
255     set(TEXT_COLOR "#4C442D")
256     set(BG_COLOR "#3E4559")
257     set(OUTLINE_COLOR "#353B4C")
258     set(BG_COLOR2 "#798199")
259     set(GAME_TITLE "FreeSpace Demo")
260   else()
261     set(TEXT_COLOR "#FFF")
262     set(BG_COLOR "#457573")
263     set(OUTLINE_COLOR "#4D8280")
264     set(BG_COLOR2 "#182928")
265     set(GAME_TITLE "FreeSpace 2 Demo")
266   endif()
267
268   configure_file(${CMAKE_SOURCE_DIR}/dist/demo_shell.html.in ${CMAKE_BINARY_DIR}/dist/demo_shell.html @ONLY)
269   configure_file(${CMAKE_SOURCE_DIR}/dist/demo-play.css.in ${CMAKE_BINARY_DIR}/dist/demo-play.css @ONLY)
270   configure_file(${CMAKE_SOURCE_DIR}/dist/demo-play.js.in ${CMAKE_BINARY_DIR}/dist/demo-play.js @ONLY)
271 endif(EMSCRIPTEN)
272
273 if(FS1)
274   set_target_properties(${FS_BINARY} PROPERTIES PROJECT_LABEL "Freespace")
275 else()
276   set_target_properties(${FS_BINARY} PROPERTIES PROJECT_LABEL "Freespace2")
277 endif()
278
279 #
280 # launcher
281 #
282
283 if(NOT GAME_ONLY)
284   set(LAUNCHER_BINARY "freespace${BIN_SUFFIX}")
285
286   add_executable(${LAUNCHER_BINARY}
287   WIN32
288     ${launcher_SOURCE}
289     ${launcher_HEADERS}
290   )
291
292   add_dependencies(${LAUNCHER_BINARY}
293     buildver
294   )
295
296   CreateSourceGroups(${launcher_SOURCE})
297
298   target_link_libraries(
299     ${LAUNCHER_BINARY}
300     ${SDL2_LIBRARY}
301     ${OPENGL_LIBRARIES}
302     ${OPENAL_LIBRARY}
303     ${wxWidgets_LIBRARIES}
304     ${PLATFORM_LIBRARIES}
305   )
306
307   set_target_properties(${LAUNCHER_BINARY} PROPERTIES PROJECT_LABEL "Launcher")
308
309   # wxWidgets appears to need c++11 for one or more headers
310   if(NOT WIN32)
311     set_target_properties(${LAUNCHER_BINARY} PROPERTIES COMPILE_FLAGS -std=c++11)
312   endif()
313 endif(NOT GAME_ONLY)
314
315 #
316 # standalone server GUI
317 #
318
319 if(NOT GAME_ONLY)
320   # Not for FS1 Demo
321   if(NOT FS1 OR NOT DEMO)
322     if(FS1)
323       set(STANDALONE_BINARY fsstandalone)
324     else()
325       set(STANDALONE_BINARY fs2standalone)
326     endif()
327
328     add_executable(${STANDALONE_BINARY}
329       WIN32
330       ${standalone_SOURCE}
331       ${standalone_HEADERS}
332     )
333
334     CreateSourceGroups(${standalone_SOURCE})
335
336     target_link_libraries(
337       ${STANDALONE_BINARY}
338       ${SDL2_LIBRARY}
339       ${wxWidgets_LIBRARIES}
340       ${LIBWEBSOCKETS_LIBRARIES}
341       ${PLATFORM_LIBRARIES}
342     )
343
344     set_target_properties(${STANDALONE_BINARY} PROPERTIES PROJECT_LABEL "Standalone")
345
346     # wxWidgets appears to need c++11 for one or more headers
347     if(NOT WIN32)
348       set_target_properties(${STANDALONE_BINARY} PROPERTIES COMPILE_FLAGS -std=c++11)
349     endif()
350   endif(NOT FS1 OR NOT DEMO)
351 endif(NOT GAME_ONLY)
352
353 #
354 # extra rule to make sure we cleanup all exe variants
355 #
356
357 set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
358   # FS1 demo
359   fsdemo${CMAKE_EXECUTABLE_SUFFIX}
360   fsdemo.html.mem
361   fsdemo.binary
362   fsdemo.wast
363   fsdemo.wasm
364   fsdemo.js
365   fsdemo.data
366   freespacedemo${CMAKE_EXECUTABLE_SUFFIX}
367   # FS1
368   fs${CMAKE_EXECUTABLE_SUFFIX}
369   freespace${CMAKE_EXECUTABLE_SUFFIX}
370   # FS2 demo
371   fs2demo${CMAKE_EXECUTABLE_SUFFIX}
372   fs2demo.html.mem
373   fs2demo.binary
374   fs2demo.wast
375   fs2demo.wasm
376   fs2demo.js
377   fs2demo.data
378   freespace2demo${CMAKE_EXECUTABLE_SUFFIX}
379   # FS2
380   fs2${CMAKE_EXECUTABLE_SUFFIX}
381   freespace2${CMAKE_EXECUTABLE_SUFFIX}
382   # standalone GUI
383   fsstandalone${CMAKE_EXECUTABLE_SUFFIX}
384   fs2standalone${CMAKE_EXECUTABLE_SUFFIX}
385 )
386
387 #
388 # ##############################################################################
389
390 # ##############################################################################
391 #
392 # toolset: targets for creating/modifying game assets
393 #
394
395 # don't build toolset unless it's a full build
396 if(NOT GAME_ONLY)
397
398   #
399   # AC: anim converter
400   #
401
402   add_executable(ac
403     EXCLUDE_FROM_ALL
404     ${ac_SOURCE}
405     ${ac_HEADERS}
406   )
407
408   CreateSourceGroups(${ac_SOURCE})
409
410   target_link_libraries(
411     ac
412     code
413     ${SDL2_LIBRARY}
414     ${OPENGL_LIBRARIES}
415     ${OPENAL_LIBRARY}
416     ${LIBWEBSOCKETS_LIBRARIES}
417     ${PLATFORM_LIBRARIES}
418   )
419
420   #
421   # CFILEARCHIVER: to create VP file archives
422   #
423
424   add_executable(cfilearchiver
425     EXCLUDE_FROM_ALL
426     ${cfilearchiver_SOURCE}
427   )
428
429   CreateSourceGroups(${cfilearchiver_SOURCE})
430
431   #
432   # CFILEUTIL: work with VP file archives
433   #
434
435   add_executable(cfileutil
436     EXCLUDE_FROM_ALL
437     ${cfileutil_SOURCE}
438   )
439
440   CreateSourceGroups(${cfileutil_SOURCE})
441
442   if(NOT WIN32)
443     set_target_properties(cfileutil PROPERTIES COMPILE_FLAGS -std=c++11)
444   endif()
445
446   #
447   # CRYPTSTRING: string encryption (for embedded cheat codes)
448   #
449
450   add_executable(cryptstring
451     EXCLUDE_FROM_ALL
452     ${cryptstring_SOURCE}
453   )
454
455   CreateSourceGroups(${cryptstring_SOURCE})
456
457   #
458   # NEBEDIT: FS1 style nebula editor/creator
459   #
460
461   add_executable(nebedit
462     EXCLUDE_FROM_ALL
463     WIN32
464     ${nebedit_SOURCE}
465   )
466
467   CreateSourceGroups(${nebedit_SOURCE})
468
469   target_link_libraries(
470     nebedit
471     code
472     ${SDL2_LIBRARY}
473     ${OPENGL_LIBRARIES}
474     ${OPENAL_LIBRARY}
475     ${wxWidgets_LIBRARIES}
476     ${LIBWEBSOCKETS_LIBRARIES}
477     ${PLATFORM_LIBRARIES}
478   )
479
480   # wxWidgets appears to need c++11 for one or more headers
481   if(NOT WIN32)
482     set_target_properties(nebedit PROPERTIES COMPILE_FLAGS -std=c++11)
483   endif()
484
485   #
486   # POFVIEW: model viewer
487   #
488
489   add_executable(pofview
490     EXCLUDE_FROM_ALL
491     WIN32
492     ${pofview_SOURCE}
493     ${pofview_HEADERS}
494   )
495
496   CreateSourceGroups(${pofview_SOURCE})
497
498   target_link_libraries(
499     pofview
500     code
501     ${SDL2_LIBRARY}
502     ${OPENGL_LIBRARIES}
503     ${OPENAL_LIBRARY}
504     ${wxWidgets_LIBRARIES}
505     ${LIBWEBSOCKETS_LIBRARIES}
506     ${PLATFORM_LIBRARIES}
507   )
508
509   # wxWidgets appears to need c++11 for one or more headers
510   if(NOT WIN32)
511     set_target_properties(pofview PROPERTIES COMPILE_FLAGS -std=c++11)
512   endif()
513
514   #
515   # SCRAMBLE: file-based encryption for TBLs
516   #
517
518   add_executable(scramble
519     EXCLUDE_FROM_ALL
520     ${scramble_SOURCE}
521     ${scramble_HEADERS}
522   )
523
524   CreateSourceGroups(${scramble_SOURCE})
525
526   #
527   # FONTTOOL: create font files / edit kerning data
528   #
529
530   add_executable(fonttool
531     EXCLUDE_FROM_ALL
532     ${fonttool_SOURCE}
533     ${fonttool_HEADERS}
534   )
535
536   CreateSourceGroups(${fonttool_SOURCE})
537
538   # background setup, to allow running from build location without installing
539   add_custom_command(TARGET fonttool
540     POST_BUILD
541     COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/src/fonttool"
542     COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/src/fonttool/fonttool.pcx" "${CMAKE_CURRENT_BINARY_DIR}/src/fonttool"
543   )
544
545   target_link_libraries(
546     fonttool
547     code
548     ${SDL2_LIBRARY}
549     ${OPENGL_LIBRARIES}
550     ${OPENAL_LIBRARY}
551     ${LIBWEBSOCKETS_LIBRARIES}
552     ${PLATFORM_LIBRARIES}
553   )
554
555   #
556   # custom target to build all tools in one pass
557   #
558
559   add_custom_target(tools)
560
561   add_dependencies(tools
562     ac
563     cfilearchiver
564     cfileutil
565     cryptstring
566     nebedit
567     pofview
568     scramble
569     fonttool
570   )
571
572 endif(NOT GAME_ONLY)
573
574 #
575 # ##############################################################################
576
577 #
578 # optionally include any dev/user preferred build commands and/or options
579 #
580
581 include(custom.cmake OPTIONAL)