Skip to content

Commit 1894579

Browse files
authored
Add SDL2 as XCFramework on Apple platforms + fix Windows build (#24)
* Add xcframework build script * Update build script to include module map * xcframework * Working macOS * Working assembled * Add link + update makefile * Update tests * Update macos pipeline * Update to macOS 13 * Publish XCFramework * Progress * Fix exported import * Fix target naming * Package dependency fixes * Imports * Fix linux tests * Working windows install script * Update win ci * Update win ci * Update README
1 parent 8f12185 commit 1894579

File tree

293 files changed

+132247
-460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

293 files changed

+132247
-460
lines changed

.github/workflows/ci-macos.yml

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,19 @@ on:
99

1010
jobs:
1111
macos-test-build-release-xcode:
12-
runs-on: macos-latest
12+
runs-on: macos-13
1313
strategy:
1414
matrix:
15-
xcode: ["14.2"]
15+
xcode: ["14.3.1"]
1616
steps:
1717
- name: Checkout
18-
uses: actions/checkout@v3.5.2
18+
uses: actions/checkout@v3.5.3
1919

2020
- name: Select Xcode ${{ matrix.xcode }}
2121
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
22-
23-
- name: Restore cached Homebrew dependencies
24-
uses: actions/cache@v3.3.1
25-
with:
26-
path: |
27-
~/Library/Caches/Homebrew/sdl2--*
28-
~/Library/Caches/Homebrew/downloads/*--sdl2-*
29-
key: ${{ runner.os }}-brew-sdl2-${{ hashFiles('.github/brew-formulae') }}
30-
restore-keys: ${{ runner.os }}-brew-sdl2-
31-
32-
- name: Restore cached SPM dependencies
33-
uses: actions/cache@v3.3.1
34-
with:
35-
path: |
36-
.build/repositories
37-
key: ${{ runner.os }}-spm-repos-${{ hashFiles('**/Package.resolved') }}
38-
restore-keys: |
39-
${{ runner.os }}-spm-repos-
40-
41-
- name: Install dependencies
42-
run: brew install sdl2
4322

4423
- name: Test
45-
run: make test
24+
run: swift test
4625
env:
4726
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer
4827

@@ -56,7 +35,7 @@ jobs:
5635
.build/**/*.xctest
5736
5837
- name: Build Release
59-
run: make build-release
38+
run: swift build -c release
6039
env:
6140
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer
6241

@@ -73,3 +52,16 @@ jobs:
7352
.build/**/*.dylib
7453
.build/**/*.dSYM
7554
.build/**/*.json
55+
56+
- name: Zip XCFramework
57+
run: |
58+
zip -q -9 -r SDL2.xcframework.zip SDL2.xcframework
59+
echo "`swift package compute-checksum SDL2.xcframework.zip`" > SDL2.xcframework.checksum.txt
60+
61+
- name: Publish XCFramework
62+
uses: actions/upload-artifact@v3.1.2
63+
with:
64+
name: build-artifacts-${{ matrix.xcode }}-${{ github.run_id }}
65+
path: |
66+
SDL2.xcframework.zip
67+
SDL2.xcframework.checksum.txt

.github/workflows/ci-windows.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,39 @@ jobs:
2323
uses: actions/cache@v3.3.1
2424
with:
2525
key: windows-x64-vcpkg
26-
path: C:/vcpkg/
26+
path: C:\vcpkg\installed\x64-windows
2727

2828
- name: Build & Test
2929
shell: pwsh
3030
run: |
31+
vcpkg integrate powershell
32+
3133
# install dependencies
32-
vcpkg install sdl2[core,vulkan] pkgconf --recurse --triplet x64-windows
33-
vcpkg integrate install
34+
vcpkg install sdl2[core,vulkan] --recurse --triplet x64-windows --no-print-usage
35+
vcpkg install pkgconf --triplet x64-windows --no-print-usage
3436
3537
# setup pkgconf environment
3638
$env:Path += ";C:\vcpkg\installed\x64-windows\tools\pkgconf\"
37-
$env:PKG_CONFIG_PATH=$VCPKG_DIR+'\installed\x64-windows\tools\pkgconf\;C:\vcpkg\packages\sdl2_x64-windows\debug\lib\pkgconfig\'
39+
$env:PKG_CONFIG_PATH = "C:\vcpkg\installed\x64-windows\lib\pkgconfig\"
40+
41+
# generate windows header
42+
$includedir = (pkgconf --variable includedir sdl2).Trim()
43+
Write-Output ('#include "' + $includedir + '/SDL2/SDL.h"') > Sources/CSDL2/windows_generated.h
44+
Write-Output ('#include "' + $includedir + '/SDL2/SDL_vulkan.h"') >> Sources/CSDL2/windows_generated.h
3845
3946
# build
4047
swift build
4148
49+
# copy SDL libraries
50+
$bindir = ((pkgconf --variable exec_prefix sdl2).Trim() + "/bin")
51+
$libdir = (pkgconf --variable libdir sdl2).Trim()
52+
foreach ($config in "debug", "release") {
53+
$path = ".build/$config"
54+
if (Test-Path -Path $path) {
55+
Copy-Item ($bindir + '/SDL2.dll') $path
56+
Copy-Item ($libdir + '/SDL2.lib') $path
57+
}
58+
}
59+
4260
# test
4361
swift test
44-

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,6 @@ Temporary Items
103103
timeline.xctimeline
104104
vcs.xml
105105
xcuserdata/
106+
*.zip
107+
vcpkg/
108+
Sources/CSDL2/windows_generated.h

.swiftlint.yml

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

Makefile

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,7 @@
1-
lint-fix:
2-
swiftlint fix --format
3-
4-
lintErrorOnly:
5-
@swiftlint lint --quiet | grep error
6-
7-
genLinuxTests:
8-
swift test --generate-linuxmain
9-
swiftlint autocorrect --format --path Tests/
10-
11-
.PHONY: test
12-
test:
13-
swift test
14-
15-
.PHONY: build-release
16-
build-release:
17-
swift build -c release
18-
19-
submodule:
20-
git submodule update --init --recursive
21-
22-
clean:
23-
swift package reset
24-
rm -rdf .swiftpm/xcode
25-
rm -rdf .build/
26-
rm Package.resolved
27-
rm .DS_Store
28-
29-
cleanArtifacts:
30-
swift package clean
1+
SWIFT_PACKAGE_VERSION := $(shell swift package tools-version)
312

32-
latest:
33-
swift package update
34-
35-
resolve:
36-
swift package resolve
37-
38-
genXcode:
39-
swift package generate-xcodeproj --enable-code-coverage --skip-extra-files
40-
41-
genXcodeOpen: genXcode
42-
open *.xcodeproj
43-
44-
precommit: lint genLinuxTests
45-
46-
testReadme:
47-
markdown-link-check -p -v ./README.md
3+
# Lint fix and format code.
4+
.PHONY: lint-fix
5+
lint-fix:
6+
mint run swiftlint --fix --quiet
7+
mint run swiftformat --quiet --swiftversion ${SWIFT_PACKAGE_VERSION} .

Mintfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
realm/SwiftLint@0.51.0
2+
nicklockwood/SwiftFormat@0.51.7

Package.swift

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.6
22
import PackageDescription
33

44
let package = Package(
5-
name: "SDL2",
5+
name: "SDL",
6+
platforms: [
7+
.macOS(.v11),
8+
.iOS(.v13),
9+
.tvOS(.v13),
10+
],
611
products: [
7-
.library(name: "SDL2",
8-
targets: ["SDL2"]),
9-
.executable(name: "MinimalApp",
10-
targets: ["Minimal"]),
11-
.executable(name: "MetalApp",
12-
targets: ["MetalApp"])
12+
.library(name: "SDL",
13+
targets: ["SDL"]),
1314
],
1415
targets: [
16+
.target(name: "SDL",
17+
dependencies: [
18+
.target(name: "SDL2-apple", condition: .when(platforms: [.macOS, .iOS, .tvOS])),
19+
.target(name: "CSDL2", condition: .when(platforms: [.linux, .windows])),
20+
],
21+
path: "Sources/SDL2"),
22+
.testTarget(name: "SDLTests", dependencies: ["SDL"]),
23+
.binaryTarget(name: "SDL2-apple", path: "SDL2.xcframework"),
1524
.systemLibrary(
1625
name: "CSDL2",
1726
pkgConfig: "sdl2",
1827
providers: [
19-
.brew(["sdl2"]),
2028
.apt(["libsdl2-dev"])
21-
]),
22-
.target(name: "SDL2", dependencies: ["CSDL2"]),
23-
.executableTarget(name: "Minimal", dependencies: ["SDL2"], path: "Sources/Demos/Minimal"),
29+
//,.vcpkg(["sdl2[core,vulkan]"])
30+
]
31+
),
32+
.executableTarget(name: "Minimal", dependencies: ["SDL"], path: "Sources/Demos/Minimal"),
2433
.executableTarget(
2534
name: "MetalApp",
26-
dependencies: ["SDL2"],
35+
dependencies: ["SDL"],
2736
path: "Sources/Demos/MetalApp",
2837
swiftSettings: [.define("METAL_ENABLED", .when(platforms: [.macOS]))]
2938
),
30-
.testTarget(name: "CSDL2Tests", dependencies: ["CSDL2"])
3139
]
3240
)

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ These instructions will get your copy of the project up and running on your loca
2525

2626
### 💻 Installing
2727

28-
Swift SDL2 is available for all platforms that support [Swift 5.1](https://swift.org/) and higher and the [Swift Package Manager (SPM)](https://github.com/apple/swift-package-manager).
28+
Swift SDL2 is available for all platforms that support [Swift 5.6](https://swift.org/) and higher and the [Swift Package Manager (SPM)](https://github.com/apple/swift-package-manager).
2929

3030
Extend the following lines in your `Package.swift` file or use it to create a new project.
3131

3232
```swift
33-
// swift-tools-version:5.1
33+
// swift-tools-version:5.6
3434

3535
import PackageDescription
3636

3737
let package = Package(
3838
name: "YourPackageName",
3939
dependencies: [
40-
.package(url: "https://github.com/ctreffs/SwiftSDL2.git", from: "1.2.0")
40+
.package(url: "https://github.com/ctreffs/SwiftSDL2.git", from: "1.4.0")
4141
],
4242
targets: [
4343
.target(
@@ -47,20 +47,27 @@ let package = Package(
4747
)
4848

4949
```
50+
Depending on your platform several extra steps have to be taken to SDL2 to compile.
5051

51-
Since it's a system library wrapper you need to install the SDL2 library either via
52+
### Apple
5253

53-
```sh
54-
brew install sdl2
55-
```
54+
For Apple platforms (macOS, iOS, tvOS) SDL2 is provided using an XCFramework so mo extra steps are needed.
55+
56+
### Linux
5657

57-
or
58+
On Linux platforms you are required to use the following command to install SDL2 as a system package:
5859

5960
```sh
6061
apt-get install libsdl2-dev
6162
```
6263

63-
depending on your platform.
64+
### Windows
65+
66+
Since Windows is a fairly new platform for Swift development there are some manual steps to perform before you can use the package.
67+
All the following steps assume you have a working swift environment on your Windows machine.
68+
If not follow the [instructions on how to install Swift on Windows](https://www.swift.org/download/#windows).
69+
70+
You can quickly test this package is working by running the `./buildPackageWin.ps1` powershell script.
6471

6572
## 📝 Code Example
6673

@@ -69,7 +76,7 @@ depending on your platform.
6976
A minimal example is located at [Sources/Demos/Minimal](Sources/Demos/Minimal).
7077

7178
```swift
72-
import SDL2
79+
import SDL
7380

7481
// Initialize SDL video systems
7582
guard SDL_Init(SDL_INIT_VIDEO) == 0 else {

0 commit comments

Comments
 (0)