1# Custom GNUMakefile to rebuild nawk for the Android NDK
2# (C) 2011 The Android Open Source Project
3# yada yda yada Apache 2.0 License....
4
5BUILD_DIR ?= /tmp/ndk-$(USER)/build-awk
6
7
8CC       := $(HOST_CC)
9CFLAGS   := $(CFLAGS) -O2 -I$(BUILD_DIR) -I.
10LDFLAGS  := $(LDFLAGS) -Wl,-s
11BISON    := bison
12
13# NATIVE_CC is to build maketab which needs to run in order
14# to generate proctab.c from ytab.h.  Set it to host native
15# cc if host can run windows binary (MINGW=yes) or darwin binary
16# (DARWIN=yes)
17NATIVE_CC       ?= $(CC)
18NATIVE_CFLAGS   ?= $(CFLAGS)
19NATIVE_LDFLAGS  ?= $(LDFLAGS)
20
21MINGW := $(strip $(WIN32))
22TRY64 := $(strip $(TRY64))
23
24ifeq ($(V),1)
25hide :=
26else
27hide := @
28endif
29
30SOURCES := \
31    lex.c \
32    b.c \
33    main.c \
34    parse.c \
35    lib.c \
36    run.c \
37    tran.c \
38
39EXE :=
40
41ifdef MINGW
42ifdef TRY64
43CC      := x86_64-pc-mingw32msvc-gcc
44else
45CC      := i586-pc-mingw32msvc-gcc
46endif
47EXE     := .exe
48endif
49
50PROGRAM := $(BUILD_DIR)/ndk-awk$(EXE)
51
52all: $(PROGRAM)
53
54OBJECTS :=
55
56YTAB_H := $(BUILD_DIR)/ytab.h
57YTAB_C := $(BUILD_DIR)/ytab.c
58YTAB_O := $(YTAB_C:.c=.o)
59$(YTAB_C): awkgram.y
60	@echo "Bison: ytab.c"
61	$(hide)$(BISON) -o $@ --defines=$(YTAB_H) awkgram.y
62
63$(YTAB_H): $(YTAB_C) awkgram.y
64
65OBJECTS += $(YTAB_O)
66
67# $1: source file
68# $2: object file
69define build-c-file
70OBJECTS += $2
71$2: $1 $(YTAB_H)
72	@echo "Compile: $1"
73	$$(hide)mkdir -p $$(dir $$@)
74	$$(hide)$$(CC) $$(CFLAGS) -c -o $$@ $$<
75endef
76
77$(foreach src,$(filter %.c,$(SOURCES)),\
78    $(eval $(call build-c-file,$(src),$(BUILD_DIR)/$(src:%.c=%.o)))\
79)
80
81MAKETAB := $(BUILD_DIR)/maketab$(EXE)
82$(MAKETAB): maketab.c $(BUILD_DIR)/ytab.h
83	@echo "Native Executable: $(notdir $@)"
84	$(hide)$(NATIVE_CC) $(NATIVE_CFLAGS) $(NATIVE_LDFLAGS) maketab.c -o $@
85
86PROCTAB_C := $(BUILD_DIR)/proctab.c
87PROCTAB_O := $(PROCTAB_C:%.c=%.o)
88
89$(eval $(call build-c-file,$(PROCTAB_C),$(PROCTAB_O)))
90
91$(PROCTAB_C): $(MAKETAB) $(YTAB_H)
92	@echo "Gen: $(notdir $@)"
93	$(hide)$(MAKETAB) $(YTAB_H) > $@
94
95
96$(PROGRAM): $(OBJECTS)
97	@echo "Link: $(notdir $@)"
98	$(hide)$(CC) $(LDFLAGS) -o $@ $(OBJECTS) -lm
99
100clean:
101	$(hide)rm -f $(OBJECTS) $(PROGRAM) $(MAKETAB) $(YTAB_C) $(YTAB_H) $(PROGTAB_C)
102