• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Build Template
3#
4# Invoke this template with a set of variables in order to make build targets
5# for a build variant that targets a specific CPU architecture.
6#
7
8include $(CHRE_PREFIX)/build/defs.mk
9
10################################################################################
11#
12# Build Template
13#
14# Invoke this to instantiate a set of build targets. Two build targets are
15# produced by this template that can be either used directly or depended on to
16# perform post processing (ie: during a nanoapp build).
17#
18# TARGET_NAME_ar - An archive of the code compiled by this template.
19# TARGET_NAME_so - A shared object of the code compiled by this template.
20# TARGET_NAME    - A convenience target that depends on the above archive and
21#                  shared object targets.
22#
23# Argument List:
24#     $1  - TARGET_NAME          - The name of the target being built.
25#     $2  - TARGET_CFLAGS        - The compiler flags to use for this target.
26#     $3  - TARGET_CC            - The C/C++ compiler for the target variant.
27#     $4  - TARGET_SO_LDFLAGS    - The linker flags to use for this target.
28#     $5  - TARGET_LD            - The linker for the target variant.
29#     $6  - TARGET_ARFLAGS       - The archival flags to use for this target.
30#     $7  - TARGET_AR            - The archival tool for the targer variant.
31#     $8  - TARGET_VARIANT_SRCS  - Source files specific to this variant.
32#     $9  - TARGET_BUILD_BIN     - Build a binary. Typically this means that the
33#                                  source files provided include an entry point.
34#     $10 - TARGET_BIN_LDFLAGS   - Linker flags that are passed to the linker
35#                                  when building an executable binary.
36#     $11 - TARGET_SO_EARLY_LIBS - Link against a set of libraries when building
37#                                  a shared object. These are placed before the
38#                                  objects produced by this build.
39#     $12 - TARGET_SO_LATE_LIBS  - Link against a set of libraries when building
40#                                  a shared object. These are placed after the
41#                                  objects produced by this build.
42#
43################################################################################
44
45ifndef BUILD_TEMPLATE
46define BUILD_TEMPLATE
47
48# Target Objects ###############################################################
49
50# Source files.
51$$(1)_CXX_SRCS = $$(filter %.cc, $(COMMON_SRCS) $(8))
52$$(1)_C_SRCS = $$(filter %.c, $(COMMON_SRCS) $(8))
53
54# Object files.
55$$(1)_OBJS_DIR = $(1)_objs
56$$(1)_CXX_OBJS = $$(patsubst %.cc, $(OUT)/$$($$(1)_OBJS_DIR)/%.o, \
57                             $$($$(1)_CXX_SRCS))
58$$(1)_C_OBJS = $$(patsubst %.c, $(OUT)/$$($$(1)_OBJS_DIR)/%.o, \
59                           $$($$(1)_C_SRCS))
60
61# Add object file directories.
62$$$(1)_DIRS = $$(sort $$(dir $$($$(1)_CXX_OBJS) $$($$(1)_C_OBJS)))
63
64# Outputs ######################################################################
65
66# Shared Object
67$$(1)_SO = $(OUT)/$$$(1)/$(OUTPUT_NAME).so
68
69# Static Archive
70$$(1)_AR = $(OUT)/$$$(1)/$(OUTPUT_NAME).a
71
72# Optional Binary
73$$(1)_BIN = $$(if $(9), $(OUT)/$$$(1)/$(OUTPUT_NAME), )
74
75# Top-level Build Rule #########################################################
76
77# Define the phony target.
78.PHONY: $(1)_ar
79$(1)_ar: $$($$(1)_AR)
80
81.PHONY: $(1)_so
82$(1)_so: $$($$(1)_SO)
83
84.PHONY: $(1)_bin
85$(1)_bin: $$($$(1)_BIN)
86
87.PHONY: $(1)
88$(1): $(1)_ar $(1)_so $(1)_bin
89
90# If building the runtime, simply add the archive and shared object to the all
91# target. When building CHRE, it is expected that this runtime just be linked
92# into another build system (or the entire runtime is built using another build
93# system).
94ifeq ($(IS_NANOAPP_BUILD),)
95all: $(1)
96endif
97
98# Compile ######################################################################
99
100# Add common and target-specific compiler flags.
101$$$(1)_CFLAGS = $(COMMON_CFLAGS) \
102    $(2)
103
104$$($$(1)_CXX_OBJS): $(OUT)/$$($$(1)_OBJS_DIR)/%.o: %.cc
105	$(3) $(COMMON_CXX_CFLAGS) $$($$$(1)_CFLAGS) -c $$< -o $$@
106
107$$($$(1)_C_OBJS): $(OUT)/$$($$(1)_OBJS_DIR)/%.o: %.c
108	$(3) $(COMMON_C_CFLAGS) $$($$$(1)_CFLAGS) -c $$< -o $$@
109
110# Archive ######################################################################
111
112# Add common and target-specific archive flags.
113$$$(1)_ARFLAGS = $(COMMON_ARFLAGS) \
114    $(6)
115
116$$($$(1)_AR): $$(OUT)/$$$(1) $$($$$(1)_DIRS) $$($$(1)_CXX_OBJS) $$($$(1)_C_OBJS)
117	$(7) $$($$$(1)_ARFLAGS) $$@ $$(filter %.o, $$^)
118
119# Link #########################################################################
120
121$$($$(1)_SO): $$(OUT)/$$$(1) $$($$$(1)_DIRS) $$($$(1)_CXX_OBJS) $$($$(1)_C_OBJS)
122	$(5) $(4) -o $$@ $(11) $$(filter %.o, $$^) $(12)
123
124$$($$(1)_BIN): $$(OUT)/$$$(1) $$($$$(1)_DIRS) $$($$(1)_CXX_OBJS) \
125               $$($$(1)_C_OBJS)
126	$(3) -o $$@ $$(filter %.o, $$^) $(10)
127
128# Output Directories ###########################################################
129
130$$($$$(1)_DIRS):
131	mkdir -p $$@
132
133$$(OUT)/$$$(1):
134	mkdir -p $$@
135
136endef
137endif
138
139# Template Invocation ##########################################################
140
141$(eval $(call BUILD_TEMPLATE, $(TARGET_NAME), \
142                              $(TARGET_CFLAGS), \
143                              $(TARGET_CC), \
144                              $(TARGET_SO_LDFLAGS), \
145                              $(TARGET_LD), \
146                              $(TARGET_ARFLAGS), \
147                              $(TARGET_AR), \
148                              $(TARGET_VARIANT_SRCS), \
149                              $(TARGET_BUILD_BIN), \
150                              $(TARGET_BIN_LDFLAGS), \
151                              $(TARGET_SO_EARLY_LIBS), \
152                              $(TARGET_SO_LATE_LIBS)))
153