1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
#
# Copyright © 2017-2022 The Crust Firmware Authors.
# SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
#
#
# Variables for use in included Makefiles
#
src = $(SRC)/$(subdir)
obj = $(OBJ)/$(subdir)
tgt = $(TGT)/$(subdir)
#
# Variables for use in the main Makefile
#
buildprogs-all :=
hostprogs-all :=
lib-all :=
obj-all :=
test-all :=
tools-all :=
#
# Internal variables
#
dir_AFLAGS = $(asflags-y)
dir_CFLAGS = $(ccflags-y)
dir_CPPFLAGS = $(cppflags-y)
dir_LDFLAGS = $(ldflags-y)
dir_BUILDAFLAGS = $(asflags-y)
dir_BUILDCFLAGS = $(ccflags-y)
dir_BUILDCPPFLAGS= $(cppflags-y)
dir_BUILDLDFLAGS = $(ldflags-y)
dir_HOSTAFLAGS = $(asflags-y)
dir_HOSTCFLAGS = $(ccflags-y)
dir_HOSTCPPFLAGS = $(cppflags-y)
dir_HOSTLDFLAGS = $(ldflags-y)
flags = AFLAGS CFLAGS CPPFLAGS LDFLAGS LDLIBS
flags_object := $(filter-out LD%,$(flags))
flags_program := $(filter CFLAGS LD%,$(flags))
vars = buildprogs hostprogs lib obj test tools
#
# Defined functions and procedures (those starting with '_' are internal)
#
#
# Adds compiler/linker flags to the target-specific variables for this file.
#
# $1: File name (no path)
# $2: List of flags, e.g. $(flags_object) or $(flags_program)
#
build_file_flags = $(foreach f,$(2:%=BUILD%),$(call _file_flag,$(obj),$1,$f))
host_file_flags = $(foreach f,$(2:%=HOST%),$(call _file_flag,$(obj),$1,$f))
target_file_flags = $(foreach f,$2,$(call _file_flag,$(tgt),$1,$f))
#
# Internal helper to add target-specific variables. $(eval) provides the
# necessary eager binding. Also avoids adding blank/whitespace variables.
#
# $1: Directory
# $2: File name
# $3: Flag name
#
_file_flag = $(if $(strip $(dir_$3)$($3_$2)), \
$(eval $1/$2: $3 += $(dir_$3) $($3_$2)))
#
# Overrides compiler/linker paths and flags to the correct variable for the
# type of object (build vs host).
#
# $1: File name (no path)
# $2: List of flags, e.g. $(flags_object) or $(flags_program)
#
build_overrides = $(foreach f,AR CC $2,$(eval $(obj)/$1: HOST$f = $$(BUILD$f)))
#
# Adds the necessary dependencies and flags for a list of build object files.
#
# $1: List of object file names (no path)
#
build_objects = $(foreach object,$1,$(eval $(value _build_object)))
define _build_object
$(call build_file_flags,$(object),$(flags_object))
$(call build_overrides,$(object),$(flags_object))
$(obj)/$(object): | $(obj)/. $(OBJ_DEPS)
-include $(obj)/$(object:.o=.d)
endef
#
# Adds the necessary dependencies and flags for a list of build programs.
#
# $1: List of program names (no path)
#
build_programs = $(foreach program,$1,$(eval $(value _build_program)))
define _build_program
$(call build_file_flags,$(program),$(flags_program))
$(call build_overrides,$(program),$(flags_program))
ifeq ($($(program)-objs),)
$(program)-objs := $(program).o
endif
$(obj)/$(program): $($(program)-objs:%=$(obj)/%) | $(obj)/.
$(call build_objects,$($(program)-objs))
endef
#
# Adds the necessary dependencies and flags for a list of host object files.
#
# $1: List of object file names (no path)
#
host_objects = $(foreach object,$1,$(eval $(value _host_object)))
define _host_object
$(call host_file_flags,$(object),$(flags_object))
$(obj)/$(object): | $(obj)/. $(OBJ_DEPS)
-include $(obj)/$(object:.o=.d)
endef
#
# Adds the necessary dependencies and flags for a list of host programs.
#
# $1: List of program names (no path)
#
host_programs = $(foreach program,$1,$(eval $(value _host_program)))
define _host_program
$(call host_file_flags,$(program),$(flags_program))
ifeq ($($(program)-objs),)
$(program)-objs := $(program).o
endif
$(obj)/$(program): $($(program)-objs:%=$(obj)/%) | $(obj)/.
$(call host_objects,$($(program)-objs))
endef
#
# Includes a list of object files in the main static library (both host and
# target versions). Also adds the necessary dependencies and flags for each
# object file.
#
# $1: List of object file names (no path)
#
library_objects = $(foreach object,$1,$(eval $(value _library_object)))
define _library_object
# Add the host version of this library object
$(OBJ)/lib.a: $(obj)/$(object)
$(call host_objects,$(object))
# Add the target version of this library object
$(TGT)/lib.a: $(tgt)/$(object)
$(call target_objects,$(object))
endef
#
# Adds the necessary dependencies and flags for a list of target object files.
#
# $1: List of object file names (no path)
#
target_objects = $(foreach object,$1,$(eval $(value _target_object)))
define _target_object
$(call target_file_flags,$(object),$(flags_object))
$(tgt)/$(object): | $(tgt)/. $(OBJ_DEPS)
-include $(tgt)/$(object:.o=.d)
endef
#
# Descend into each of a list of directories. For every directory entered,
# recursively read and evaluate rules from its Makefile.
#
# $1: List of directories (relative to the root of the source tree)
#
descend = $(foreach subdir,$1,$(eval $(value _descend)))
define _descend
# Clear all per-directory variables
$(foreach var,$(filter %-objs %-y,$(.VARIABLES)),$(eval $(var) :=))
# Read the Makefile in this directory
$(src)/Makefile:;
include $(src)/Makefile
# Filter out directory dependencies
dirs-y := $(filter %/,$(foreach var,$(vars),$($(var)-y)))
buildprogs-y := $(filter-out %/,$(buildprogs-y))
hostprogs-y := $(filter-out %/,$(hostprogs-y))
lib-y := $(filter-out %/,$(lib-y))
obj-y := $(filter-out %/,$(obj-y))
test-y := $(filter-out %/,$(test-y))
tools-y := $(filter-out %/,$(tools-y))
# Combine all sources of flags
asflags-y += $(parent-asflags) $(subdir-asflags-y)
ccflags-y += $(parent-ccflags) $(subdir-ccflags-y)
cppflags-y += $(parent-cppflags) $(subdir-cppflags-y)
ldflags-y += $(parent-ldflags) $(subdir-ldflags-y)
# Handle file rules
$(call build_programs, $(buildprogs-y))
$(call host_programs, $(hostprogs-y))
$(call library_objects,$(lib-y))
$(call target_objects, $(obj-y))
$(call host_programs, $(test-y))
$(call host_programs, $(tools-y))
# Accumulate file names (library objects are handled above)
buildprogs-all+= $(buildprogs-y:%=$(obj)/%)
hostprogs-all += $(hostprogs-y:%=$(obj)/%)
obj-all += $(obj-y:%=$(tgt)/%)
test-all += $(test-y:%=$(obj)/%)
tools-all += $(tools-y:%=$(obj)/%)
# Push recursive flags
$(subdir)-asflags := $(parent-asflags)
$(subdir)-ccflags := $(parent-ccflags)
$(subdir)-cppflags := $(parent-cppflags)
$(subdir)-ldflags := $(parent-ldflags)
parent-asflags += $(subdir-asflags-y)
parent-ccflags += $(subdir-ccflags-y)
parent-cppflags += $(subdir-cppflags-y)
parent-ldflags += $(subdir-ldflags-y)
# Recursively descend into sub-directories
$(call descend,$(dirs-y:%/=$(subdir)/%))
# Pop recursive flags
parent-asflags := $($(subdir)-asflags)
parent-ccflags := $($(subdir)-ccflags)
parent-cppflags := $($(subdir)-cppflags)
parent-ldflags := $($(subdir)-ldflags)
# Clear this directory's flags
ifneq ($(filter undefine,$(.FEATURES)),)
undefine $(subdir)-asflags
undefine $(subdir)-ccflags
undefine $(subdir)-cppflags
undefine $(subdir)-ldflags
else
$(subdir)-asflags :=
$(subdir)-ccflags :=
$(subdir)-cppflags :=
$(subdir)-ldflags :=
endif
endef
|