Home | History | Annotate | Line # | Download | only in unit-tests
      1  1.6    rillig # $NetBSD: posix1.mk,v 1.6 2020/10/24 08:50:17 rillig Exp $
      2  1.1  christos 
      3  1.1  christos # Keep the default suffixes from interfering, just in case.
      4  1.1  christos .SUFFIXES:
      5  1.1  christos 
      6  1.1  christos all:	line-continuations suffix-substitution localvars
      7  1.1  christos 
      8  1.2       sjg # we need to clean for repeatable results
      9  1.2       sjg .BEGIN: clean
     10  1.2       sjg clean:
     11  1.2       sjg 	@rm -f lib.a dir/* dummy obj*
     12  1.4    rillig .END:
     13  1.4    rillig 	@rm -f lib.a dir/* dummy obj*
     14  1.2       sjg 
     15  1.1  christos #
     16  1.1  christos # Line continuations
     17  1.1  christos #
     18  1.1  christos 
     19  1.1  christos # Escaped newlines and leading whitespace from the next line are replaced
     20  1.1  christos # with single space, except in commands, where the escape and the newline
     21  1.1  christos # are retained, but a single leading tab (if any) from the next line is
     22  1.1  christos # removed. (PR 49085)
     23  1.1  christos # Expect:
     24  1.3       sjg # ${VAR} = "foo  bar baz"
     25  1.1  christos # a
     26  1.1  christos # b
     27  1.1  christos # c
     28  1.1  christos VAR = foo\
     29  1.1  christos \
     30  1.1  christos 	  bar\
     31  1.1  christos  baz
     32  1.1  christos 
     33  1.1  christos line-continuations:
     34  1.3       sjg 	@echo '$${VAR} = "${VAR}"'
     35  1.1  christos 	@echo 'aXbXc' | sed -e 's/X/\
     36  1.1  christos 	/g'
     37  1.1  christos 
     38  1.1  christos 
     39  1.1  christos #
     40  1.1  christos # Suffix substitution
     41  1.1  christos #
     42  1.1  christos 
     43  1.1  christos # The only variable modifier accepted by POSIX.
     44  1.3       sjg # ${VAR:s1=s2}: replace s1, if found, with s2 at end of each word in
     45  1.3       sjg # ${VAR}.  s1 and s2 may contain macro expansions.
     46  1.1  christos # Expect: foo baR baz, bar baz, foo bar baz, fooadd baradd bazadd
     47  1.1  christos suffix-substitution:
     48  1.3       sjg 	@echo '${VAR:r=R}, ${VAR:foo=}, ${VAR:not_there=wrong}, ${VAR:=add}'
     49  1.1  christos 
     50  1.1  christos 
     51  1.1  christos #
     52  1.1  christos # Local variables: regular forms, D/F forms and suffix substitution.
     53  1.1  christos #
     54  1.1  christos 
     55  1.1  christos # In the past substitutions did not work with the D/F forms and those
     56  1.1  christos # forms were not available for $?.  (PR 49085)
     57  1.1  christos 
     58  1.6    rillig ARFLAGS=	-rcv
     59  1.1  christos 
     60  1.1  christos localvars: lib.a
     61  1.1  christos 
     62  1.1  christos # $@ = target or archive name	$< = implied source
     63  1.5    rillig # $* = target without suffix	$? = sources newer than target
     64  1.1  christos # $% = archive member name
     65  1.6    rillig LOCALS= \
     66  1.1  christos 	"Local variables\n\
     67  1.3       sjg 	\$${@}=\"${@}\" \$${<}=\"${<}\"\n\
     68  1.3       sjg 	\$${*}=\"${*}\" \$${?}=\"${?}\"\n\
     69  1.3       sjg 	\$${%%}=\"${%}\"\n\n"
     70  1.1  christos 
     71  1.1  christos # $XD = directory part of X	$XF = file part of X
     72  1.1  christos # X is one of the local variables.
     73  1.6    rillig LOCAL_ALTERNATIVES= \
     74  1.1  christos 	"Directory and filename parts of local variables\n\
     75  1.3       sjg 	\$${@D}=\"${@D}\" \$${@F}=\"${@F}\"\n\
     76  1.3       sjg 	\$${<D}=\"${<D}\" \$${<F}=\"${<F}\"\n\
     77  1.3       sjg 	\$${*D}=\"${*D}\" \$${*F}=\"${*F}\"\n\
     78  1.3       sjg 	\$${?D}=\"${?D}\" \$${?F}=\"${?F}\"\n\
     79  1.3       sjg 	\$${%%D}=\"${%D}\" \$${%%F}=\"${%F}\"\n\n"
     80  1.1  christos 
     81  1.1  christos # Do all kinds of meaningless substitutions on local variables to see
     82  1.1  christos # if they work.  Add, remove and replace things.
     83  1.6    rillig VAR2=	.o
     84  1.6    rillig VAR3=	foo
     85  1.6    rillig LOCAL_SUBSTITUTIONS= \
     86  1.1  christos 	"Local variable substitutions\n\
     87  1.3       sjg 	\$${@:.o=}=\"${@:.o=}\" \$${<:.c=.C}=\"${<:.c=.C}\"\n\
     88  1.3       sjg 	\$${*:=.h}=\"${*:=.h}\" \$${?:.h=.H}=\"${?:.h=.H}\"\n\
     89  1.3       sjg 	\$${%%:=}=\"${%:=}\"\n\n"
     90  1.1  christos 
     91  1.6    rillig LOCAL_ALTERNATIVE_SUBSTITUTIONS= \
     92  1.1  christos 	"Target with suffix transformations\n\
     93  1.3       sjg 	\$${@D:=append}=\"${@D:=append}\"\n\
     94  1.3       sjg 	\$${@F:.o=.O}=\"${@F:.o=.O}\"\n\
     95  1.1  christos 	\n\
     96  1.1  christos 	Implied source with suffix transformations\n\
     97  1.3       sjg 	\$${<D:r=rr}=\"${<D:r=rr}\"\n\
     98  1.3       sjg 	\$${<F:.c=.C}=\"${<F:.c=.C}\"\n\
     99  1.1  christos 	\n\
    100  1.1  christos 	Suffixless target with suffix transformations\n\
    101  1.3       sjg 	\$${*D:.=dot}=\"${*D:.=dot}\"\n\
    102  1.3       sjg 	\$${*F:.a=}=\"${*F:.a=}\"\n\
    103  1.1  christos 	\n\
    104  1.1  christos 	Out-of-date dependencies with suffix transformations\n\
    105  1.3       sjg 	\$${?D:ir=}=\"${?D:ir=}\"\n\
    106  1.3       sjg 	\$${?F:.h=.H}=\"${?F:.h=.H}\"\n\
    107  1.1  christos 	\n\
    108  1.1  christos 	Member with suffix transformations\n\
    109  1.3       sjg 	\$${%%D:.=}=\"${%D:.=}\"\n\
    110  1.3       sjg 	\$${%%F:\$${VAR2}=\$${VAR}}=\"${%F:${VAR2}=${VAR}}\"\n\n"
    111  1.1  christos 
    112  1.1  christos .SUFFIXES: .c .o .a
    113  1.1  christos 
    114  1.1  christos # The system makefiles make the .c.a rule .PRECIOUS with a special source,
    115  1.1  christos # but such a thing is not POSIX compatible.  It's also somewhat useless
    116  1.1  christos # in a test makefile.
    117  1.1  christos .c.a:
    118  1.3       sjg 	@printf ${LOCALS}
    119  1.3       sjg 	@printf ${LOCAL_ALTERNATIVES}
    120  1.3       sjg 	@printf ${LOCAL_SUBSTITUTIONS}
    121  1.3       sjg 	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
    122  1.3       sjg 	cc -c -o '${%}' '${<}'
    123  1.3       sjg 	ar ${ARFLAGS} '${@}' '${%}'
    124  1.3       sjg 	rm -f '${%}'
    125  1.1  christos 
    126  1.1  christos .c.o:
    127  1.3       sjg 	@printf ${LOCALS}
    128  1.3       sjg 	@printf ${LOCAL_ALTERNATIVES}
    129  1.3       sjg 	@printf ${LOCAL_SUBSTITUTIONS}
    130  1.3       sjg 	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
    131  1.3       sjg 	cc -c -o '${@}' '${<}'
    132  1.1  christos 
    133  1.1  christos # Some of these rules are padded with useless extra dependencies just so
    134  1.3       sjg # that ${?} has more than one file.
    135  1.1  christos 
    136  1.1  christos lib.a: lib.a(obj1.o) lib.a(obj2.o) lib.a(obj3.o)
    137  1.3       sjg 	ar -s '${@}'
    138  1.1  christos 
    139  1.1  christos # Explicit rule where the dependency is an inferred file.  The dependency
    140  1.1  christos # object's name differs from the member's because there was a bug which
    141  1.1  christos # forced a dependency on member even when no such dependency was specified
    142  1.1  christos # (PR 49086).
    143  1.1  christos lib.a(obj1.o): dir/obj_1.o dummy
    144  1.3       sjg 	@printf ${LOCALS}
    145  1.3       sjg 	@printf ${LOCAL_ALTERNATIVES}
    146  1.3       sjg 	@printf ${LOCAL_SUBSTITUTIONS}
    147  1.3       sjg 	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
    148  1.1  christos 	cp 'dir/obj_1.o' '$%'
    149  1.3       sjg 	ar ${ARFLAGS} '${@}' '$%'
    150  1.1  christos 	rm -f '$%'
    151  1.1  christos 
    152  1.1  christos # Excplicit rule where the dependency also has an explicit rule.
    153  1.1  christos lib.a(obj2.o): obj2.o
    154  1.3       sjg 	ar ${ARFLAGS} '${@}' '${%}'
    155  1.1  christos 
    156  1.1  christos # Use .c.a inference with an extra dependency.
    157  1.1  christos lib.a(obj3.o): obj3.h dir/dummy
    158  1.1  christos 
    159  1.1  christos # Use .c.o inference with an extra dependency.
    160  1.1  christos dir/obj_1.o: dir/obj_1.h
    161  1.1  christos 
    162  1.1  christos # According to POSIX, $* is only required for inference rules and $<'s
    163  1.1  christos # value is unspecified outside of inference rules.  Strictly speaking
    164  1.1  christos # we shouldn't be expanding them here but who cares.  At least we get
    165  1.1  christos # to check that the program does nothing stupid (like crash) with them.
    166  1.1  christos # The C file is named differently from the object file because there
    167  1.1  christos # was a bug which forced dependencies based on inference rules on all
    168  1.1  christos # applicable targets (PR 49086).
    169  1.1  christos obj2.o: obj_2.c obj_2.h dir/obj_1.h
    170  1.3       sjg 	@printf ${LOCALS}
    171  1.3       sjg 	@printf ${LOCAL_ALTERNATIVES}
    172  1.3       sjg 	@printf ${LOCAL_SUBSTITUTIONS}
    173  1.3       sjg 	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
    174  1.3       sjg 	cc -c -o '${@}' 'obj_2.c'
    175  1.1  christos 
    176  1.1  christos # Hey, this is make, we can make our own test data setup!  obj1.c
    177  1.1  christos # and obj2.c are not used, so they should not get created.  They're here
    178  1.1  christos # as a bait for a regression into the forced dependencies discussed earlier.
    179  1.1  christos obj1.c dir/obj_1.c obj2.c obj_2.c obj3.c:
    180  1.3       sjg 	mkdir -p '${@D}'
    181  1.3       sjg 	printf '#include "${@F:.c=.h}"\nconst char* ${@F:.c=} = "${@}";\n' \
    182  1.3       sjg 	    >'${@}'
    183  1.1  christos 
    184  1.1  christos dir/obj_1.h obj_2.h obj3.h dummy dir/dummy:
    185  1.3       sjg 	mkdir -p '${@D}'
    186  1.3       sjg 	touch '${@}'
    187