Feature: Press ctrl to build diagonal rivers in Scenario Editor (#8880)
[openttd-github.git] / CMakeLists.txt
blobc5f1d11b50cf8ea9cdf3c83ccf6ff1532cabad29
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 if(APPLE)
108         # Avoid searching for headers in Frameworks, and libraries in LIBDIR.
109         set(CMAKE_FIND_FRAMEWORK LAST)
110 endif()
112 # Prefer -pthread over -lpthread, which is often the better option of the two.
113 set(CMAKE_THREAD_PREFER_PTHREAD YES)
114 # Make sure we have Threads available.
115 find_package(Threads REQUIRED)
117 find_package(ZLIB)
118 find_package(LibLZMA)
119 find_package(LZO)
120 find_package(PNG)
122 if(NOT OPTION_DEDICATED)
123     if(NOT WIN32)
124         find_package(Allegro)
125         if(NOT APPLE)
126             find_package(Freetype)
127             find_package(SDL2)
128             if(NOT SDL2_FOUND)
129                 find_package(SDL)
130             endif()
131             find_package(Fluidsynth)
132             find_package(Fontconfig)
133             find_package(ICU OPTIONAL_COMPONENTS i18n lx)
134         endif()
135     endif()
136 endif()
137 if(APPLE)
138     find_package(Iconv)
140     find_library(AUDIOTOOLBOX_LIBRARY AudioToolbox)
141     find_library(AUDIOUNIT_LIBRARY AudioUnit)
142     find_library(COCOA_LIBRARY Cocoa)
143     find_library(QUARTZCORE_LIBRARY QuartzCore)
144 endif()
146 if(NOT EMSCRIPTEN AND NOT OPTION_DEDICATED)
147     find_package(OpenGL COMPONENTS OpenGL)
148 endif()
150 if(MSVC)
151     find_package(Editbin REQUIRED)
152 endif()
154 find_package(SSE)
155 find_package(Xaudio2)
157 find_package(Grfcodec)
159 include(CheckIPOSupported)
160 check_ipo_supported(RESULT IPO_FOUND)
162 show_options()
164 if(UNIX AND NOT APPLE AND NOT OPTION_DEDICATED)
165     if(NOT SDL_FOUND AND NOT SDL2_FOUND AND NOT ALLEGRO_FOUND)
166         message(FATAL_ERROR "SDL, SDL2 or Allegro is required for this platform")
167     endif()
168 endif()
169 if(APPLE)
170     if(NOT AUDIOTOOLBOX_LIBRARY)
171         message(FATAL_ERROR "AudioToolbox is required for this platform")
172     endif()
173     if(NOT AUDIOUNIT_LIBRARY)
174         message(FATAL_ERROR "AudioUnit is required for this platform")
175     endif()
176     if(NOT COCOA_LIBRARY)
177         message(FATAL_ERROR "Cocoa is required for this platform")
178     endif()
179     if(NOT QUARTZCORE_LIBRARY)
180         message(FATAL_ERROR "QuartzCore is required for this platform")
181     endif()
182 endif()
184 if(OPTION_PACKAGE_DEPENDENCIES)
185     if(NOT UNIX)
186         message(FATAL_ERROR "Can only package dependencies on Linux")
187     endif()
188     if(OPTION_INSTALL_FHS)
189         message(FATAL_ERROR "Cannot install in FHS folders when we are packaging dependencies")
190     endif()
191     if(${CMAKE_VERSION} VERSION_LESS "3.16.0")
192         message(FATAL_ERROR "OPTION_PACKAGE_DEPENDENCIES can only work with CMake 3.16+; you are using ${CMAKE_VERSION}")
193     endif()
195     # If we are packaging dependencies, we do two things:
196     # 1) set the RPATH to include $ORIGIN/lib; $ORIGIN (that literal string)
197     #    is a Linux indicator for "path where application is". In CMake, we
198     #    have to do this before add_executable() is executed.
199     # 2) copy the libraries that we compile against to the "lib" folder.
200     #    This is done in InstallAndPackage.cmake.
201     set(CMAKE_INSTALL_RPATH "\$ORIGIN/lib")
202     set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
203 endif()
205 include(SourceList)
207 # Needed by rev.cpp
208 include_directories(${CMAKE_SOURCE_DIR}/src)
209 # Needed by everything that uses Squirrel
210 include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/squirrel/include)
212 include(MSVCFilters)
214 add_executable(openttd WIN32 ${GENERATED_SOURCE_FILES})
215 set_target_properties(openttd PROPERTIES OUTPUT_NAME "${BINARY_NAME}")
216 # All other files are added via target_sources()
218 if(MSVC)
219     # Add DPI manifest to project; other WIN32 targets get this via ottdres.rc
220     target_sources(openttd PRIVATE "${CMAKE_SOURCE_DIR}/os/windows/openttd.manifest")
221 endif()
223 add_subdirectory(${CMAKE_SOURCE_DIR}/src)
224 add_subdirectory(${CMAKE_SOURCE_DIR}/media)
226 add_dependencies(openttd
227     find_version)
229 target_link_libraries(openttd
230     openttd::languages
231     openttd::settings
232     openttd::media
233     openttd::basesets
234     openttd::script_api
235     Threads::Threads
238 if(IPO_FOUND)
239     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE True)
240     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL True)
241     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO True)
242 endif()
243 set_target_properties(openttd PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
244 process_compile_flags()
246 include(LinkPackage)
247 link_package(PNG TARGET PNG::PNG ENCOURAGED)
248 link_package(ZLIB TARGET ZLIB::ZLIB ENCOURAGED)
249 link_package(LIBLZMA TARGET LibLZMA::LibLZMA ENCOURAGED)
250 link_package(LZO)
252 if(NOT OPTION_DEDICATED)
253     link_package(Fluidsynth)
254     link_package(SDL)
255     link_package(SDL2 TARGET SDL2::SDL2)
256     link_package(Allegro)
257     link_package(FREETYPE TARGET Freetype::Freetype)
258     link_package(Fontconfig TARGET Fontconfig::Fontconfig)
259     link_package(ICU_lx)
260     link_package(ICU_i18n)
262     if(SDL2_FOUND AND OPENGL_FOUND AND UNIX)
263         # SDL2 dynamically loads OpenGL if needed, so do not link to OpenGL when
264         # on Linux. For Windows, we need to link to OpenGL as we also have a win32
265         # driver using it.
266         add_definitions(-DWITH_OPENGL)
267         message(STATUS "OpenGL found -- -DWITH_OPENGL -- (via SDL2)")
268     else()
269         link_package(OpenGL TARGET OpenGL::GL)
270     endif()
271 endif()
273 if(APPLE)
274     link_package(Iconv TARGET Iconv::Iconv)
276     target_link_libraries(openttd
277         ${AUDIOTOOLBOX_LIBRARY}
278         ${AUDIOUNIT_LIBRARY}
279         ${COCOA_LIBRARY}
280         ${QUARTZCORE_LIBRARY}
281     )
283     add_definitions(
284         -DWITH_COCOA
285     )
286 endif()
288 if(EMSCRIPTEN)
289     add_library(WASM::WASM INTERFACE IMPORTED)
291     # Allow heap-growth, and start with a bigger memory size.
292     target_link_libraries(WASM::WASM INTERFACE "-s ALLOW_MEMORY_GROWTH=1")
293     target_link_libraries(WASM::WASM INTERFACE "-s INITIAL_MEMORY=33554432")
294     target_link_libraries(WASM::WASM INTERFACE "-s DISABLE_EXCEPTION_CATCHING=0")
295     add_definitions(-s DISABLE_EXCEPTION_CATCHING=0)
297     # Export functions to Javascript.
298     target_link_libraries(WASM::WASM INTERFACE "-s EXPORTED_FUNCTIONS='[\"_main\", \"_em_openttd_add_server\"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]'")
300     # Preload all the files we generate during build.
301     # As we do not compile with FreeType / FontConfig, we also have no way to
302     # render several languages (like Chinese, ..), so where do you draw the
303     # line what languages to include and which not? In the end, especially as
304     # the more languages you add the slower downloading becomes, we decided to
305     # only ship the English language.
306     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_BINARY_DIR}/baseset@/baseset")
307     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_BINARY_DIR}/lang/english.lng@/lang/english.lng")
308     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_SOURCE_DIR}/bin/ai@/ai")
309     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_SOURCE_DIR}/bin/game@/game")
311     # We use IDBFS for persistent storage.
312     target_link_libraries(WASM::WASM INTERFACE "-lidbfs.js")
314     # Use custom pre-js and shell.html.
315     target_link_libraries(WASM::WASM INTERFACE "--pre-js ${CMAKE_SOURCE_DIR}/os/emscripten/pre.js")
316     target_link_libraries(WASM::WASM INTERFACE "--shell-file ${CMAKE_SOURCE_DIR}/os/emscripten/shell.html")
318     # Build the .html (which builds the .js, .wasm, and .data too).
319     set_target_properties(openttd PROPERTIES SUFFIX ".html")
320     target_link_libraries(openttd WASM::WASM)
321 endif()
323 if(NOT PERSONAL_DIR STREQUAL "(not set)")
324     add_definitions(
325         -DWITH_PERSONAL_DIR
326         -DPERSONAL_DIR="${PERSONAL_DIR}"
327     )
328 endif()
330 if(NOT SHARED_DIR STREQUAL "(not set)")
331     add_definitions(
332         -DWITH_SHARED_DIR
333         -DSHARED_DIR="${SHARED_DIR}"
334     )
335 endif()
337 if(NOT GLOBAL_DIR STREQUAL "(not set)")
338     add_definitions(
339         -DGLOBAL_DATA_DIR="${GLOBAL_DIR}"
340     )
341 endif()
343 link_package(SSE)
345 add_definitions_based_on_options()
347 if(WIN32)
348     add_definitions(
349         -DUNICODE
350         -D_UNICODE
351         -DWITH_UNISCRIBE
352     )
354     target_link_libraries(openttd
355         ws2_32
356         winmm
357         imm32
358     )
359 endif()
361 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
362     add_definitions(-D_SQ64)
363 endif()
365 include(CreateRegression)
366 create_regression()
368 if(APPLE OR WIN32)
369     find_package(Pandoc)
370 endif()
372 include(InstallAndPackage)