1 cmake_minimum_required(VERSION 3.9)
4 set(BINARY_NAME openttd)
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")
13 # Debug mode by default.
14 if(NOT CMAKE_BUILD_TYPE)
15 set(CMAKE_BUILD_TYPE Debug)
19 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/os/emscripten/cmake")
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")
30 if(NOT CMAKE_INSTALL_BINDIR)
31 set(CMAKE_INSTALL_BINDIR "games")
33 include(GNUInstallDirs)
37 set_directory_options()
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)
52 add_endian_definition()
58 add_definitions(-DUNIX)
65 list(APPEND GENERATED_SOURCE_FILES "${CMAKE_BINARY_DIR}/generated/rev.cpp")
67 list(APPEND GENERATED_SOURCE_FILES "${CMAKE_BINARY_DIR}/generated/ottdres.rc")
70 # Generate a target to determine version, which is execute every 'make' run
71 add_custom_target(find_version
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}
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"
89 add_dependencies(docs_source
97 include(AddCustomXXXTimestamp)
101 unset(HOST_BINARY_DIR CACHE)
103 add_subdirectory(${CMAKE_SOURCE_DIR}/src)
108 # Avoid searching for headers in Frameworks, and libraries in LIBDIR.
109 set(CMAKE_FIND_FRAMEWORK LAST)
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)
118 find_package(LibLZMA)
122 if(NOT OPTION_DEDICATED)
124 find_package(Allegro)
126 find_package(Freetype)
131 find_package(Fluidsynth)
132 find_package(Fontconfig)
133 find_package(ICU OPTIONAL_COMPONENTS i18n lx)
140 find_library(AUDIOTOOLBOX_LIBRARY AudioToolbox)
141 find_library(AUDIOUNIT_LIBRARY AudioUnit)
142 find_library(COCOA_LIBRARY Cocoa)
143 find_library(QUARTZCORE_LIBRARY QuartzCore)
146 if(NOT EMSCRIPTEN AND NOT OPTION_DEDICATED)
147 find_package(OpenGL COMPONENTS OpenGL)
151 find_package(Editbin REQUIRED)
155 find_package(Xaudio2)
157 find_package(Grfcodec)
159 include(CheckIPOSupported)
160 check_ipo_supported(RESULT IPO_FOUND)
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")
170 if(NOT AUDIOTOOLBOX_LIBRARY)
171 message(FATAL_ERROR "AudioToolbox is required for this platform")
173 if(NOT AUDIOUNIT_LIBRARY)
174 message(FATAL_ERROR "AudioUnit is required for this platform")
176 if(NOT COCOA_LIBRARY)
177 message(FATAL_ERROR "Cocoa is required for this platform")
179 if(NOT QUARTZCORE_LIBRARY)
180 message(FATAL_ERROR "QuartzCore is required for this platform")
184 if(OPTION_PACKAGE_DEPENDENCIES)
186 message(FATAL_ERROR "Can only package dependencies on Linux")
188 if(OPTION_INSTALL_FHS)
189 message(FATAL_ERROR "Cannot install in FHS folders when we are packaging dependencies")
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}")
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)
208 include_directories(${CMAKE_SOURCE_DIR}/src)
209 # Needed by everything that uses Squirrel
210 include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/squirrel/include)
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()
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")
223 add_subdirectory(${CMAKE_SOURCE_DIR}/src)
224 add_subdirectory(${CMAKE_SOURCE_DIR}/media)
226 add_dependencies(openttd
229 target_link_libraries(openttd
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)
243 set_target_properties(openttd PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
244 process_compile_flags()
247 link_package(PNG TARGET PNG::PNG ENCOURAGED)
248 link_package(ZLIB TARGET ZLIB::ZLIB ENCOURAGED)
249 link_package(LIBLZMA TARGET LibLZMA::LibLZMA ENCOURAGED)
252 if(NOT OPTION_DEDICATED)
253 link_package(Fluidsynth)
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)
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
266 add_definitions(-DWITH_OPENGL)
267 message(STATUS "OpenGL found -- -DWITH_OPENGL -- (via SDL2)")
269 link_package(OpenGL TARGET OpenGL::GL)
274 link_package(Iconv TARGET Iconv::Iconv)
276 target_link_libraries(openttd
277 ${AUDIOTOOLBOX_LIBRARY}
280 ${QUARTZCORE_LIBRARY}
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)
323 if(NOT PERSONAL_DIR STREQUAL "(not set)")
326 -DPERSONAL_DIR="${PERSONAL_DIR}"
330 if(NOT SHARED_DIR STREQUAL "(not set)")
333 -DSHARED_DIR="${SHARED_DIR}"
337 if(NOT GLOBAL_DIR STREQUAL "(not set)")
339 -DGLOBAL_DATA_DIR="${GLOBAL_DIR}"
345 add_definitions_based_on_options()
354 target_link_libraries(openttd
361 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
362 add_definitions(-D_SQ64)
365 include(CreateRegression)
372 include(InstallAndPackage)