Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions .github/scripts/buildzlib.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2026 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for Swift project authors
#

<#
.SYNOPSIS
Fetches a pinned zlib release and builds it as a static library on Windows.

.DESCRIPTION
Downloads the source tarball for a specific zlib release then configures
and builds with CMake (Visual Studio generator by default, or whatever
-Generator specifies). Only the static target is built. Output goes to
<OutDir>\include and <OutDir>\lib.

.PARAMETER WorkDir
Scratch directory for the download, extracted source and build tree.

.PARAMETER OutDir
Where the headers and zlib static library are installed.

.PARAMETER Config
Release or Debug.

.PARAMETER Arch
Target architecture for the Visual Studio generator: x64, Win32 or ARM64.

.PARAMETER StaticRuntime
Link the static MSVC runtime (/MT) instead of the DLL runtime (/MD).

.PARAMETER Generator
CMake generator. Defaults to the newest Visual Studio found by CMake.

.PARAMETER Version
The zlib release to build, without the leading 'v'. Defaults to 1.3.2.

.EXAMPLE
.\buildzlib.ps1 -OutDir C:\libs\zlib -StaticRuntime
#>
[CmdletBinding()]
param(
[string]$WorkDir = (Join-Path $PWD 'zlib-build'),
[string]$OutDir = (Join-Path $PWD 'zlib-install'),
[ValidateSet('Release', 'Debug')][string]$Config = 'Release',
[ValidateSet('x64', 'Win32', 'ARM64')][string]$Arch = 'x64',
[switch]$StaticRuntime,
[string]$Generator,
[string]$Version = '1.3.2'
)

$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue' # Invoke-WebRequest is very slow otherwise
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13

function Invoke-Native {
& $args[0] $args[1..($args.Length - 1)]
if ($LASTEXITCODE -ne 0) { throw "'$($args -join ' ')' failed with exit code $LASTEXITCODE" }
}

foreach ($tool in 'cmake', 'tar') {
if (-not (Get-Command $tool -ErrorAction SilentlyContinue)) {
throw "Required tool '$tool' not found on PATH. Install CMake and run from a VS Developer prompt if needed."
}
}

# 1. Choose the version
$zlibVersion = $Version.TrimStart('v')
Write-Host "zlib version: $zlibVersion"

# 2. Download and extract into fresh directories
$downloadDir = Join-Path $WorkDir "download-$zlibVersion"
$srcRoot = Join-Path $WorkDir "src-$zlibVersion"
$buildDir = Join-Path $WorkDir "build-$zlibVersion-$Arch"
foreach ($d in $downloadDir, $srcRoot, $buildDir) {
if (Test-Path $d) { Remove-Item $d -Recurse -Force }
New-Item -ItemType Directory -Path $d | Out-Null
}

$url = "https://github.com/madler/zlib/releases/download/v$zlibVersion/zlib-$zlibVersion.tar.gz"
$archive = Join-Path $downloadDir "zlib-$zlibVersion.tar.gz"
Write-Host "Downloading $url"
Invoke-WebRequest $url -OutFile $archive

# Paths are passed as arguments; nothing is run from inside the extracted tree.
Invoke-Native tar -xzf $archive -C $srcRoot
$srcDir = (Get-ChildItem $srcRoot -Directory | Select-Object -First 1).FullName
if (-not $srcDir) { throw 'Archive did not extract to a directory.' }

# 3. Configure
$cmakeArgs = @('-S', $srcDir, '-B', $buildDir)
if ($Generator) { $cmakeArgs += @('-G', $Generator) }
if (-not $Generator -or $Generator -like 'Visual Studio*') { $cmakeArgs += @('-A', $Arch) }
$cmakeArgs += '-DZLIB_BUILD_SHARED=OFF' # ignored by older releases; we only build zlibstatic anyway
if ($StaticRuntime) {
$cmakeArgs += '-DCMAKE_POLICY_DEFAULT_CMP0091=NEW'
$cmakeArgs += '-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Debug>:Debug>'
}
Invoke-Native cmake @cmakeArgs

# 4. Build only the static library
Invoke-Native cmake --build $buildDir --config $Config --target zlibstatic --parallel

# 5. Install headers and library
$incOut = Join-Path $OutDir 'include'
$libOut = Join-Path $OutDir 'lib'
New-Item -ItemType Directory -Force -Path $incOut, $libOut | Out-Null

# zconf.h is generated into the build tree; the source copy is renamed zconf.h.included
Copy-Item (Join-Path $srcDir 'zlib.h') $incOut -Force
$zconf = Join-Path $buildDir 'zconf.h'
if (-not (Test-Path $zconf)) { $zconf = Join-Path $srcDir 'zconf.h' }
Copy-Item $zconf $incOut -Force

# Multi-config generators put the lib under <build>\<Config>; single-config in <build>.
# Pre-1.3.2, the output is named zlibstatic or, for debug, zlibstaticd.
# As of zlib-1.3.2, the output is named zs or, for debug, zsd.
# Normalize to zlib[d]
$built = Get-ChildItem $buildDir -Recurse -Filter 'z*.lib' |
Where-Object { $_.Directory.Name -in $Config, (Split-Path $buildDir -Leaf) } |
Select-Object -First 1
if (-not $built) { $built = Get-ChildItem $buildDir -Recurse -Filter 'z*.lib' | Select-Object -First 1 }
if (-not $built) { throw 'Could not find the built static library.' }

$destName = $built.Name -replace '^(zlibstatic|zs)', 'zlib'
Copy-Item $built.FullName (Join-Path $libOut $destName) -Force

# Write a pkgconfig file
$pcName = Join-Path $OutDir 'zlib.pc'

# pkg-config treats backslash as an escape character, so use forward slashes
$pcLib = $libOut -replace '\\', '/'
$pcInc = $incOut -replace '\\', '/'
# Quote paths containing whitespace, so they stay a single flag argument
if ($pcLib -match '\s') { $pcLib = "`"$pcLib`"" }
if ($pcInc -match '\s') { $pcInc = "`"$pcInc`"" }
$pcLibName = [System.IO.Path]::GetFileNameWithoutExtension($destName)

@"
Name: zlib
Version: $zlibVersion
Description: zlib compression library
Libs: -L$pcLib -l$pcLibName
Cflags: -I$pcInc
"@ | Out-File $pcName -Encoding ascii

Write-Host ''
Write-Host "zlib $zlibVersion ($Config, $Arch) built successfully."
Write-Host " Headers: $incOut"
Write-Host " Library: $(Join-Path $libOut $destName)"
Write-Host " pkgconfig: $pcName"
20 changes: 17 additions & 3 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,29 @@ jobs:
# due to issues with filenames on Windows. For now, the Windows CI runs
# build the package, and only build the tests without running them.
# Filenames PR: https://github.com/swiftlang/swift-docc/pull/668
windows_build_command: "Invoke-Program swift build --build-tests"

# Windows needs to build a copy of zlib first, because SwiftPM isn't
# able to fetch a built copy there. The CMake build below needs it too,
# because nothing else provides ZLIB::ZLIB on Windows.
windows_pre_build_command: |
Invoke-Program .\.github\scripts\prebuild.ps1 -InstallCMake -SkipAndroid
Invoke-Program .\.github\scripts\buildzlib.ps1 -OutDir $Env:TEMP\zlib -WorkDir $Env:TEMP\zlib-build
windows_build_command: 'Invoke-Program swift build --pkg-config-path $Env:TEMP\zlib --build-tests'

# We don't support Swift 6.1 any more
macos_exclude_xcode_versions: "[{\"xcode_version\": \"swift_6.1\"}]"
linux_exclude_swift_versions: "[{\"swift_version\": \"6.1\"}]"
windows_exclude_swift_versions: "[{\"swift_version\": \"6.1\"}]"

cmake:
name: CMake
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@0.0.15
with:
enable_linux_checks: false
windows_swift_versions: '["nightly-6.4.x"]'
windows_pre_build_command: 'Invoke-Program .\.github\scripts\prebuild.ps1 -InstallCMake -SkipAndroid'
windows_pre_build_command: |
Invoke-Program .\.github\scripts\prebuild.ps1 -InstallCMake -SkipAndroid
Invoke-Program .\.github\scripts\buildzlib.ps1 -OutDir $Env:TEMP\zlib -WorkDir $Env:TEMP\zlib-build
windows_build_command: |
$Src = Join-Path $PWD ".build\checkouts"
$Bin = Join-Path $PWD "BinaryCache"
Expand Down Expand Up @@ -61,7 +75,7 @@ jobs:
Build "$Bin\swift-lmdb" "$Src\swift-lmdb" @("-DBUILD_SHARED_LIBS=NO")
Build "$Bin\swift-docc-symbolkit" "$Src\swift-docc-symbolkit" @("-DBUILD_SHARED_LIBS=NO")
# Build DocC
Build "$Bin\swift-docc" "$PWD" @("-DBUILD_SHARED_LIBS=YES", "-DArgumentParser_DIR=$Bin\swift-argument-parser\cmake\modules", "-DSwiftASN1_DIR=$Bin\swift-asn1\cmake\modules", "-DSwiftCrypto_DIR=$Bin\swift-crypto\cmake\modules", "-DSwiftMarkdown_DIR=$Bin\swift-markdown\cmake\modules", "-DLMDB_DIR=$Bin\swift-lmdb\cmake\modules", "-DSymbolKit_DIR=$Bin\swift-docc-symbolkit\cmake\modules", "-Dcmark-gfm_DIR=$Install\swift-cmark\lib\cmake")
Build "$Bin\swift-docc" "$PWD" @("-DBUILD_SHARED_LIBS=YES", "-DArgumentParser_DIR=$Bin\swift-argument-parser\cmake\modules", "-DSwiftASN1_DIR=$Bin\swift-asn1\cmake\modules", "-DSwiftCrypto_DIR=$Bin\swift-crypto\cmake\modules", "-DSwiftMarkdown_DIR=$Bin\swift-markdown\cmake\modules", "-DLMDB_DIR=$Bin\swift-lmdb\cmake\modules", "-DSymbolKit_DIR=$Bin\swift-docc-symbolkit\cmake\modules", "-DZLIB_ROOT=$Env:TEMP\zlib", "-Dcmark-gfm_DIR=$Install\swift-cmark\lib\cmake")

soundness:
name: Soundness
Expand Down
5 changes: 5 additions & 0 deletions .swift-format

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather that we don't commit any formatting configuration files.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drat. I hadn't intended to add that (again). I should add it to .gitignores or similar, maybe.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"indentation": {
"spaces": 4
}
}
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ find_package(SwiftMarkdown)
find_package(LMDB)
find_package(SymbolKit)
find_package(cmark-gfm)
find_package(ZLIB)

add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:-package-name;SwiftDocC>")

Expand Down
31 changes: 31 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ let package = Package(
dependencies: [
.target(name: "DocCCommon"),
.target(name: "DocCHTML"),
.target(name: "Zip"),
.product(name: "Markdown", package: "swift-markdown"),
.product(name: "SymbolKit", package: "swift-docc-symbolkit"),
.product(name: "CLMDB", package: "swift-lmdb"),
Expand Down Expand Up @@ -225,6 +226,36 @@ let package = Package(
],
swiftSettings: swiftSettings(.v5)
),

// Zip file handling
.target(
name: "Zip",
dependencies: [
.target(name: "ZLib")
],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings(.v6)
),
.testTarget(
name: "ZipTests",
dependencies: [
.target(name: "Zip")
],
swiftSettings: swiftSettings(.v6)
),

.systemLibrary(
name: "CZLib",
pkgConfig: "zlib"
),
.target(
name: "ZLib",
dependencies: [
.target(name: "CZLib")
],
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings(.v6)
)
]
)

Expand Down
2 changes: 2 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ add_subdirectory(DocCHTML)
add_subdirectory(SwiftDocC)
add_subdirectory(DocCCommandLine)
add_subdirectory(docc)
add_subdirectory(Zip)
add_subdirectory(ZLib)
11 changes: 11 additions & 0 deletions Sources/CZLib/CZLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2026 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

#include <zlib.h>
4 changes: 4 additions & 0 deletions Sources/CZLib/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module CZLib {
header "CZLib.h"
export *
}
7 changes: 4 additions & 3 deletions Sources/SwiftDocC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,13 @@ add_library(SwiftDocC
Utility/SymbolGraphAvailability+Filter.swift
Utility/Synchronization.swift
Utility/ValidatedURL.swift
Utility/Version.swift)
target_link_libraries(SwiftDocC PRIVATE
DocCCommon)
Utility/Version.swift
Utility/ZipFileReader+ReadOnlyFileManagerProtocol.swift)
target_link_libraries(SwiftDocC PRIVATE
DocCCommon
DocCHTML)
target_link_libraries(SwiftDocC PUBLIC
Zip
SwiftMarkdown::Markdown
DocC::SymbolKit
LMDB::CLMDB
Expand Down
Loading
Loading