From 06600440feee6b879c6283290ae9126318772ebc Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sat, 26 Jan 2019 18:41:56 +0100 Subject: [PATCH 1/6] Enable LTO/IPO on Clang and GCC --- CMakeLists.txt | 168 ++++++++++++++++++-------------- src/openrct2-cli/CMakeLists.txt | 6 +- src/openrct2-ui/CMakeLists.txt | 6 +- src/openrct2/CMakeLists.txt | 2 +- 4 files changed, 108 insertions(+), 74 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 06329c0699..6ca9377612 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,19 @@ # CMAKE project for openrct2 -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.9) if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR) message(FATAL_ERROR "Building in-source is not supported! Create a build dir and remove ${CMAKE_SOURCE_DIR}/CMakeCache.txt") endif() project(openrct2 CXX) +include(CheckIPOSupported) +check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_LOG) +if(IPO_SUPPORTED) + message(INFO "IPO supported") +else() + message(WARNING "IPO not supported: ${IPO_LOG}") +endif() + if (NOT MSVC) include(FindPkgConfig) endif () @@ -80,76 +88,6 @@ execute_process( ERROR_QUIET ) -function (ADD_CHECK_CXX_COMPILER_FLAG _CXXFLAGS _CACHE_VAR _FLAG) - CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}") - if (${_CACHE_VAR}) - set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE) - else () - message(STATUS "Unsupported CXXFLAG: ${_FLAG}") - endif () -endfunction () - -if (MSVC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /permissive- /Zc:externConstexpr /WX") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244") # C4244: 'conversion_type': conversion from 'type1' to 'type2', possible loss of data - - add_definitions(-D_CRT_SECURE_NO_WARNINGS) - add_definitions(-D_SCL_SECURE_NO_WARNINGS) - add_definitions(-D__SSE4_1__) - add_definitions(-D__AVX2__) -else () - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_NULL_DEREFERENCE -Wnull-dereference) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_FINAL_TYPES -Wsuggest-final-types) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_FINAL_METHODS -Wsuggest-final-methods) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_OVERRIDE -Wsuggest-override) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DUPLICATED_COND -Wduplicated-cond) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_NON_VIRTUAL_DTOR -Wnon-virtual-dtor) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_MISSING_VARIABLE_DECLARATIONS -Wmissing-variable-declarations) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DUPLICATED_BRANCHES -Wduplicated-branches) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_RESTRICT -Wrestrict) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_MISSING_FIELD_INITIALIZERS -Wmissing-field-initializers) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_UNREACHABLE_CODE_BREAK -Wunreachable-code-break) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_LOGICAL_OP -Wlogical-op) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_RANGE_LOOP_ANALYSIS -Wrange-loop-analysis) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_TAUTOLOGICAL_ZERO_COMPARE -Wtautological-unsigned-zero-compare) - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WNO_CLOBBERED -Wno-clobbered) - # Disabled due to problems compiling OpenSSL on macOS. - # ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DOCUMENTATION -Wdocumentation) - - # Items below are not supported by ICC - if (NOT MINGW) - # Do not enable for MinGW, as its headers contain redundant declarations of builtin functions - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_REDUNDANT_DECLS -Wredundant-decls) - endif () - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_IGNORED_QUALIFIERS -Wignored-qualifiers) - - # -Wstrict-overflow is only active when -fstrict-overflow is enabled, but -fstrict-overflow - # is enabled on -O2, -O3, -Os. This should help catch bugs locally before they reach Travis - # As of 2a435bf -Wstrict-overflow=1 passes, but higher values do not. - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-overflow") - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_STRICT_OVERFLOW -Wstrict-overflow=1) - - # Compiler flags - set(DEBUG_LEVEL 0 CACHE STRING "Select debug level for compilation. Use value in range 0–3.") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-aliasing -Werror -Wundef -Wmissing-declarations -Winit-self -Wall -Wextra -Wshadow") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas -Wno-missing-braces -Wno-comment -Wnonnull -Wno-unused-parameter") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG=${DEBUG_LEVEL}") - - if(APPLE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=objc-method-access") - endif() - - # On mingw all code is already PIC, this will avoid compiler error on redefining this option - if (NOT MINGW) - set(CMAKE_POSITION_INDEPENDENT_CODE ON) - endif () - - if (APPLE AND NOT USE_MMAP) - set(CMAKE_POSITION_INDEPENDENT_CODE OFF) - else () - set(CMAKE_POSITION_INDEPENDENT_CODE ON) - endif () -endif () # Defines if (USE_MMAP) @@ -227,6 +165,94 @@ if (WITH_TESTS) include("${ROOT_DIR}/test/tests/CMakeLists.txt" NO_POLICY_SCOPE) endif () +# Check if a flag exists and add it to the compiler (so, not the linker) options +function (ADD_CHECK_CXX_COMPILER_FLAG _CXXFLAGS _CACHE_VAR _FLAG) + CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}") + if (${_CACHE_VAR}) + if(TARGET ${PROJECT}) + target_compile_options(${PROJECT} PRIVATE ${_FLAG}) + endif() + if(TARGET ${PROJECT}-cli) + target_compile_options(${PROJECT}-cli PRIVATE ${_FLAG}) + endif() + else () + message(STATUS "Unsupported CXXFLAG: ${_FLAG}") + endif () +endfunction () + +# Check if a flag exists and add it to the compiler and the linker options +function (ADD_CHECK_CXX_FLAG _CXXFLAGS _CACHE_VAR _FLAG) + CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}") + if (${_CACHE_VAR}) + set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE) + else () + message(STATUS "Unsupported CXXFLAG: ${_FLAG}") + endif () +endfunction () + +if (MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /permissive- /Zc:externConstexpr /WX") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244") # C4244: 'conversion_type': conversion from 'type1' to 'type2', possible loss of data + + add_definitions(-D_CRT_SECURE_NO_WARNINGS) + add_definitions(-D_SCL_SECURE_NO_WARNINGS) + add_definitions(-D__SSE4_1__) + add_definitions(-D__AVX2__) +else () + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_NULL_DEREFERENCE -Wnull-dereference) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_FINAL_TYPES -Wsuggest-final-types) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_FINAL_METHODS -Wsuggest-final-methods) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_OVERRIDE -Wsuggest-override) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DUPLICATED_COND -Wduplicated-cond) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_NON_VIRTUAL_DTOR -Wnon-virtual-dtor) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_MISSING_VARIABLE_DECLARATIONS -Wmissing-variable-declarations) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DUPLICATED_BRANCHES -Wduplicated-branches) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_RESTRICT -Wrestrict) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_MISSING_FIELD_INITIALIZERS -Wmissing-field-initializers) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_UNREACHABLE_CODE_BREAK -Wunreachable-code-break) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_LOGICAL_OP -Wlogical-op) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_RANGE_LOOP_ANALYSIS -Wrange-loop-analysis) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_TAUTOLOGICAL_ZERO_COMPARE -Wtautological-unsigned-zero-compare) + ADD_CHECK_CXX_FLAG(CMAKE_CXX_FLAGS CXX_WNO_CLOBBERED -Wno-clobbered) + # Disabled due to problems compiling OpenSSL on macOS. + # ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DOCUMENTATION -Wdocumentation) + + # Items below are not supported by ICC + if (NOT MINGW) + # Do not enable for MinGW, as its headers contain redundant declarations of builtin functions + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_REDUNDANT_DECLS -Wredundant-decls) + endif () + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_IGNORED_QUALIFIERS -Wignored-qualifiers) + + # -Wstrict-overflow is only active when -fstrict-overflow is enabled, but -fstrict-overflow + # is enabled on -O2, -O3, -Os. This should help catch bugs locally before they reach Travis + # As of 2a435bf -Wstrict-overflow=1 passes, but higher values do not. + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-overflow") + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_STRICT_OVERFLOW -Wstrict-overflow=1) + + # Compiler flags + set(DEBUG_LEVEL 0 CACHE STRING "Select debug level for compilation. Use value in range 0–3.") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-aliasing -Werror -Wundef -Wmissing-declarations -Winit-self -Wall -Wextra -Wshadow") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas -Wno-missing-braces -Wno-comment -Wnonnull -Wno-unused-parameter") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG=${DEBUG_LEVEL}") + + if(APPLE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=objc-method-access") + endif() + + # On mingw all code is already PIC, this will avoid compiler error on redefining this option + if (NOT MINGW) + set(CMAKE_POSITION_INDEPENDENT_CODE ON) + endif () + + if (APPLE AND NOT USE_MMAP) + set(CMAKE_POSITION_INDEPENDENT_CODE OFF) + else () + set(CMAKE_POSITION_INDEPENDENT_CODE ON) + endif () +endif () + + # Install # Don't recurse, grab all *.txt and *.md files file(GLOB DOC_FILES "${ROOT_DIR}/distribution/*.txt") diff --git a/src/openrct2-cli/CMakeLists.txt b/src/openrct2-cli/CMakeLists.txt index 0f6e093162..441324fa44 100644 --- a/src/openrct2-cli/CMakeLists.txt +++ b/src/openrct2-cli/CMakeLists.txt @@ -1,5 +1,5 @@ # CMAKE project for openrct2-cli (CLI-only build of OpenRCT2) -cmake_minimum_required(VERSION 3.1) +cmake_minimum_required(VERSION 3.9) if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR) message(FATAL_ERROR "Building in-source is not supported! Create a build dir and remove ${CMAKE_SOURCE_DIR}/CMakeCache.txt") endif () @@ -15,6 +15,10 @@ set (PROJECT openrct2-cli) project(${PROJECT} CXX) add_executable(${PROJECT} ${OPENRCT2_CLI_SOURCES}) +if(IPO_SUPPORTED) + set_property(TARGET ${PROJECT} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) +endif() + target_link_libraries(${PROJECT} "libopenrct2") # Needed for interactive console diff --git a/src/openrct2-ui/CMakeLists.txt b/src/openrct2-ui/CMakeLists.txt index 40e120d4aa..031b46f8c8 100644 --- a/src/openrct2-ui/CMakeLists.txt +++ b/src/openrct2-ui/CMakeLists.txt @@ -1,5 +1,5 @@ # CMAKE project for openrct2-ui (UI build of OpenRCT2) -cmake_minimum_required(VERSION 3.1) +cmake_minimum_required(VERSION 3.9) if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR) message(FATAL_ERROR "Building in-source is not supported! Create a build dir and remove ${CMAKE_SOURCE_DIR}/CMakeCache.txt") endif () @@ -50,6 +50,10 @@ set (PROJECT openrct2) project(${PROJECT} CXX) add_executable(${PROJECT} ${OPENRCT2_UI_SOURCES} ${OPENRCT2_UI_MM_SOURCES}) +if(IPO_SUPPORTED) + set_property(TARGET ${PROJECT} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) +endif() + target_link_libraries(${PROJECT} "libopenrct2" ${SDL2_LDFLAGS} ${SPEEX_LDFLAGS}) diff --git a/src/openrct2/CMakeLists.txt b/src/openrct2/CMakeLists.txt index bc54c296c1..0471db4717 100644 --- a/src/openrct2/CMakeLists.txt +++ b/src/openrct2/CMakeLists.txt @@ -1,5 +1,5 @@ # CMAKE project for libopenrct2 (core OpenRCT2 component) -cmake_minimum_required(VERSION 3.7) +cmake_minimum_required(VERSION 3.9) if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR) message(FATAL_ERROR "Building in-source is not supported! Create a build dir and remove ${CMAKE_SOURCE_DIR}/CMakeCache.txt") endif () From 87d9932792130c2485145fa209318b89f6572d0b Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 27 Jan 2019 12:56:36 +0100 Subject: [PATCH 2/6] Conditionally enable IPO Put corresponding functionality in `cmake/ipo.cmake`. This directory allows for future further separation of build concerns. --- CMakeLists.txt | 12 ++++-------- cmake/ipo.cmake | 24 ++++++++++++++++++++++++ src/openrct2-cli/CMakeLists.txt | 5 +---- src/openrct2-ui/CMakeLists.txt | 5 +---- 4 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 cmake/ipo.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ca9377612..6850784e8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,14 +6,6 @@ endif() project(openrct2 CXX) -include(CheckIPOSupported) -check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_LOG) -if(IPO_SUPPORTED) - message(INFO "IPO supported") -else() - message(WARNING "IPO not supported: ${IPO_LOG}") -endif() - if (NOT MSVC) include(FindPkgConfig) endif () @@ -60,6 +52,10 @@ if (PORTABLE OR WIN32) set(CMAKE_INSTALL_RPATH "$ORIGIN") endif () +include(cmake/ipo.cmake) +list(APPEND IPO_ENABLED_BUILDS Release RelWithDebInfo MinSizeRel) +ipo_enable("${IPO_ENABLED_BUILDS}") + # Describe current version in terms of closest tag execute_process( COMMAND git describe HEAD diff --git a/cmake/ipo.cmake b/cmake/ipo.cmake new file mode 100644 index 0000000000..5df2e9ef06 --- /dev/null +++ b/cmake/ipo.cmake @@ -0,0 +1,24 @@ +# Helpers for inter-procedural optimizations (IPO) + +# Enabled IPO for a LIST of CMake build types. +# Provides IPO_BUILD_ENABLED to the parent scope. +function(ipo_enable IPO_ENABLED_BUILDS) + include(CheckIPOSupported) + check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_LOG) + + set(IPO_BUILD_ENABLED OFF PARENT_SCOPE) + if(IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE} IN_LIST IPO_ENABLED_BUILDS) + message(STATUS "IPO supported and enabled in ${CMAKE_BUILD_TYPE}.") + set(IPO_BUILD_ENABLED ON PARENT_SCOPE) + elseif(NOT IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE} IN_LIST IPO_ENABLED_BUILDS) + message(STATUS "IPO not supported: ${IPO_LOG}.") + endif() +endfunction() + +# Sets appropriate IPO properties on target _target +# if IPO is supported and enabled for the current build. +function(ipo_set_target_properties _target) + if(IPO_BUILD_ENABLED) + set_property(TARGET ${_target} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() +endfunction() diff --git a/src/openrct2-cli/CMakeLists.txt b/src/openrct2-cli/CMakeLists.txt index 441324fa44..e2c444fdf9 100644 --- a/src/openrct2-cli/CMakeLists.txt +++ b/src/openrct2-cli/CMakeLists.txt @@ -14,10 +14,7 @@ file(GLOB_RECURSE OPENRCT2_CLI_SOURCES set (PROJECT openrct2-cli) project(${PROJECT} CXX) add_executable(${PROJECT} ${OPENRCT2_CLI_SOURCES}) - -if(IPO_SUPPORTED) - set_property(TARGET ${PROJECT} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) -endif() +ipo_set_target_properties(${PROJECT}) target_link_libraries(${PROJECT} "libopenrct2") diff --git a/src/openrct2-ui/CMakeLists.txt b/src/openrct2-ui/CMakeLists.txt index 031b46f8c8..5f5e271a24 100644 --- a/src/openrct2-ui/CMakeLists.txt +++ b/src/openrct2-ui/CMakeLists.txt @@ -49,10 +49,7 @@ endif () set (PROJECT openrct2) project(${PROJECT} CXX) add_executable(${PROJECT} ${OPENRCT2_UI_SOURCES} ${OPENRCT2_UI_MM_SOURCES}) - -if(IPO_SUPPORTED) - set_property(TARGET ${PROJECT} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) -endif() +ipo_set_target_properties(${PROJECT}) target_link_libraries(${PROJECT} "libopenrct2" ${SDL2_LDFLAGS} From 0789ef541d509161afc1322d8d02feb0b5e93975 Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 27 Jan 2019 13:10:56 +0100 Subject: [PATCH 3/6] Add IPO override cmake option --- cmake/ipo.cmake | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmake/ipo.cmake b/cmake/ipo.cmake index 5df2e9ef06..b760f3cddf 100644 --- a/cmake/ipo.cmake +++ b/cmake/ipo.cmake @@ -1,5 +1,7 @@ # Helpers for inter-procedural optimizations (IPO) +option(DISABLE_IPO "Disable IPO in supported release builds." OFF) + # Enabled IPO for a LIST of CMake build types. # Provides IPO_BUILD_ENABLED to the parent scope. function(ipo_enable IPO_ENABLED_BUILDS) @@ -8,8 +10,12 @@ function(ipo_enable IPO_ENABLED_BUILDS) set(IPO_BUILD_ENABLED OFF PARENT_SCOPE) if(IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE} IN_LIST IPO_ENABLED_BUILDS) - message(STATUS "IPO supported and enabled in ${CMAKE_BUILD_TYPE}.") - set(IPO_BUILD_ENABLED ON PARENT_SCOPE) + if(NOT DISABLE_IPO) + message(STATUS "IPO supported and enabled in ${CMAKE_BUILD_TYPE}.") + set(IPO_BUILD_ENABLED ON PARENT_SCOPE) + else() + message(STATUS "IPO explicitly disabled.") + endif() elseif(NOT IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE} IN_LIST IPO_ENABLED_BUILDS) message(STATUS "IPO not supported: ${IPO_LOG}.") endif() From 9186c8b7d9ade3315e42ec268322d5899a03d8b7 Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 27 Jan 2019 13:23:36 +0100 Subject: [PATCH 4/6] Check for unset CMAKE_BUILD_TYPE --- cmake/ipo.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmake/ipo.cmake b/cmake/ipo.cmake index b760f3cddf..ce54b0e381 100644 --- a/cmake/ipo.cmake +++ b/cmake/ipo.cmake @@ -9,7 +9,9 @@ function(ipo_enable IPO_ENABLED_BUILDS) check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_LOG) set(IPO_BUILD_ENABLED OFF PARENT_SCOPE) - if(IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE} IN_LIST IPO_ENABLED_BUILDS) + if(NOT CMAKE_BUILD_TYPE) + message(STATUS "CMAKE_BUILD_TYPE not explicitly set. Not enabling IPO.") + elseif(IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE} IN_LIST IPO_ENABLED_BUILDS) if(NOT DISABLE_IPO) message(STATUS "IPO supported and enabled in ${CMAKE_BUILD_TYPE}.") set(IPO_BUILD_ENABLED ON PARENT_SCOPE) From b1bc3d93223ee27eba9c1e97525e3293e522a59b Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 27 Jan 2019 13:44:16 +0100 Subject: [PATCH 5/6] Make build type check case insensitive --- CMakeLists.txt | 2 +- cmake/ipo.cmake | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6850784e8d..b6cb7c657a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ if (PORTABLE OR WIN32) endif () include(cmake/ipo.cmake) -list(APPEND IPO_ENABLED_BUILDS Release RelWithDebInfo MinSizeRel) +list(APPEND IPO_ENABLED_BUILDS RELEASE RELWITHDEBINFO MINSIZEREL) ipo_enable("${IPO_ENABLED_BUILDS}") # Describe current version in terms of closest tag diff --git a/cmake/ipo.cmake b/cmake/ipo.cmake index ce54b0e381..7c77c62a20 100644 --- a/cmake/ipo.cmake +++ b/cmake/ipo.cmake @@ -4,21 +4,23 @@ option(DISABLE_IPO "Disable IPO in supported release builds." OFF) # Enabled IPO for a LIST of CMake build types. # Provides IPO_BUILD_ENABLED to the parent scope. +# Make sure to supply the build types in UPPER CASE function(ipo_enable IPO_ENABLED_BUILDS) include(CheckIPOSupported) check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_LOG) set(IPO_BUILD_ENABLED OFF PARENT_SCOPE) + string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER) if(NOT CMAKE_BUILD_TYPE) message(STATUS "CMAKE_BUILD_TYPE not explicitly set. Not enabling IPO.") - elseif(IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE} IN_LIST IPO_ENABLED_BUILDS) + elseif(IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE_UPPER} IN_LIST IPO_ENABLED_BUILDS) if(NOT DISABLE_IPO) message(STATUS "IPO supported and enabled in ${CMAKE_BUILD_TYPE}.") set(IPO_BUILD_ENABLED ON PARENT_SCOPE) else() message(STATUS "IPO explicitly disabled.") endif() - elseif(NOT IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE} IN_LIST IPO_ENABLED_BUILDS) + elseif(NOT IPO_SUPPORTED AND ${CMAKE_BUILD_TYPE_UPPER} IN_LIST IPO_ENABLED_BUILDS) message(STATUS "IPO not supported: ${IPO_LOG}.") endif() endfunction() From ce6e08c63367acb6808413c4694b1e9f693ce25a Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 3 Feb 2019 23:32:51 +0100 Subject: [PATCH 6/6] Introduce SET_CHECK_CXX_FLAGS --- CMakeLists.txt | 65 ++++++++++++++++++---------------- src/openrct2-ui/CMakeLists.txt | 1 + src/openrct2/CMakeLists.txt | 1 + test/testpaint/CMakeLists.txt | 1 + test/tests/CMakeLists.txt | 10 ++++++ 5 files changed, 47 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6cb7c657a..6338c54611 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,10 @@ if (PORTABLE OR WIN32) set(CMAKE_INSTALL_RPATH "$ORIGIN") endif () +# LIST of supported flags, use SET_CHECK_CXX_FLAGS() to apply to target. +# Use ADD_CHECK_CXX_COMPILER_FLAG() to add to list. +list(APPEND SUPPORTED_CHECK_CXX_COMPILER_FLAGS) + include(cmake/ipo.cmake) list(APPEND IPO_ENABLED_BUILDS RELEASE RELWITHDEBINFO MINSIZEREL) ipo_enable("${IPO_ENABLED_BUILDS}") @@ -137,45 +141,21 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i686.*|i386.*|x86.*|amd64.*|AMD64.*") set(X86 1) endif() -# Include sub-projects -include("${ROOT_DIR}/src/openrct2/CMakeLists.txt" NO_POLICY_SCOPE) -include("${ROOT_DIR}/src/openrct2-cli/CMakeLists.txt" NO_POLICY_SCOPE) -if(NOT DISABLE_GUI) - include("${ROOT_DIR}/src/openrct2-ui/CMakeLists.txt" NO_POLICY_SCOPE) -endif() - -# g2 -add_custom_command( - OUTPUT g2.dat - COMMAND ./openrct2-cli sprite build \"${CMAKE_BINARY_DIR}/g2.dat\" \"${ROOT_DIR}/resources/g2/sprites.json\" - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} -) -add_custom_target(g2 DEPENDS ${PROJECT} g2.dat) - -# Include tests -if (WITH_TESTS) - enable_testing() - if (UNIX AND (NOT USE_MMAP)) - include("${ROOT_DIR}/test/testpaint/CMakeLists.txt" NO_POLICY_SCOPE) - endif () - include("${ROOT_DIR}/test/tests/CMakeLists.txt" NO_POLICY_SCOPE) -endif () - -# Check if a flag exists and add it to the compiler (so, not the linker) options +# Check if a flag exists and add it to the list of compiler (so, not linker) options function (ADD_CHECK_CXX_COMPILER_FLAG _CXXFLAGS _CACHE_VAR _FLAG) CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}") if (${_CACHE_VAR}) - if(TARGET ${PROJECT}) - target_compile_options(${PROJECT} PRIVATE ${_FLAG}) - endif() - if(TARGET ${PROJECT}-cli) - target_compile_options(${PROJECT}-cli PRIVATE ${_FLAG}) - endif() + list(APPEND SUPPORTED_CHECK_CXX_COMPILER_FLAGS "${_FLAG}") else () message(STATUS "Unsupported CXXFLAG: ${_FLAG}") endif () endfunction () +# Add check flags to a compile TARGET +function (SET_CHECK_CXX_FLAGS _TARGET) + target_compile_options("${_TARGET}" PRIVATE "${SUPPORTED_CHECK_CXX_COMPILER_FLAGS}") +endfunction() + # Check if a flag exists and add it to the compiler and the linker options function (ADD_CHECK_CXX_FLAG _CXXFLAGS _CACHE_VAR _FLAG) CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}") @@ -248,6 +228,29 @@ else () endif () endif () +# Include sub-projects +include("${ROOT_DIR}/src/openrct2/CMakeLists.txt" NO_POLICY_SCOPE) +include("${ROOT_DIR}/src/openrct2-cli/CMakeLists.txt" NO_POLICY_SCOPE) +if(NOT DISABLE_GUI) + include("${ROOT_DIR}/src/openrct2-ui/CMakeLists.txt" NO_POLICY_SCOPE) +endif() + +# g2 +add_custom_command( + OUTPUT g2.dat + COMMAND ./openrct2-cli sprite build \"${CMAKE_BINARY_DIR}/g2.dat\" \"${ROOT_DIR}/resources/g2/sprites.json\" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} +) +add_custom_target(g2 DEPENDS ${PROJECT} g2.dat) + +# Include tests +if (WITH_TESTS) + enable_testing() + if (UNIX AND (NOT USE_MMAP)) + include("${ROOT_DIR}/test/testpaint/CMakeLists.txt" NO_POLICY_SCOPE) + endif () + include("${ROOT_DIR}/test/tests/CMakeLists.txt" NO_POLICY_SCOPE) +endif () # Install # Don't recurse, grab all *.txt and *.md files diff --git a/src/openrct2-ui/CMakeLists.txt b/src/openrct2-ui/CMakeLists.txt index 5f5e271a24..52bd7a62b9 100644 --- a/src/openrct2-ui/CMakeLists.txt +++ b/src/openrct2-ui/CMakeLists.txt @@ -49,6 +49,7 @@ endif () set (PROJECT openrct2) project(${PROJECT} CXX) add_executable(${PROJECT} ${OPENRCT2_UI_SOURCES} ${OPENRCT2_UI_MM_SOURCES}) +SET_CHECK_CXX_FLAGS(${PROJECT}) ipo_set_target_properties(${PROJECT}) target_link_libraries(${PROJECT} "libopenrct2" diff --git a/src/openrct2/CMakeLists.txt b/src/openrct2/CMakeLists.txt index 0471db4717..1bec314524 100644 --- a/src/openrct2/CMakeLists.txt +++ b/src/openrct2/CMakeLists.txt @@ -74,6 +74,7 @@ set(PROJECT libopenrct2) project(${PROJECT} CXX) add_library(${PROJECT} ${OPENRCT2_CORE_SOURCES} ${OPENRCT2_CORE_MM_SOURCES} ${RCT2_SECTIONS}) set_target_properties(${PROJECT} PROPERTIES PREFIX "") +SET_CHECK_CXX_FLAGS(${PROJECT}) if (benchmark_FOUND) message("Found Google benchmark, enabling support") diff --git a/test/testpaint/CMakeLists.txt b/test/testpaint/CMakeLists.txt index 83faee5fef..11c56ba2e1 100644 --- a/test/testpaint/CMakeLists.txt +++ b/test/testpaint/CMakeLists.txt @@ -83,6 +83,7 @@ file(GLOB_RECURSE ORCT2_TESTPAINT_SOURCES "${CMAKE_CURRENT_LIST_DIR}/*.c" set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/addresses.c PROPERTIES COMPILE_FLAGS -O0) add_executable(testpaint EXCLUDE_FROM_ALL ${ORCT2_RIDE_SOURCES} ${ORCT2_RIDE_DEP_SOURCES} ${ORCT2_TESTPAINT_SOURCES} ${RCT2_SECTIONS}) +SET_CHECK_CXX_FLAGS(testpaint) target_include_directories(testpaint PRIVATE "${ROOT_DIR}/src/") target_link_libraries(testpaint z) diff --git a/test/tests/CMakeLists.txt b/test/tests/CMakeLists.txt index 25d946547b..c8f2c923e0 100644 --- a/test/tests/CMakeLists.txt +++ b/test/tests/CMakeLists.txt @@ -137,6 +137,7 @@ set(INI_TEST_SOURCES "${ROOT_DIR}/src/openrct2/core/MemoryStream.cpp" ) add_executable(test_ini ${INI_TEST_SOURCES}) +SET_CHECK_CXX_FLAGS(test_ini) target_link_libraries(test_ini ${GTEST_LIBRARIES} test-common ${LDL} z) add_test(NAME ini COMMAND test_ini) @@ -145,12 +146,14 @@ set(STRING_TEST_SOURCES "${CMAKE_CURRENT_LIST_DIR}/StringTest.cpp" ) add_executable(test_string ${STRING_TEST_SOURCES}) +SET_CHECK_CXX_FLAGS(test_string) target_link_libraries(test_string ${GTEST_LIBRARIES} test-common ${LDL} z) add_test(NAME string COMMAND test_string) # Localisation test set(STRING_TEST_SOURCES "${CMAKE_CURRENT_LIST_DIR}/Localisation.cpp") add_executable(test_localisation ${STRING_TEST_SOURCES}) +SET_CHECK_CXX_FLAGS(test_localisation) target_link_libraries(test_localisation ${GTEST_LIBRARIES} test-common ${LDL} z) add_test(NAME localisation COMMAND test_localisation) @@ -158,6 +161,7 @@ if (NOT DISABLE_NETWORK) # Crypt tests add_executable(test_crypt "${CMAKE_CURRENT_LIST_DIR}/CryptTests.cpp" "${CMAKE_CURRENT_LIST_DIR}/TestData.cpp") + SET_CHECK_CXX_FLAGS(test_crypt) target_link_libraries(test_crypt ${GTEST_LIBRARIES} libopenrct2) add_test(NAME Crypt COMMAND test_crypt) endif () @@ -165,6 +169,7 @@ endif () # ImageImporter tests add_executable(test_imageimporter "${CMAKE_CURRENT_LIST_DIR}/ImageImporterTests.cpp" "${CMAKE_CURRENT_LIST_DIR}/TestData.cpp") +SET_CHECK_CXX_FLAGS(test_imageimporter) target_link_libraries(test_imageimporter ${GTEST_LIBRARIES} libopenrct2) add_test(NAME ImageImporter COMMAND test_imageimporter) @@ -172,6 +177,7 @@ add_test(NAME ImageImporter COMMAND test_imageimporter) set(RIDE_RATINGS_TEST_SOURCES "${CMAKE_CURRENT_LIST_DIR}/RideRatings.cpp" "${CMAKE_CURRENT_LIST_DIR}/TestData.cpp") add_executable(test_ride_ratings ${RIDE_RATINGS_TEST_SOURCES}) +SET_CHECK_CXX_FLAGS(test_ride_ratings) target_link_libraries(test_ride_ratings ${GTEST_LIBRARIES} libopenrct2 ${LDL} z) add_test(NAME ride_ratings COMMAND test_ride_ratings) @@ -179,6 +185,7 @@ add_test(NAME ride_ratings COMMAND test_ride_ratings) set(MULTILAUNCH_TEST_SOURCES "${CMAKE_CURRENT_LIST_DIR}/MultiLaunch.cpp" "${CMAKE_CURRENT_LIST_DIR}/TestData.cpp") add_executable(test_multilaunch ${MULTILAUNCH_TEST_SOURCES}) +SET_CHECK_CXX_FLAGS(test_multilaunch) target_link_libraries(test_multilaunch ${GTEST_LIBRARIES} libopenrct2 ${LDL} z) add_test(NAME multilaunch COMMAND test_multilaunch) @@ -186,6 +193,7 @@ add_test(NAME multilaunch COMMAND test_multilaunch) set(TILE_ELEMENT_TEST_SOURCES "${CMAKE_CURRENT_LIST_DIR}/TileElements.cpp" "${CMAKE_CURRENT_LIST_DIR}/TestData.cpp") add_executable(test_tile_elements ${TILE_ELEMENT_TEST_SOURCES}) +SET_CHECK_CXX_FLAGS(test_tile_elements) target_link_libraries(test_tile_elements ${GTEST_LIBRARIES} libopenrct2 ${LDL} z) add_test(NAME tile_elements COMMAND test_tile_elements) @@ -194,6 +202,7 @@ if (NOT DISABLE_NETWORK) set(REPLAY_TEST_SOURCES "${CMAKE_CURRENT_LIST_DIR}/ReplayTests.cpp" "${CMAKE_CURRENT_LIST_DIR}/TestData.cpp") add_executable(test_replays ${REPLAY_TEST_SOURCES}) + SET_CHECK_CXX_FLAGS(test_replays) target_link_libraries(test_replays ${GTEST_LIBRARIES} libopenrct2 ${LDL} z) add_test(NAME replay_tests COMMAND test_replays) endif () @@ -202,5 +211,6 @@ endif () set(PATHFINDING_TEST_SOURCES "${CMAKE_CURRENT_LIST_DIR}/Pathfinding.cpp" "${CMAKE_CURRENT_LIST_DIR}/TestData.cpp") add_executable(test_pathfinding ${PATHFINDING_TEST_SOURCES}) +SET_CHECK_CXX_FLAGS(test_pathfinding) target_link_libraries(test_pathfinding ${GTEST_LIBRARIES} libopenrct2 ${LDL} z) add_test(NAME pathfinding COMMAND test_pathfinding)