Repository navigation
Expand file tree
/
Copy pathbuild.c
More file actions
395 lines (371 loc) · 11.5 KB
/
Copy pathbuild.c
File metadata and controls
395 lines (371 loc) · 11.5 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
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
/* The advanced layer (src/sx_adv.c, the sx_reg_* regex family) links
PCRE2, and every test compiles the library sources directly, so
pcre2-8 is attached to every test target here — via forge's
reserved per-test DSL overrides (LANGUAGE / TOOLCHAIN expand inside
each test target block; see build.h "test DSL default macros").
Must be #defined before #include <build.h> (the guards).
Tests disable LTO because the pinned Forge emits GCC-only -flto=N
for parallel Clang links. Production library targets keep LTO. */
#define LANGUAGE \
do { \
set_language(C); \
set(TARGET_LTO, 1, DISABLE); \
add_link_lib("pcre2-8"); \
} while (0)
#ifndef CONF_compiler
#define CONF_compiler GCC
#endif
#define TOOLCHAIN set_toolchain(CONF_compiler)
#include <build.h>
#define STR_IMPL(tok) #tok
#define STR(tok) STR_IMPL(tok)
#ifndef CONF_version
#define SX_VERSION "dev"
#else
#define SX_VERSION STR(CONF_version)
#endif
function(check) {
parse_args(builtin_args);
target("stringx") {
set_toolchain(CONF_compiler);
add_sources_r("src");
set_type(FORGE_SHARED_LIB);
add_include_path("include");
add_link_lib("pcre2-8");
set_visibility(HIDDEN);
set_optimization(OPT_AGGRESSIVE);
set_standard("c23");
set_output_path("build");
add(COMPILER_OPTIONS, 4, "-Wall", "-Wextra", "-Wpedantic", "-Werror");
if (CONF_compiler == GCC)
add(COMPILER_OPTIONS, 1, "-fanalyzer");
set_output_intermediate_path("build/exe-obj");
return compile();
}
}
function(build) {
if (os_mkdir_r("build/output/include") != 0)
return -1;
{
int n = 0;
char **names = os_listdir("include", &n);
if (!names)
return -1;
for (int i = 0; i < n; i++) {
if (!strstr(names[i], ".h"))
continue;
char *src = os_path_join("include", names[i]);
char *dst = os_path_join("build/output/include", names[i]);
int r = (!src || !dst) ? -1 : os_copy_file(src, dst);
free(src);
free(dst);
if (r != 0) {
for (int j = 0; j < n; j++)
free(names[j]);
free(names);
return -1;
}
}
for (int i = 0; i < n; i++)
free(names[i]);
free(names);
}
target("stringx") {
set_toolchain(CONF_compiler);
add_sources_r("src");
set_type(FORGE_SHARED_LIB);
add_include_path("include");
add_link_lib("pcre2-8");
set_visibility(HIDDEN);
set_optimization(OPT_AGGRESSIVE);
set_standard("c23");
set_output_path("build");
set_output_intermediate_path("build/exe-obj");
return compile();
}
return 0;
}
/* ---------------- install / gen_deb (Linux only) ----------------
* Same pattern as the forge project itself: install copies the
* artifacts (shared library, headers, license/notices) under a
* prefix; gen_deb stages the same tree into a Debian package built
* with tar/ar (no dpkg-deb dependency),
* build/deb/libstringx_<ver>_<arch>.deb.
* Effective only on Linux (guarded by the LINUX compile-time macro
* from build.h); elsewhere the function bodies are a runtime error +
* return -1 (deliberately not #error, so build.c still compiles
* everywhere). */
#if LINUX
/* Recursive copy of every entry under src_dir into dst_dir
(os_copy_file handles files only). Used by install_to. */
static int copy_dir_r(const char *src_dir, const char *dst_dir) {
if (os_mkdir_r(dst_dir) != 0)
return -1;
int n = 0;
char **names = os_listdir(src_dir, &n);
if (!names)
return -1;
int ret = 0;
for (int i = 0; i < n && ret == 0; i++) {
char *s = os_path_join(src_dir, names[i]);
char *d = os_path_join(dst_dir, names[i]);
if (!s || !d) {
ret = -1;
} else if (os_path_is_dir(s)) {
ret = copy_dir_r(s, d);
} else if (os_copy_file(s, d) != 0) {
ret = -1;
}
free(s);
free(d);
}
for (int i = 0; i < n; i++)
free(names[i]);
free(names);
return ret;
}
/* Copy the built artifacts to the system paths under prefix (shared
by install and gen_deb's data root): build/output/libstringx.so ->
<prefix>/lib, the synced headers (build/output/include) ->
<prefix>/include, and LICENSE + THIRD_PARTY_NOTICES.md ->
<prefix>/share/doc/libstringx (LICENSE also as the Debian `copyright`).
`pc_prefix` is the prefix baked into lib/pkgconfig/libstringx.pc: the
dpkg data tree is staged under build/deb/data/usr but the package is
INSTALLED at the FHS root, so gen_deb passes "/usr" while a plain
install passes the real prefix (the staging path must never leak
into the .pc). Returns 0 on success. */
static int install_to(const char *prefix, const char *pc_prefix) {
int ret = 0;
char *lib = os_path_join(prefix, "lib");
char *inc = os_path_join(prefix, "include");
char *doc = os_path_join(prefix, "share/doc/libstringx");
if (!lib || !inc || !doc || os_mkdir_r(lib) != 0 || os_mkdir_r(inc) != 0 ||
os_mkdir_r(doc) != 0) {
free(lib);
free(inc);
free(doc);
return -1;
}
char *dst = os_path_join(lib, "libstringx.so");
if (os_copy_file("build/output/libstringx.so", dst) != 0)
ret = -1;
free(dst);
if (copy_dir_r("build/output/include", inc) != 0)
ret = -1;
char *f = os_path_join(doc, "LICENSE");
if (os_copy_file("LICENSE", f) != 0)
ret = -1;
free(f);
f = os_path_join(doc, "THIRD_PARTY_NOTICES.md");
if (os_copy_file("THIRD_PARTY_NOTICES.md", f) != 0)
ret = -1;
free(f);
f = os_path_join(doc, "copyright"); /* Debian's conventional name */
if (os_copy_file("LICENSE", f) != 0)
ret = -1;
free(f);
/* pkg-config metadata: <prefix>/lib/pkgconfig/libstringx.pc. The
baked-in prefix is pc_prefix (see above), NOT the staging prefix.
Version comes from build.conf (SX_VERSION), leading v stripped. */
{
const char *v = SX_VERSION;
if (*v == 'v')
v++;
char *pcdir = os_path_join(lib, "pkgconfig");
char *pc = os_path_join(pcdir, "libstringx.pc");
if (!pcdir || !pc || os_mkdir_r(pcdir) != 0) {
ret = -1;
} else {
FILE *f = fopen(pc, "w");
if (!f)
ret = -1;
else {
fprintf(f,
"prefix=%s\n"
"exec_prefix=${prefix}\n"
"libdir=${prefix}/lib\n"
"includedir=${prefix}/include\n"
"\n"
"Name: libstringx\n"
"Description: String X - a small C string library with a "
"single-header API and a byte-exact, zero-copy view model\n"
"Version: %s\n"
"Libs: -L${libdir} -lstringx\n"
"Cflags: -I${includedir}\n",
pc_prefix, v);
fclose(f);
}
}
free(pcdir);
free(pc);
}
free(lib);
free(inc);
free(doc);
return ret;
}
/* uname -m -> Debian architecture name: x86_64->amd64,
aarch64->arm64; anything else is kept as-is */
static const char *deb_arch(void) {
static char buf[64];
char *out = os_shell((char *[]){"uname", "-m", NULL});
if (!out)
return "amd64";
char *s = out;
size_t len = strlen(s);
while (len > 0 && (s[len - 1] == '\n' || s[len - 1] == '\r'))
s[--len] = '\0';
const char *m = s;
if (strcmp(m, "x86_64") == 0)
m = "amd64";
else if (strcmp(m, "aarch64") == 0)
m = "arm64";
snprintf(buf, sizeof(buf), "%s", m);
free(out);
return buf;
}
/* Packaging helper: tar/ar run directly via argv (status captured);
returns -1 on failure */
static int run_quiet(char **argv) {
int st = -1;
free(os_execute_capture_all_status(argv[0], argv, "", NULL, &st));
return st == 0 ? 0 : -1;
}
#endif /* LINUX */
function(install) {
parse_args();
const char *prefix =
(argc > 1 && argv[1] && *argv[1]) ? argv[1] : "/usr/local";
#if LINUX
/* Ensure the artifacts (including the header sync) are fresh first */
int r = function_build(argc, argv);
if (r != 0)
return r;
r = install_to(prefix, prefix);
if (r != 0)
fprintf(stderr,
"forge: install to %s failed\n"
" (check write permission, or pass a prefix you own, e.g. "
"make install ~/.local)\n",
prefix);
return r;
#else
(void)prefix;
fprintf(stderr, "forge: install is only supported on Linux\n");
return -1;
#endif
}
function(gen_deb) {
parse_args();
#if LINUX
/* Version: SX_VERSION from build.conf (single source of truth),
leading v stripped ("v0.5" -> "0.5") */
const char *v = SX_VERSION;
if (*v == 'v')
v++;
char ver[64];
snprintf(ver, sizeof(ver), "%s", v);
const char *arch = deb_arch();
int ret = function_build(argc, argv); /* ensure artifacts are fresh first */
if (ret != 0)
return ret;
/* Layout:
build/deb/
debian-binary data/ control-dir/control
control.tar.gz data.tar.gz libstringx_<ver>_<arch>.deb */
char *work = os_path_join("build", "deb");
char *data = os_path_join(work, "data");
char *cdir = os_path_join(work, "control-dir");
if (!work || !data || !cdir) {
free(work);
free(data);
free(cdir);
return -1;
}
os_remove_r(work);
if (os_mkdir_r(data) != 0 || os_mkdir_r(cdir) != 0) {
free(work);
free(data);
free(cdir);
return -1;
}
/* Data root = reuse install's copy logic. The deb data tree
mirrors the filesystem root: packages install under /usr (FHS),
so the copy root is <data>/usr — lib/libstringx.so, the headers,
share/doc/libstringx (LICENSE, notices, copyright). */
char *data_usr = os_path_join(data, "usr");
if (!data_usr) {
free(work);
free(data);
free(cdir);
return -1;
}
if (install_to(data_usr, "/usr") != 0) {
free(data_usr);
free(work);
free(data);
free(cdir);
return -1;
}
free(data_usr);
char *deb_bin = os_path_join(work, "debian-binary");
char *ctl = os_path_join(cdir, "control");
char *cgz = os_path_join(work, "control.tar.gz");
char *dgz = os_path_join(work, "data.tar.gz");
size_t deblen = strlen(work) + strlen(ver) + strlen(arch) + 28;
char *deb = malloc(deblen);
if (!deb_bin || !ctl || !cgz || !dgz || !deb) {
ret = -1;
goto cleanup;
}
snprintf(deb, deblen, "%s/libstringx_%s_%s.deb", work, ver, arch);
FILE *f = fopen(deb_bin, "w");
if (f) {
fputs("2.0\n", f);
fclose(f);
} else {
ret = -1;
}
f = fopen(ctl, "w");
if (f) {
fprintf(f,
"Package: libstringx\n"
"Version: %s\n"
"Section: libs\n"
"Priority: optional\n"
"Architecture: %s\n"
"Maintainer: Minelogy <minelogy-dev@users.noreply.github.com>\n"
"Depends: libc6, libpcre2-8-0\n"
"Description: libstringx - a small C string library with a single-\n"
" header API and a byte-exact, zero-copy view model\n",
ver, arch);
fclose(f);
} else {
ret = -1;
}
if (ret == 0) {
char *t1[] = {"tar", "-czf", cgz, "-C", cdir, "control", NULL};
char *t2[] = {"tar", "-czf", dgz, "-C", data, ".", NULL};
char *ar[] = {"ar", "rcs", deb, deb_bin, cgz, dgz, NULL};
if (run_quiet(t1) != 0 || run_quiet(t2) != 0 || run_quiet(ar) != 0)
ret = -1;
else
printf("forge: %s\n", deb);
}
cleanup:
free(deb_bin);
free(ctl);
free(cgz);
free(dgz);
free(deb);
free(work);
free(data);
free(cdir);
return ret;
#else
fprintf(stderr, "forge: gen_deb is only supported on Linux\n");
return -1;
#endif
}
set_default(build);
default_test();