From f1740a2c5e0d2fce39ae009a5c1324dd04989a92 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 30 Jan 2020 01:19:45 +0000 Subject: [PATCH] Add native-optimisations and LTO to DoConfig and bin2h Forgot bin2h even had a CMake file --- DoConfig/CMakeLists.txt | 25 +++++++++++++++++++++++-- bin2h/CMakeLists.txt | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/DoConfig/CMakeLists.txt b/DoConfig/CMakeLists.txt index f0ff9253..f395045d 100644 --- a/DoConfig/CMakeLists.txt +++ b/DoConfig/CMakeLists.txt @@ -9,6 +9,8 @@ if((${CMAKE_VERSION} VERSION_EQUAL 3.11) OR (${CMAKE_VERSION} VERSION_GREATER 3. endif() option(LTO "Enable link-time optimisation" OFF) +option(NATIVE_OPTIMIZATIONS "Enable processor-specific optimisations (executable might not work on other architectures) (GCC-compatible compilers only)" OFF) + option(WARNINGS "Enable common compiler warnings (for GCC-compatible compilers and MSVC only)" OFF) option(WARNINGS_ALL "Enable ALL compiler warnings (for Clang and MSVC only)" OFF) option(WARNINGS_FATAL "Stop compilation on any compiler warning (for GCC-compatible compilers and MSVC only)" OFF) @@ -54,7 +56,7 @@ if(WARNINGS) # if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") # string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # else() - # target_compile_options(CSE2 PRIVATE /W4) + # target_compile_options(DoConfig PRIVATE /W4) # endif() target_compile_options(DoConfig PRIVATE /W4) @@ -75,7 +77,7 @@ if(WARNINGS_ALL) # if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") # string(REGEX REPLACE "/W[0-4]" "/Wall" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # else() - # target_compile_options(CSE2 PRIVATE /Wall) + # target_compile_options(DoConfig PRIVATE /Wall) # endif() target_compile_options(DoConfig PRIVATE /Wall) @@ -150,3 +152,22 @@ if(LTO) endif() endif() endif() + +# Enable -march=native if available +if(NATIVE_OPTIMIZATIONS) + include(CheckCXXCompilerFlag) + CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) # GCC flag + if(COMPILER_SUPPORTS_MARCH_NATIVE) + target_compile_options(DoConfig PRIVATE -march=native) + else() + CHECK_CXX_COMPILER_FLAG("-xHost" COMPILER_SUPPORTS_XHOST) # ICC (Linux) flag + CHECK_CXX_COMPILER_FLAG("/QxHost" COMPILER_SUPPORTS_QXHOST) # ICC (Windows) flag + if(COMPILER_SUPPORTS_XHOST) + target_compile_options(DoConfig PRIVATE -xHost) + elseif(COMPILER_SUPPORTS_QXHOST) + target_compile_options(DoConfig PRIVATE /QxHost) + else() + message(WARNING "Couldn't activate native optimizations ! (Unsupported compiler)") + endif() + endif() +endif() diff --git a/bin2h/CMakeLists.txt b/bin2h/CMakeLists.txt index a6ab99b1..aedb376e 100644 --- a/bin2h/CMakeLists.txt +++ b/bin2h/CMakeLists.txt @@ -4,6 +4,9 @@ if((${CMAKE_VERSION} VERSION_EQUAL 3.9) OR (${CMAKE_VERSION} VERSION_GREATER 3.9 cmake_policy(SET CMP0069 NEW) endif() +option(LTO "Enable link-time optimisation" OFF) +option(NATIVE_OPTIMIZATIONS "Enable processor-specific optimisations (executable might not work on other architectures) (GCC-compatible compilers only)" OFF) + option(WARNINGS "Enable common compiler warnings (for gcc-compatible compilers and MSVC only)" OFF) option(WARNINGS_ALL "Enable ALL compiler warnings (for clang and MSVC only)" OFF) option(WARNINGS_FATAL "Stop compilation on any compiler warning (for gcc-compatible compilers and MSVC only)" OFF) @@ -54,7 +57,7 @@ if (WARNINGS) # if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") # string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # else() - # target_compile_options(CSE2 PRIVATE /W4) + # target_compile_options(bin2h PRIVATE /W4) # endif() target_compile_options(bin2h PRIVATE /W4) @@ -75,7 +78,7 @@ if (WARNINGS_ALL) # if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") # string(REGEX REPLACE "/W[0-4]" "/Wall" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # else() - # target_compile_options(CSE2 PRIVATE /Wall) + # target_compile_options(bin2h PRIVATE /Wall) # endif() target_compile_options(bin2h PRIVATE /Wall) @@ -90,10 +93,8 @@ if (WARNINGS_FATAL) # HACK : Replace this with CMake provided stuff when possible (when CMake makes avoiding this possible (it isn't currently)) if (MSVC) - target_compile_options(CSE2 PRIVATE /WX) target_compile_options(bin2h PRIVATE /WX) elseif(COMPILER_IS_GCC_COMPATIBLE) - target_compile_options(CSE2 PRIVATE -Werror) target_compile_options(bin2h PRIVATE -Werror) else() message(WARNING "Could not activate fatal warnings ! (Unsupported compiler)") @@ -119,4 +120,34 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") endif() endif() +# Enable link-time optimisation if available +if(LTO) + if((${CMAKE_VERSION} VERSION_EQUAL 3.9) OR (${CMAKE_VERSION} VERSION_GREATER 3.9)) + include(CheckIPOSupported) + check_ipo_supported(RESULT result) + if(result) + set_target_properties(bin2h PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() + endif() +endif() + +# Enable -march=native if available +if(NATIVE_OPTIMIZATIONS) + include(CheckCXXCompilerFlag) + CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) # GCC flag + if(COMPILER_SUPPORTS_MARCH_NATIVE) + target_compile_options(bin2h PRIVATE -march=native) + else() + CHECK_CXX_COMPILER_FLAG("-xHost" COMPILER_SUPPORTS_XHOST) # ICC (Linux) flag + CHECK_CXX_COMPILER_FLAG("/QxHost" COMPILER_SUPPORTS_QXHOST) # ICC (Windows) flag + if(COMPILER_SUPPORTS_XHOST) + target_compile_options(bin2h PRIVATE -xHost) + elseif(COMPILER_SUPPORTS_QXHOST) + target_compile_options(bin2h PRIVATE /QxHost) + else() + message(WARNING "Couldn't activate native optimizations ! (Unsupported compiler)") + endif() + endif() +endif() + install(TARGETS bin2h RUNTIME DESTINATION bin)