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)
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)
113 find_package(LibLZMA)
118 find_package(Allegro)
120 find_package(Freetype)
125 find_package(Fluidsynth)
126 find_package(Fontconfig)
127 find_package(ICU OPTIONAL_COMPONENTS i18n lx)
131 find_library(AUDIOTOOLBOX_LIBRARY AudioToolbox)
132 find_library(AUDIOUNIT_LIBRARY AudioUnit)
133 find_library(COCOA_LIBRARY Cocoa)
134 find_library(QUARTZCORE_LIBRARY QuartzCore)
139 find_package(Editbin REQUIRED)
143 find_package(Xaudio2)
145 find_package(Grfcodec)
147 include(CheckIPOSupported)
148 check_ipo_supported(RESULT IPO_FOUND)
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")
158 if(NOT AUDIOTOOLBOX_LIBRARY)
159 message(FATAL_ERROR "AudioToolbox is required for this platform")
161 if(NOT AUDIOUNIT_LIBRARY)
162 message(FATAL_ERROR "AudioUnit is required for this platform")
164 if(NOT COCOA_LIBRARY)
165 message(FATAL_ERROR "Cocoa is required for this platform")
167 if(NOT QUARTZCORE_LIBRARY)
168 message(FATAL_ERROR "QuartzCore is required for this platform")
172 if(OPTION_PACKAGE_DEPENDENCIES)
174 message(FATAL_ERROR "Can only package dependencies on Linux")
176 if(OPTION_INSTALL_FHS)
177 message(FATAL_ERROR "Cannot install in FHS folders when we are packaging dependencies")
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}")
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)
196 include_directories(${CMAKE_SOURCE_DIR}/src)
197 # Needed by everything that uses Squirrel
198 include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/squirrel/include)
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()
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")
211 add_subdirectory(${CMAKE_SOURCE_DIR}/src)
212 add_subdirectory(${CMAKE_SOURCE_DIR}/media/baseset)
214 add_dependencies(openttd
217 target_link_libraries(openttd
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)
230 set_target_properties(openttd PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
231 process_compile_flags()
234 link_package(PNG TARGET PNG::PNG ENCOURAGED)
235 link_package(ZLIB TARGET ZLIB::ZLIB ENCOURAGED)
236 link_package(LIBLZMA TARGET LibLZMA::LibLZMA ENCOURAGED)
239 if(NOT OPTION_DEDICATED)
240 link_package(Fluidsynth)
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)
247 link_package(ICU_i18n)
251 link_package(Iconv TARGET Iconv::Iconv)
253 target_link_libraries(openttd
254 ${AUDIOTOOLBOX_LIBRARY}
257 ${QUARTZCORE_LIBRARY}
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)
300 if(NOT PERSONAL_DIR STREQUAL "(not set)")
303 -DPERSONAL_DIR="${PERSONAL_DIR}"
307 if(NOT SHARED_DIR STREQUAL "(not set)")
310 -DSHARED_DIR="${SHARED_DIR}"
314 if(NOT GLOBAL_DIR STREQUAL "(not set)")
316 -DGLOBAL_DATA_DIR="${GLOBAL_DIR}"
322 add_definitions_based_on_options()
331 target_link_libraries(openttd
338 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
339 add_definitions(-D_SQ64)
342 include(CreateRegression)
349 include(InstallAndPackage)