aboutsummaryrefslogtreecommitdiff
path: root/lib/internal_cmd.c
blob: 3fb383a8b47dedb643b598d6bee0206f3a4c5b48 (plain) (blame)
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
/**
 * @file internal_cmd.c
 */
#include "spm.h"

/**
 * List of valid internal commands
 */
static char *internal_commands[] = {
        "mkprefixbin", "generate prefix manifest (binary)",
        "mkprefixtext", "generate prefix manifest (text)",
        "mkmanifest", "generate package repository manifest",
        "mkruntime", "emit runtime environment (stdout)",
        "mirror_clone", "mirror a mirror",
        "rpath_set", "modify binary RPATH",
        "rpath_autoset", "determine nearest lib directory and set RPATH",
        "get_package_ext", "show the default archive extension",
        "get_sys_target", "show this system's arch/platform",
        "check_rt_env", "check the integrity of the calling runtime environment",
        NULL, NULL,
};

/**
 *
 */
void mkprefix_interface_usage(void) {
    printf("usage: mkprefix[bin|text] {output_file} {dir} {prefix ...}\n");
}

/**
 * Create prefix manifests from the CLI
 * @param argc
 * @param argv
 * @return return value of `prefixes_write`
 */
int mkprefix_interface(int argc, char **argv) {
    char *command = argv[0];
    char *outfile = argv[1];
    char *tree = argv[2];

    size_t prefix_start = 3;
    size_t prefixes = 0;
    for (size_t i = prefix_start; i < (size_t) argc; i++) {
        prefixes = i;
    }

    // Check arguments
    if (!outfile) {
        fprintf(stderr, "error: missing output file name\n");
        mkprefix_interface_usage();
        return -1;
    }
    if (!tree) {
        fprintf(stderr, "error: missing directory path\n");
        mkprefix_interface_usage();
        return -1;
    }
    if (!prefixes) {
        fprintf(stderr, "error: missing prefix string(s)\n");
        mkprefix_interface_usage();
        return -1;
    }

    char **prefix = (char **) calloc(prefixes + 1, sizeof(char *));
    if (!prefix) {
        perror("prefix array");
        fprintf(SYSERROR);
        return -1;
    }

    // Populate array of prefixes; reusing pointers from argv
    for (size_t i = 0; (i + prefix_start) < (size_t) argc; i++) {
        prefix[i] = argv[(i + prefix_start)];
    }

    if (SPM_GLOBAL.verbose) {
        printf("Generating prefix manifest: %s\n", outfile);
    }

    int result = 0;
    if (strcmp(command, "mkprefixbin") == 0) {
        result = prefixes_write(outfile, PREFIX_WRITE_BIN, prefix, tree);
    } else if (strcmp(command, "mkprefixtext") == 0) {
        result = prefixes_write(outfile, PREFIX_WRITE_TEXT, prefix, tree);
    }
    return result;
}

/**
 *
 */
void mkmanifest_interface_usage(void) {
    printf("usage: mkmanifest [-p target ...] [package_dir ...]\n");
}

/**
 * Generate a named package manifest
 * @param argc
 * @param argv
 * @return value of `manifest_write`
 */
int mkmanifest_interface(int argc, char **argv) {
    Manifest *manifest = NULL;
    int result = 0;
    char *pkgdir = NULL;
    char *path = NULL;
    char *target = NULL;
    StrList *paths = NULL;
    StrList *targets = NULL;

    if (argc < 2) {
        mkmanifest_interface_usage();
        return -1;
    }

    paths = strlist_init();
    targets = strlist_init();

    char *pth = NULL;
    for (size_t i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--platform") == 0) {
            i++;
            strlist_append(targets, argv[i]);
            continue;
        }

        pth = expandpath(argv[i]);
        if (pth == NULL || exists(pth) != 0) {
            fprintf(stderr, "%s does not exist\n", argv[i]);
            return -2;
        }

        strlist_append(paths, pth);
        free(pth);
    }

    size_t target_count = strlist_count(targets);
    size_t path_count = strlist_count(paths);

    if (target_count < 1) {
        strlist_append(targets, SPM_GLOBAL.repo_target);
        target_count = strlist_count(targets);
    }

    for (size_t t = 0; t < target_count; t++) {
        target = strlist_item(targets, t);
        for (size_t i = 0; i < path_count; i++) {
            path = strlist_item(paths, i);

            if (endswith(path, target)) {
                pkgdir = strdup(path);
            } else {
                pkgdir = join((char *[]) {path, target, NULL}, DIRSEPS);
            }

            if (SPM_GLOBAL.verbose) {
                printf("Indexing: %s\n", pkgdir);
            }

            if (exists(pkgdir) != 0) {
                fprintf(stderr, "WARNING: target directory does not exist: '%s'\n", pkgdir);
                continue;
            }

            manifest = manifest_from(pkgdir);
            if (manifest == NULL) {
                fprintf(stderr, "No packages\n");
                return -4;
            }

            result = manifest_write(manifest, pkgdir);
            if (result != 0) {
                fprintf(stderr, "ERROR:  while writing manifest data: '%s'\n", pkgdir);
                manifest_free(manifest);
                return -5;
            }
            free(pkgdir);
        }
    }

    if (targets != NULL)
        strlist_free(targets);

    if (paths != NULL)
        strlist_free(paths);

    if (manifest != NULL)
        manifest_free(manifest);

    return result;
}

/**
 *
 */
void mkruntime_interface_usage(void) {
    printf("usage: mkruntime {root_dir}\n");
}

/**
 * Generate a "key=value" string. If `_str` exists in the environment return a
 * a string like:
 *
 * ~~~{.c}
 * the_key=its_value
 * ~~~
 *
 * However, when `_str` does not exist in the environment it creates an empty
 * variable using setenv() and returns:
 *
 * ~~~{.c}
 * the_key=
 * ~~~
 *
 * @param _str a shell environment variable
 * @return `key=value` string
 */
static char *getenv_pair(const char *_str) {
    char *str = strdup(_str);
    char *result = NULL;

    if (getenv(str) == NULL) {
        if (setenv(str, "", 1) < 0) {
            perror("setenv failed");
            return NULL;
        }
    }
    result = join((char *[]){str, getenv(str), NULL}, "=");
    free(str);
    return result;
}

/**
 *
 * @param argc
 * @param argv
 * @return
 */
int mkruntime_interface(int argc, char **argv) {
    if (argc < 2) {
        mkruntime_interface_usage();
        return -1;
    }

    // Environment variables listed here should also be referenced by the runtime_set calls below.
    // Do not needlessly append to this array.
    char *passthrough_runtime[] = {
            getenv_pair("PATH"),
            getenv_pair("MANPATH"),
            getenv_pair("PKG_CONFIG_PATH"),
            getenv_pair("ACLOCAL_PATH"),
            getenv_pair("CFLAGS"),
            getenv_pair("CPPFLAGS"),
            getenv_pair("CXXFLAGS"),
            getenv_pair("LDFLAGS"),
            NULL,
    };

    RuntimeEnv *rt = runtime_copy(passthrough_runtime);
    if (rt == NULL) {
        return -1;
    }

    char *root = argv[1];
    SPM_Hierarchy *fs = spm_hierarchy_init(root);
    char *spm_pkgconfigdir = join((char *[]) {fs->libdir, "pkgconfig", NULL}, DIRSEPS);

    runtime_set(rt, "SPM_ROOT", root);
    runtime_set(rt, "SPM_BIN", fs->bindir);
    runtime_set(rt, "SPM_INCLUDE", fs->includedir);
    runtime_set(rt, "SPM_LIB", fs->libdir);
    runtime_set(rt, "SPM_LIB64", "${SPM_LIB}64");
    runtime_set(rt, "SPM_DATA", fs->datadir);
    runtime_set(rt, "SPM_MAN", fs->mandir);
    runtime_set(rt, "SPM_LOCALSTATE", fs->localstatedir);
    runtime_set(rt, "SPM_PKGCONFIG", spm_pkgconfigdir);
#if OS_DARWIN
    runtime_set(rt, "SPM_PKGCONFIG", "${SPM_PKGCONFIG}:${SPM_DATA}/pkgconfig");
#elif OS_LINUX
    runtime_set(rt, "SPM_PKGCONFIG", "${SPM_PKGCONFIG}:${SPM_LIB64}/pkgconfig:${SPM_DATA}/pkgconfig");
#endif
    runtime_set(rt, "SPM_META_DEPENDS", SPM_META_DEPENDS);
    runtime_set(rt, "SPM_META_PREFIX_BIN", SPM_META_PREFIX_BIN);
    runtime_set(rt, "SPM_META_PREFIX_TEXT", SPM_META_PREFIX_TEXT);
    runtime_set(rt, "SPM_META_DESCRIPTOR", SPM_META_DESCRIPTOR);
    runtime_set(rt, "SPM_META_FILELIST", SPM_META_FILELIST);
    runtime_set(rt, "SPM_META_PREFIX_PLACEHOLDER", SPM_META_PREFIX_PLACEHOLDER);

    runtime_set(rt, "PATH", "$SPM_BIN:$$PATH");
    runtime_set(rt, "MANPATH", "$SPM_MAN:$$MANPATH");
    runtime_set(rt, "PKG_CONFIG_PATH", "$SPM_PKGCONFIG:$$PKG_CONFIG_PATH");
    runtime_set(rt, "ACLOCAL_PATH", "${SPM_DATA}/aclocal:$$ACLOCAL_PATH");

    char *spm_ccpath = join((char *[]) {fs->bindir, "gcc", NULL}, DIRSEPS);
    char *spm_cxxpath = join((char *[]) {fs->bindir, "g++", NULL}, DIRSEPS);
    char *spm_fcpath = join((char *[]) {fs->bindir, "gfortran", NULL}, DIRSEPS);

    if (exists(spm_ccpath) == 0) {
        runtime_set(rt, "CC", "$SPM_BIN/gcc");
    }

    if (exists(spm_cxxpath) == 0) {
        runtime_set(rt, "CXX", "$SPM_BIN/g++");
    }

    if (exists(spm_fcpath) == 0) {
        runtime_set(rt, "FC", "$SPM_BIN/gfortran");
    }

    runtime_set(rt, "CFLAGS", "-I$SPM_INCLUDE");
    runtime_set(rt, "CPPFLAGS", "-I$SPM_INCLUDE");
    runtime_set(rt, "CXXFLAGS", "-I$SPM_INCLUDE");
#if OS_DARWIN
    // For now `reloc` can fix up the LC_ID_DYLIB on its own without install_name_tool
    runtime_set(rt, "LDFLAGS", "-Wl,-rpath,$SPM_LIB -L$SPM_LIB");
#elif OS_LINUX
    runtime_set(rt, "LDFLAGS", "-Wl,-rpath=$SPM_LIB:$SPM_LIB64 -L$SPM_LIB -L$SPM_LIB64");
#else
    // TODO: Windows?
#endif
    runtime_export(rt, NULL);
    runtime_free(rt);

    for (size_t i = 0; passthrough_runtime[i] != NULL; i++) {
        free(passthrough_runtime[i]);
    }

    free(spm_pkgconfigdir);
    free(spm_ccpath);
    free(spm_cxxpath);
    free(spm_fcpath);
    spm_hierarchy_free(fs);
    return 0;
}

/**
 *
 */
void mirror_clone_interface_usage(void) {
    printf("usage: mirror_clone {url} {output_dir}\n");
}

/**
 * Mirror packages referenced by a remote manifest
 * @param argc
 * @param argv
 * @return value of `manifest_write`
 */
int mirror_clone_interface(int argc, char **argv) {
    if (argc < 3) {
        mirror_clone_interface_usage();
        return -1;
    }
    char *url = argv[1];
    char *_path = argv[2];
    char *path;
    StrList *targets;
    char *target_file;
    size_t target_count;
    int no_mirroring;

    path = NULL;
    no_mirroring = 0;
    targets = strlist_init();
    target_file = join((char *[]) {url, "spm_mirror.targets", NULL}, DIRSEPS);

    if (strlist_append_file(targets, target_file, NULL) != 0) {
        no_mirroring = 1;
    }

    if (targets != NULL) {
        target_count = strlist_count(targets);
        if (target_count == 0) {
            no_mirroring = 1;
        }
    }

    if (no_mirroring) {
        fprintf(stderr, "Mirroring is disabled by the server.");
        return 1;
    }

    for (size_t tgt = 0; tgt < target_count; tgt++) {
        char *manifest_location;
        char *target;

        target = strlist_item(targets, tgt);
        strip(target);

        manifest_location = join((char *[]) {url, target, NULL}, DIRSEPS);
        Manifest *manifest = manifest_read(manifest_location);
        if (manifest == NULL) {
            return -2;
        }

        path = join((char *[]) {_path, target, NULL}, DIRSEPS);
        mirror_clone(manifest, path);
        manifest_free(manifest);
    }

    return 0;
}
/**
 *
 */
void rpath_set_interface_usage(void) {
    printf("usage: rpath_set {file} {rpath}\n");
}

/**
 * Set a RPATH from the CLI
 * @param argc
 * @param argv
 * @return return value of `rpath_set`
 */
int rpath_set_interface(int argc, char **argv) {
    if (argc < 3) {
        rpath_set_interface_usage();
        return -1;
    }
    char *filename = argv[1];
    char *rpath = argv[2];
    int result = rpath_set(filename, rpath);
    if (result < 0) {
        fprintf(SYSERROR);
    }
    return result;
}

/**
 *
 */
void rpath_autoset_interface_usage(void) {
    printf("usage: rpath_autoset {file} {topdir} {destroot}\n");
}

/**
 * Set a RPATH automatically from the CLI
 * @param argc
 * @param argv
 * @return return value of `rpath_autoset`
 */
int rpath_autoset_interface(int argc, char **argv) {
    if (argc < 4) {
        rpath_autoset_interface_usage();
        return -1;
    }
    char *filename = argv[1];
    const char *topdir = argv[2];
    const char *destroot = argv[3];

    if (exists(filename) != 0) {
        perror(filename);
        return -1;
    }

    if (exists(topdir) != 0) {
        perror(topdir);
        return -1;
    }

    FSTree *libs = rpath_libraries_available(topdir);
    int result = rpath_autoset(filename, libs, destroot);

    if (result < 0) {
        fprintf(SYSERROR);
    }

    return result;
}

/**
 * Dump the default package extension for SPM archives to `stdout`
 * @return
 */
int get_package_ext_interface(void) {
    puts(SPM_PACKAGE_EXTENSION);
    return 0;
}

/**
 * Dump the system arch/platform (i.e. Linux/x86_64)
 * @return
 */
int get_sys_target_interface(void) {
    puts(SPM_GLOBAL.repo_target);
    return 0;
}
/**
 * Execute builtin runtime check.
 *
 * On failure this function will EXIT the program with a non-zero value
 *
 * @return
 */
int check_runtime_environment_interface(void) {
    check_runtime_environment();
    return 0;
}

/**
 * Show a listing of valid internal commands
 */
void internal_command_list(void) {
    printf("possible commands:\n");
    for (size_t i = 0; internal_commands[i] != NULL; i += 2) {
        printf("  %-20s - %-20s\n", internal_commands[i], internal_commands[i + 1]);
    }
}

/**
 * Execute an internal command
 * @param argc
 * @param argv
 * @return success=0, failure=1, error=-1
 */
int internal_cmd(int argc, char **argv) {
    int command_valid = 0;
    char *command = argv[1];
    if (argc < 2) {
        internal_command_list();
        return 1;
    }

    for (int i = 0; internal_commands[i] != NULL; i++) {
        if (strcmp(internal_commands[i], command) == 0) {
            command_valid = 1;
            break;
        }
    }

    if (!command_valid) {
        fprintf(stderr, "error: '%s' is not a valid command\n", command);
        internal_command_list();
        return 1;
    }

    // Strip the first argument (this level) before passing it along to the interface
    int arg_count = argc - 1;
    char **arg_array = &argv[1];

    // Normalize the argument counter when there's no arguments to speak of
    if (arg_count < 0) {
        arg_count = 0;
    }

    if (strcmp(command, "mkprefixbin") == 0 || strcmp(command, "mkprefixtext") == 0) {
        return mkprefix_interface(arg_count, arg_array);
    }
    else if (strcmp(command, "mkmanifest") == 0) {
        return mkmanifest_interface(arg_count, arg_array);
    }
    else if (strcmp(command, "mkruntime") == 0) {
        return mkruntime_interface(arg_count, arg_array);
    }
    else if (strcmp(command, "mirror_clone") == 0) {
        return mirror_clone_interface(arg_count, arg_array);
    }
    else if (strcmp(command, "rpath_set") == 0) {
        return rpath_set_interface(arg_count, arg_array);
    }
    else if (strcmp(command, "rpath_autoset") == 0) {
        return rpath_autoset_interface(arg_count, arg_array);
    }
    else if (strcmp(command, "get_package_ext") == 0) {
        return get_package_ext_interface();
    }
    else if (strcmp(command, "get_sys_target") == 0) {
        return get_sys_target_interface();
    }
    else if (strcmp(command, "check_rt_env") == 0) {
        return check_runtime_environment_interface();
    }
    return 0;
}