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