Skip to content

Commit 172ca25

Browse files
fix(cmake): handle no backend audio for cmake and add system libs (#37)
* fix(cmake): throw fatal error if no backend specified for openal * feat: allow options to enable usage of system libraries instead of vendor compiled ones * chore: simplify output dir setting of cmake * fix(cmake): improve system library detection for GLFW, Bullet, and OpenAL * fix(justfile): remove USE_SYSTEM_LIBS as default in run * fix(flake.nix): enable USE_SYSTEM_LIBS by default in devShells * fix(justfile): stop rebuilding buildfiles each run * fix(build): add pipewire-dev dependency and configure ccache for Linux * fix(build): fix pipewire package name
1 parent 5c693a1 commit 172ca25

4 files changed

Lines changed: 159 additions & 59 deletions

File tree

.github/workflows/build.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ jobs:
8686
cmake ninja-build \
8787
libgl1-mesa-dev \
8888
libx11-dev libxrandr-dev libxi-dev \
89-
libxinerama-dev libxcursor-dev
89+
libxinerama-dev libxcursor-dev \
90+
libpipewire-0.3-dev pkg-config
91+
92+
- name: ccache
93+
if: runner.os == 'Linux'
94+
uses: hendrikmuhs/ccache-action@v1.2
9095

9196
- name: Cache build directory
9297
uses: actions/cache@v5

CMakeLists.txt

Lines changed: 135 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,139 @@ if(UNIX AND NOT APPLE)
2626
find_package(OpenGL REQUIRED)
2727
endif()
2828

29+
option(USE_SYSTEM_LIBS "Use system-installed libraries instead of the bundled versions" $ENV{USE_SYSTEM_LIBS})
30+
31+
option(USE_SYSTEM_OPENAL "Use system-installed OpenAL-Soft instead of the bundled version" $ENV{USE_SYSTEM_OPENAL})
32+
option(USE_SYSTEM_BULLET "Use system-installed Bullet3 instead of the bundled version" $ENV{USE_SYSTEM_BULLET})
33+
option(USE_SYSTEM_GLFW "Use system-installed GLFW instead of the bundled version" $ENV{USE_SYSTEM_GLFW})
34+
35+
if(USE_SYSTEM_LIBS)
36+
set(USE_SYSTEM_OPENAL ON CACHE BOOL "" FORCE)
37+
set(USE_SYSTEM_BULLET ON CACHE BOOL "" FORCE)
38+
set(USE_SYSTEM_GLFW ON CACHE BOOL "" FORCE)
39+
endif()
40+
2941
# ---- Vendor Libraries ----
3042
# ---- GLFW ----
31-
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
32-
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
33-
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
34-
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
35-
set(GLFW_USE_HYBRID_HPG ON CACHE BOOL "" FORCE)
36-
37-
if(UNIX AND NOT APPLE AND NOT DEFINED GLFW_BUILD_WAYLAND)
38-
find_program(GLFW_WAYLAND_SCANNER_EXECUTABLE NAMES wayland-scanner)
39-
find_package(PkgConfig QUIET)
40-
41-
if(PkgConfig_FOUND)
42-
pkg_check_modules(GLFW_WAYLAND_DEPS QUIET
43-
wayland-client
44-
wayland-cursor
45-
wayland-egl
46-
xkbcommon
47-
)
48-
endif()
43+
set(USE_VENDOR_GLFW ON CACHE BOOL "" FORCE)
4944

50-
if(GLFW_WAYLAND_SCANNER_EXECUTABLE AND GLFW_WAYLAND_DEPS_FOUND)
51-
set(GLFW_BUILD_WAYLAND ON CACHE BOOL "Build support for Wayland")
45+
if (USE_SYSTEM_GLFW)
46+
find_package(glfw3 QUIET)
47+
if(glfw3_FOUND)
48+
message(STATUS "Using system-installed GLFW: ${glfw3_VERSION}")
49+
set(USE_VENDOR_GLFW OFF CACHE BOOL "" FORCE)
5250
else()
53-
set(GLFW_BUILD_WAYLAND OFF CACHE BOOL "Build support for Wayland")
51+
message(STATUS "Using bundled GLFW")
52+
endif()
53+
endif()
54+
55+
if(USE_VENDOR_GLFW)
56+
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
57+
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
58+
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
59+
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
60+
set(GLFW_USE_HYBRID_HPG ON CACHE BOOL "" FORCE)
61+
62+
if(UNIX AND NOT APPLE AND NOT DEFINED GLFW_BUILD_WAYLAND)
63+
find_program(GLFW_WAYLAND_SCANNER_EXECUTABLE NAMES wayland-scanner)
64+
find_package(PkgConfig QUIET)
65+
66+
if(PkgConfig_FOUND)
67+
pkg_check_modules(GLFW_WAYLAND_DEPS QUIET
68+
wayland-client
69+
wayland-cursor
70+
wayland-egl
71+
xkbcommon
72+
)
73+
endif()
5474

55-
if(NOT GLFW_WAYLAND_SCANNER_EXECUTABLE)
56-
set(_glfw_wayland_reason "missing wayland-scanner")
57-
elseif(NOT PkgConfig_FOUND)
58-
set(_glfw_wayland_reason "pkg-config is unavailable")
75+
if(GLFW_WAYLAND_SCANNER_EXECUTABLE AND GLFW_WAYLAND_DEPS_FOUND)
76+
set(GLFW_BUILD_WAYLAND ON CACHE BOOL "Build support for Wayland")
5977
else()
60-
set(_glfw_wayland_reason "missing Wayland development packages")
78+
set(GLFW_BUILD_WAYLAND OFF CACHE BOOL "Build support for Wayland")
79+
80+
if(NOT GLFW_WAYLAND_SCANNER_EXECUTABLE)
81+
set(_glfw_wayland_reason "missing wayland-scanner")
82+
elseif(NOT PkgConfig_FOUND)
83+
set(_glfw_wayland_reason "pkg-config is unavailable")
84+
else()
85+
set(_glfw_wayland_reason "missing Wayland development packages")
86+
endif()
87+
88+
message(STATUS "Disabling GLFW Wayland (${_glfw_wayland_reason})")
6189
endif()
90+
endif()
91+
add_subdirectory(vendor/glfw)
92+
endif()
93+
6294

63-
message(STATUS "Disabling GLFW Wayland (${_glfw_wayland_reason})")
95+
# ---- BULLET3 ----
96+
set(USE_VENDOR_BULLET ON CACHE BOOL "" FORCE)
97+
if (USE_SYSTEM_BULLET)
98+
find_package(Bullet QUIET)
99+
if(Bullet_FOUND)
100+
message(STATUS "Using system-installed Bullet: ${Bullet_VERSION}")
101+
set(USE_BUILD_BULLET OFF CACHE BOOL "" FORCE)
102+
else()
103+
message(STATUS "Using bundled Bullet3")
64104
endif()
65105
endif()
66106

67-
add_subdirectory(vendor/glfw)
107+
if(USE_VENDOR_BULLET)
108+
set(BUILD_CPU_DEMOS OFF CACHE BOOL "" FORCE)
109+
set(BUILD_OPENGL3_DEMOS OFF CACHE BOOL "" FORCE)
110+
set(BUILD_BULLET2_DEMOS OFF CACHE BOOL "" FORCE)
111+
set(BUILD_EXTRAS OFF CACHE BOOL "" FORCE)
112+
set(BUILD_UNIT_TESTS OFF CACHE BOOL "" FORCE)
113+
set(BUILD_BULLET3 ON CACHE BOOL "" FORCE)
114+
add_subdirectory(vendor/bullet3)
115+
endif()
116+
117+
# --- OpenAL-Soft ----
118+
set(USE_VENDOR_OPENAL ON CACHE BOOL "" FORCE)
119+
120+
if(USE_SYSTEM_OPENAL)
121+
find_package(OpenAL QUIET)
122+
if (OpenAL_FOUND)
123+
message(STATUS "Using system-installed OpenAL: ${OpenAL_VERSION}")
124+
set(USE_VENDOR_OPENAL OFF CACHE BOOL "" FORCE)
125+
else()
126+
message(STATUS "Using bundled OpenAL-Soft")
127+
endif()
128+
endif()
129+
130+
if(USE_VENDOR_OPENAL)
131+
set(ALSOFT_ENABLE_MODULES OFF CACHE BOOL "" FORCE)
132+
set(LIBTYPE STATIC CACHE STRING "" FORCE)
133+
set(ALSOFT_EMBED_HRTF_DATA ON CACHE BOOL "" FORCE)
134+
set(ALSOFT_EAX ON CACHE BOOL "" FORCE)
135+
set(ALSOFT_EXAMPLES OFF CACHE BOOL "" FORCE)
136+
set(ALSOFT_TESTS OFF CACHE BOOL "" FORCE)
137+
set(ALSOFT_UTILS OFF CACHE BOOL "" FORCE)
138+
set(ALSOFT_INSTALL_CONFIG OFF CACHE BOOL "" FORCE)
139+
set(ALSOFT_INSTALL OFF CACHE BOOL "" FORCE)
140+
if(WIN32)
141+
set(ALSOFT_BACKEND_WASAPI ON CACHE BOOL "" FORCE)
142+
set(ALSOFT_BACKEND_DSOUND OFF CACHE BOOL "" FORCE)
143+
endif()
144+
if (UNIX AND NOT APPLE)
145+
find_package(PkgConfig REQUIRED)
146+
pkg_check_modules(PIPEWIRE libpipewire-0.3>=0.3.23 QUIET)
147+
pkg_check_modules(PULSEAUDIO libpulse QUIET)
148+
pkg_check_modules(ALSA alsa QUIET)
149+
if(PIPEWIRE_FOUND)
150+
set(ALSOFT_BACKEND_PIPEWIRE ON CACHE BOOL "" FORCE)
151+
elseif(PULSEAUDIO_FOUND)
152+
set(ALSOFT_BACKEND_PULSEAUDIO ON CACHE BOOL "" FORCE)
153+
elseif(ALSA_FOUND)
154+
set(ALSOFT_BACKEND_ALSA ON CACHE BOOL "" FORCE)
155+
else()
156+
message(FATAL_ERROR "No suitable audio backend found for OpenAL-Soft. Please install PipeWire, PulseAudio, or ALSA development packages.")
157+
endif()
158+
endif()
159+
160+
add_subdirectory(vendor/openal-soft)
161+
endif()
68162

69163
# ---- GLAD ----
70164
add_library(glad STATIC
@@ -74,30 +168,6 @@ target_include_directories(glad
74168
PUBLIC vendor/glad/include
75169
)
76170

77-
# ---- BULLET3 ----
78-
set(BUILD_CPU_DEMOS OFF CACHE BOOL "" FORCE)
79-
set(BUILD_OPENGL3_DEMOS OFF CACHE BOOL "" FORCE)
80-
set(BUILD_BULLET2_DEMOS OFF CACHE BOOL "" FORCE)
81-
set(BUILD_EXTRAS OFF CACHE BOOL "" FORCE)
82-
set(BUILD_UNIT_TESTS OFF CACHE BOOL "" FORCE)
83-
set(BUILD_BULLET3 ON CACHE BOOL "" FORCE)
84-
add_subdirectory(vendor/bullet3)
85-
86-
# --- OpenAL-Soft ----
87-
set(ALSOFT_ENABLE_MODULES OFF CACHE BOOL "" FORCE)
88-
set(LIBTYPE STATIC CACHE STRING "" FORCE)
89-
set(ALSOFT_EMBED_HRTF_DATA ON CACHE BOOL "" FORCE)
90-
set(ALSOFT_EAX ON CACHE BOOL "" FORCE)
91-
set(ALSOFT_EXAMPLES OFF CACHE BOOL "" FORCE)
92-
set(ALSOFT_TESTS OFF CACHE BOOL "" FORCE)
93-
set(ALSOFT_UTILS OFF CACHE BOOL "" FORCE)
94-
set(ALSOFT_INSTALL_CONFIG OFF CACHE BOOL "" FORCE)
95-
set(ALSOFT_INSTALL OFF CACHE BOOL "" FORCE)
96-
set(ALSOFT_BACKEND_WASAPI ON CACHE BOOL "" FORCE)
97-
set(ALSOFT_BACKEND_DSOUND OFF CACHE BOOL "" FORCE)
98-
99-
add_subdirectory(vendor/openal-soft)
100-
101171
# ---- GLM (Header-only) ----
102172
add_library(glm INTERFACE)
103173
target_include_directories(glm
@@ -194,17 +264,27 @@ target_link_libraries(${PROJECT_NAME}
194264
BulletCollision
195265
BulletSoftBody
196266
LinearMath
197-
OpenAL
198267
)
199268

269+
if(USE_SYSTEM_OPENAL AND OpenAL_FOUND)
270+
target_link_libraries(${PROJECT_NAME}
271+
PRIVATE
272+
OpenAL::OpenAL
273+
)
274+
else()
275+
target_link_libraries(${PROJECT_NAME}
276+
PRIVATE
277+
OpenAL
278+
)
279+
endif()
280+
200281
target_compile_definitions(${PROJECT_NAME}
201282
PRIVATE
202283
$<$<CONFIG:Debug>:COLLISION_DEBUG_DRAW>
203284
)
204285

205286
set_target_properties(${PROJECT_NAME} PROPERTIES
206-
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/bin
207-
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/bin
287+
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin
208288
)
209289

210290
install(TARGETS ${PROJECT_NAME}

flake.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
# Audio
2727
pipewire
2828

29+
# extra precompiled libs
30+
openal-soft
31+
bullet
32+
glfw
33+
2934
# Wayland
3035
wayland
3136
wayland-protocols
@@ -56,6 +61,7 @@
5661
devShells.default = pkgs.mkShell {
5762

5863
inherit buildInputs nativeBuildInputs LD_LIBRARY_PATH;
64+
USE_SYSTEM_LIBS = "ON";
5965

6066
packages = with pkgs; [
6167
# helper
@@ -84,6 +90,7 @@
8490
"-DGLFW_BUILD_WAYLAND=1"
8591
"-DGLFW_BUILD_X11=1"
8692
"-DCMAKE_POLICY_VERSION_MINIMUM=3.5"
93+
"-DUSE_SYSTEM_LIBS=ON"
8794
];
8895

8996
installPhase = ''

justfile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ dev:
44

55
# Configure the project with CMake
66
configure:
7-
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_COLOR_DIAGNOSTICS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5
7+
cmake -S . -B build -G Ninja \
8+
-DCMAKE_BUILD_TYPE=Debug \
9+
-DCMAKE_COLOR_DIAGNOSTICS=ON \
10+
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
811

912
# Build the project using Ninja
10-
build: configure
13+
build:
14+
#!/usr/bin/env bash
15+
if [[ ! -f build/build.ninja || CMakeLists.txt -nt build/build.ninja ]]; then
16+
echo "Configuring..."
17+
just configure
18+
fi
1119
cmake --build build -- -j$(nproc)
1220
1321
# Runs the game using the dedicated Nvidia/AMD GPU with optional config
1422
[default]
15-
run config="": build
23+
run config="":
1624
#!/usr/bin/env bash
1725
if command -v nvidia-smi > /dev/null 2>&1; then
1826
export DRI_PRIME=1

0 commit comments

Comments
 (0)