ae2fa3fcaf855d7413cb216326c84f885a76e745
[libc-test] / Makefile.inc
1 # gnu makefile
2 # when included in src/*/Makefile then it builds a binary locally
3 # when included in ./Makefile then all tests are linked into one binary
4
5 ROOTDIR ?= ../..
6 ifeq ($(ROOTDIR), .)
7 SRCS = $(sort $(wildcard src/*/*.c))
8 else
9 SRCS = $(sort $(wildcard *.c))
10 endif
11 OBJS = $(SRCS:.c=.o)
12
13 usemusl = yes
14 prefix = /usr/local/musl
15 includedir = $(prefix)/include
16 libdir = $(prefix)/lib
17
18 AR=ar
19 RANLIB=ranlib
20
21 CFLAGS += -g -D_POSIX_C_SOURCE=200809L -std=c99 -pipe -Wall
22 LDFLAGS += -g -lpthread -lrt -lm
23 INC += -I$(ROOTDIR)/common
24
25 -include $(ROOTDIR)/Makefile.conf
26
27 ifeq ($(usemusl), yes)
28 CC=gcc
29 LIBCC=$(shell gcc -print-file-name=libgcc.a |sed 's,/libgcc.a,,')
30 #LIBCC=$(shell pcc -v /dev/null 2>&1 |sed -n 's,/crtbegin.o.*,,;s,.* /,/,p')
31 CFLAGS  += -nostdinc -ffreestanding -fno-stack-protector
32 LDFLAGS += -nostdlib -Wl,-e,_start,-Bstatic $(libdir)/crti.o $(libdir)/crt1.o $(libdir)/crtn.o -L $(libdir) -lc -L $(LIBCC) -l$(CC)
33 INC     += -isystem $(includedir)
34 endif
35
36 all: t b
37
38 clean:
39         rm -f $(OBJS) t t.o b b.o tests.a tests.h
40
41 .c.o:
42         $(CC) $(CFLAGS) $(INC) -c -o $@ $<
43
44 $(OBJS): $(ROOTDIR)/common/test.h $(ROOTDIR)/Makefile.conf
45
46 tests.h: $(OBJS)
47         nm -f posix $+ |awk ' \
48                 /^test/ && $$2=="T"{print "T(" $$1 ")"} \
49                 /^bench/ && $$2=="T"{print "B(" $$1 ")"} \
50         ' >tests.h
51
52 tests.a: $(OBJS)
53         $(AR) rc $@ $+
54         $(RANLIB) $@
55
56 t.o: $(ROOTDIR)/common/t.c $(ROOTDIR)/common/test.h tests.h
57         $(CC) $(CFLAGS) $(INC) -I. -c -o $@ $<
58
59 t: t.o tests.a
60         $(CC) $+ $(LDFLAGS) -o $@
61
62 b.o: $(ROOTDIR)/common/b.c $(ROOTDIR)/common/test.h tests.h
63         $(CC) $(CFLAGS) $(INC) -I. -c -o $@ $<
64
65 b: b.o tests.a
66         $(CC) $+ $(LDFLAGS) -lrt -o $@
67
68 .PHONY: all clean