diff --git a/lib/include/build.h b/lib/include/build.h index a8afbcd..5b45c5d 100644 --- a/lib/include/build.h +++ b/lib/include/build.h @@ -124,8 +124,8 @@ * The input/output table of forge_parse_args(); slots are indexed by * key (filled inside the parse_args macro by the OPT / builtin_args * macros with designated-initializer syntax): - * - `long_name == NULL` means the slot is unused (other fields are - * treated as 0); + * - `long_name == NULL` means the option has no long form; short + * options are selected by their character index independently; * - when `takes_arg` is non-zero, the parsed argument is written to * `arg` (pointing into argv, not written back; with `--jobs=8`, the * '=' in argv is rewritten to '\0' as the split); @@ -134,7 +134,7 @@ */ typedef struct { const char *long_name; /**< long option name (without --); NULL means */ - /**< the slot is unused */ + /**< the option has no long form */ int takes_arg; /**< whether the option takes an argument */ int flag_store; /**< number of hits (count/boolean for no-arg */ /**< options) */ diff --git a/lib/src/build.c b/lib/src/build.c index 05baa87..d26804a 100644 --- a/lib/src/build.c +++ b/lib/src/build.c @@ -1827,7 +1827,7 @@ static int needs_rebuild(const char *obj, const char *src, const char *cmd) { goto out; } for (int i = 0; i < dc; i++) { - if (os_mtime_ms(deps[i]) > om) { + if (!os_path_is_file(deps[i]) || os_mtime_ms(deps[i]) > om) { rebuild = 1; break; } diff --git a/lib/src/strv.c b/lib/src/strv.c index c53d3be..73038b1 100644 --- a/lib/src/strv.c +++ b/lib/src/strv.c @@ -40,6 +40,9 @@ void strv_destroy(strv_t *strv) { } } free(strv->strs); + strv->strs = NULL; + strv->count = 0; + strv->capacity = 0; } int strv_append(strv_t* strv, const char* str) { @@ -62,7 +65,7 @@ int strv_append_non_copy(strv_t* strv, char* str) { int strv_set(strv_t* strv, int idx, const char* str) { if(strv == NULL || str == NULL) return -1; - if(idx < 0 || idx > strv->count) return -1; + if(idx < 0 || idx >= strv->count) return -1; char* ptr = forge_strdup(str); if(ptr == NULL) return -1; free(strv->strs[idx]); diff --git a/src/parser.c b/src/parser.c index 32a54c4..050656d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -26,19 +26,16 @@ static char *dup_n(const char *s, size_t n) { return out; } -/* If the line containing the key match has a '#' between its start - and the match, the match is inside a comment and is invalid (same - rule as conf_defines in forge.c: '#' starts a comment cut off at - the end of the line), preventing commented lines such as - `# compiler=MSVC` from silently taking effect as configuration. */ -static int hash_before_key(const char *p, const char *buf) { +/* Keys must begin a line after optional indentation. Matches inside + comments, other key names, or values are not configuration keys. */ +static int is_key_start(const char *p, const char *buf) { const char *q = p; while (q > buf && q[-1] != '\n') q--; for (; q < p; q++) - if (*q == '#') - return 1; - return 0; + if (*q != ' ' && *q != '\t') + return 0; + return 1; } static char *get_value(char *buf, const char *key) { @@ -48,7 +45,7 @@ static char *get_value(char *buf, const char *key) { if (klen == 0) return NULL; char *p = buf; while ((p = strstr(p, key)) != NULL) { - if (p[klen] == '=' && !hash_before_key(p, buf)) + if (p[klen] == '=' && is_key_start(p, buf)) break; p += klen; } @@ -100,4 +97,4 @@ char *get_compiler(char *buf) { default: return get_value(buf, "compiler"); } -} \ No newline at end of file +} diff --git a/tests/edge/test_ed_flow.c b/tests/edge/test_ed_flow.c index cc09a4a..943b1b5 100644 --- a/tests/edge/test_ed_flow.c +++ b/tests/edge/test_ed_flow.c @@ -132,7 +132,15 @@ int main(void) { TEST(st != 0); TEST(out_has(out, "not found") || out_has(out, "undefined")); + /* A deleted header must invalidate the cached object and report the + compiler error, rather than link a stale successful build. */ + char *header = os_path_join(dir, "include/flow.h"); + TEST(header && os_remove_r(header) == 0); + free(header); + TEST(edge_make(dir, "make", out, sizeof(out)) != 0); + TEST(out_has(out, "flow.h")); + free(dir); PASS(); return 0; -} \ No newline at end of file +} diff --git a/tests/sub/test_args.c b/tests/sub/test_args.c index cfa4d5a..bb24096 100644 --- a/tests/sub/test_args.c +++ b/tests/sub/test_args.c @@ -9,6 +9,21 @@ #include int main(void) { + /* Short options do not require a corresponding long option. */ + opt_t opts[OPTS_COUNT] = { + ['x'] = {.long_name = NULL}, + ['y'] = {.long_name = NULL}, + ['o'] = {.long_name = NULL, .takes_arg = 1}, + }; + char *args[] = {"test", "-x", "-xy", "-oattached", "-o", "split", NULL}; + TEST(forge_parse_args(6, args, opts, OPTS_COUNT) == 6); + TEST(opts['x'].flag_store == 2); + TEST(opts['y'].flag_store == 1); + TEST(opts['o'].argc == 2); + TEST(strcmp(opts['o'].argv[0], "attached") == 0); + TEST(strcmp(opts['o'].argv[1], "split") == 0); + free(opts['o'].argv); + TEST(atoi("42") == 42); TEST(atoi("-7") == -7); diff --git a/tests/test_parser.c b/tests/test_parser.c new file mode 100644 index 0000000..f037723 --- /dev/null +++ b/tests/test_parser.c @@ -0,0 +1,41 @@ +// Source src/parser.c +// Include include +// Include lib/include +#include +#include "parser.h" + +#include +#include + +int main(void) { + char conf[] = "other_compiler=MSVC\n" + "other_compiler_path=/missing/cc\n" + "note=compiler=CLANG\n" + "# compiler=MSVC\n" + " compiler=GCC # chosen compiler\r\n" + "\tcompiler_path=gcc\n"; + char *value = get_compiler(conf); + TEST(value && strcmp(value, "GCC") == 0); + free(value); + value = get_compiler_path(conf); + TEST(value && strcmp(value, "gcc") == 0); + free(value); + + char unrelated[] = "my_compiler=MSVC\nvalue=compiler=CLANG\n"; + TEST(get_compiler(unrelated) == NULL); + TEST(get_compiler(NULL) == NULL); + + char overrides[] = "compiler=GCC\ncompiler_linux=CLANG\n" + "compiler_windows=MSVC\ncompiler_mac_os=CLANG\n"; + value = get_compiler(overrides); +#if defined(__linux__) || defined(__APPLE__) + TEST(value && strcmp(value, "CLANG") == 0); +#elif defined(_WIN32) + TEST(value && strcmp(value, "MSVC") == 0); +#else + TEST(value && strcmp(value, "GCC") == 0); +#endif + free(value); + PASS(); + return 0; +} diff --git a/tests/test_strv.c b/tests/test_strv.c new file mode 100644 index 0000000..c10d984 --- /dev/null +++ b/tests/test_strv.c @@ -0,0 +1,32 @@ +// Source lib/src/strv.c +// Include lib/include +#include +#include + +#include + +int main(void) { + strv_t v = {0}; + TEST(strv_init(&v) == 0); + TEST(strv_append(&v, "first") == 0); + TEST(strv_set(&v, v.count, "outside") == -1); + TEST(v.strs[v.count] == NULL); + TEST(strv_set(&v, 0, "updated") == 0); + TEST(strcmp(strv_get(&v, 0), "updated") == 0); + + strv_destroy(&v); + TEST(v.strs == NULL && v.count == 0 && v.capacity == 0); + strv_destroy(&v); + + strv_t dest = {0}; + TEST(strv_init(&dest) == 0); + TEST(strv_append(&dest, "old") == 0); + TEST(strv_copy(&dest, &v) == 0); + TEST(dest.strs == NULL && dest.count == 0 && dest.capacity == 0); + strv_destroy(&dest); + TEST(strv_init(&dest) == 0); + TEST(strv_append(&dest, "new") == 0); + strv_destroy(&dest); + PASS(); + return 0; +}