From b598911e96e0ec77c7e5f92303efa1df985bfe9f Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sat, 9 Jan 2021 03:57:48 +0100 Subject: [PATCH] Enable LTO in release builds Fixes #693 --- .github/actions/install_deps.sh | 2 ++ .github/workflows/testing.yml | 2 +- CMakeLists.txt | 26 ++++++++++++++++++++------ Makefile | 2 +- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/.github/actions/install_deps.sh b/.github/actions/install_deps.sh index 80b2bc17..1066d6af 100755 --- a/.github/actions/install_deps.sh +++ b/.github/actions/install_deps.sh @@ -12,3 +12,5 @@ case `echo $1 | cut -d '-' -f 1` in esac bison --version +make --version +cmake --version diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 24449d9d..2b034feb 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -34,7 +34,7 @@ jobs: if: matrix.buildsys == 'make' - name: Build & install using CMake run: | - cmake -S . -B build -DCMAKE_VERBOSE_MAKEFILE=ON ${{ matrix.cmakevars }} + cmake -S . -B build -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Release ${{ matrix.cmakevars }} cmake --build build cp build/src/rgb{asm,link,fix,gfx} . sudo cmake --install build diff --git a/CMakeLists.txt b/CMakeLists.txt index 4842361a..29cfd82e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,8 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.0) -cmake_policy(VERSION 3.0) +# 3.9 required for LTO checks +cmake_minimum_required(VERSION 3.9 FATAL_ERROR) project(rgbds LANGUAGES C) @@ -23,10 +23,8 @@ if(srcdir STREQUAL bindir) message(FATAL_ERROR "Terminating configuration") endif() -include_directories("${PROJECT_SOURCE_DIR}/include") - -option(SANITIZERS "Build with sanitizers enabled" OFF) -option(MORE_WARNINGS "Turn on more warnings" OFF) +option(SANITIZERS "Build with sanitizers enabled" OFF) # Ignored on MSVC +option(MORE_WARNINGS "Turn on more warnings" OFF) # Ignored on MSVC option(TRACE_PARSER "Trace parser execution" OFF) option(TRACE_LEXER "Trace lexer execution" OFF) @@ -67,6 +65,8 @@ execute_process(COMMAND git describe --tags --dirty --always ERROR_QUIET) string(STRIP "${GIT_REV}" GIT_REV) +include_directories("${PROJECT_SOURCE_DIR}/include") + add_definitions(-DBUILD_VERSION_STRING="${GIT_REV}") set(CMAKE_C_STANDARD 11) @@ -74,6 +74,20 @@ set(CMAKE_C_STANDARD_REQUIRED True) add_subdirectory(src) +# By default, build in Release mode; Debug mode must be explicitly requested +# (You may want to augment it with the options above) +if(CMAKE_BUILD_TYPE STREQUAL "Release") + message(CHECK_START "Checking if LTO is supported") + include(CheckIPOSupported) + check_ipo_supported(RESULT enable_lto) + if(enable_lto) + message(CHECK_PASS "yes") + set_property(TARGET rgbasm rgblink rgbfix rgbgfx PROPERTY INTERPROCEDURAL_OPTIMIZATION ON) + else() + message(CHECK_FAIL "no") + endif() +endif() + if(TRACE_PARSER) target_compile_definitions(rgbasm PRIVATE -DYYDEBUG) endif() diff --git a/Makefile b/Makefile index 283118d3..45497b93 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ VERSION_STRING := `git describe --tags --dirty --always 2>/dev/null` WARNFLAGS := -Wall # Overridable CFLAGS -CFLAGS ?= -O3 -DNDEBUG +CFLAGS ?= -O3 -flto -DNDEBUG # Non-overridable CFLAGS REALCFLAGS := ${CFLAGS} ${WARNFLAGS} -std=gnu11 -D_POSIX_C_SOURCE=200809L \ -Iinclude