1# Copyright (C) 2011 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16# The following variables can be over-ridden by the caller
17CC        := gcc
18CXX       := g++
19STRIP     := strip
20BUILD_DIR := /tmp/ndk-$(USER)/build/build-ndk-stack
21PROGNAME  := /tmp/ndk-$(USER)/ndk-stack
22
23EXECUTABLE := $(PROGNAME)
24
25all: $(EXECUTABLE)
26
27# The rest should be left alone
28EXTRA_CFLAGS := -Wall -Werror
29EXTRA_LDFLAGS := -lstdc++
30
31ifneq (,$(strip $(DEBUG)))
32  CFLAGS += -O0 -g
33  hide = @
34  strip-cmd =
35else
36  CFLAGS += -O2 -s
37  hide =
38  strip-cmd = $(STRIP) $1
39endif
40
41ELFF_SOURCES := elff/dwarf_cu.cc \
42                elff/dwarf_die.cc \
43                elff/dwarf_utils.cc \
44                elff/elf_alloc.cc \
45                elff/elf_file.cc \
46                elff/elf_mapped_section.cc \
47                elff/elff_api.cc \
48                elff/mapfile.c
49
50BINUTILS_SOURCES := binutils/addr2line.c
51
52REGEX_SOURCES := regex/regcomp.c \
53                 regex/regerror.c \
54                 regex/regexec.c \
55                 regex/regfree.c
56
57NDK_STACK_SOURCES := ndk-stack.c \
58                     ndk-stack-parser.c
59
60ifdef WITH_LIBBFD
61SOURCES := $(NDK_STACK_SOURCES) $(BINUTILS_SOURCES) $(REGEX_SOURCES)
62else
63SOURCES := $(NDK_STACK_SOURCES) $(ELFF_SOURCES) $(REGEX_SOURCES)
64endif
65
66OBJECTS=
67
68define build-c-object
69OBJECTS += $1
70$1: $2
71	mkdir -p $$(dir $1)
72	$$(CC) $$(CFLAGS) $$(EXTRA_CFLAGS) -c -o $1 $2
73endef
74
75define build-cxx-object
76OBJECTS += $1
77$1: $2
78	mkdir -p $$(dir $1)
79	$$(CXX) $$(CFLAGS) $$(EXTRA_CFLAGS) -c -o $1 $2
80endef
81
82$(foreach src,$(filter %.c,$(SOURCES)),\
83    $(eval $(call build-c-object,$(BUILD_DIR)/$(src:%.c=%.o),$(src)))\
84)
85
86$(foreach src,$(filter %.cc,$(SOURCES)),\
87    $(eval $(call build-cxx-object,$(BUILD_DIR)/$(src:%.cc=%.o),$(src)))\
88)
89
90clean:
91	rm -f $(EXECUTABLE)
92
93$(EXECUTABLE): $(OBJECTS)
94	$(CXX) $(OBJECTS) $(LDFLAGS) -o $@ $(EXTRA_LDFLAGS)
95	$(call strip-cmd,$@)
96