-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.js
294 lines (224 loc) · 8.69 KB
/
main.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const core = require("@actions/core")
const exec = require("@actions/exec")
const io = require("@actions/io")
const tc = require("@actions/tool-cache")
const ch = require("@actions/cache")
const fsp = require("fs").promises
const notice = (msg) => core.notice(`gh-actions-lua: ${msg}`)
const warning = (msg) => core.warning(`gh-actions-lua: ${msg}`)
const path = require("path")
const BUILD_PREFIX = ".lua-build" // this is a temporary folder where lua will be built
const LUA_PREFIX = ".lua" // this is where Lua will be installed
const VERSION_ALIASES = {
"5.1": "5.1.5",
"5.2": "5.2.4",
"5.3": "5.3.6",
"5.4": "5.4.4",
"luajit": "luajit-2.1.0-beta3",
}
const isMacOS = () => (process.platform || "").startsWith("darwin")
const isWindows = () => (process.platform || "").startsWith("win32")
const exists = (filename, mode) => fsp.access(filename, mode).then(() => true, () => false)
// Returns posix path for path.join()
const pathJoin = path.posix.join
// Returns posix path for process.cwd()
const processCwd = () => {
return process.cwd().split(path.sep).join(path.posix.sep);
}
async function finish_luajit_install(src, dst, luajit) {
if (isWindows()) {
await fsp.copyFile(pathJoin(src, "lua51.dll"), pathJoin(dst, "bin", "lua51.dll"))
await exec.exec(`ln -s ${luajit} lua.exe`, undefined, {
cwd: pathJoin(dst, "bin")
})
} else {
await exec.exec(`ln -s ${luajit} lua`, undefined, {
cwd: pathJoin(dst, "bin")
})
}
}
async function install_luajit_openresty(luaInstallPath) {
const buildPath = path.join(process.env["RUNNER_TEMP"], BUILD_PREFIX)
const luaCompileFlags = core.getInput('luaCompileFlags')
await io.mkdirP(buildPath)
await exec.exec("git clone https://github.com/openresty/luajit2.git", undefined, {
cwd: buildPath
})
let finalCompileFlags = "-j"
if (isMacOS()) {
finalCompileFlags += " MACOSX_DEPLOYMENT_TARGET=10.15"
}
if (luaCompileFlags) {
finalCompileFlags += ` ${luaCompileFlags}`
}
await exec.exec(`make ${finalCompileFlags}`, undefined, {
cwd: pathJoin(buildPath, "luajit2"),
...(isWindows() ? { env: { SHELL: 'cmd' }} : {})
})
await exec.exec(`make -j install PREFIX="${luaInstallPath}"`, undefined, {
cwd: pathJoin(buildPath, "luajit2")
})
await finish_luajit_install(pathJoin(buildPath, "luajit2", "src"), luaInstallPath, "luajit")
}
async function install_luajit(luaInstallPath, luajitVersion) {
const luaExtractPath = pathJoin(process.env["RUNNER_TEMP"], BUILD_PREFIX, `LuaJIT-${luajitVersion}`)
const luaCompileFlags = core.getInput('luaCompileFlags')
const luaSourceTar = await tc.downloadTool(`https://luajit.org/download/LuaJIT-${luajitVersion}.tar.gz`)
await io.mkdirP(luaExtractPath)
await tc.extractTar(luaSourceTar, path.join(process.env["RUNNER_TEMP"], BUILD_PREFIX))
let finalCompileFlags = "-j"
if (isMacOS()) {
finalCompileFlags += " MACOSX_DEPLOYMENT_TARGET=10.15"
}
if (luaCompileFlags) {
finalCompileFlags += ` ${luaCompileFlags}`
}
await exec.exec(`make ${finalCompileFlags}`, undefined, {
cwd: luaExtractPath
})
await exec.exec(`make -j install PREFIX="${luaInstallPath}"`, undefined, {
cwd: luaExtractPath
})
await finish_luajit_install(pathJoin(luaExtractPath, "src"), luaInstallPath, `luajit-${luajitVersion}`)
}
async function msvc_link(luaExtractPath, linkCmd, outFile, objs) {
await exec.exec(linkCmd + " /out:" + outFile, objs, {
cwd: luaExtractPath
})
let manifest = outFile + ".manifest"
if (await exists(manifest)) {
await exec.exec("mt /nologo", ["-manifest", manifest, "-outputresource:" + outFile], {
cwd: luaExtractPath
})
}
}
async function install_files(dstDir, srcDir, files) {
await io.mkdirP(dstDir)
for (let file of files) {
await fsp.copyFile(pathJoin(srcDir, file), pathJoin(dstDir, path.posix.basename(file)))
}
}
async function install_plain_lua_windows(luaExtractPath, luaInstallPath, luaVersion) {
const luaCompileFlags = core.getInput('luaCompileFlags')
let cl = "cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE"
let objs = {
"lib": [],
"lua": [],
"luac": [],
}
let sources = {
"lua": [ "lua.c" ],
"luac": [ "luac.c", "print.c" ],
}
let src = pathJoin(luaExtractPath, "src")
await fsp.readdir(src).then(async (files) => {
for (let file of files) {
if (file.endsWith(".c")) {
let mode = sources["lua"].includes(file)
? "lua"
: sources["luac"].includes(file)
? "luac"
: "lib"
let srcName = pathJoin("src", file)
let args = (mode === "lib")
? [ "-DLUA_BUILD_AS_DLL", srcName ]
: [ srcName ]
objs[mode].push(file.replace(".c", ".obj"))
await exec.exec(cl, args, {
cwd: luaExtractPath
})
}
}
})
objs["lua"] = [ ...objs["lua"], ...objs["lib"] ]
objs["luac"] = [ ...objs["luac"], ...objs["lib"] ]
let luaXYZ = luaVersion.split(".")
let libFile = "lua" + luaXYZ[0] + luaXYZ[1] + ".lib"
let dllFile = "lua" + luaXYZ[0] + luaXYZ[1] + ".dll"
await msvc_link(luaExtractPath, "link /nologo /DLL", dllFile, objs["lib"]);
await msvc_link(luaExtractPath, "link /nologo", "luac.exe", objs["luac"]);
await msvc_link(luaExtractPath, "link /nologo", "lua.exe", objs["lua"]);
const luaHpp = (await exists(pathJoin(src, "lua.hpp"))) ? "lua.hpp" : "../etc/lua.hpp"
const headers = [ "lua.h", "luaconf.h", "lualib.h", "lauxlib.h", luaHpp ]
await install_files(pathJoin(luaInstallPath, "bin"), luaExtractPath, [ "lua.exe", "luac.exe" ])
await install_files(pathJoin(luaInstallPath, "lib"), luaExtractPath, [ dllFile, libFile ])
await install_files(pathJoin(luaInstallPath, "include"), src, headers)
}
async function install_plain_lua(luaInstallPath, luaVersion) {
const luaExtractPath = pathJoin(process.env["RUNNER_TEMP"], BUILD_PREFIX, `lua-${luaVersion}`)
const luaCompileFlags = core.getInput('luaCompileFlags')
const luaSourceTar = await tc.downloadTool(`https://lua.org/ftp/lua-${luaVersion}.tar.gz`)
await io.mkdirP(luaExtractPath)
await tc.extractTar(luaSourceTar, path.join(process.env["RUNNER_TEMP"], BUILD_PREFIX))
if (isWindows()) {
return await install_plain_lua_windows(luaExtractPath, luaInstallPath, luaVersion);
}
if (isMacOS()) {
await exec.exec("brew install readline ncurses")
} else {
await exec.exec("sudo apt-get install -q libreadline-dev libncurses-dev", undefined, {
env: {
DEBIAN_FRONTEND: "noninteractive",
TERM: "linux"
}
})
}
let finalCompileFlags = `-j ${isMacOS() ? "macosx" : "linux"}`
if (luaCompileFlags) {
finalCompileFlags += ` ${luaCompileFlags}`
}
await exec.exec(`make ${finalCompileFlags}`, undefined, {
cwd: luaExtractPath
})
await exec.exec(`make -j INSTALL_TOP="${luaInstallPath}" install`, undefined, {
cwd: luaExtractPath
})
}
async function install(luaInstallPath, luaVersion) {
if (luaVersion == "luajit-openresty") {
return await install_luajit_openresty(luaInstallPath)
}
if (luaVersion.startsWith("luajit-")) {
const luajitVersion = luaVersion.substr("luajit-".length)
return await install_luajit(luaInstallPath, luajitVersion)
}
return await install_plain_lua(luaInstallPath, luaVersion)
}
const makeCacheKey = (luaVersion, compileFlags) => `lua:${luaVersion}:${process.platform}:${process.arch}:${compileFlags}`
async function main() {
let luaVersion = core.getInput('luaVersion', { required: true })
if (VERSION_ALIASES[luaVersion]) {
luaVersion = VERSION_ALIASES[luaVersion]
}
const luaInstallPath = pathJoin(processCwd(), LUA_PREFIX)
let toolCacheDir = tc.find('lua', luaVersion)
if (!toolCacheDir) {
const cacheKey = makeCacheKey(luaVersion, core.getInput('luaCompileFlags') || "")
if (core.getInput('buildCache') == 'true') {
const restoredCache = await ch.restoreCache([luaInstallPath], cacheKey)
if (restoredCache) {
notice(`Cache restored: ${restoredCache}`)
} else {
notice(`No cache available, clean build`)
}
}
if (!(await exists(luaInstallPath))) {
await install(luaInstallPath, luaVersion)
try {
notice(`Storing into cache: ${cacheKey}`)
await ch.saveCache([luaInstallPath], cacheKey)
} catch (e) {
warning(`Failed to save to cache (continuing anyway): ${e}`)
}
}
toolCacheDir = await tc.cacheDir(luaInstallPath, 'lua', luaVersion)
}
// If .lua doesn't exist, symlink it to the tool cache dir
if (toolCacheDir && !(await exists(luaInstallPath))) {
await fsp.symlink(toolCacheDir, luaInstallPath);
}
core.addPath(pathJoin(luaInstallPath, "bin"))
}
main().catch(err => {
core.setFailed(`Failed to install Lua: ${err}`);
})