shithub: rgbds

Download patch

ref: b598911e96e0ec77c7e5f92303efa1df985bfe9f
parent: cab9cb06a3816964cf72da4017baa304729ab82d
author: ISSOtm <eldredhabert0@gmail.com>
date: Fri Jan 8 22:57:48 EST 2021

Enable LTO in release builds

Fixes #693

--- a/.github/actions/install_deps.sh
+++ b/.github/actions/install_deps.sh
@@ -12,3 +12,5 @@
 esac
 
 bison --version
+make --version
+cmake --version
--- a/.github/workflows/testing.yml
+++ b/.github/workflows/testing.yml
@@ -34,7 +34,7 @@
         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
--- 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 @@
   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 @@
                 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)
@@ -73,6 +73,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)
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@
 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