-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrelease.lua
More file actions
187 lines (162 loc) · 5.54 KB
/
release.lua
File metadata and controls
187 lines (162 loc) · 5.54 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
187
-- usage: xmake l release.lua
-- Add proxy: xmake g --proxy=ip:port
import("core.base.json")
import("core.base.global")
import("devel.git")
import("utils.archive")
function _get_current_commit_hash()
return os.iorunv("git rev-parse --short HEAD"):trim()
end
function _get_current_tag()
return os.iorunv("git describe --tags --abbrev=0"):trim()
end
-- @param llvm_archive string
function _get_require_libs(llvm_archive)
git.clone("https://github.com/clice-io/clice.git", {treeless = true})
local old_dir = os.cd("clice")
os.mkdir("package")
os.mkdir("package/backup")
archive.extract(llvm_archive, "package/llvm")
-- Use --project to specify the clice project and avoid xmake finding parent directory xmake.lua
local argv = {
"config",
"--yes",
"--clean",
"--project=.",
"--llvm=package/llvm",
}
if is_host("linux") then
table.insert(argv, "--toolchain=clang-20")
else
table.insert(argv, "--toolchain=clang")
end
if is_host("macosx") then
table.insert(argv, "--sdk=/opt/homebrew/opt/llvm@20")
end
os.vrunv(os.programfile(), argv)
os.vexecv(os.programfile(), {"--project=."})
local unused_libs = {}
local libs = table.join(os.files("build/.packages/**.lib"), os.files("build/.packages/**.a"))
for _, lib in ipairs(libs) do
printf("checking %s...", path.basename(lib))
os.vmv(lib, "package/backup")
-- Force xmake fetch package and avoid xmake using package cache
os.vrunv(os.programfile(), argv)
try
{
function ()
os.vrunv(os.programfile(), {"--project=."})
table.insert(unused_libs, path.basename(lib))
cprint("${bright red} unused.")
end,
catch
{
function (errors)
cprint("${bright green} require!")
os.vmv(path.join("package/backup", path.filename(lib)), path.directory(lib))
end
}
}
end
print("build %d libs, unused %d libs", #libs, #unused_libs)
os.cd(old_dir)
os.rm("clice")
return unused_libs
end
-- @param llvm_archive string
-- @param unused_libs array
-- @return archive_file string
function _reduce_package_size(llvm_archive, unused_libs)
local workdir = "build/.pack"
os.tryrm(workdir)
local archive_name = path.filename(llvm_archive)
print("extract ", archive_name)
archive.extract(llvm_archive, workdir)
print("handle ", archive_name)
-- we use dynamic lib for debug mode, and its deps are
-- different with release, so skip them now.
if llvm_archive:find("releasedbg") then
for _, lib in ipairs(unused_libs) do
os.rm(path.join(workdir, format("lib/%s.*", lib)))
end
end
local opt = {}
opt.recurse = true
opt.compress = "best"
opt.curdir = workdir
local archive_dirs
if is_host("windows") then
archive_dirs = "*"
elseif is_host("linux", "macosx") then
-- workaround for tar
archive_dirs = {}
for _, dir in ipairs(os.dirs(path.join(opt.curdir, "*"))) do
table.insert(archive_dirs, path.filename(dir))
end
end
print("archive ", archive_name)
os.mkdir("build/pack")
local archive_file = path.absolute(path.join("build/pack", archive_name))
import("utils.archive").archive(archive_file, archive_dirs, opt)
return archive_file
end
function main()
local envs = {}
if global.get("proxy") then
envs.HTTPS_PROXY = global.get("proxy")
end
local tag = _get_current_tag()
local current_commit = _get_current_commit_hash()
print("current tag: ", tag)
print("current commit: ", current_commit)
local dir = path.join(os.scriptdir(), "artifacts", current_commit)
os.mkdir(dir)
local workflow = os.host()
if is_host("macosx") then
workflow = "macos"
end
-- Get latest workflow id
local result = json.decode(os.iorunv(format("gh run list --json databaseId --limit 1 --workflow=%s.yml", workflow)))
for _, json in pairs(result) do
-- float -> int
local run_id = format("%d", json["databaseId"])
-- download all artifacts
os.execv("gh", {"run", "download", run_id, "--dir", dir}, {envs = envs})
end
local origin_files = {}
table.join2(origin_files, os.files(path.join(dir, "**.7z")))
table.join2(origin_files, os.files(path.join(dir, "**.tar.xz")))
local unused_libs
for _, llvm_archive in ipairs(origin_files) do
if llvm_archive:find("releasedbg") and llvm_archive:find("lto_n") then
unused_libs = _get_require_libs(path.absolute(llvm_archive))
break
end
end
if not unused_libs then
print("No unused libs?")
end
print(origin_files)
local files = {}
for _, llvm_archive in ipairs(origin_files) do
table.insert(files, _reduce_package_size(path.absolute(llvm_archive), unused_libs))
end
local binaries = {}
-- greater than 2 Gib?
for _, i in ipairs(files) do
local file = io.open(i, "r")
local size, error = file:size()
-- github release limit 2 Gib
if size > 2 * 1024 * 1024 * 1024 then
print("%s > 2 Gib, skip", path.filename(i))
print(file)
else
table.insert(binaries, i)
end
end
print(binaries)
-- clobber: overwrite
for _, binary in ipairs(binaries) do
os.execv("gh", {"release", "upload", tag, binary, "--clobber"}, {envs = envs})
end
end