From 15ed39b5acb3da1fe5fd5bb3d2b3a02cde2a99f9 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 13 Dec 2023 17:26:37 +0000 Subject: [PATCH] CMake: Use openttd_lib split build from upstream Add option to disable for cross-compiling --- CMakeLists.txt | 45 +++++++++++++++++++++++++++++------------ cmake/CheckAtomic.cmake | 2 +- cmake/LinkPackage.cmake | 4 ++-- cmake/SourceList.cmake | 6 ++++-- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 57f5a5f874..ac3f0bfa23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -254,6 +254,12 @@ if(OPTION_PACKAGE_DEPENDENCIES) set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) endif() +if(OPTION_NO_SPLIT_LIB) + set(OPENTTD_LIB openttd) +else() + set(OPENTTD_LIB openttd_lib) +endif() + include(CTest) include(SourceList) @@ -264,11 +270,16 @@ include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/squirrel/include) include(MSVCFilters) -add_executable(openttd WIN32 ${GENERATED_SOURCE_FILES}) -set_target_properties(openttd PROPERTIES OUTPUT_NAME "${BINARY_NAME}") +if(OPTION_NO_SPLIT_LIB) + add_executable(openttd WIN32 ${GENERATED_SOURCE_FILES}) +else() + add_library(openttd_lib OBJECT ${GENERATED_SOURCE_FILES}) + add_executable(openttd WIN32) + add_executable(openttd_test) + set_target_properties(openttd_test PROPERTIES EXCLUDE_FROM_ALL TRUE) +endif() -add_executable(openttd_test) -set_target_properties(openttd_test PROPERTIES EXCLUDE_FROM_ALL TRUE) +set_target_properties(openttd PROPERTIES OUTPUT_NAME "${BINARY_NAME}") # All other files are added via target_sources() set(host_tools_list strgen settingsgen) @@ -310,6 +321,7 @@ if(MSVC) # If target -static is used, switch our project to static (/MT) too. # If the target ends on -static-md, it will remain dynamic (/MD). if(VCPKG_TARGET_TRIPLET MATCHES "-static" AND NOT VCPKG_TARGET_TRIPLET MATCHES "-md") + set_property(TARGET openttd_lib PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") set_property(TARGET openttd PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") set_property(TARGET openttd_test PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") endif() @@ -330,18 +342,25 @@ endif() add_dependencies(openttd find_version) -target_link_libraries(openttd +if(NOT OPTION_NO_SPLIT_LIB) + target_link_libraries(openttd openttd_lib) + + target_link_libraries(openttd_test PRIVATE openttd_lib) + include(Catch) + catch_discover_tests(openttd_test) +endif() + +target_link_libraries(${OPENTTD_LIB} openttd::languages openttd::settings - openttd::media - openttd::basesets openttd::script_api - openttd::binfiles Threads::Threads ) -target_link_libraries(openttd_test) -include(Catch) -catch_discover_tests(openttd_test) + +target_link_libraries(openttd + openttd::media + openttd::basesets +) if(HAIKU) target_link_libraries(openttd "be" "network" "midi") @@ -395,7 +414,7 @@ include(CheckAtomic) if(APPLE) link_package(Iconv TARGET Iconv::Iconv) - target_link_libraries(openttd + target_link_libraries(${OPENTTD_LIB} ${AUDIOTOOLBOX_LIBRARY} ${AUDIOUNIT_LIBRARY} ${COCOA_LIBRARY} @@ -492,7 +511,7 @@ if(WIN32) -DPSAPI_VERSION=1 ) - target_link_libraries(openttd + target_link_libraries(${OPENTTD_LIB} ws2_32 winmm imm32 diff --git a/cmake/CheckAtomic.cmake b/cmake/CheckAtomic.cmake index 52d93f4f5b..63aa13b58b 100644 --- a/cmake/CheckAtomic.cmake +++ b/cmake/CheckAtomic.cmake @@ -83,5 +83,5 @@ else() endif() if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB) - target_link_libraries(openttd atomic) + target_link_libraries(${OPENTTD_LIB} atomic) endif() diff --git a/cmake/LinkPackage.cmake b/cmake/LinkPackage.cmake index e555c12b5a..7c1a494ded 100644 --- a/cmake/LinkPackage.cmake +++ b/cmake/LinkPackage.cmake @@ -8,13 +8,13 @@ function(link_package NAME) # which (later) cmake considers to be an error. Work around this with by stripping the incoming string. if(LP_TARGET AND TARGET ${LP_TARGET}) string(STRIP "${LP_TARGET}" LP_TARGET) - target_link_libraries(openttd ${LP_TARGET}) + target_link_libraries(${OPENTTD_LIB} ${LP_TARGET}) message(STATUS "${NAME} found -- -DWITH_${UCNAME} -- ${LP_TARGET}") else() string(STRIP "${${NAME}_LIBRARY}" ${NAME}_LIBRARY) string(STRIP "${${NAME}_LIBRARIES}" ${NAME}_LIBRARIES) include_directories(${${NAME}_INCLUDE_DIRS} ${${NAME}_INCLUDE_DIR}) - target_link_libraries(openttd ${${NAME}_LIBRARIES} ${${NAME}_LIBRARY}) + target_link_libraries(${OPENTTD_LIB} ${${NAME}_LIBRARIES} ${${NAME}_LIBRARY}) message(STATUS "${NAME} found -- -DWITH_${UCNAME} -- ${${NAME}_INCLUDE_DIRS} ${${NAME}_INCLUDE_DIR} -- ${${NAME}_LIBRARIES} ${${NAME}_LIBRARY}") endif() elseif(LP_ENCOURAGED) diff --git a/cmake/SourceList.cmake b/cmake/SourceList.cmake index 41a9c3b51a..4d8961a78f 100644 --- a/cmake/SourceList.cmake +++ b/cmake/SourceList.cmake @@ -32,7 +32,7 @@ endfunction() # For example: ADD_IF SDL_FOUND AND Allegro_FOUND # function(add_files) - _add_files_tgt(openttd ${ARGV}) + _add_files_tgt(${OPENTTD_LIB} ${ARGV}) endfunction() # Add a test file to be compiled. @@ -44,7 +44,9 @@ endfunction() # For example: ADD_IF SDL_FOUND AND Allegro_FOUND # function(add_test_files) - _add_files_tgt(openttd_test ${ARGV}) + if(NOT OPTION_NO_SPLIT_LIB) + _add_files_tgt(openttd_test ${ARGV}) + endif() endfunction() # This function works around an 'issue' with CMake, where