Codechange: deduplicate tick-handlers of all video drivers
[openttd-github.git] / CMakeLists.txt
blob24496f459c6f213142ee8c1a3f6619012e65ef03
1 cmake_minimum_required(VERSION 3.9)
3 if(NOT BINARY_NAME)
4     set(BINARY_NAME openttd)
5 endif()
7 project(${BINARY_NAME})
9 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
10     message(FATAL_ERROR "In-source builds not allowed. Please run \"cmake ..\" from the bin directory")
11 endif()
13 # Debug mode by default.
14 if(NOT CMAKE_BUILD_TYPE)
15     set(CMAKE_BUILD_TYPE Debug)
16 endif()
18 if (EMSCRIPTEN)
19     set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/os/emscripten/cmake")
20 endif()
22 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
23 set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9)
25 # Use GNUInstallDirs to allow customisation
26 # but set our own default data and bin dir
27 if(NOT CMAKE_INSTALL_DATADIR)
28     set(CMAKE_INSTALL_DATADIR "share/games")
29 endif()
30 if(NOT CMAKE_INSTALL_BINDIR)
31     set(CMAKE_INSTALL_BINDIR "games")
32 endif()
33 include(GNUInstallDirs)
35 include(Options)
36 set_options()
37 set_directory_options()
39 include(Static)
40 set_static_if_needed()
42 set(CMAKE_CXX_STANDARD 17)
43 set(CMAKE_CXX_STANDARD_REQUIRED YES)
44 set(CMAKE_CXX_EXTENSIONS NO)
46 set(CMAKE_EXPORT_COMPILE_COMMANDS YES)
48 # An empty target for the tools
49 add_custom_target(tools)
51 include(Endian)
52 add_endian_definition()
54 include(CompileFlags)
55 compile_flags()
57 if(APPLE OR UNIX)
58     add_definitions(-DUNIX)
59 endif()
61 if(UNIX)
62     find_package(Doxygen)
63 endif()
65 list(APPEND GENERATED_SOURCE_FILES "${CMAKE_BINARY_DIR}/generated/rev.cpp")
66 if(WIN32)
67     list(APPEND GENERATED_SOURCE_FILES "${CMAKE_BINARY_DIR}/generated/ottdres.rc")
68 endif()
70 # Generate a target to determine version, which is execute every 'make' run
71 add_custom_target(find_version
72         ${CMAKE_COMMAND}
73                 -DFIND_VERSION_BINARY_DIR=${CMAKE_BINARY_DIR}/generated
74                 -DCPACK_BINARY_DIR=${CMAKE_BINARY_DIR}
75                 -P "${CMAKE_SOURCE_DIR}/cmake/scripts/FindVersion.cmake"
76         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
77         BYPRODUCTS ${GENERATED_SOURCE_FILES}
80 # Documentation
81 if(DOXYGEN_EXECUTABLE)
82     add_custom_target(docs)
83     add_custom_target(docs_source
84         ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/docs
85         COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
86         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
87         COMMENT "Generating documentation for source"
88     )
89     add_dependencies(docs_source
90         find_version
91     )
92     add_dependencies(docs
93         docs_source
94     )
95 endif()
97 include(AddCustomXXXTimestamp)
99 if(OPTION_TOOLS_ONLY)
100     if(HOST_BINARY_DIR)
101         unset(HOST_BINARY_DIR CACHE)
102     endif()
103     add_subdirectory(${CMAKE_SOURCE_DIR}/src)
104     return()
105 endif()
107 # Prefer -pthread over -lpthread, which is often the better option of the two.
108 set(CMAKE_THREAD_PREFER_PTHREAD YES)
109 # Make sure we have Threads available.
110 find_package(Threads REQUIRED)
112 find_package(ZLIB)
113 find_package(LibLZMA)
114 find_package(LZO)
115 find_package(PNG)
117 if(NOT WIN32)
118     find_package(Allegro)
119     if(NOT APPLE)
120         find_package(Freetype)
121         find_package(SDL2)
122         if(NOT SDL2_FOUND)
123             find_package(SDL)
124         endif()
125         find_package(Fluidsynth)
126         find_package(Fontconfig)
127         find_package(ICU OPTIONAL_COMPONENTS i18n lx)
128     else()
129         find_package(Iconv)
131         find_library(AUDIOTOOLBOX_LIBRARY AudioToolbox)
132         find_library(AUDIOUNIT_LIBRARY AudioUnit)
133         find_library(COCOA_LIBRARY Cocoa)
134         find_library(QUARTZCORE_LIBRARY QuartzCore)
135     endif()
136 endif()
138 if(MSVC)
139     find_package(Editbin REQUIRED)
140 endif()
142 find_package(SSE)
143 find_package(Xaudio2)
145 find_package(Grfcodec)
147 include(CheckIPOSupported)
148 check_ipo_supported(RESULT IPO_FOUND)
150 show_options()
152 if(UNIX AND NOT APPLE AND NOT OPTION_DEDICATED)
153     if(NOT SDL_FOUND AND NOT SDL2_FOUND)
154         message(FATAL_ERROR "SDL or SDL2 is required for this platform")
155     endif()
156 endif()
157 if(APPLE)
158     if(NOT AUDIOTOOLBOX_LIBRARY)
159         message(FATAL_ERROR "AudioToolbox is required for this platform")
160     endif()
161     if(NOT AUDIOUNIT_LIBRARY)
162         message(FATAL_ERROR "AudioUnit is required for this platform")
163     endif()
164     if(NOT COCOA_LIBRARY)
165         message(FATAL_ERROR "Cocoa is required for this platform")
166     endif()
167     if(NOT QUARTZCORE_LIBRARY)
168         message(FATAL_ERROR "QuartzCore is required for this platform")
169     endif()
170 endif()
172 if(OPTION_PACKAGE_DEPENDENCIES)
173     if(NOT UNIX)
174         message(FATAL_ERROR "Can only package dependencies on Linux")
175     endif()
176     if(OPTION_INSTALL_FHS)
177         message(FATAL_ERROR "Cannot install in FHS folders when we are packaging dependencies")
178     endif()
179     if(${CMAKE_VERSION} VERSION_LESS "3.16.0")
180         message(FATAL_ERROR "OPTION_PACKAGE_DEPENDENCIES can only work with CMake 3.16+; you are using ${CMAKE_VERSION}")
181     endif()
183     # If we are packaging dependencies, we do two things:
184     # 1) set the RPATH to include $ORIGIN/lib; $ORIGIN (that literal string)
185     #    is a Linux indicator for "path where application is". In CMake, we
186     #    have to do this before add_executable() is executed.
187     # 2) copy the libraries that we compile against to the "lib" folder.
188     #    This is done in InstallAndPackage.cmake.
189     set(CMAKE_INSTALL_RPATH "\$ORIGIN/lib")
190     set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
191 endif()
193 include(SourceList)
195 # Needed by rev.cpp
196 include_directories(${CMAKE_SOURCE_DIR}/src)
197 # Needed by everything that uses Squirrel
198 include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/squirrel/include)
200 include(MSVCFilters)
202 add_executable(openttd WIN32 ${GENERATED_SOURCE_FILES})
203 set_target_properties(openttd PROPERTIES OUTPUT_NAME "${BINARY_NAME}")
204 # All other files are added via target_sources()
206 if(MSVC)
207     # Add DPI manifest to project; other WIN32 targets get this via ottdres.rc
208     target_sources(openttd PRIVATE "${CMAKE_SOURCE_DIR}/os/windows/openttd.manifest")
209 endif()
211 add_subdirectory(${CMAKE_SOURCE_DIR}/src)
212 add_subdirectory(${CMAKE_SOURCE_DIR}/media/baseset)
214 add_dependencies(openttd
215     find_version)
217 target_link_libraries(openttd
218     openttd::languages
219     openttd::settings
220     openttd::basesets
221     openttd::script_api
222     Threads::Threads
225 if(IPO_FOUND)
226     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE True)
227     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL True)
228     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO True)
229 endif()
230 set_target_properties(openttd PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
231 process_compile_flags()
233 include(LinkPackage)
234 link_package(PNG TARGET PNG::PNG ENCOURAGED)
235 link_package(ZLIB TARGET ZLIB::ZLIB ENCOURAGED)
236 link_package(LIBLZMA TARGET LibLZMA::LibLZMA ENCOURAGED)
237 link_package(LZO)
239 if(NOT OPTION_DEDICATED)
240     link_package(Fluidsynth)
241     link_package(SDL)
242     link_package(SDL2 TARGET SDL2::SDL2)
243     link_package(Allegro)
244     link_package(FREETYPE TARGET Freetype::Freetype)
245     link_package(Fontconfig TARGET Fontconfig::Fontconfig)
246     link_package(ICU_lx)
247     link_package(ICU_i18n)
248 endif()
250 if(APPLE)
251     link_package(Iconv TARGET Iconv::Iconv)
253     target_link_libraries(openttd
254         ${AUDIOTOOLBOX_LIBRARY}
255         ${AUDIOUNIT_LIBRARY}
256         ${COCOA_LIBRARY}
257         ${QUARTZCORE_LIBRARY}
258     )
260     add_definitions(
261         -DWITH_COCOA
262     )
263 endif()
265 if(EMSCRIPTEN)
266     add_library(WASM::WASM INTERFACE IMPORTED)
268     # Allow heap-growth, and start with a bigger memory size.
269     target_link_libraries(WASM::WASM INTERFACE "-s ALLOW_MEMORY_GROWTH=1")
270     target_link_libraries(WASM::WASM INTERFACE "-s INITIAL_MEMORY=33554432")
271     target_link_libraries(WASM::WASM INTERFACE "-s DISABLE_EXCEPTION_CATCHING=0")
272     add_definitions(-s DISABLE_EXCEPTION_CATCHING=0)
274     # Export functions to Javascript.
275     target_link_libraries(WASM::WASM INTERFACE "-s EXPORTED_FUNCTIONS='[\"_main\", \"_em_openttd_add_server\"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]'")
277     # Preload all the files we generate during build.
278     # As we do not compile with FreeType / FontConfig, we also have no way to
279     # render several languages (like Chinese, ..), so where do you draw the
280     # line what languages to include and which not? In the end, especially as
281     # the more languages you add the slower downloading becomes, we decided to
282     # only ship the English language.
283     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_BINARY_DIR}/baseset@/baseset")
284     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_BINARY_DIR}/lang/english.lng@/lang/english.lng")
285     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_SOURCE_DIR}/bin/ai@/ai")
286     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_SOURCE_DIR}/bin/game@/game")
288     # We use IDBFS for persistent storage.
289     target_link_libraries(WASM::WASM INTERFACE "-lidbfs.js")
291     # Use custom pre-js and shell.html.
292     target_link_libraries(WASM::WASM INTERFACE "--pre-js ${CMAKE_SOURCE_DIR}/os/emscripten/pre.js")
293     target_link_libraries(WASM::WASM INTERFACE "--shell-file ${CMAKE_SOURCE_DIR}/os/emscripten/shell.html")
295     # Build the .html (which builds the .js, .wasm, and .data too).
296     set_target_properties(openttd PROPERTIES SUFFIX ".html")
297     target_link_libraries(openttd WASM::WASM)
298 endif()
300 if(NOT PERSONAL_DIR STREQUAL "(not set)")
301     add_definitions(
302         -DWITH_PERSONAL_DIR
303         -DPERSONAL_DIR="${PERSONAL_DIR}"
304     )
305 endif()
307 if(NOT SHARED_DIR STREQUAL "(not set)")
308     add_definitions(
309         -DWITH_SHARED_DIR
310         -DSHARED_DIR="${SHARED_DIR}"
311     )
312 endif()
314 if(NOT GLOBAL_DIR STREQUAL "(not set)")
315     add_definitions(
316         -DGLOBAL_DATA_DIR="${GLOBAL_DIR}"
317     )
318 endif()
320 link_package(SSE)
322 add_definitions_based_on_options()
324 if(WIN32)
325     add_definitions(
326         -DUNICODE
327         -D_UNICODE
328         -DWITH_UNISCRIBE
329     )
331     target_link_libraries(openttd
332         ws2_32
333         winmm
334         imm32
335     )
336 endif()
338 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
339     add_definitions(-D_SQ64)
340 endif()
342 include(CreateRegression)
343 create_regression()
345 if(APPLE OR WIN32)
346     find_package(Pandoc)
347 endif()
349 include(InstallAndPackage)