Seek lib deps via CMake FetchContent

`FetchContent` respects existing installs, and downloads and compiles the libs
if they aren't found.
This is admittedly a little finicky, since this ignores the usual `Find*`
modules provided by CMake, requiring a bit of glue on our side.

But, one upside is that this moves that logic from our CI into the build system,
which can thus benefit other downstream users.
This also opens the door to some improvements in upcoming commits.

Doing this with Bison turned out to be much more painful, however, due to
`FindBISON` providing the specific `bison_target` command; thus, it remains
installed externally, so that it can be picked up by `FindBISON`.

This also bumps our CMake version requirement slightly, though it's
possible that older versions keep working, or could be supported with small patches;
however, our CI doesn't provide anything below 3.31, so we can't check.
This commit is contained in:
ISSOtm
2026-03-18 02:34:30 +01:00
committed by Eldred Habert
parent fd38a3dbc4
commit d6d1844d60
5 changed files with 52 additions and 67 deletions

View File

@@ -15,9 +15,4 @@ function getlibrary ([string] $URI, [string] $filename, [string] $hash, [string]
Expand-Archive -DestinationPath $destdir $filename Expand-Archive -DestinationPath $destdir $filename
} }
getlibrary 'https://www.zlib.net/zlib132.zip' 'zlib.zip' 'e8bf55f3017aa181690990cb58a994e77885da140609fc8f94abe9b65d2cae28' . getlibrary 'https://github.com/lexxmark/winflexbison/releases/download/v2.5.25/win_flex_bison-2.5.25.zip' 'winflexbison.zip' '8d324b62be33604b2c45ad1dd34ab93d722534448f55a16ca7292de32b6ac135' bison
getlibrary 'https://github.com/pnggroup/libpng/archive/refs/tags/v1.6.53.zip' 'libpng.zip' '9fb99118ec4523d9a9dab652ce7c2472ec76f6ccd69d1aba3ab873bb8cf84b98' .
getlibrary 'https://github.com/lexxmark/winflexbison/releases/download/v2.5.25/win_flex_bison-2.5.25.zip' 'winflexbison.zip' '8d324b62be33604b2c45ad1dd34ab93d722534448f55a16ca7292de32b6ac135' install_dir
Move-Item zlib-1.3.2 zlib
Move-Item libpng-1.6.53 libpng

View File

@@ -28,27 +28,14 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install deps - name: Install deps
run: .github/scripts/get_win_deps.ps1 run: .github/scripts/get_win_deps.ps1
- name: Build zlib
run: | # BUILD_SHARED_LIBS causes the output DLL to be called `z.dll` as of zlib 1.3.2 (formerly `zlib1.dll`)
cmake -S zlib -B zbuild -A ${{ matrix.platform }} -Wno-dev -DCMAKE_INSTALL_PREFIX=install_dir -DBUILD_SHARED_LIBS=ON
cmake --build zbuild --config Release -j
- name: Install zlib
run: |
cmake --install zbuild --config Release
- name: Build libpng
shell: bash
run: |
cmake -S libpng -B pngbuild -A ${{ matrix.platform }} -Wno-dev -DCMAKE_INSTALL_PREFIX=install_dir -DPNG_SHARED=ON -DPNG_STATIC=OFF -DPNG_TESTS=OFF
cmake --build pngbuild --config Release -j
- name: Install libpng
run: |
cmake --install pngbuild --config Release
- name: Build Windows binaries - name: Build Windows binaries
shell: bash shell: bash
# We instruct CMake to download and build third-party projects outside of our source tree,
# otherwise they can trigger `-Werror=dev` (from the `develop` preset).
run: | run: |
cmake -S . -B build -A ${{ matrix.platform }} -DCMAKE_INSTALL_PREFIX=install_dir -DCMAKE_BUILD_TYPE=Release cmake -S . -B build -A ${{ matrix.platform }} -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR="$TEMP/cmake-deps" -DBISON_EXECUTABLE=bison/win_bison.exe
cmake --build build --config Release -j --verbose cmake --build build --config Release -j
cmake --install build --config Release --strip cmake --install build --config Release --prefix install_dir --verbose
- name: Package binaries - name: Package binaries
run: | run: |
Compress-Archive -LiteralPath @("install_dir/bin/rgbasm.exe", "install_dir/bin/rgblink.exe", "install_dir/bin/rgbfix.exe", "install_dir/bin/rgbgfx.exe", "install_dir/bin/z.dll", "install_dir/bin/libpng16.dll") "rgbds-win${{ matrix.bits }}.zip" Compress-Archive -LiteralPath @("install_dir/bin/rgbasm.exe", "install_dir/bin/rgblink.exe", "install_dir/bin/rgbfix.exe", "install_dir/bin/rgbgfx.exe", "install_dir/bin/z.dll", "install_dir/bin/libpng16.dll") "rgbds-win${{ matrix.bits }}.zip"

View File

@@ -30,8 +30,13 @@ jobs:
sudo make install -j Q= sudo make install -j Q=
- name: Build & install using CMake - name: Build & install using CMake
if: matrix.buildsys == 'cmake' if: matrix.buildsys == 'cmake'
# Since GitHub's runners are basically kitchen sinks,
# the Mono framework exposes a libpng 1.4.x header, breaking everything.
# Searching frameworks last makes Homebrew's libpng be discovered first, which works.
# Note that since this is specific to our CI environment, the workaround is
# better applied here than in our CMakeLists, where it could affect and break someone else.
run: | run: |
cmake -S . -B build --preset develop -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DTESTS_OS_NAME=${{ matrix.os }} cmake -S . -B build --preset develop -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DTESTS_OS_NAME=${{ matrix.os }}
cmake --build build -j --verbose cmake --build build -j --verbose
sudo cmake --install build --verbose sudo cmake --install build --verbose
- name: Package binaries - name: Package binaries
@@ -147,38 +152,14 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install deps - name: Install deps
run: .github/scripts/get_win_deps.ps1 run: .github/scripts/get_win_deps.ps1
- name: Check libraries cache
id: cache
uses: actions/cache@v4
with:
path: |
zbuild
pngbuild
key: ${{ matrix.os }}-${{ matrix.arch }}-${{ hashFiles('zlib/**', 'libpng/**') }}
- name: Build zlib
if: steps.cache.outputs.cache-hit != 'true'
shell: bash
run: | # BUILD_SHARED_LIBS causes the output DLL to be called `z.dll` as of zlib 1.3.2 (formerly `zlib1.dll`)
cmake -S zlib -B zbuild -A ${{ matrix.platform }} -Wno-dev -DCMAKE_INSTALL_PREFIX=install_dir -DBUILD_SHARED_LIBS=ON
cmake --build zbuild --config Debug -j
- name: Install zlib
run: |
cmake --install zbuild --config Debug
- name: Build libpng
if: steps.cache.outputs.cache-hit != 'true'
shell: bash
run: |
cmake -S libpng -B pngbuild -A ${{ matrix.platform }} -Wno-dev -DCMAKE_INSTALL_PREFIX=install_dir -DCMAKE_BUILD_TYPE=Debug -DPNG_SHARED=ON -DPNG_STATIC=OFF -DPNG_TESTS=OFF
cmake --build pngbuild --config Debug -j
- name: Install libpng
run: |
cmake --install pngbuild --config Debug
- name: Build Windows binaries - name: Build Windows binaries
shell: bash shell: bash
run: | # ASan seems to be broken on Windows. # We instruct CMake to download and build third-party projects outside of our source tree,
cmake -S . -B build -A ${{ matrix.platform }} --preset develop -DCMAKE_INSTALL_PREFIX=install_dir -DSANITIZERS=OFF # otherwise they can trigger `-Werror=dev` (from the `develop` preset).
cmake --build build --config Debug -j --verbose run: | # ASan seems to be broken on Windows, so we disable it.
cmake --install build --verbose cmake -S . -B build -A ${{ matrix.platform }} --preset develop -DFETCHCONTENT_BASE_DIR="$TEMP/cmake-deps" -DBISON_EXECUTABLE=bison/win_bison.exe -DSANITIZERS=OFF
cmake --build build --config Debug -j
cmake --install build --config Debug --prefix install_dir --verbose
- name: Package binaries - name: Package binaries
shell: bash shell: bash
run: | run: |

View File

@@ -1,7 +1,10 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
# 3.17 required for LTO checks messages # - 3.24 is required for `FetchContent`'s `find_package` integration.
cmake_minimum_required(VERSION 3.17...4.2 FATAL_ERROR) # Older versions *may* work, but we can't test them; compat patches are welcome.
# - 3.17 is required for `CHECK_*` messages to display properly, but is not essential.
# - 3.9 is required for LTO checks.
cmake_minimum_required(VERSION 3.24...4.2 FATAL_ERROR)
# Read the project version from the header (the canonical source of truth). # Read the project version from the header (the canonical source of truth).
file(STRINGS "include/version.hpp" version_defines REGEX "^[ \t]*#define[ \t]+PACKAGE_VERSION_") file(STRINGS "include/version.hpp" version_defines REGEX "^[ \t]*#define[ \t]+PACKAGE_VERSION_")
@@ -136,15 +139,34 @@ endif()
## Dependencies. ## Dependencies.
find_package(PkgConfig) include(FetchContent)
if(MSVC OR NOT PKG_CONFIG_FOUND) FetchContent_Declare(PNG
# fallback to find_package URL https://download.sourceforge.net/libpng/libpng-1.6.55.tar.xz
# cmake's FindPNG is very fragile; it breaks when multiple versions are installed URL_HASH SHA256=d925722864837ad5ae2a82070d4b2e0603dc72af44bd457c3962298258b8e82d
# this is most evident on macOS but can occur on Linux too FIND_PACKAGE_ARGS 1.5.4)
find_package(PNG REQUIRED) FetchContent_Declare(ZLIB
else() URL https://www.zlib.net/zlib-1.3.2.tar.xz
pkg_check_modules(LIBPNG REQUIRED IMPORTED_TARGET libpng) URL_HASH SHA256=d7a0654783a4da529d1bb793b7ad9c3318020af77667bcae35f95d0e42a792f3
add_library(PNG::PNG ALIAS PkgConfig::LIBPNG) # libpng documents requiring "zlib 1.0.4 or later (1.2.13 or later recommended for performance and security reasons)".
# We thus enforce 1.0.4, but note that the libpng source code mentions that "it may work with versions as old as zlib 0.95".
FIND_PACKAGE_ARGS 1.0.4)
set(ZLIB_BUILD_SHARED ON CACHE INTERNAL "")
set(ZLIB_BUILD_STATIC OFF CACHE INTERNAL "")
FetchContent_MakeAvailable(ZLIB)
if(NOT DEFINED ZLIB_INCLUDE_DIRS)
set(ZLIB_INCLUDE_DIRS "${zlib_BINARY_DIR};${zlib_SOURCE_DIR}") # libpng's `genout` script relies on this variable to be set.
endif()
set(PNG_TESTS OFF CACHE INTERNAL "") # We do not care for these two (and they can even cause compile errors!)
set(PNG_TOOLS OFF CACHE INTERNAL "")
set(PNG_SHARED ON CACHE INTERNAL "") # Upstream seems to favour the dynamic lib over the static one?
set(PNG_STATIC OFF CACHE INTERNAL "")
FetchContent_MakeAvailable(PNG)
if(NOT TARGET PNG::PNG)
if(PNG_SHARED)
add_library(PNG::PNG ALIAS png_shared)
else()
add_library(PNG::PNG ALIAS png_static)
endif()
endif() endif()
## The actual stuff. ## The actual stuff.

View File

@@ -13,7 +13,7 @@ target_compile_definitions(common PRIVATE "BUILD_VERSION_STRING=\"${GIT_REV}\"")
find_package(BISON 3.0.0 REQUIRED) find_package(BISON 3.0.0 REQUIRED)
set(BISON_FLAGS "-Wall -Dlr.type=ielr") set(BISON_FLAGS "-Wall -Dlr.type=ielr")
# Set some flags on versions that support them # Set some flags on versions that support them.
if(BISON_VERSION VERSION_GREATER_EQUAL "3.5") if(BISON_VERSION VERSION_GREATER_EQUAL "3.5")
set(BISON_FLAGS "${BISON_FLAGS} -Dparse.lac=full -Dapi.token.raw=true -Wdangling-alias") set(BISON_FLAGS "${BISON_FLAGS} -Dparse.lac=full -Dapi.token.raw=true -Wdangling-alias")
endif() endif()