-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
66 lines (53 loc) · 2.09 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# ----------------------------------------
# Definitions
# ----------------------------------------
ifndef CC
CC := gcc
endif
CFLAGS := -W -Wall -Wextra -pedantic \
-Wconversion -Wswitch-enum \
-Wno-nonnull -Wno-nonnull-compare -Wno-format \
-flto -std=c11 -O0
# List of functions linked from libcj or libc
FUNCTIONS := tolower toupper \
memcpy memmove strcpy strncpy strcat strncat \
memcmp strcmp strncmp \
memchr strchr strcspn strpbrk strchr strspn \
strstr strtok memset strlen \
atoi atol atoll strtol strtoll strtoul strtoull \
atof strtof strtod strtold \
snprintf sscanf
# Function that uses nm to check if a function name (2nd argument) is undefined in a object file (1st argument)
is_undefined = $(if $(shell nm -uj "$(1)" | grep "$(2)"),undefined)
# Function to check if FUNCTIONS are defined or undefined in an object file (1st argument).
# The second argument determines the linking behavior: "defined" requires all functions to be defined locally,
# while "undefined" mandates that all functions should be linked from libc.
define check_functions
$(foreach fn,$(FUNCTIONS),\
$(if $(call is_undefined,$(1),$(fn)),\
$(if $(findstring "undefined",$(2)), $(error "$(1) shouldn't link with $(fn) from libc")),\
$(if $(findstring "defined",$(2)), $(error "$(1) should link with $(fn) from libc"))\
)\
)
endef
# ----------------------------------------
# Compilation rules
# ----------------------------------------
all: test_libc test_libcj
test_libc: main.c test.h Makefile
$(CC) $(CFLAGS) $(filter %.c %.s %.o,$^) -o $@
test_libcj: main.c libcj.c libcj.h test.h Makefile
$(CC) $(CFLAGS) $(filter %.c %.s %.o,$^) -o $@ -DUSE_LIB_CJ
# ----------------------------------------
# Script rules
# ----------------------------------------
test: test_libc test_libcj
$(call check_functions,test_libc,"defined")
$(call check_functions,test_libcj,"undefined")
./test_libc
./test_libcj
@ echo "Congrats! All tests passed!"
clean:
rm -rf test_lib* *.o
.PHONY: all test clean
# ----------------------------------------