Add native-optimisations and LTO to DoConfig and bin2h

Forgot bin2h even had a CMake file
This commit is contained in:
Clownacy 2020-01-30 01:19:45 +00:00
parent ed1ff2f275
commit f1740a2c5e
2 changed files with 58 additions and 6 deletions

View file

@ -9,6 +9,8 @@ if((${CMAKE_VERSION} VERSION_EQUAL 3.11) OR (${CMAKE_VERSION} VERSION_GREATER 3.
endif() endif()
option(LTO "Enable link-time optimisation" OFF) 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 "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_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) 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]") # if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
# string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# else() # else()
# target_compile_options(CSE2 PRIVATE /W4) # target_compile_options(DoConfig PRIVATE /W4)
# endif() # endif()
target_compile_options(DoConfig PRIVATE /W4) target_compile_options(DoConfig PRIVATE /W4)
@ -75,7 +77,7 @@ if(WARNINGS_ALL)
# if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") # if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
# string(REGEX REPLACE "/W[0-4]" "/Wall" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # string(REGEX REPLACE "/W[0-4]" "/Wall" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# else() # else()
# target_compile_options(CSE2 PRIVATE /Wall) # target_compile_options(DoConfig PRIVATE /Wall)
# endif() # endif()
target_compile_options(DoConfig PRIVATE /Wall) target_compile_options(DoConfig PRIVATE /Wall)
@ -150,3 +152,22 @@ if(LTO)
endif() endif()
endif() 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()

View file

@ -4,6 +4,9 @@ if((${CMAKE_VERSION} VERSION_EQUAL 3.9) OR (${CMAKE_VERSION} VERSION_GREATER 3.9
cmake_policy(SET CMP0069 NEW) cmake_policy(SET CMP0069 NEW)
endif() 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 "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_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) 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]") # if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
# string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# else() # else()
# target_compile_options(CSE2 PRIVATE /W4) # target_compile_options(bin2h PRIVATE /W4)
# endif() # endif()
target_compile_options(bin2h PRIVATE /W4) target_compile_options(bin2h PRIVATE /W4)
@ -75,7 +78,7 @@ if (WARNINGS_ALL)
# if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") # if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
# string(REGEX REPLACE "/W[0-4]" "/Wall" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # string(REGEX REPLACE "/W[0-4]" "/Wall" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# else() # else()
# target_compile_options(CSE2 PRIVATE /Wall) # target_compile_options(bin2h PRIVATE /Wall)
# endif() # endif()
target_compile_options(bin2h PRIVATE /Wall) 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)) # HACK : Replace this with CMake provided stuff when possible (when CMake makes avoiding this possible (it isn't currently))
if (MSVC) if (MSVC)
target_compile_options(CSE2 PRIVATE /WX)
target_compile_options(bin2h PRIVATE /WX) target_compile_options(bin2h PRIVATE /WX)
elseif(COMPILER_IS_GCC_COMPATIBLE) elseif(COMPILER_IS_GCC_COMPATIBLE)
target_compile_options(CSE2 PRIVATE -Werror)
target_compile_options(bin2h PRIVATE -Werror) target_compile_options(bin2h PRIVATE -Werror)
else() else()
message(WARNING "Could not activate fatal warnings ! (Unsupported compiler)") message(WARNING "Could not activate fatal warnings ! (Unsupported compiler)")
@ -119,4 +120,34 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
endif() endif()
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) install(TARGETS bin2h RUNTIME DESTINATION bin)