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
|
/*
pkgit - package it!
Copyright (C) 2026 dacctal
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include "build.h"
#include "globs.h"
#include "log.h"
#include "pkg.h"
#include "pkgit_lua.h"
bool target_build(lua_State *L, char* lua_file, str *target) {
if (!lua_try_table(L, lua_file, target->data)) return false;
if (lua_try_table(L, lua_file, "dependencies")) {
lua_pushnil(L);
install_dependencies(L);
}
lua_pop(L, 1);
if (!lua_try_function(L, lua_file, "build"))
lua_pop(L, 2);
return true;
}
bool repo_build(package_t *pkg) {
lua_getglobal(L, "repositories");
if (!lua_try_table(L, "init.lua", pkg->name.data)) {
lua_pop(L, 2);
return false;
}
if (lua_try_table(L, "init.lua", "dependencies")) {
lua_pushnil(L);
install_dependencies(L);
}
lua_pop(L, 1);
if (!lua_try_table(L, "init.lua", "targets")) {
lua_pop(L, 3);
return false;
}
bool target_success = target_build(L, "init.lua", &pkg->target);
lua_pop(L, 3);
return target_success;
}
bool bldit(package_t *pkg) {
init_bldit_state();
if (!bldit_loaded) return false;
lua_getglobal(B, "dependencies");
if (!lua_istable(B, -1)) {
bldit_isnt_type("dependencies", "table");
} else {
lua_pushnil(B);
install_dependencies(B);
}
lua_pop(B, 1);
lua_getglobal(B, "targets");
if (!lua_istable(B, -1)) {
bldit_isnt_type("targets", "table");
lua_pop(B, 3);
lua_close(B);
return false;
}
bool target_success = target_build(B, "bldit.lua", &pkg->target);
lua_pop(B, 2);
lua_close(B);
return target_success;
}
bool config_build(package_t *pkg) {
lua_getglobal(L, "build_systems");
if (!lua_istable(L, -1)) {
lua_isnt_type("build_systems", "table");
lua_pop(L, 1);
return false;
}
bool target_success = false;
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
str key = mstr(lua_tostring(L, -2));
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
str_free(&key);
continue;
}
str file_path = str_format("%.*s/%s", str_fmt(&pkg->src), key.data);
if (access(file_path.data, F_OK) != 0) {
lua_pop(L, 1);
str_free(&file_path);
str_free(&key);
continue;
}
str_free(&file_path);
str_free(&key);
if (!lua_try_table(L, "init.lua", "targets")) continue;
target_success = target_build(L, "init.lua", &pkg->target);
if (target_success) break;
lua_pop(L, 1);
}
lua_pop(L, 1);
return target_success;
}
bool build_loop(package_t *pkg) {
log_info("attempting init.lua: 'repositories.%.*s.build'", str_fmt(&pkg->name));
if (repo_build(pkg)) { return true; }
log_warn("failed init.lua: 'repositories.%.*s.build'", str_fmt(&pkg->name));
log_info("attempting bldit.lua");
if (bldit(pkg)) { return true; }
log_warn("failed bldit.lua");
log_info("attempting init.lua: 'build_systems'");
if (config_build(pkg)) { return true; }
log_warn("failed init.lua: 'build_systems'");
return false;
}
bool build(package_t *pkg) {
char cwd[MAX_PATH_LEN];
getcwd(cwd, MAX_PATH_LEN);
if (str_equal_cstr(&pkg->src, cwd) && !pkg->is_local) chdir(pkg->src.data);
if (build_loop(pkg)) return true;
log_error("no usable build system was found for %.*s", str_fmt(&pkg->name));
return false;
}
|