Skip to content

Commit 65ec1af

Browse files
authored
Switch macOS static build to CMake (#1938)
This moves the tortuous flags out of our CI scripts, and allows us to benefit from the libpng download caching.
1 parent bc3572e commit 65ec1af

5 files changed

Lines changed: 47 additions & 33 deletions

File tree

.github/scripts/build_libpng.sh

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/workflows/create-release-artifacts.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ jobs:
5454
- name: Install deps
5555
run: |
5656
./.github/scripts/install_deps.sh macos
57-
- name: Build libpng
58-
run: |
59-
./.github/scripts/build_libpng.sh
60-
# We force linking libpng statically; the other libs are provided by macOS itself
6157
- name: Build binaries
6258
run: |
63-
make -kj CXXFLAGS="-O3 -flto -DNDEBUG -mmacosx-version-min=10.9 -arch x86_64 -arch arm64" PNGCFLAGS="-I libpng-staging/include" PNGLDLIBS="libpng-staging/lib/libpng.a -lz" Q=
59+
cmake -S . -B build --preset macos-static -DFETCHCONTENT_BASE_DIR="${{ env.DEPS_ROOT_DIR }}"
60+
cmake --build build
6461
strip rgb{asm,link,fix,gfx}
6562
- name: Package binaries
6663
run: |

.github/workflows/testing.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,16 @@ jobs:
109109
- name: Install deps
110110
run: |
111111
./.github/scripts/install_deps.sh macos
112-
- name: Build libpng
113-
run: |
114-
./.github/scripts/build_libpng.sh
112+
- name: Cache library deps
113+
uses: actions/cache@v5
114+
with:
115+
path: ${{ env.DEPS_ROOT_DIR }}/*-tmp/
116+
key: dep-srcs-${{ hashFiles('cmake/deps.cmake') }}
117+
enableCrossOsArchive: true
115118
- name: Build & install
116119
run: |
117-
make -kj CXXFLAGS="-O3 -flto -DNDEBUG -mmacosx-version-min=10.9 -arch x86_64 -arch arm64" PNGCFLAGS="-I libpng-staging/include" PNGLDLIBS="libpng-staging/lib/libpng.a -lz" Q=
120+
cmake -S . -B build --preset macos-static -DFETCHCONTENT_BASE_DIR="${{ env.DEPS_ROOT_DIR }}"
121+
cmake --build build
118122
- name: Package binaries
119123
run: |
120124
mkdir bins
@@ -168,7 +172,7 @@ jobs:
168172
with:
169173
path: ${{ env.DEPS_ROOT_DIR }}/*-tmp/
170174
key: dep-srcs-${{ hashFiles('cmake/deps.cmake') }}
171-
enableCrossOsArchive: true # Currently only used on Windows, but the contents are OS-agnostic.
175+
enableCrossOsArchive: true
172176
- name: Build Windows binaries
173177
run: | # ASan seems to be broken on Windows, so we disable it.
174178
cmake -S . -B build --preset develop-msvc${{ matrix.bits }} -DSANITIZERS=OFF \

CMakePresets.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
"name": "msvc64",
3232
"description": "Flags for building 64-bit executables with MSVC",
3333
"architecture": "x64"
34+
},
35+
36+
{
37+
"name": "macos-static",
38+
"description": "Flags for building an executable compatible with old macOS",
39+
"cacheVariables": {
40+
"CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "cmake/macos-static.cmake"
41+
}
3442
}
3543
]
3644
}

cmake/macos-static.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file is meant to be included at the project level,
2+
# in order to generate executables compatible with old macOS versions.
3+
# See our `macos-static` CMake preset for how it's meant to be used.
4+
5+
# The `-mmacosx-version-min=10.9` flag ensures that the binary only uses APIs available on Mac OS X 10.9 Mavericks.
6+
# The `-arch` flags build a "fat binary" that works on both Apple architectures:
7+
# older Intel x64 Macs and newer ARM "Apple Silicon" ones.
8+
set("-mmacosx-version-min=10.9 -arch x86_64 -arch arm64")
9+
set(CMAKE_C_FLAGS "${secret_sauce}" CACHE STRING "Flags used by the C compiler during all build types.")
10+
set(CMAKE_CXX_FLAGS "${secret_sauce}" CACHE STRING "Flags used by the CXX compiler during all build types.")
11+
12+
# Mac OS X has always provided zlib, so we can safely link dynamically against it.
13+
# However, libpng is *not* provided by default, so we link it statically, which requires downloading and building it from source.
14+
set(PNG_SHARED OFF)
15+
set(PNG_STATIC ON)
16+
# If libpng is already available (e.g. via Homebrew), we ignore that and still build our own.
17+
set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER)
18+
# But we still want to attempt linking against the system's zlib.
19+
function(rgbds_provide_dependency method dep_name)
20+
if(dep_name STREQUAL "ZLIB")
21+
find_package(ZLIB)
22+
if(ZLIB_FOUND)
23+
FetchContent_SetPopulated(ZLIB)
24+
endif()
25+
endif()
26+
endfunction(rgbds_provide_dependency)
27+
cmake_language(SET_DEPENDENCY_PROVIDER rgbds_provide_dependency
28+
SUPPORTED_METHODS FETCHCONTENT_MAKEAVAILABLE_SERIAL)

0 commit comments

Comments
 (0)