forked from clibs/clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclib-configure.c
executable file
·705 lines (577 loc) · 15.8 KB
/
clib-configure.c
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//
// clib-configure.c
//
// Copyright (c) 2012-2021 clib authors
// MIT licensed
//
#include <curl/curl.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif
#ifdef HAVE_PTHREADS
#include <pthread.h>
#endif
#ifdef _WIN32
#include <direct.h>
#define getcwd _getcwd
#else
#include <unistd.h>
#endif
#include "common/clib-cache.h"
#include "common/clib-package.h"
#include "common/clib-settings.h"
#include <asprintf/asprintf.h>
#include <commander/commander.h>
#include <debug/debug.h>
#include <fs/fs.h>
#include <hash/hash.h>
#include <list/list.h>
#include <logger/logger.h>
#include <path-join/path-join.h>
#include <str-flatten/str-flatten.h>
#include <trim/trim.h>
#include "version.h"
#define PROGRAM_NAME "clib-configure"
#define SX(s) #s
#define S(s) SX(s)
#if defined(_WIN32) || defined(WIN32) || defined(__MINGW32__) || \
defined(__MINGW64__)
#define setenv(k, v, _) _putenv_s(k, v)
#define realpath(a, b) _fullpath(a, b, strlen(a))
#endif
typedef struct options options_t;
struct options {
const char *dir;
char *prefix;
int force;
int verbose;
int dev;
int skip_cache;
int flags;
int global;
#ifdef HAVE_PTHREADS
unsigned int concurrency;
#endif
};
clib_package_opts_t package_opts = {0};
clib_package_t *root_package = 0;
hash_t *configured = 0;
command_t program = {0};
debug_t debugger = {0};
char **rest_argv = 0;
int rest_offset = 0;
int rest_argc = 0;
options_t opts = {.skip_cache = 0,
.verbose = 1,
.force = 0,
.dev = 0,
#ifdef HAVE_PTHREADS
.concurrency = MAX_THREADS,
#endif
#ifdef _WIN32
.dir = ".\\deps"
#else
.dir = "./deps"
#endif
};
int configure_package(const char *dir);
#ifdef HAVE_PTHREADS
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct clib_package_thread clib_package_thread_t;
struct clib_package_thread {
const char *dir;
};
void *configure_package_with_manifest_name_thread(void *arg) {
clib_package_thread_t *wrap = arg;
const char *dir = wrap->dir;
configure_package(dir);
return 0;
}
#endif
int configure_package_with_manifest_name(const char *dir, const char *file) {
clib_package_t *package = 0;
char *json = NULL;
int ok = 0;
int rc = 0;
#ifdef PATH_MAX
long path_max = PATH_MAX;
#elif defined(_PC_PATH_MAX)
long path_max = pathconf(dir, _PC_PATH_MAX);
#else
long path_max = 4096;
#endif
char *path = path_join(dir, file);
if (0 == path) {
return -ENOMEM;
}
#ifdef HAVE_PTHREADS
pthread_mutex_lock(&mutex);
#endif
if (!root_package) {
const char *name = NULL;
unsigned int i = 0;
do {
name = manifest_names[i];
json = fs_read(name);
} while (NULL != manifest_names[++i] && !json);
if (json) {
root_package = clib_package_new(json, opts.verbose);
}
if (root_package && root_package->prefix) {
char prefix[path_max];
memset(prefix, 0, path_max);
realpath(root_package->prefix, prefix);
unsigned long int size = strlen(prefix) + 1;
free(root_package->prefix);
root_package->prefix = malloc(size);
memset((void *)root_package->prefix, 0, size);
memcpy((void *)root_package->prefix, prefix, size);
}
}
if (hash_has(configured, path)) {
#ifdef HAVE_PTHREADS
pthread_mutex_unlock(&mutex);
#endif
goto cleanup;
}
#ifdef HAVE_PTHREADS
pthread_mutex_unlock(&mutex);
#endif
// Free the json if it was allocated before attempting to modify it
free(json);
json = NULL;
if (0 == fs_exists(path)) {
debug(&debugger, "read %s", path);
json = fs_read(path);
}
if (0 != json) {
#ifdef DEBUG
package = clib_package_new(json, 1);
#else
package = clib_package_new(json, 0);
#endif
} else {
#ifdef DEBUG
package = clib_package_new_from_slug(dir, 1);
#else
package = clib_package_new_from_slug(dir, 0);
#endif
}
if (0 == package) {
rc = -ENOMEM;
goto cleanup;
}
if (0 != package->flags && opts.flags) {
#ifdef HAVE_PTHREADS
rc = pthread_mutex_lock(&mutex);
#endif
hash_set(configured, path, "t");
ok = 1;
fprintf(stdout, "%s ", trim(package->flags));
fflush(stdout);
} else if (0 != package->configure) {
char *command = 0;
char *args = rest_argc > 0
? str_flatten((const char **)rest_argv, 0, rest_argc)
: "";
asprintf(&command, "cd %s && %s %s", dir, package->configure, args);
if (root_package && root_package->prefix) {
package_opts.prefix = root_package->prefix;
clib_package_set_opts(package_opts);
setenv("PREFIX", package_opts.prefix, 1);
} else if (opts.prefix) {
setenv("PREFIX", opts.prefix, 1);
} else if (package->prefix) {
char prefix[path_max];
memset(prefix, 0, path_max);
realpath(package->prefix, prefix);
unsigned long int size = strlen(prefix) + 1;
free(package->prefix);
package->prefix = malloc(size);
memset((void *)package->prefix, 0, size);
memcpy((void *)package->prefix, prefix, size);
setenv("PREFIX", package->prefix, 1);
}
if (rest_argc > 0) {
free(args);
}
if (0 != opts.verbose) {
logger_warn("configure", "%s: %s", package->name, package->configure);
}
debug(&debugger, "system: %s", command);
rc = system(command);
free(command);
command = 0;
#ifdef HAVE_PTHREADS
rc = pthread_mutex_lock(&mutex);
#endif
hash_set(configured, path, "t");
ok = 1;
} else {
#ifdef HAVE_PTHREADS
rc = pthread_mutex_lock(&mutex);
#endif
hash_set(configured, path, "f");
ok = 1;
}
if (0 != rc) {
goto cleanup;
}
#ifdef HAVE_PTHREADS
pthread_mutex_unlock(&mutex);
#endif
if (0 != package->dependencies) {
list_iterator_t *iterator = 0;
list_node_t *node = 0;
#ifdef HAVE_PTHREADS
clib_package_thread_t wraps[opts.concurrency];
pthread_t threads[opts.concurrency];
unsigned int i = 0;
#endif
iterator = list_iterator_new(package->dependencies, LIST_HEAD);
while ((node = list_iterator_next(iterator))) {
clib_package_dependency_t *dep = node->val;
char *slug = 0;
asprintf(&slug, "%s/%s@%s", dep->author, dep->name, dep->version);
clib_package_t *dependency = clib_package_new_from_slug(slug, 0);
char *dep_dir = path_join(opts.dir, dependency->name);
free(slug);
clib_package_free(dependency);
#ifdef HAVE_PTHREADS
clib_package_thread_t *wrap = &wraps[i];
pthread_t *thread = &threads[i];
wrap->dir = dep_dir;
rc = pthread_create(thread, 0,
configure_package_with_manifest_name_thread, wrap);
if (++i >= opts.concurrency) {
for (int j = 0; j < i; ++j) {
pthread_join(threads[j], 0);
free((void *)wraps[j].dir);
}
i = 0;
}
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
if (!opts.flags) {
usleep(1024 * 10);
}
#endif
#else
if (0 == dep_dir) {
rc = -ENOMEM;
goto cleanup;
}
rc = configure_package(dep_dir);
free((void *)dep_dir);
if (0 != rc) {
goto cleanup;
}
#endif
}
#ifdef HAVE_PTHREADS
for (int j = 0; j < i; ++j) {
pthread_join(threads[j], 0);
free((void *)wraps[j].dir);
}
#endif
if (0 != iterator) {
list_iterator_destroy(iterator);
}
}
if (opts.dev && 0 != package->development) {
list_iterator_t *iterator = 0;
list_node_t *node = 0;
#ifdef HAVE_PTHREADS
clib_package_thread_t wraps[opts.concurrency];
pthread_t threads[opts.concurrency];
unsigned int i = 0;
#endif
iterator = list_iterator_new(package->development, LIST_HEAD);
while ((node = list_iterator_next(iterator))) {
clib_package_dependency_t *dep = node->val;
char *slug = 0;
asprintf(&slug, "%s/%s@%s", dep->author, dep->name, dep->version);
clib_package_t *dependency = clib_package_new_from_slug(slug, 0);
char *dep_dir = path_join(opts.dir, dependency->name);
free(slug);
clib_package_free(dependency);
#ifdef HAVE_PTHREADS
clib_package_thread_t *wrap = &wraps[i];
pthread_t *thread = &threads[i];
wrap->dir = dep_dir;
rc = pthread_create(thread, 0,
configure_package_with_manifest_name_thread, wrap);
if (++i >= opts.concurrency) {
for (int j = 0; j < i; ++j) {
pthread_join(threads[j], 0);
free((void *)wraps[j].dir);
}
i = 0;
}
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
if (!opts.flags) {
usleep(1024 * 10);
}
#endif
#else
if (0 == dep_dir) {
rc = -ENOMEM;
goto cleanup;
}
rc = configure_package(dep_dir);
free((void *)dep_dir);
if (0 != rc) {
goto cleanup;
}
#endif
}
#ifdef HAVE_PTHREADS
for (int j = 0; j < i; ++j) {
pthread_join(threads[j], 0);
free((void *)wraps[j].dir);
}
#endif
if (0 != iterator) {
list_iterator_destroy(iterator);
}
}
cleanup:
if (0 != package) {
clib_package_free(package);
}
if (0 != json) {
free(json);
}
if (0 == ok) {
if (0 != path) {
free(path);
}
}
return rc;
}
int configure_package(const char *dir) {
const char *name = NULL;
unsigned int i = 0;
int rc = 0;
do {
name = manifest_names[i];
rc = configure_package_with_manifest_name(dir, name);
} while (NULL != manifest_names[++i] && 0 != rc);
return rc;
}
static void setopt_skip_cache(command_t *self) {
opts.skip_cache = 1;
debug(&debugger, "set skip cache flag");
}
static void setopt_dev(command_t *self) {
opts.dev = 1;
debug(&debugger, "set dev flag");
}
static void setopt_force(command_t *self) {
opts.force = 1;
debug(&debugger, "set force flag");
}
static void setopt_global(command_t *self) {
opts.global = 1;
debug(&debugger, "set global flag");
}
static void setopt_flags(command_t *self) {
opts.flags = 1;
opts.verbose = 0;
debug(&debugger, "set flags flag");
}
static void setopt_prefix(command_t *self) {
opts.prefix = (char *)self->arg;
debug(&debugger, "set prefix: %s", opts.prefix);
}
static void setopt_dir(command_t *self) {
opts.dir = (char *)self->arg;
debug(&debugger, "set dir: %s", opts.dir);
}
static void setopt_quiet(command_t *self) {
opts.verbose = 0;
debug(&debugger, "set quiet flag");
}
#ifdef HAVE_PTHREADS
static void setopt_concurrency(command_t *self) {
if (self->arg) {
opts.concurrency = atol(self->arg);
debug(&debugger, "set concurrency: %lu", opts.concurrency);
}
}
#endif
int main(int argc, char **argv) {
int rc = 0;
#ifdef PATH_MAX
long path_max = PATH_MAX;
#elif defined(_PC_PATH_MAX)
long path_max = pathconf(opts.dir, _PC_PATH_MAX);
#else
long path_max = 4096;
#endif
char CWD[path_max];
memset(CWD, 0, path_max);
if (0 == getcwd(CWD, path_max)) {
return -errno;
}
configured = hash_new();
hash_set(configured, strdup("__" PROGRAM_NAME "__"), CLIB_VERSION);
command_init(&program, PROGRAM_NAME, CLIB_VERSION);
debug_init(&debugger, PROGRAM_NAME);
program.usage = "[options] [name ...]";
command_option(&program, "-o", "--out <dir>",
"change the output directory [deps]", setopt_dir);
command_option(&program, "-P", "--prefix <dir>",
"change the prefix directory (usually '/usr/local')",
setopt_prefix);
command_option(&program, "-q", "--quiet", "disable verbose output",
setopt_quiet);
command_option(&program, "-d", "--dev", "configure development dependencies",
setopt_dev);
command_option(&program, "-f", "--force",
"force the action of something, like overwriting a file",
setopt_force);
command_option(&program, "--cflags", "--flags",
"output compiler flags instead of configuring", setopt_flags);
command_option(&program, "-c", "--skip-cache", "skip cache when configuring",
setopt_skip_cache);
#ifdef HAVE_PTHREADS
command_option(&program, "-C", "--concurrency <number>",
"Set concurrency (default: " S(MAX_THREADS) ")",
setopt_concurrency);
#endif
command_parse(&program, argc, argv);
if (opts.dir) {
char dir[path_max];
memset(dir, 0, path_max);
realpath(opts.dir, dir);
unsigned long int size = strlen(dir) + 1;
opts.dir = malloc(size);
memset((void *)opts.dir, 0, size);
memcpy((void *)opts.dir, dir, size);
}
if (opts.prefix) {
char prefix[path_max];
memset(prefix, 0, path_max);
realpath(opts.prefix, prefix);
unsigned long int size = strlen(prefix) + 1;
opts.prefix = malloc(size);
memset((void *)opts.prefix, 0, size);
memcpy((void *)opts.prefix, prefix, size);
}
rest_offset = program.argc;
if (argc > 0) {
int rest = 0;
int i = 0;
do {
char *arg = program.nargv[i];
if (arg && '-' == arg[0] && '-' == arg[1] && 2 == strlen(arg)) {
rest = 1;
rest_offset = i + 1;
} else if (arg && rest) {
(void)rest_argc++;
}
} while (program.nargv[++i]);
}
if (rest_argc > 0) {
rest_argv = malloc(rest_argc * sizeof(char *));
memset(rest_argv, 0, rest_argc * sizeof(char *));
int j = 0;
int i = rest_offset;
do {
rest_argv[j++] = program.nargv[i++];
} while (program.nargv[i]);
}
if (0 != curl_global_init(CURL_GLOBAL_ALL)) {
logger_error("error", "Failed to initialize cURL");
return 1;
}
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
package_opts.skip_cache = opts.skip_cache;
package_opts.prefix = opts.prefix;
package_opts.global = opts.global;
package_opts.force = opts.force;
clib_package_set_opts(package_opts);
if (0 == program.argc || (argc == rest_offset + rest_argc)) {
rc = configure_package(CWD);
} else {
for (int i = 1; i <= rest_offset; ++i) {
char *dep = program.nargv[i];
if ('.' == dep[0]) {
char dir[path_max];
memset(dir, 0, path_max);
dep = realpath(dep, dir);
} else {
fs_stats *stats = fs_stat(dep);
if (!stats) {
dep = path_join(opts.dir, dep);
} else {
free(stats);
}
}
fs_stats *stats = fs_stat(dep);
if (stats && (S_IFREG == (stats->st_mode & S_IFMT)
#if defined(__unix__) || defined(__linux__) || defined(_POSIX_VERSION)
|| S_IFLNK == (stats->st_mode & S_IFMT)
#endif
)) {
dep = basename(dep);
rc = configure_package_with_manifest_name(dirname(dep), basename(dep));
} else {
rc = configure_package(dep);
// try with slug
if (0 != rc) {
rc = configure_package(program.nargv[i]);
}
}
if (stats) {
free(stats);
stats = 0;
}
}
}
int total_configured = 0;
hash_each(configured, {
if (0 == strncmp("t", val, 1)) {
(void)total_configured++;
}
if (0 != key) {
free((void *)key);
}
});
hash_free(configured);
command_free(&program);
curl_global_cleanup();
clib_package_cleanup();
if (opts.dir) {
free((void *)opts.dir);
}
if (opts.prefix) {
free(opts.prefix);
}
if (rest_argc > 0) {
free(rest_argv);
rest_offset = 0;
rest_argc = 0;
rest_argv = 0;
}
if (0 == rc) {
if (opts.flags && total_configured > 0) {
printf("\n");
}
if (opts.verbose) {
if (total_configured > 1) {
logger_info("info", "configured %d packages", total_configured);
} else if (1 == total_configured) {
logger_info("info", "configured 1 package");
} else {
logger_info("info", "configured 0 packages");
}
}
}
return rc;
}