Fix #9015: Don't set free space value if not requested. (#9016)
[openttd-github.git] / CMakeLists.txt
blob63a0ce2fe8b0953aa74d9cf655ef44ab8f77b9db
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 build directory. You may need to delete \"${CMAKE_SOURCE_DIR}/CMakeCache.txt\" first.")
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}/bin)
224 add_subdirectory(${CMAKE_SOURCE_DIR}/src)
225 add_subdirectory(${CMAKE_SOURCE_DIR}/media)
227 add_dependencies(openttd
228     find_version)
230 target_link_libraries(openttd
231     openttd::languages
232     openttd::settings
233     openttd::media
234     openttd::basesets
235     openttd::script_api
236     Threads::Threads
239 if(IPO_FOUND)
240     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE True)
241     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL True)
242     set_target_properties(openttd PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO True)
243 endif()
244 set_target_properties(openttd PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
245 process_compile_flags()
247 include(LinkPackage)
248 link_package(PNG TARGET PNG::PNG ENCOURAGED)
249 link_package(ZLIB TARGET ZLIB::ZLIB ENCOURAGED)
250 link_package(LIBLZMA TARGET LibLZMA::LibLZMA ENCOURAGED)
251 link_package(LZO)
253 if(NOT OPTION_DEDICATED)
254     link_package(Fluidsynth)
255     link_package(SDL)
256     link_package(SDL2 TARGET SDL2::SDL2)
257     link_package(Allegro)
258     link_package(FREETYPE TARGET Freetype::Freetype)
259     link_package(Fontconfig TARGET Fontconfig::Fontconfig)
260     link_package(ICU_lx)
261     link_package(ICU_i18n)
263     if(SDL2_FOUND AND OPENGL_FOUND AND UNIX)
264         # SDL2 dynamically loads OpenGL if needed, so do not link to OpenGL when
265         # on Linux. For Windows, we need to link to OpenGL as we also have a win32
266         # driver using it.
267         add_definitions(-DWITH_OPENGL)
268         message(STATUS "OpenGL found -- -DWITH_OPENGL -- (via SDL2)")
269     else()
270         link_package(OpenGL TARGET OpenGL::GL)
271     endif()
272 endif()
274 if(APPLE)
275     link_package(Iconv TARGET Iconv::Iconv)
277     target_link_libraries(openttd
278         ${AUDIOTOOLBOX_LIBRARY}
279         ${AUDIOUNIT_LIBRARY}
280         ${COCOA_LIBRARY}
281         ${QUARTZCORE_LIBRARY}
282     )
284     add_definitions(
285         -DWITH_COCOA
286     )
287 endif()
289 if(EMSCRIPTEN)
290     add_library(WASM::WASM INTERFACE IMPORTED)
292     # Allow heap-growth, and start with a bigger memory size.
293     target_link_libraries(WASM::WASM INTERFACE "-s ALLOW_MEMORY_GROWTH=1")
294     target_link_libraries(WASM::WASM INTERFACE "-s INITIAL_MEMORY=33554432")
295     target_link_libraries(WASM::WASM INTERFACE "-s DISABLE_EXCEPTION_CATCHING=0")
296     add_definitions(-s DISABLE_EXCEPTION_CATCHING=0)
298     # Export functions to Javascript.
299     target_link_libraries(WASM::WASM INTERFACE "-s EXPORTED_FUNCTIONS='[\"_main\", \"_em_openttd_add_server\"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]'")
301     # Preload all the files we generate during build.
302     # As we do not compile with FreeType / FontConfig, we also have no way to
303     # render several languages (like Chinese, ..), so where do you draw the
304     # line what languages to include and which not? In the end, especially as
305     # the more languages you add the slower downloading becomes, we decided to
306     # only ship the English language.
307     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_BINARY_DIR}/baseset@/baseset")
308     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_BINARY_DIR}/lang/english.lng@/lang/english.lng")
309     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_SOURCE_DIR}/bin/ai@/ai")
310     target_link_libraries(WASM::WASM INTERFACE "--preload-file ${CMAKE_SOURCE_DIR}/bin/game@/game")
312     # We use IDBFS for persistent storage.
313     target_link_libraries(WASM::WASM INTERFACE "-lidbfs.js")
315     # Use custom pre-js and shell.html.
316     target_link_libraries(WASM::WASM INTERFACE "--pre-js ${CMAKE_SOURCE_DIR}/os/emscripten/pre.js")
317     target_link_libraries(WASM::WASM INTERFACE "--shell-file ${CMAKE_SOURCE_DIR}/os/emscripten/shell.html")
319     # Build the .html (which builds the .js, .wasm, and .data too).
320     set_target_properties(openttd PROPERTIES SUFFIX ".html")
321     target_link_libraries(openttd WASM::WASM)
322 endif()
324 if(NOT PERSONAL_DIR STREQUAL "(not set)")
325     add_definitions(
326         -DWITH_PERSONAL_DIR
327         -DPERSONAL_DIR="${PERSONAL_DIR}"
328     )
329 endif()
331 if(NOT SHARED_DIR STREQUAL "(not set)")
332     add_definitions(
333         -DWITH_SHARED_DIR
334         -DSHARED_DIR="${SHARED_DIR}"
335     )
336 endif()
338 if(NOT GLOBAL_DIR STREQUAL "(not set)")
339     add_definitions(
340         -DGLOBAL_DATA_DIR="${GLOBAL_DIR}"
341     )
342 endif()
344 link_package(SSE)
346 add_definitions_based_on_options()
348 if(WIN32)
349     add_definitions(
350         -DUNICODE
351         -D_UNICODE
352         -DWITH_UNISCRIBE
353     )
355     target_link_libraries(openttd
356         ws2_32
357         winmm
358         imm32
359     )
360 endif()
362 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
363     add_definitions(-D_SQ64)
364 endif()
366 include(CreateRegression)
367 create_regression()
369 if(APPLE OR WIN32)
370     find_package(Pandoc)
371 endif()
373 include(InstallAndPackage)