forked from chrisant996/clink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpremake5.lua
417 lines (351 loc) · 14.4 KB
/
premake5.lua
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
-- Copyright (c) 2012 Martin Ridgers
-- License: http://opensource.org/licenses/MIT
--------------------------------------------------------------------------------
local any_warnings_or_failures = nil
--------------------------------------------------------------------------------
local release_manifest = {
"clink.bat",
"clink.lua",
"clink_x*.exe",
"clink*.dll",
"CHANGES",
"LICENSE",
"clink_x*.pdb",
"clink_dll_x*.pdb",
"_default_settings",
"_default_inputrc",
}
--------------------------------------------------------------------------------
local function warn(msg)
print("\x1b[0;33;1mWARNING: " .. msg.."\x1b[m")
any_warnings_or_failures = true
end
--------------------------------------------------------------------------------
local function failed(msg)
print("\x1b[0;31;1mFAILED: " .. msg.."\x1b[m")
any_warnings_or_failures = true
end
--------------------------------------------------------------------------------
local exec_lead = "\n"
local function exec(cmd, silent)
print(exec_lead .. "## EXEC: " .. cmd)
if silent then
cmd = "1>nul 2>nul "..cmd
else
-- cmd = "1>nul "..cmd
end
-- Premake replaces os.execute() with a version that runs path.normalize()
-- which converts \ to /. This is fine for everything except cmd.exe.
local prev_norm = path.normalize
path.normalize = function (x) return x end
local _, _, ret = os.execute(cmd)
path.normalize = prev_norm
return ret == 0
end
--------------------------------------------------------------------------------
local function mkdir(dir)
if os.isdir(dir) then
return
end
local ret = exec("md " .. path.translate(dir), true)
if not ret then
error("Failed to create directory '" .. dir .. "' ("..tostring(ret)..")", 2)
end
end
--------------------------------------------------------------------------------
local function rmdir(dir)
if not os.isdir(dir) then
return
end
return exec("rd /q /s " .. path.translate(dir), true)
end
--------------------------------------------------------------------------------
local function unlink(file)
return exec("del /q " .. path.translate(file), true)
end
--------------------------------------------------------------------------------
local function copy(src, dest)
src = path.translate(src)
dest = path.translate(dest)
return exec("copy /y " .. src .. " " .. dest, true)
end
--------------------------------------------------------------------------------
local function rename(src, dest)
src = path.translate(src)
return exec("ren " .. src .. " " .. dest, true)
end
--------------------------------------------------------------------------------
local function file_exists(name)
local f = io.open(name, "r")
if f ~= nil then
io.close(f)
return true
end
return false
end
--------------------------------------------------------------------------------
local function have_required_tool(name, fallback)
if exec("where " .. name, true) then
return name
end
if fallback then
local t
if type(fallback) == "table" then
t = fallback
else
t = { fallback }
end
for _,dir in ipairs(t) do
local file = dir.."\\"..name..".exe"
if file_exists(file) then
return '"'..file..'"'
end
end
end
return nil
end
--------------------------------------------------------------------------------
newaction {
trigger = "nsis",
description = "Clink: Creates a (debug) installer for Clink",
execute = function()
local premake = _PREMAKE_COMMAND
local root_dir = path.getabsolute(".build/vs2019/bin").."/"
local code_dir = path.getabsolute(".").."/"
exec_lead = ""
if not os.isdir(code_dir.."clink") or not os.isdir(code_dir.."readline") or not os.isdir(code_dir.."lua") then
error(code_dir.." does not appear to be a Clink repo root directory.");
end
-- Check we have the tools we need.
local have_msbuild = have_required_tool("msbuild", { "c:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin", "c:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\Current\\Bin" })
local have_nsis = have_required_tool("makensis", "c:\\Program Files (x86)\\NSIS")
if not have_msbuild then error("MSBUILD NOT FOUND.") end
if not have_nsis then error("NSIS NOT FOUND.") end
-- Build the code.
local x86_ok = true
local x64_ok = true
local toolchain = "ERROR"
local build_code = function (target)
target = target or "build"
toolchain = _OPTIONS["vsver"] or "vs2019"
os.chdir(".build/" .. toolchain)
x86_ok = exec(have_msbuild .. " /m /v:q /p:configuration=debug /p:platform=win32 clink.sln /t:" .. target)
x64_ok = exec(have_msbuild .. " /m /v:q /p:configuration=debug /p:platform=x64 clink.sln /t:" .. target)
os.chdir("../..")
end
build_code()
local src = path.getabsolute(".build/" .. toolchain .. "/bin/debug").."/"
-- Do a coarse check to make sure there's a build available.
if not os.isdir(src .. ".") or not (x86_ok or x64_ok) then
error("There's no build available in '" .. src .. "'")
end
-- Now we can extract the version from the executables.
local version = nil
local clink_exe = x86_ok and "clink_x86.exe" or "clink_x64.exe"
local ver_cmd = src:gsub("/", "\\")..clink_exe.." --version"
for line in io.popen(ver_cmd):lines() do
version = line
end
if not version then
error("Failed to extract version from build executables")
end
local docversion = version:match("%d+%.%d+%.%d+")
-- Create the output directory.
local dest = path.getabsolute(".build/nsis").."/"
rmdir(dest)
mkdir(dest)
-- Copy release files to a directory.
for _, mask in ipairs(release_manifest) do
local from = src
if mask == "CHANGES" or mask == "LICENSE" or mask == "_default_settings" or mask == "_default_inputrc" then
from = code_dir
elseif mask:sub(-4) == ".pdb" then
from = nil
end
if from then
copy(from .. mask, dest)
end
end
-- Generate documentation.
print()
exec(premake .. " docs --docver="..docversion)
copy(".build/docs/clink.html", dest)
-- Build the installer.
local nsis_ok = false
if have_nsis then
local nsis_cmd = have_nsis
nsis_cmd = nsis_cmd .. " /DCLINK_BUILD=" .. dest
nsis_cmd = nsis_cmd .. " /DCLINK_VERSION=" .. version
nsis_cmd = nsis_cmd .. " /DCLINK_SOURCE=" .. code_dir
nsis_cmd = nsis_cmd .. " " .. code_dir .. "/installer/clink.nsi"
nsis_ok = exec(nsis_cmd)
if nsis_ok then
rename(dest.."_setup.exe", "clink_setup.exe")
end
end
-- Report some facts about what just happened.
print("\n\n")
if not nsis_ok then warn("INSTALLER PACKAGE FAILED") end
if not x86_ok then failed("x86 BUILD FAILED") end
if not x64_ok then failed("x64 BUILD FAILED") end
end
}
--------------------------------------------------------------------------------
newaction {
trigger = "release",
description = "Clink: Creates a release of Clink",
execute = function ()
local premake = _PREMAKE_COMMAND
local root_dir = path.getabsolute(".build/release").."/"
-- Check we have the tools we need.
local have_msbuild = have_required_tool("msbuild", { "c:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin", "c:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\Current\\Bin" })
local have_mingw = have_required_tool("mingw32-make")
local have_nsis = have_required_tool("makensis", "c:\\Program Files (x86)\\NSIS")
local have_7z = have_required_tool("7z", { "c:\\Program Files\\7-Zip", "c:\\Program Files (x86)\\7-Zip" })
-- Clone repo in release folder and checkout the specified version
local code_dir = root_dir.."~working/"
rmdir(code_dir)
mkdir(code_dir)
exec("git clone . " .. code_dir)
if not os.chdir(code_dir) then
error("Failed to chdir to '" .. code_dir .. "'")
end
exec("git checkout " .. (_OPTIONS["commit"] or "HEAD"))
-- Build the code.
local x86_ok = true
local x64_ok = true
local toolchain = "ERROR"
local build_code = function (target)
if have_msbuild then
target = target or "build"
toolchain = _OPTIONS["vsver"] or "vs2019"
exec(premake .. " " .. toolchain)
os.chdir(".build/" .. toolchain)
x86_ok = exec(have_msbuild .. " /m /v:q /p:configuration=final /p:platform=win32 clink.sln /t:" .. target)
x64_ok = exec(have_msbuild .. " /m /v:q /p:configuration=final /p:platform=x64 clink.sln /t:" .. target)
os.chdir("../..")
elseif have_mingw then
target = target or "build"
toolchain = "gmake"
exec(premake .. " " .. toolchain)
os.chdir(".build/" .. toolchain)
x86_ok = exec(have_mingw .. " CC=gcc config=final_x32 -j%number_of_processors% " .. target)
x64_ok = exec(have_mingw .. " CC=gcc config=final_x64 -j%number_of_processors% " .. target)
os.chdir("../..")
else
error("Unable to locate either msbuild.exe or mingw32-make.exe")
end
end
-- Build everything.
build_code("luac")
exec(premake .. " embed")
build_code()
local src = path.getabsolute(".build/" .. toolchain .. "/bin/final").."/"
-- Do a coarse check to make sure there's a build available.
if not os.isdir(src .. ".") or not (x86_ok or x64_ok) then
error("There's no build available in '" .. src .. "'")
end
-- Run tests.
if x86_ok then
test_exe = path.translate(src.."/clink_test_x86.exe")
if not exec(test_exe) then
error("x86 tests failed")
end
end
if x64_ok then
test_exe = path.translate(src.."/clink_test_x64.exe")
if not exec(test_exe) then
error("x64 tests failed")
end
end
-- Now we can extract the version from the executables.
local version = nil
local clink_exe = x86_ok and "clink_x86.exe" or "clink_x64.exe"
local ver_cmd = src:gsub("/", "\\")..clink_exe.." --version"
for line in io.popen(ver_cmd):lines() do
version = line
end
if not version then
error("Failed to extract version from build executables")
end
local docversion = version:match("%d+%.%d+%.%d+")
-- Now we know the version we can create our output directory.
local target_dir = root_dir..os.date("%Y%m%d_%H%M%S").."_"..version.."/"
rmdir(target_dir)
mkdir(target_dir)
local clink_suffix = "clink."..version
local dest = target_dir..clink_suffix.."/"
mkdir(dest)
-- Copy release files to a directory.
for _, mask in ipairs(release_manifest) do
local from = src
if mask == "_default_settings" or mask == "_default_inputrc" then
from = code_dir
end
copy(from .. mask, dest)
end
-- Generate documentation.
exec(premake .. " docs --docver="..docversion)
copy(".build/docs/clink.html", dest)
-- Build the installer.
local nsis_ok = false
if have_nsis then
local nsis_cmd = have_nsis
nsis_cmd = nsis_cmd .. " /DCLINK_BUILD=" .. path.getabsolute(dest)
nsis_cmd = nsis_cmd .. " /DCLINK_VERSION=" .. version
nsis_cmd = nsis_cmd .. " /DCLINK_SOURCE=" .. code_dir
nsis_cmd = nsis_cmd .. " " .. code_dir .. "/installer/clink.nsi"
nsis_ok = exec(nsis_cmd)
end
-- Tidy up code directory.
rmdir(".build")
rmdir(".git")
unlink(".gitignore")
-- Zip up the source code.
os.chdir("..")
local src_dir_name = target_dir..clink_suffix.."_src"
exec("move ~working " .. src_dir_name)
if have_7z then
os.chdir(src_dir_name)
exec(have_7z .. " a -r " .. target_dir..clink_suffix .. "_src.zip .")
end
os.chdir(target_dir)
rmdir(src_dir_name)
-- Package the release and the pdbs separately.
os.chdir(dest)
if have_7z then
if have_msbuild then
exec(have_7z .. " a -r ../"..clink_suffix .. "_symbols.zip *.pdb")
end
exec(have_7z .. " a -x!*.pdb -r ../"..clink_suffix .. ".zip *")
end
-- Report some facts about what just happened.
print("\n\n")
if not have_7z then warn("7-ZIP NOT FOUND -- Packing to .zip files was skipped.") end
if not have_nsis then warn("NSIS NOT FOUND -- No installer was created.") end
if not nsis_ok then warn("INSTALLER PACKAGE FAILED") end
if not x86_ok then failed("x86 BUILD FAILED") end
if not x64_ok then failed("x64 BUILD FAILED") end
if not any_warnings_or_failures then
print("\x1b[0;32;1mRelease "..version.." built successfully.\x1b[m")
end
end
}
--------------------------------------------------------------------------------
newoption {
trigger = "vsver",
value = "VER",
description = "Clink: Version of Visual Studio to build release with"
}
--------------------------------------------------------------------------------
newoption {
trigger = "docver",
value = "DOCVER",
description = "Clink: Clink version to inject in documentation"
}
--------------------------------------------------------------------------------
newoption {
trigger = "commit",
value = "SPEC",
description = "Clink: Git commit/tag to build Clink release from"
}