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 8 include $(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 45 ifndef BUILD_TEMPLATE 46 define 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 # Automatic dependency resolution Makefiles. 62 $$(1)_CXX_DEPS = $$(patsubst %.cc, $(OUT)/$$($$(1)_OBJS_DIR)/%.d, \ 63 $$($$(1)_CXX_SRCS)) 64 $$(1)_C_DEPS = $$(patsubst %.c, $(OUT)/$$($$(1)_OBJS_DIR)/%.d, \ 65 $$($$(1)_C_SRCS)) 66 67 # Add object file directories. 68 $$$(1)_DIRS = $$(sort $$(dir $$($$(1)_CXX_OBJS) $$($$(1)_C_OBJS))) 69 70 # Outputs ###################################################################### 71 72 # Shared Object 73 $$(1)_SO = $(OUT)/$$$(1)/$(OUTPUT_NAME).so 74 75 # Static Archive 76 $$(1)_AR = $(OUT)/$$$(1)/$(OUTPUT_NAME).a 77 78 # Optional Binary 79 $$(1)_BIN = $$(if $(9), $(OUT)/$$$(1)/$(OUTPUT_NAME), ) 80 81 # Top-level Build Rule ######################################################### 82 83 # Define the phony target. 84 .PHONY: $(1)_ar 85 $(1)_ar: $$($$(1)_AR) 86 87 .PHONY: $(1)_so 88 $(1)_so: $$($$(1)_SO) 89 90 .PHONY: $(1)_bin 91 $(1)_bin: $$($$(1)_BIN) 92 93 .PHONY: $(1) 94 $(1): $(1)_ar $(1)_so $(1)_bin 95 96 # If building the runtime, simply add the archive and shared object to the all 97 # target. When building CHRE, it is expected that this runtime just be linked 98 # into another build system (or the entire runtime is built using another build 99 # system). 100 ifeq ($(IS_NANOAPP_BUILD),) 101 all: $(1) 102 endif 103 104 # Compile ###################################################################### 105 106 $$($$(1)_CXX_OBJS): $(OUT)/$$($$(1)_OBJS_DIR)/%.o: %.cc 107 $(3) $(COMMON_CXX_CFLAGS) -DCHRE_FILENAME=\"$$(notdir $$<)\" $(2) -c $$< \ 108 -o $$@ 109 110 $$($$(1)_C_OBJS): $(OUT)/$$($$(1)_OBJS_DIR)/%.o: %.c 111 $(3) $(COMMON_C_CFLAGS) -DCHRE_FILENAME=\"$$(notdir $$<)\" $(2) -c $$< \ 112 -o $$@ 113 114 # Archive ###################################################################### 115 116 # Add common and target-specific archive flags. 117 $$$(1)_ARFLAGS = $(COMMON_ARFLAGS) \ 118 $(6) 119 120 $$($$(1)_AR): $$(OUT)/$$$(1) $$($$$(1)_DIRS) $$($$(1)_CXX_OBJS) $$($$(1)_C_OBJS) 121 $(7) $$($$$(1)_ARFLAGS) $$@ $$(filter %.o, $$^) 122 123 # Link ######################################################################### 124 125 $$($$(1)_SO): $$(OUT)/$$$(1) $$($$$(1)_DIRS) $$($$(1)_CXX_DEPS) \ 126 $$($$(1)_C_DEPS) $$($$(1)_CXX_OBJS) $$($$(1)_C_OBJS) 127 $(5) $(4) -o $$@ $(11) $$(filter %.o, $$^) $(12) 128 129 $$($$(1)_BIN): $$(OUT)/$$$(1) $$($$$(1)_DIRS) $$($$(1)_CXX_DEPS) \ 130 $$($$(1)_C_DEPS) $$($$(1)_CXX_OBJS) $$($$(1)_C_OBJS) 131 $(3) -o $$@ $$(filter %.o, $$^) $(10) 132 133 # Output Directories ########################################################### 134 135 $$($$$(1)_DIRS): 136 mkdir -p $$@ 137 138 $$(OUT)/$$$(1): 139 mkdir -p $$@ 140 141 # Automatic Dependency Resolution ############################################## 142 143 $$($$(1)_CXX_DEPS): $(OUT)/$$($$(1)_OBJS_DIR)/%.d: %.cc 144 mkdir -p $$(dir $$@) 145 $(3) $(DEP_CFLAGS) $(COMMON_CXX_CFLAGS) \ 146 -DCHRE_FILENAME=\"$$(notdir $$<)\" $(2) -c $$< -o $$@ 147 $(DEP_POST_COMPILE) 148 149 $$($$(1)_C_DEPS): $(OUT)/$$($$(1)_OBJS_DIR)/%.d: %.c 150 mkdir -p $$(dir $$@) 151 $(3) $(DEP_CFLAGS) $(COMMON_C_CFLAGS) \ 152 -DCHRE_FILENAME=\"$$(notdir $$<)\" $(2) -c $$< -o $$@ 153 $(DEP_POST_COMPILE) 154 155 # Include generated dependency files if they are in the requested build target. 156 # This avoids dependency generation from occuring for a debug target when a 157 # non-debug target is requested. 158 ifneq ($(filter $(1) all, $(MAKECMDGOALS)),) 159 include $$(patsubst %.o, %.d, $$($$(1)_CXX_DEPS)) 160 include $$(patsubst %.o, %.d, $$($$(1)_C_DEPS)) 161 endif 162 163 endef 164 endif 165 166 # Template Invocation ########################################################## 167 168 $(eval $(call BUILD_TEMPLATE, $(TARGET_NAME), \ 169 $(COMMON_CFLAGS) $(TARGET_CFLAGS), \ 170 $(TARGET_CC), \ 171 $(TARGET_SO_LDFLAGS), \ 172 $(TARGET_LD), \ 173 $(TARGET_ARFLAGS), \ 174 $(TARGET_AR), \ 175 $(TARGET_VARIANT_SRCS), \ 176 $(TARGET_BUILD_BIN), \ 177 $(TARGET_BIN_LDFLAGS), \ 178 $(TARGET_SO_EARLY_LIBS), \ 179 $(TARGET_SO_LATE_LIBS))) 180 181 # Debug Template Invocation #################################################### 182 183 $(eval $(call BUILD_TEMPLATE, $(TARGET_NAME)_debug, \ 184 $(COMMON_CFLAGS) $(COMMON_DEBUG_CFLAGS) \ 185 $(TARGET_CFLAGS) $(TARGET_DEBUG_CFLAGS), \ 186 $(TARGET_CC), \ 187 $(TARGET_SO_LDFLAGS), \ 188 $(TARGET_LD), \ 189 $(TARGET_ARFLAGS), \ 190 $(TARGET_AR), \ 191 $(TARGET_VARIANT_SRCS), \ 192 $(TARGET_BUILD_BIN), \ 193 $(TARGET_BIN_LDFLAGS), \ 194 $(TARGET_SO_EARLY_LIBS), \ 195 $(TARGET_SO_LATE_LIBS))) 196