xparq / bbmake

Tinkering with rmyorston's pdpmake
https://frippery.org/make
Other
0 stars 0 forks source link

Proper pattern rules (a'la GNU make) #2

Open xparq opened 10 months ago

xparq commented 10 months ago
xparq commented 10 months ago

It would be very likely useful to generalize this part out of the static modify_words in input.c (maybe straight to utils.c?):

if (find_pref) {
    // get length of find prefix, e.g: src/
    find_pref_len = strlen(find_pref);
    // get length of find suffix, e.g: .c
    find_suff_len = lenf - find_pref_len - 1;
}

s = copy = xstrdup(val);
while ((word = gettok(&s)) != NULL) {
    newword = NULL;
    if (IF_FEATURE_MAKE_POSIX_202X(find_pref != NULL ||)
            lenf != 0 || lenr != 0) {
        size_t lenw = strlen(word);
#if ENABLE_FEATURE_MAKE_POSIX_202X
        // This code implements pattern macro expansions:
        //    https://austingroupbugs.net/view.php?id=519
        //
        // find: <prefix>%<suffix>
        // example: src/%.c
        //
        // For a pattern of the form:
        //    $(string1:[op]%[os]=[np][%][ns])
        // lenf is the length of [op]%[os].  So lenf >= 1.
        if (find_pref != NULL && lenw + 1 >= lenf) {
            // If prefix and suffix of word match find_pref and
            // find_suff, then do substitution.
            if (strncmp(word, find_pref, find_pref_len) == 0 &&
                    strcmp(word + lenw - find_suff_len, find_suff) == 0) {
                // replace: <prefix>[%<suffix>]
                // example: build/%.o or build/all.o (notice no %)
                // If repl_suff is NULL, replace whole word with repl_pref.
                if (!repl_suff) {
                    word = newword = xstrdup(repl_pref);
                } else {
                    word[lenw - find_suff_len] = '\0';
                    word = newword = xconcat3(repl_pref,
                                word + find_pref_len, repl_suff);
                }
            }
        } else
#endif
        if (lenw >= lenf && strcmp(word + lenw - lenf, find_suff) == 0) {
            word[lenw - lenf] = '\0';
            word = newword = xconcat3(word, repl_suff, "");
        }
    }