-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluacmake.lua
More file actions
227 lines (200 loc) · 7.29 KB
/
luacmake.lua
File metadata and controls
227 lines (200 loc) · 7.29 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
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
require "init"
local work_dir = olua.pwd()
olua.print("work dir: ${work_dir}")
local function print_help()
olua.print([[
luacmake: no target to install
available options are:
install specify library to install
clean remove build and cache directory
--lua specify lua version
--output specify output path
--arch specify architecture
--parallel specify parallel jobs
example:
luacmake install cjson
luacmake install cjson --lua 5.4
luacmake install cjson --output ./output
]])
end
-------------------------------------------------------------------------------
-- parsing args
-------------------------------------------------------------------------------
local luacmake_command
local luacmake_jobs = ""
local luacmake_packages = {}
local luacmake_lua_version = "54"
local luacmake_output = "output"
local luacmake_arch = ""
local args = { ... }
while #args > 0 do
local c = table.remove(args, 1)
if c == "install" then
luacmake_command = c
local target = assert(table.remove(args, 1), "no install target")
luacmake_packages[#luacmake_packages + 1] = target
elseif c == "--arch" then
luacmake_arch = assert(table.remove(args, 1), "no architecture")
elseif c == "--lua" then
local version = assert(table.remove(args, 1), "no lua version")
luacmake_command = c
if version == "5.4" then
luacmake_lua_version = "54"
elseif version == "5.3" then
luacmake_lua_version = "53"
else
olua.error("unsupport lua version: ${version}")
end
elseif c == "--output" then
luacmake_command = c
luacmake_output = assert(table.remove(args, 1), "no install path")
elseif c == "clean" then
luacmake_command = c
olua.rmdir("${work_dir}/build")
olua.rmdir("${work_dir}/cache")
olua.rmdir("${work_dir}/output")
return
elseif c == "-j" then
local n = assert(table.remove(args, 1), "no jobs")
luacmake_jobs = olua.format("-j ${n}")
elseif not string.find(c, "^[%-]") and luacmake_command == "install" then
luacmake_packages[#luacmake_packages + 1] = c
else
olua.print("unknow args: ${c}")
print_help()
return
end
end
if #luacmake_packages == 0 then
print_help()
return
end
if not (string.find(luacmake_output, "^/") or string.find(luacmake_output, "^%w+:")) then
luacmake_output = olua.format("${work_dir}/${luacmake_output}")
end
olua.print("output dir: ${luacmake_output}")
luacmake_output = string.gsub(luacmake_output, "\\", "/")
-------------------------------------------------------------------------------
-- update luacmake
-------------------------------------------------------------------------------
olua.exec("git submodule init")
olua.exec("git submodule update")
-------------------------------------------------------------------------------
-- checkout and build targets
-------------------------------------------------------------------------------
olua.mkdir("${work_dir}/cache")
local luacmake_install_targets = olua.newarray("\n")
for _, target in ipairs(luacmake_packages) do
local package_name, package_git_dir, package_manifest, build_dir, source_dir
if target == "lua" then
package_name = olua.format("lua${luacmake_lua_version}")
package_git_dir = olua.format("${work_dir}/${package_name}")
package_manifest = { target = "lua luac", cmakeargs = "" }
else
package_name = olua.format("lua-${target}")
package_git_dir = olua.format("${work_dir}/cache/${package_name}/git")
package_manifest = olua.load_manifest("${work_dir}/package/${package_name}/manifest")
end
if not package_manifest then
olua.error("package '${target}' not found")
end
package_manifest.cmakeargs = assert(package_manifest).cmakeargs or ""
build_dir = olua.format("build/${package_name}")
source_dir = olua.format("cache/${package_name}")
if target == "lua" then
olua.mkdir("${work_dir}/cache/${package_name}")
else
assert(package_manifest)
olua.git_clone(
package_git_dir,
package_manifest.git,
package_manifest.branch,
package_manifest.commit
)
-- checkout dependencies
for _, v in ipairs(package_manifest.dependencies or {}) do
local name = string.match(v.git, "([^/]+)%.git$")
local git_dir = olua.format("${package_git_dir}/${name}")
olua.git_clone(
git_dir,
v.git,
v.branch,
v.commit
)
end
end
if luacmake_arch == "" then
if olua.os == "windows" then
luacmake_arch = "x64"
elseif olua.os == "macos" then
luacmake_arch = "x86_64;arm64"
end
end
local CMakeLists = olua.newarray("\n")
local lua_version = olua.format("lua${luacmake_lua_version}")
CMakeLists:pushf([[
cmake_minimum_required(VERSION 3.20)
project(luacmake)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
if(NOT LINUX)
set(LINUX TRUE)
endif()
endif()
if(APPLE)
set(CMAKE_OSX_ARCHITECTURES "${luacmake_arch}")
endif()
include(${work_dir}/tools/util.cmake)
]])
CMakeLists:push("")
CMakeLists:pushf([[
# lua
add_subdirectory(${work_dir}/${lua_version} lua)
get_property(LUA_INCLUDE_DIR TARGET liblua PROPERTY INCLUDE_DIRECTORIES)
set_target_properties(liblua PROPERTIES
LIBRARY_OUTPUT_NAME liblua
RUNTIME_OUTPUT_NAME ${lua_version}
)
set_target_properties(lua PROPERTIES
OUTPUT_NAME ${lua_version}
)
set_target_properties(luac PROPERTIES
OUTPUT_NAME luac${luacmake_lua_version}
)
]])
local lua_targets = ""
if target ~= "lua" then
CMakeLists:pushf([[
# package
set(LUACMAKE_SOURCE_DIR ${package_git_dir})
add_subdirectory(${work_dir}/package/${package_name} ${package_name})
]])
CMakeLists:push("")
else
lua_targets = "lua luac liblua"
end
olua.use(lua_targets, luacmake_jobs, lua_version)
CMakeLists:pushf([[
# install
install(
TARGETS
${package_manifest.target}
${lua_targets}
DESTINATION
${luacmake_output}
COMPONENT
luacmake
)
]])
olua.write("${work_dir}/cache/${package_name}/CMakeLists.txt", tostring(CMakeLists))
if olua.is_windows() then
olua.exec("cmake -B ${build_dir} -S ${source_dir} -A ${luacmake_arch} ${package_manifest.cmakeargs}")
olua.exec("cmake --build ${build_dir} --target ${package_manifest.target} --config Release")
else
olua.exec("cmake -B ${build_dir} -S ${source_dir} -DCMAKE_BUILD_TYPE=Release ${package_manifest.cmakeargs}")
olua.exec("cmake --build ${build_dir} --target ${package_manifest.target} ${luacmake_jobs}")
end
luacmake_install_targets:pushf("cmake --install ${build_dir} --component luacmake")
end
for _, v in ipairs(luacmake_install_targets) do
olua.exec(v)
end