Skip to content

Commit 46e2037

Browse files
Enable IPO and runtime optimization flags
Introduce LUNAR_ENABLE_IPO and LUNAR_ENABLE_FAST_MATH CMake options and detect/enable IPO (LTO) when supported. Add lunar_enable_runtime_optimizations() helper to apply platform- and config-specific compile flags (e.g. /Gw, /Ob2, -O3, -ffast-math or /fp:fast) and apply it to core, executable, dll and test targets. Update CI and Release workflows to use a configurable matrix build_type (Release/RelWithDebInfo), pass matrix build type into cmake/ctest, enable IPO in native Release builds and add a RelWithDebInfo job, and explicitly control fast-math flags. Add a CMakeSettings.json file with MSVC Ninja configurations for local developer use.
1 parent 2b7f2b9 commit 46e2037

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

.github/workflows/CI.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,37 @@ jobs:
1313
include:
1414
- os: ubuntu-latest
1515
name: linux-x64
16+
build_type: Release
1617
cmake_args: ""
1718

1819
- os: ubuntu-24.04-arm
1920
name: linux-arm64
21+
build_type: Release
2022
cmake_args: ""
2123

2224
- os: windows-latest
2325
name: windows-x64
26+
build_type: Release
2427
cmake_args: "-A x64"
2528

2629
- os: windows-11-arm
2730
name: windows-arm64
31+
build_type: Release
2832
cmake_args: "-A ARM64"
2933

3034
- os: macos-15-intel
3135
name: macos-x64
36+
build_type: Release
3237
cmake_args: ""
3338

3439
- os: macos-latest
3540
name: macos-arm64
41+
build_type: Release
42+
cmake_args: ""
43+
44+
- os: ubuntu-latest
45+
name: linux-x64-relwithdebinfo
46+
build_type: RelWithDebInfo
3647
cmake_args: ""
3748

3849
runs-on: ${{ matrix.os }}
@@ -43,13 +54,15 @@ jobs:
4354
- name: Configure
4455
run: >
4556
cmake -S . -B build
46-
-DCMAKE_BUILD_TYPE=Release
57+
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
4758
-DLUNAR_ENABLE_SERIES_FALLBACK=ON
4859
-DLUNAR_BUILD_TESTS=ON
60+
-DLUNAR_ENABLE_IPO=ON
61+
-DLUNAR_ENABLE_FAST_MATH=OFF
4962
${{ matrix.cmake_args }}
5063
5164
- name: Build
52-
run: cmake --build build --config Release --parallel
65+
run: cmake --build build --config ${{ matrix.build_type }} --parallel
5366

5467
- name: Test
55-
run: ctest --test-dir build -C Release --output-on-failure
68+
run: ctest --test-dir build -C ${{ matrix.build_type }} --output-on-failure

.github/workflows/Release.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ jobs:
5959

6060
- name: Configure (native)
6161
if: matrix.kind == 'native'
62-
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DLUNAR_ENABLE_SERIES_FALLBACK=ON ${{ matrix.cmake_args }}
62+
run: >
63+
cmake -S . -B build
64+
-DCMAKE_BUILD_TYPE=Release
65+
-DLUNAR_ENABLE_SERIES_FALLBACK=ON
66+
-DLUNAR_ENABLE_IPO=ON
67+
-DLUNAR_ENABLE_FAST_MATH=OFF
68+
${{ matrix.cmake_args }}
6369
6470
- name: Build (native)
6571
if: matrix.kind == 'native'
@@ -83,6 +89,8 @@ jobs:
8389
emcmake cmake -S . -B build \
8490
-DCMAKE_BUILD_TYPE=Release \
8591
-DLUNAR_ENABLE_SERIES_FALLBACK=ON \
92+
-DLUNAR_ENABLE_IPO=OFF \
93+
-DLUNAR_ENABLE_FAST_MATH=OFF \
8694
-DLUNAR_BUILD_TESTS=OFF
8795
8896
- name: Build (emscripten)
@@ -119,6 +127,8 @@ jobs:
119127
cmake -S . -B build \
120128
-DCMAKE_BUILD_TYPE=Release \
121129
-DLUNAR_ENABLE_SERIES_FALLBACK=ON \
130+
-DLUNAR_ENABLE_IPO=ON \
131+
-DLUNAR_ENABLE_FAST_MATH=OFF \
122132
-DCMAKE_C_COMPILER="$PWD/zigcc" \
123133
-DCMAKE_CXX_COMPILER="$PWD/zigcxx" \
124134
-DCMAKE_C_FLAGS="-Wno-error=date-time -Wno-date-time" \

CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.20)
33
project(lunar LANGUAGES C CXX)
44

55
include(CTest)
6+
include(CheckIPOSupported)
67

78
set(CMAKE_CXX_STANDARD 20)
89
set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -11,6 +12,44 @@ set(CMAKE_CXX_EXTENSIONS OFF)
1112
option(LUNAR_ENABLE_DIMENSION_TYPES "Enable compile-time tagged physical vectors" ON)
1213
option(LUNAR_ENABLE_SERIES_FALLBACK "Enable VSOP87A/ELPMPP02 fallback when BSP is unavailable" ON)
1314
option(LUNAR_BUILD_TESTS "Build GoogleTest-based test targets" ${BUILD_TESTING})
15+
option(LUNAR_ENABLE_IPO "Enable link-time/interprocedural optimization for release-like builds" ON)
16+
option(LUNAR_ENABLE_FAST_MATH "Enable faster but less strict floating-point optimizations" OFF)
17+
18+
if(LUNAR_ENABLE_IPO AND NOT EMSCRIPTEN)
19+
check_ipo_supported(RESULT LUNAR_IPO_SUPPORTED OUTPUT LUNAR_IPO_OUTPUT LANGUAGES C CXX)
20+
if(LUNAR_IPO_SUPPORTED)
21+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
22+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON)
23+
else()
24+
message(STATUS "IPO/LTO disabled: ${LUNAR_IPO_OUTPUT}")
25+
endif()
26+
endif()
27+
28+
function(lunar_enable_runtime_optimizations target_name)
29+
if(MSVC)
30+
target_compile_options(${target_name} PRIVATE
31+
$<$<CONFIG:Release>:/Gw>
32+
$<$<CONFIG:RelWithDebInfo>:/Gw>
33+
$<$<CONFIG:RelWithDebInfo>:/Ob2>
34+
)
35+
elseif(NOT EMSCRIPTEN)
36+
target_compile_options(${target_name} PRIVATE
37+
$<$<CONFIG:RelWithDebInfo>:-O3>
38+
)
39+
endif()
40+
41+
if(LUNAR_ENABLE_FAST_MATH AND NOT EMSCRIPTEN)
42+
if(MSVC)
43+
target_compile_options(${target_name} PRIVATE
44+
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:/fp:fast>
45+
)
46+
else()
47+
target_compile_options(${target_name} PRIVATE
48+
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:-ffast-math>
49+
)
50+
endif()
51+
endif()
52+
endfunction()
1453

1554
if(EMSCRIPTEN)
1655
set(LUNAR_EMSCRIPTEN_COMPILE_OPTIONS
@@ -109,6 +148,7 @@ endif()
109148
add_library(lunar_core STATIC
110149
${LUNAR_CORE_SOURCES}
111150
)
151+
lunar_enable_runtime_optimizations(lunar_core)
112152

113153
set_target_properties(lunar_core PROPERTIES
114154
POSITION_INDEPENDENT_CODE ON
@@ -134,6 +174,7 @@ endif()
134174
add_executable(lunar
135175
src/main.cpp
136176
)
177+
lunar_enable_runtime_optimizations(lunar)
137178
target_link_libraries(lunar PRIVATE lunar_core)
138179
target_include_directories(lunar PRIVATE
139180
${CMAKE_CURRENT_SOURCE_DIR}/include
@@ -157,6 +198,7 @@ endif()
157198
add_library(lunar_dll ${LUNAR_DLL_LIBRARY_TYPE}
158199
src/c_api.cpp
159200
)
201+
lunar_enable_runtime_optimizations(lunar_dll)
160202
target_link_libraries(lunar_dll PRIVATE lunar_core)
161203
target_include_directories(lunar_dll PRIVATE
162204
${CMAKE_CURRENT_SOURCE_DIR}/include

CMakeSettings.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "x64-Debug",
5+
"generator": "Ninja",
6+
"configurationType": "Debug",
7+
"inheritEnvironments": [ "msvc_x64_x64" ],
8+
"buildRoot": "${projectDir}\\out\\build\\${name}",
9+
"installRoot": "${projectDir}\\out\\install\\${name}",
10+
"cmakeCommandArgs": "",
11+
"buildCommandArgs": "",
12+
"ctestCommandArgs": ""
13+
},
14+
{
15+
"name": "x64-Release",
16+
"generator": "Ninja",
17+
"configurationType": "Release",
18+
"inheritEnvironments": [ "msvc_x64_x64" ],
19+
"buildRoot": "${projectDir}\\out\\build\\${name}",
20+
"installRoot": "${projectDir}\\out\\install\\${name}",
21+
"cmakeCommandArgs": "",
22+
"buildCommandArgs": "",
23+
"ctestCommandArgs": ""
24+
}
25+
]
26+
}

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_executable(lunar_tests
2222
test_eclipse.cpp
2323
test_almanac_i18n.cpp
2424
)
25+
lunar_enable_runtime_optimizations(lunar_tests)
2526

2627
target_link_libraries(lunar_tests PRIVATE
2728
lunar_core

0 commit comments

Comments
 (0)