From 6317176d7e890292cb40d6add96a1c81e8eeb08f Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 21 May 2020 23:52:19 +0100 Subject: More CMake cleanup --- CMakeLists.txt | 232 +++++---------------------------------------------------- 1 file changed, 20 insertions(+), 212 deletions(-) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f58696fe..f31f33f13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ cmake_minimum_required (VERSION 3.13) +cmake_policy(VERSION 3.13...3.17.2) project( Cuberite DESCRIPTION "A lightweight, fast and extensible game server for Minecraft" @@ -22,76 +23,11 @@ project( ) option(BUILD_TOOLS "Sets up additional executables to be built along with the server" OFF) +option(WHOLE_PROGRAM_OPTIMISATION "Enables link time optimisation for Release" ON) option(PRECOMPILE_HEADERS "Enable precompiled headers for faster builds" ON) option(SELF_TEST "Enables testing code to be built" OFF) option(UNITY_BUILDS "Enables source aggregation for faster builds" ON) -# These env variables are used for configuring Travis CI builds. -if(DEFINED ENV{TRAVIS_CUBERITE_BUILD_TYPE}) - message("Setting build type to $ENV{TRAVIS_CUBERITE_BUILD_TYPE}") - set(CMAKE_BUILD_TYPE $ENV{TRAVIS_CUBERITE_BUILD_TYPE}) -endif() - -if(DEFINED ENV{TRAVIS_CUBERITE_FORCE32}) - set(FORCE32 $ENV{TRAVIS_CUBERITE_FORCE32}) -endif() - -if(DEFINED ENV{TRAVIS_BUILD_WITH_COVERAGE}) - set(BUILD_WITH_COVERAGE $ENV{TRAVIS_BUILD_WITH_COVERAGE}) -endif() - -if(DEFINED ENV{CUBERITE_BUILD_ID}) - # The build info is defined by the build system (Travis / Jenkins) - set(BUILD_ID $ENV{CUBERITE_BUILD_ID}) - set(BUILD_SERIES_NAME $ENV{CUBERITE_BUILD_SERIES_NAME}) - set(BUILD_DATETIME $ENV{CUBERITE_BUILD_DATETIME}) - if(DEFINED ENV{CUBERITE_BUILD_COMMIT_ID}) - set(BUILD_COMMIT_ID $ENV{CUBERITE_BUILD_COMMIT_ID}) - else() - message("Commit id not set, attempting to determine id from git") - execute_process( - COMMAND git rev-parse HEAD - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} - RESULT_VARIABLE GIT_EXECUTED - OUTPUT_VARIABLE BUILD_COMMIT_ID - ) - string(STRIP ${BUILD_COMMIT_ID} BUILD_COMMIT_ID) - if (NOT (GIT_EXECUTED EQUAL 0)) - message(FATAL_ERROR "Could not identifiy git commit id") - endif() - endif() -else() - # This is a local build, stuff in some basic info: - set(BUILD_ID "Unknown") - set(BUILD_SERIES_NAME "local build") - execute_process( - COMMAND git rev-parse HEAD - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} - RESULT_VARIABLE GIT_EXECUTED - OUTPUT_VARIABLE BUILD_COMMIT_ID - ) - if (NOT(GIT_EXECUTED EQUAL 0)) - set(BUILD_COMMIT_ID "Unknown") - endif() - string(STRIP ${BUILD_COMMIT_ID} BUILD_COMMIT_ID) - execute_process( - COMMAND git log -1 --date=iso --pretty=format:%ai - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} - RESULT_VARIABLE GIT_EXECUTED - OUTPUT_VARIABLE BUILD_DATETIME - ) - if (NOT(GIT_EXECUTED EQUAL 0)) - set(BUILD_DATETIME "Unknown") - endif() - string(STRIP ${BUILD_DATETIME} BUILD_DATETIME) - - # The BUILD_COMMIT_ID and BUILD_DATETIME aren't updated on each repo pull - # They are only updated when cmake re-configures the project - # Therefore mark them as "approx: " - set(BUILD_COMMIT_ID "approx: ${BUILD_COMMIT_ID}") - set(BUILD_DATETIME "approx: ${BUILD_DATETIME}") -endif() - # We need C++17 features set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -107,108 +43,9 @@ endif() # Static CRT set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") -# This has to be done before any flags have been set up. -if(${BUILD_TOOLS}) - message("Building tools") - add_subdirectory(Tools/GrownBiomeGenVisualiser/) - add_subdirectory(Tools/MCADefrag/) - add_subdirectory(Tools/NoiseSpeedTest/) - add_subdirectory(Tools/ProtoProxy/) -endif() - -if(${BUILD_UNSTABLE_TOOLS}) - message("Building unstable tools") - add_subdirectory(Tools/GeneratorPerformanceTest/) -endif() - -include(SetFlags.cmake) -set_flags() -set_lib_flags() -enable_profile() - -# Set options for SQLiteCpp, disable all their tests and lints: -set(SQLITECPP_RUN_CPPLINT OFF CACHE BOOL "Run cpplint.py tool for Google C++ StyleGuide." FORCE) -set(SQLITECPP_RUN_CPPCHECK OFF CACHE BOOL "Run cppcheck C++ static analysis tool." FORCE) -set(SQLITECPP_RUN_DOXYGEN OFF CACHE BOOL "Run Doxygen C++ documentation tool." FORCE) -set(SQLITECPP_BUILD_EXAMPLES OFF CACHE BOOL "Build examples." FORCE) -set(SQLITECPP_BUILD_TESTS OFF CACHE BOOL "Build and run tests." FORCE) -set(SQLITECPP_INTERNAL_SQLITE ON CACHE BOOL "Add the internal SQLite3 source to the project." FORCE) -set(SQLITE_ENABLE_COLUMN_METADATA OFF CACHE BOOL "" FORCE) - -# Set options for LibEvent, disable all their tests and benchmarks: -set(EVENT__DISABLE_OPENSSL YES CACHE BOOL "Disable OpenSSL in LibEvent" FORCE) -set(EVENT__DISABLE_BENCHMARK YES CACHE BOOL "Disable LibEvent benchmarks" FORCE) -set(EVENT__DISABLE_TESTS YES CACHE BOOL "Disable LibEvent tests" FORCE) -set(EVENT__DISABLE_REGRESS YES CACHE BOOL "Disable LibEvent regression tests" FORCE) -set(EVENT__DISABLE_SAMPLES YES CACHE BOOL "Disable LibEvent samples" FORCE) -set(EVENT__LIBRARY_TYPE "STATIC" CACHE STRING "Use static LibEvent libraries" FORCE) - -# Set options for JsonCPP, disabling all of their tests: -set(JSONCPP_WITH_TESTS OFF CACHE BOOL "Compile and (for jsoncpp_check) run JsonCpp test executables") -set(JSONCPP_WITH_POST_BUILD_UNITTEST OFF CACHE BOOL "Automatically run unit-tests as a post build step") -set(JSONCPP_WITH_PKGCONFIG_SUPPORT OFF CACHE BOOL "Generate and install .pc files") - -# Set options for mbedtls: -set(ENABLE_PROGRAMS OFF CACHE BOOL "Build mbed TLS programs.") -set(ENABLE_TESTING OFF CACHE BOOL "Build mbed TLS tests.") - -# Check that the libraries are present: -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/SQLiteCpp/CMakeLists.txt) - message(FATAL_ERROR "SQLiteCpp is missing in folder lib/SQLiteCpp. Have you initialized the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/mbedtls/CMakeLists.txt) - message(FATAL_ERROR "mbedTLS is missing in folder lib/mbedtls. Have you initialized the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/libevent/CMakeLists.txt) - message(FATAL_ERROR "LibEvent is missing in folder lib/libevent. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/jsoncpp/CMakeLists.txt) - message(FATAL_ERROR "JsonCPP is missing in folder lib/jsoncpp. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/cmake-coverage/CodeCoverage.cmake) - message(FATAL_ERROR "cmake-coverage is missing in folder lib/cmake-coverage. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/expat/CMakeLists.txt) - message(FATAL_ERROR "expat is missing in folder lib/expat. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/fmt/CMakeLists.txt) - message(FATAL_ERROR "fmt is missing in folder lib/fmt. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/lua/CMakeLists.txt) - message(FATAL_ERROR "lua is missing in folder lib/lua. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/luaexpat/CMakeLists.txt) - message(FATAL_ERROR "luaexpat is missing in folder lib/luaexpat. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/luaproxy/CMakeLists.txt) - message(FATAL_ERROR "luaproxy is missing in folder lib/luaproxy. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/sqlite/CMakeLists.txt) - message(FATAL_ERROR "sqlite is missing in folder lib/sqlite. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/tolua++/CMakeLists.txt) - message(FATAL_ERROR "tolua++ is missing in folder lib/tolua++. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() -if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/zlib/CMakeLists.txt) - message(FATAL_ERROR "zlib is missing in folder lib/zlib. Have you initialized and updated the submodules / downloaded the extra libraries?") -endif() - -# Include all the libraries -# We use EXCLUDE_FROM_ALL so that only the explicit dependencies are compiled -# (mbedTLS also has test and example programs in their CMakeLists.txt, we don't want those): -add_subdirectory(lib/expat) -add_subdirectory(lib/fmt) -add_subdirectory(lib/jsoncpp EXCLUDE_FROM_ALL) -add_subdirectory(lib/libevent EXCLUDE_FROM_ALL) -add_subdirectory(lib/lua) -add_subdirectory(lib/luaexpat) -add_subdirectory(lib/mbedtls) -add_subdirectory(lib/SQLiteCpp) # SQLiteCpp needs to be included before sqlite so the lsqlite target is available -add_subdirectory(lib/sqlite) -add_subdirectory(lib/tolua++ EXCLUDE_FROM_ALL) -add_subdirectory(lib/zlib) +# Add build timestamp and details: +include("CMake/StampBuild.cmake") -set_exe_flags() add_executable(${CMAKE_PROJECT_NAME}) add_subdirectory(src) @@ -222,54 +59,11 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.16") target_precompile_headers(${CMAKE_PROJECT_NAME} PRIVATE src/Globals.h) endif() - if (UNITY_BUILDS) - set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES UNITY_BUILD ON) - endif() + set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES UNITY_BUILD ${UNITY_BUILDS}) else() message(WARNING "Precompiled headers for FASTER BUILDS not enabled, upgrade to CMake 1.16 or newer!") endif() -# Add required includes: -target_include_directories( - ${CMAKE_PROJECT_NAME} SYSTEM PRIVATE - lib/mbedtls/include - lib/TCLAP/include - lib # TODO fix files including zlib/x instead of x -) - -# Link dependencies as private: -target_link_libraries( - ${CMAKE_PROJECT_NAME} PRIVATE - event_core - event_extra - fmt::fmt - jsoncpp_lib - lsqlite - lua - luaexpat - mbedtls - SQLiteCpp - tolualib - zlib -) - -# Link process information library: -if (WIN32) - target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Psapi.lib) -endif() - -# Special case handling for libevent pthreads: -if(NOT WIN32) - target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE event_pthreads_static) -endif() - -# Prettify jsoncpp_lib name in VS solution explorer: -set_property(TARGET jsoncpp_lib PROPERTY PROJECT_LABEL "jsoncpp") - -if (WIN32) - add_subdirectory(lib/luaproxy) -endif() - # Selectively disable warnings in the level where the target is created: if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") # Generated file has old-style casts, missing prototypes, and deprecated declarations @@ -279,6 +73,19 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set_source_files_properties("${CMAKE_SOURCE_DIR}/src/IniFile.cpp" PROPERTIES COMPILE_OPTIONS -Wno-header-hygiene) endif() +if(${BUILD_TOOLS}) + message("Building tools") + add_subdirectory(Tools/GrownBiomeGenVisualiser/) + add_subdirectory(Tools/MCADefrag/) + add_subdirectory(Tools/NoiseSpeedTest/) + add_subdirectory(Tools/ProtoProxy/) +endif() + +if(${BUILD_UNSTABLE_TOOLS}) + message("Building unstable tools") + add_subdirectory(Tools/GeneratorPerformanceTest/) +endif() + # Self Test Mode enables extra checks at startup if(${SELF_TEST}) message("Tests enabled") @@ -286,7 +93,8 @@ if(${SELF_TEST}) add_subdirectory(tests) endif() +include("CMake/AddDependencies.cmake") include("CMake/Fixups.cmake") include("CMake/GenerateBindings.cmake") include("CMake/GroupSources.cmake") -# TODO: include("CMake/SetCompilerFlags.cmake") +include("SetFlags.cmake") -- cgit v1.2.3