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 5 BUILD_DIR ?= /tmp/ndk-$(USER)/build-awk 6 7 8 CC := $(HOST_CC) 9 CFLAGS := $(CFLAGS) -O2 -I$(BUILD_DIR) -I. 10 LDFLAGS := $(LDFLAGS) -Wl,-s 11 BISON := 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) 17 NATIVE_CC ?= $(CC) 18 NATIVE_CFLAGS ?= $(CFLAGS) 19 NATIVE_LDFLAGS ?= $(LDFLAGS) 20 21 MINGW := $(strip $(WIN32)) 22 TRY64 := $(strip $(TRY64)) 23 24 ifeq ($(V),1) 25 hide := 26 else 27 hide := @ 28 endif 29 30 SOURCES := \ 31 lex.c \ 32 b.c \ 33 main.c \ 34 parse.c \ 35 lib.c \ 36 run.c \ 37 tran.c \ 38 39 EXE := 40 41 ifdef MINGW 42 ifdef TRY64 43 CC := x86_64-pc-mingw32msvc-gcc 44 else 45 CC := i586-pc-mingw32msvc-gcc 46 endif 47 EXE := .exe 48 endif 49 50 PROGRAM := $(BUILD_DIR)/ndk-awk$(EXE) 51 52 all: $(PROGRAM) 53 54 OBJECTS := 55 56 YTAB_H := $(BUILD_DIR)/ytab.h 57 YTAB_C := $(BUILD_DIR)/ytab.c 58 YTAB_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 65 OBJECTS += $(YTAB_O) 66 67 # $1: source file 68 # $2: object file 69 define build-c-file 70 OBJECTS += $2 71 $2: $1 $(YTAB_H) 72 @echo "Compile: $1" 73 $$(hide)mkdir -p $$(dir $$@) 74 $$(hide)$$(CC) $$(CFLAGS) -c -o $$@ $$< 75 endef 76 77 $(foreach src,$(filter %.c,$(SOURCES)),\ 78 $(eval $(call build-c-file,$(src),$(BUILD_DIR)/$(src:%.c=%.o)))\ 79 ) 80 81 MAKETAB := $(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 86 PROCTAB_C := $(BUILD_DIR)/proctab.c 87 PROCTAB_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 100 clean: 101 $(hide)rm -f $(OBJECTS) $(PROGRAM) $(MAKETAB) $(YTAB_C) $(YTAB_H) $(PROGTAB_C) 102