-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
186 lines (169 loc) · 6.1 KB
/
index.ts
File metadata and controls
186 lines (169 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as os from 'os'
import * as path from 'path'
import {promises as fs} from 'fs'
import * as core from '@actions/core'
import * as semver from 'semver'
import {VerifyingToolchainInstaller} from '../verify'
import {WindowsToolchainSnapshot} from '../../snapshot'
import {VisualStudio, VISUAL_STUDIO_WINSDK_COMPONENT_REGEX} from '../../utils'
import {program86} from '../../utils/windows'
import {Installation, CustomInstallation} from './installation'
import {updateSdkModules} from './modules'
import {
INPUT_VISUAL_STUDIO_COMPONENTS,
INPUT_PREFER_VISUAL_STUDIO_LINKER,
INPUT_UPDATE_SDK_MODULES
} from '../../const'
export class WindowsToolchainInstaller extends VerifyingToolchainInstaller<WindowsToolchainSnapshot> {
private async winsdk() {
const win11Semver = '10.0.22000'
const recommended = semver.gte(this.version ?? '6.2.0', '6.2.0')
? win11Semver
: '10.0.17763'
const current = os.release()
let version = semver.gte(current, recommended) ? current : recommended
const installed = await this.installedSdks()
if (installed.length && !installed.includes(version)) {
version = installed[0]
}
const major = semver.lt(version, win11Semver) ? semver.major(version) : 11
const minor = semver.patch(version)
return `Microsoft.VisualStudio.Component.Windows${major}SDK.${minor}`
}
private async installedSdks() {
const sdksPath = path.join(program86(), 'Windows Kits', '10', 'Include')
try {
const dirs = await fs.readdir(sdksPath, {withFileTypes: true})
return dirs
.filter(dirent => dirent.isDirectory())
.map(dirent => {
const parts = dirent.name.split('.')
return parts.length >= 3 ? parts.slice(0, 3).join('.') : dirent.name
})
.filter(version => semver.valid(version))
.sort(semver.rcompare)
} catch (error) {
core.warning(`Unable to get installed SDKs due to: "${error}"`)
return []
}
}
private async vsRequirement(arch: string) {
const componentsStr = core.getInput(INPUT_VISUAL_STUDIO_COMPONENTS)
const providedComponents = componentsStr ? componentsStr.split(';') : []
const winsdkComponent = providedComponents.find(component => {
return (
(VISUAL_STUDIO_WINSDK_COMPONENT_REGEX.exec(component)?.length ?? 0) > 1
)
})
let compArch: string
switch (arch) {
case 'arm64':
case 'arm':
case 'arm64ec':
compArch = arch.toUpperCase()
break
default:
compArch = 'x86.x64'
break
}
const vsComponents = [
`Microsoft.VisualStudio.Component.VC.Tools.${compArch}`,
...providedComponents
]
if (!winsdkComponent) {
vsComponents.push(await this.winsdk())
}
return {
version: '16',
swift: this.version,
components: vsComponents
}
}
protected async download(arch: string) {
let vsSetupAction = Promise.resolve({})
const vsComponents = core.getInput(INPUT_VISUAL_STUDIO_COMPONENTS)
if (
vsComponents.length ||
core.getBooleanInput(INPUT_PREFER_VISUAL_STUDIO_LINKER)
) {
core.debug(
`Using VS requirement ${JSON.stringify(this.vsRequirement(arch))}`
)
vsSetupAction = VisualStudio.setup(await this.vsRequirement(arch))
}
const [, toolchain] = await Promise.all([
vsSetupAction,
super.download(arch)
])
const exeFile = `${toolchain}.exe`
await fs.rename(toolchain, exeFile)
core.debug(`Toolchain installer downloaded at "${exeFile}"`)
return exeFile
}
protected async unpack(exe: string, arch: string) {
core.debug(`Unpacking for architecture "${arch}"`)
const version = this.version?.version ?? ''
const installation = await Installation.install(exe, version)
return installation instanceof Installation ? installation.location : ''
}
protected async add(installLocation: string, arch: string) {
const swiftVersion = this.version?.version ?? ''
const installation = await Installation.get(swiftVersion, installLocation)
const sdkrootKey = 'SDKROOT'
let sdkroot: string | undefined
if (installation instanceof Installation) {
sdkroot = installation?.sdkroot
core.exportVariable(sdkrootKey, sdkroot)
if (installation.devdir) {
core.exportVariable('DEVELOPER_DIR', installation.devdir)
}
const location = installation.location
const swiftPath = path.join(installation.toolchain, 'usr', 'bin')
const swiftDev = path.join(location, 'Swift-development', 'bin')
const icu67 = path.join(location, 'icu-67', 'usr', 'bin')
const tools = path.join(location, 'Tools')
const runtimePath = path.join(installation.runtime, 'usr', 'bin')
const requirePaths = [swiftPath, swiftDev, icu67, tools, runtimePath]
for (const envPath of requirePaths) {
try {
await fs.access(envPath)
core.debug(`Adding "${envPath}" to PATH`)
core.addPath(envPath)
} catch {
core.debug(`"${envPath}" doesn't exist. Skip adding to PATH`)
}
}
core.debug(`Swift installed at "${swiftPath}"`)
} else if (installation instanceof CustomInstallation) {
sdkroot = installation.variables[sdkrootKey]
}
if (!sdkroot) {
core.warning(
`SDKROOT was not set after installation at ${installLocation}; Visual Studio/Swift environment setup cannot continue and the installation may be unusable.`
)
return
}
const version = await this.installedSwiftVersion()
if (
semver.lte(semver.coerce(version) ?? version, '5.9.0') ||
core.getBooleanInput(INPUT_PREFER_VISUAL_STUDIO_LINKER)
) {
const visualStudio = await VisualStudio.setup(
await this.vsRequirement(arch)
)
await visualStudio.update(sdkroot)
}
if (core.getBooleanInput(INPUT_UPDATE_SDK_MODULES)) {
await updateSdkModules(sdkroot)
}
const swiftFlags = [
'-sdk',
sdkroot,
'-I',
path.join(sdkroot, 'usr', 'lib', 'swift'),
'-L',
path.join(sdkroot, 'usr', 'lib', 'swift', 'windows')
].join(' ')
core.exportVariable('SWIFTFLAGS', swiftFlags)
}
}