Skip to content

Commit 32528b0

Browse files
authored
feat: added open source toolchain preference option for xcode
1 parent a861e37 commit 32528b0

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

Diff for: .github/workflows/main.yml

+17
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ jobs:
180180
- os: ubuntu-22.04
181181
swift: ${{ fromJSON(vars.SETUPSWIFT_CUSTOM_TOOLCHAINS).ubuntu2204 }} # custom toolchain
182182
development: true
183+
- os: macos-14
184+
swift: '6.0.3'
185+
development: false
186+
sdk: aarch64-swift-linux-musl
187+
sdk-url: https://download.swift.org/swift-6.0.3-release/static-sdk/swift-6.0.3-RELEASE/swift-6.0.3-RELEASE_static-linux-0.0.1.artifactbundle.tar.gz
188+
sdk-checksum: 67f765e0030e661a7450f7e4877cfe008db4f57f177d5a08a6e26fd661cdd0bd
183189

184190
steps:
185191
- name: Checkout repository
@@ -210,6 +216,7 @@ jobs:
210216
development: ${{ matrix.development }}
211217
check-latest: ${{ needs.ci.outputs.check_latest }}
212218
cache-snapshot: ${{ !matrix.development }}
219+
prefer-oss-toolchain: ${{ matrix.sdk != '' }}
213220

214221
- name: Verify Swift version in macos
215222
if: runner.os == 'macOS'
@@ -222,6 +229,16 @@ jobs:
222229
if: runner.os == 'Windows'
223230
run: which link | grep "Microsoft Visual Studio" || exit 1
224231

232+
- name: Install SDK
233+
if: matrix.sdk != ''
234+
run: swift sdk install "${{ matrix.sdk-url }}" --checksum "${{ matrix.sdk-checksum }}"
235+
236+
- name: Test Swift package
237+
if: matrix.sdk != ''
238+
run: |
239+
swift package init --type library --name SetupLib
240+
swift build --swift-sdk ${{ matrix.sdk }}
241+
225242
- name: Get cached installation
226243
id: get-tool
227244
if: failure()

Diff for: __tests__/installer/xcode.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('macOS toolchain installation verification', () => {
3838
const installer = new XcodeToolchainInstaller(toolchain)
3939
jest.spyOn(fs, 'access').mockResolvedValue()
4040
jest.spyOn(exec, 'exec').mockResolvedValue(0)
41+
jest.spyOn(core, 'getBooleanInput').mockReturnValue(false)
4142
jest.spyOn(exec, 'getExecOutput').mockResolvedValue({
4243
exitCode: 0,
4344
stdout: `swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)\nTarget: arm64-apple-macosx13.0`,
@@ -55,6 +56,48 @@ describe('macOS toolchain installation verification', () => {
5556
expect(process.env.DEVELOPER_DIR).toBe(toolchain.xcodePath)
5657
})
5758

59+
it('tests toolchain preinstalled not preferred', async () => {
60+
const installer = new XcodeToolchainInstaller(toolchain)
61+
const identifier = 'org.swift.581202305171a'
62+
jest.spyOn(fs, 'access').mockResolvedValue()
63+
jest.spyOn(exec, 'exec').mockResolvedValue(0)
64+
jest.spyOn(core, 'getBooleanInput').mockReturnValue(true)
65+
jest.spyOn(cache, 'restoreCache').mockResolvedValue(undefined)
66+
jest.spyOn(toolCache, 'find').mockReturnValue('')
67+
jest.spyOn(cache, 'saveCache').mockResolvedValue(1)
68+
jest.spyOn(fs, 'access').mockResolvedValue()
69+
jest.spyOn(fs, 'readFile').mockResolvedValue('')
70+
jest.spyOn(fs, 'cp').mockResolvedValue()
71+
jest.spyOn(plist, 'parse').mockReturnValue({CFBundleIdentifier: identifier})
72+
jest.spyOn(exec, 'getExecOutput').mockResolvedValue({
73+
exitCode: 0,
74+
stdout: `swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)\nTarget: arm64-apple-macosx13.0`,
75+
stderr: ''
76+
})
77+
const download = path.resolve('tool', 'download', 'path')
78+
const extracted = path.resolve('tool', 'extracted', 'path')
79+
const deployed = path.resolve('tool', 'deployed', 'path')
80+
const cached = path.resolve('tool', 'cached', 'path')
81+
const installationNeededSpy = jest.spyOn(installer, 'isInstallationNeeded')
82+
const downloadSpy = jest
83+
.spyOn(toolCache, 'downloadTool')
84+
.mockResolvedValue(download)
85+
const extractXarSpy = jest
86+
.spyOn(toolCache, 'extractXar')
87+
.mockResolvedValue(extracted)
88+
const extractTarSpy = jest
89+
.spyOn(toolCache, 'extractTar')
90+
.mockResolvedValue(deployed)
91+
const cacheSpy = jest.spyOn(toolCache, 'cacheDir').mockResolvedValue(cached)
92+
await installer.install('x86_64')
93+
await installer.install('aarch64')
94+
for (const spy of [downloadSpy, extractXarSpy, extractTarSpy, cacheSpy]) {
95+
expect(spy).toHaveBeenCalled()
96+
}
97+
expect(installationNeededSpy).toHaveBeenCalled()
98+
expect(process.env.DEVELOPER_DIR).toBe(toolchain.xcodePath)
99+
})
100+
58101
it('tests download', async () => {
59102
const installer = new XcodeToolchainInstaller(toolchain)
60103
expect(installer['version']).toStrictEqual(parseSemVer('5.8.1'))
@@ -185,6 +228,7 @@ describe('macOS toolchain installation verification', () => {
185228
jest.spyOn(toolCache, 'find').mockReturnValue('')
186229
jest.spyOn(fs, 'access').mockResolvedValue()
187230
jest.spyOn(exec, 'exec').mockResolvedValue(0)
231+
jest.spyOn(core, 'getBooleanInput').mockReturnValue(false)
188232
jest.spyOn(exec, 'getExecOutput').mockResolvedValue({
189233
exitCode: 0,
190234
stdout: `swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)\nTarget: arm64-apple-macosx13.0`,

Diff for: action.yml

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ inputs:
4848
i.e. Microsoft.VisualStudio.Component.VC.ATL;Microsoft.VisualStudio.Component.VC.CMake.Project;Microsoft.VisualStudio.Component.Windows10SDK
4949
required: false
5050
default: ''
51+
prefer-oss-toolchain:
52+
description: >-
53+
Whether to prefer installing Swift open source toolchain over using Xcode integrated toolchain.
54+
i.e. Enable this option for installing static SDK: https://www.swift.org/documentation/articles/static-linux-getting-started.html
55+
required: false
56+
default: 'false'
5157
outputs:
5258
swift-version:
5359
description: The actual Swift version that was configured.

Diff for: dist/index.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/installer/xcode.ts

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export class XcodeToolchainInstaller extends ToolchainInstaller<XcodeToolchainSn
3838
const command = this.swiftVersionCommand('')
3939
const version = await this.installedSwiftVersion(command)
4040
core.debug(`Found toolchain "${version}" bundled with Xcode ${xcode}`)
41+
if (core.getBooleanInput('prefer-oss-toolchain')) {
42+
core.debug(`Giving preference to open source toolchain`)
43+
return true
44+
}
4145
return this.data.dir !== `swift-${version}-RELEASE`
4246
}
4347

0 commit comments

Comments
 (0)