1 cmake_minimum_required(VERSION 3.9)
4 set(BINARY_NAME openttd)
11 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
12 message(FATAL_ERROR "In-source builds not allowed. Please run \"cmake ..\" from the build directory. You may need to delete \"${CMAKE_SOURCE_DIR}/CMakeCache.txt\" first.")
15 # Debug mode by default.
16 if(NOT CMAKE_BUILD_TYPE)
17 set(CMAKE_BUILD_TYPE Debug)
21 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/os/emscripten/cmake")
24 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
25 set(CMAKE_OSX_DEPLOYMENT_TARGET 10.14)
27 # Use GNUInstallDirs to allow customisation
28 # but set our own default data and bin dir
29 if(NOT CMAKE_INSTALL_DATADIR)
30 set(CMAKE_INSTALL_DATADIR "share/games")
32 if(NOT CMAKE_INSTALL_BINDIR)
33 set(CMAKE_INSTALL_BINDIR "games")
35 include(GNUInstallDirs)
39 set_directory_options()
42 set_static_if_needed()
44 set(CMAKE_CXX_STANDARD 17)
45 set(CMAKE_CXX_STANDARD_REQUIRED YES)
46 set(CMAKE_CXX_EXTENSIONS NO)
48 set(CMAKE_EXPORT_COMPILE_COMMANDS YES)
50 # An empty target for the tools
51 add_custom_target(tools)
54 add_endian_definition()
60 add_definitions(-DUNIX)
67 list(APPEND GENERATED_SOURCE_FILES "${CMAKE_BINARY_DIR}/generated/rev.cpp")
69 list(APPEND GENERATED_SOURCE_FILES "${CMAKE_BINARY_DIR}/generated/ottdres.rc")
72 # Generate a target to determine version, which is execute every 'make' run
73 add_custom_target(find_version
75 -DFIND_VERSION_BINARY_DIR=${CMAKE_BINARY_DIR}/generated
76 -DCPACK_BINARY_DIR=${CMAKE_BINARY_DIR}
77 -DREV_MAJOR=${PROJECT_VERSION_MAJOR}
78 -DREV_MINOR=${PROJECT_VERSION_MINOR}
80 -P "${CMAKE_SOURCE_DIR}/cmake/scripts/FindVersion.cmake"
81 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
82 BYPRODUCTS ${GENERATED_SOURCE_FILES}
86 if(DOXYGEN_EXECUTABLE)
87 add_custom_target(docs)
88 add_custom_target(docs_source
89 ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/docs
90 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
91 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
92 COMMENT "Generating documentation for source"
94 add_dependencies(docs_source
102 include(AddCustomXXXTimestamp)
104 if(OPTION_TOOLS_ONLY)
106 unset(HOST_BINARY_DIR CACHE)
108 add_subdirectory(${CMAKE_SOURCE_DIR}/src)
113 # Avoid searching for headers in Frameworks, and libraries in LIBDIR.
114 set(CMAKE_FIND_FRAMEWORK LAST)
117 # Prefer -pthread over -lpthread, which is often the better option of the two.
118 set(CMAKE_THREAD_PREFER_PTHREAD YES)
119 # Make sure we have Threads available.
120 find_package(Threads REQUIRED)
123 find_package(LibLZMA)
127 if(NOT OPTION_DEDICATED)
129 find_package(Allegro)
131 find_package(Freetype)
136 find_package(Fluidsynth)
137 find_package(Fontconfig)
138 find_package(ICU OPTIONAL_COMPONENTS i18n lx)
145 find_library(AUDIOTOOLBOX_LIBRARY AudioToolbox)
146 find_library(AUDIOUNIT_LIBRARY AudioUnit)
147 find_library(COCOA_LIBRARY Cocoa)
148 find_library(QUARTZCORE_LIBRARY QuartzCore)
151 if(NOT EMSCRIPTEN AND NOT OPTION_DEDICATED)
152 find_package(OpenGL COMPONENTS OpenGL)
156 find_package(Editbin REQUIRED)
160 find_package(Xaudio2)
162 find_package(Grfcodec)
164 include(CheckIPOSupported)
165 check_ipo_supported(RESULT IPO_FOUND)
169 if(UNIX AND NOT APPLE AND NOT OPTION_DEDICATED)
170 if(NOT SDL_FOUND AND NOT SDL2_FOUND AND NOT ALLEGRO_FOUND)
171 message(FATAL_ERROR "SDL, SDL2 or Allegro is required for this platform")
175 if(NOT AUDIOTOOLBOX_LIBRARY)
176 message(FATAL_ERROR "AudioToolbox is required for this platform")
178 if(NOT AUDIOUNIT_LIBRARY)
179 message(FATAL_ERROR "AudioUnit is required for this platform")
181 if(NOT COCOA_LIBRARY)
182 message(FATAL_ERROR "Cocoa is required for this platform")
184 if(NOT QUARTZCORE_LIBRARY)
185 message(FATAL_ERROR "QuartzCore is required for this platform")
189 if(OPTION_PACKAGE_DEPENDENCIES)
191 message(FATAL_ERROR "Can only package dependencies on Linux")
193 if(OPTION_INSTALL_FHS)
194 message(FATAL_ERROR "Cannot install in FHS folders when we are packaging dependencies")
196 if(${CMAKE_VERSION} VERSION_LESS "3.16.0")
197 message(FATAL_ERROR "OPTION_PACKAGE_DEPENDENCIES can only work with CMake 3.16+; you are using ${CMAKE_VERSION}")
200 # If we are packaging dependencies, we do two things:
201 # 1) set the RPATH to include $ORIGIN/lib; $ORIGIN (that literal string)
202 # is a Linux indicator for "path where application is". In CMake, we
203 # have to do this before add_executable() is executed.
204 # 2) copy the libraries that we compile against to the "lib" folder.
205 # This is done in InstallAndPackage.cmake.
206 set(CMAKE_INSTALL_RPATH "\$ORIGIN/lib")
207 set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
213 include_directories(${CMAKE_SOURCE_DIR}/src)
214 # Needed by everything that uses Squirrel
215 include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/squirrel/include)
219 add_executable(openttd WIN32 ${GENERATED_SOURCE_FILES})
220 set_target_properties(openttd PROPERTIES OUTPUT_NAME "${BINARY_NAME}")
221 # All other files are added via target_sources()
224 # Add DPI manifest to project; other WIN32 targets get this via ottdres.rc
225 target_sources(openttd PRIVATE "${CMAKE_SOURCE_DIR}/os/windows/openttd.manifest")
228 add_subdirectory(${CMAKE_SOURCE_DIR}/bin)
229 add_subdirectory(${CMAKE_SOURCE_DIR}/src)
230 add_subdirectory(${CMAKE_SOURCE_DIR}/media)
232 add_dependencies(openttd
235 target_link_libraries(openttd
245 target_link_libraries(openttd "be" "network" "midi")
249 set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE True)
250 set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL True)
251 set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO True)
253 set_target_properties(openttd PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
254 process_compile_flags()
257 link_package(PNG TARGET PNG::PNG ENCOURAGED)
258 link_package(ZLIB TARGET ZLIB::ZLIB ENCOURAGED)
259 link_package(LIBLZMA TARGET LibLZMA::LibLZMA ENCOURAGED)
262 if(NOT OPTION_DEDICATED)
263 link_package(Fluidsynth)
265 link_package(SDL2 TARGET SDL2::SDL2)
266 link_package(Allegro)
267 link_package(FREETYPE TARGET Freetype::Freetype)
268 link_package(Fontconfig TARGET Fontconfig::Fontconfig)
270 link_package(ICU_i18n)
272 if(SDL2_FOUND AND OPENGL_FOUND AND UNIX)
273 # SDL2 dynamically loads OpenGL if needed, so do not link to OpenGL when
274 # on Linux. For Windows, we need to link to OpenGL as we also have a win32
276 add_definitions(-DWITH_OPENGL)
277 message(STATUS "OpenGL found -- -DWITH_OPENGL -- (via SDL2)")
279 link_package(OpenGL TARGET OpenGL::GL)
284 link_package(Iconv TARGET Iconv::Iconv)
286 target_link_libraries(openttd
287 ${AUDIOTOOLBOX_LIBRARY}
290 ${QUARTZCORE_LIBRARY}
299 add_library(WASM::WASM INTERFACE IMPORTED)
301 # Allow heap-growth, and start with a bigger memory size.
302 target_link_libraries(WASM::WASM INTERFACE "-s ALLOW_MEMORY_GROWTH=1")
303 target_link_libraries(WASM::WASM INTERFACE "-s INITIAL_MEMORY=33554432")
304 target_link_libraries(WASM::WASM INTERFACE "-s DISABLE_EXCEPTION_CATCHING=0")
305 add_definitions(-s DISABLE_EXCEPTION_CATCHING=0)
307 # Export functions to Javascript.
308 target_link_libraries(WASM::WASM INTERFACE "-s EXPORTED_FUNCTIONS='[\"_main\", \"_em_openttd_add_server\"]' -s EXPORTED_RUNTIME_METHODS='[\"cwrap\"]'")
310 # Preload all the files we generate during build.
311 # As we do not compile with FreeType / FontConfig, we also have no way to
312 # render several languages (like Chinese, ..), so where do you draw the
313 # line what languages to include and which not? In the end, especially as
314 # the more languages you add the slower downloading becomes, we decided to
315 # only ship the English language.
316 target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_BINARY_DIR}/baseset@/baseset")
317 target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_BINARY_DIR}/lang/english.lng@/lang/english.lng")
318 target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_SOURCE_DIR}/bin/ai@/ai")
319 target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_SOURCE_DIR}/bin/game@/game")
321 # We use IDBFS for persistent storage.
322 target_link_libraries(WASM::WASM INTERFACE "-lidbfs.js")
324 # Use custom pre-js and shell.html.
325 target_link_libraries(WASM::WASM INTERFACE "--pre-js ${CMAKE_SOURCE_DIR}/os/emscripten/pre.js")
326 target_link_libraries(WASM::WASM INTERFACE "--shell-file ${CMAKE_SOURCE_DIR}/os/emscripten/shell.html")
328 # Build the .html (which builds the .js, .wasm, and .data too).
329 set_target_properties(openttd PROPERTIES SUFFIX ".html")
330 target_link_libraries(openttd WASM::WASM)
333 if(NOT PERSONAL_DIR STREQUAL "(not set)")
336 -DPERSONAL_DIR="${PERSONAL_DIR}"
340 if(NOT SHARED_DIR STREQUAL "(not set)")
343 -DSHARED_DIR="${SHARED_DIR}"
347 if(NOT GLOBAL_DIR STREQUAL "(not set)")
349 -DGLOBAL_DATA_DIR="${GLOBAL_DIR}"
355 add_definitions_based_on_options()
365 target_link_libraries(openttd
374 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
375 add_definitions(-DPOINTER_IS_64BIT)
378 include(CreateRegression)
385 include(InstallAndPackage)