1# Copyright (C) 2022 Huawei Technologies Co., Ltd. 2# Licensed under the Mulan PSL v2. 3# You can use this software according to the terms and conditions of the Mulan PSL v2. 4# You may obtain a copy of Mulan PSL v2 at: 5# http://license.coscl.org.cn/MulanPSL2 6# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR 7# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR 8# PURPOSE. 9# See the Mulan PSL v2 for more details. 10 11## This function will generate $(1)_objs variables which provided 12## the objects needed to compile to the $(1) module. 13## 14## Args: 15## $(1) : the module name 16 17define eval_objs 18ifneq ($(NO_OBJFILE_SORT),y) 19$(1)_c_obj_files = $$(sort $$(patsubst %.c,%.o,$$($(1)_c_files))) 20$(1)_cpp_obj_files = $$(sort $$(patsubst %.cpp,%.o,$$($(1)_cpp_files))) 21$(1)_asm_obj_files = $$(sort $$(patsubst %.s,%.o,$$($(1)_asm_files))) 22$(1)_ASM_obj_files = $$(sort $$(patsubst %.S,%.o,$$($(1)_ASM_files))) 23else 24$(1)_c_obj_files = $$(patsubst %.c,%.o,$$($(1)_c_files)) 25$(1)_cpp_obj_files = $$(patsubst %.cpp,%.o,$$($(1)_cpp_files)) 26$(1)_asm_obj_files = $$(patsubst %.s,%.o,$$($(1)_asm_files)) 27$(1)_ASM_obj_files = $$(patsubst %.S,%.o,$$($(1)_ASM_files)) 28endif 29 30$(1)_objs = $$(addprefix $(BUILD_DIR)/,$$($(1)_c_obj_files)) 31$(1)_objs += $$(addprefix $(BUILD_DIR)/,$$($(1)_cpp_obj_files)) 32$(1)_objs += $$(addprefix $(BUILD_DIR)/,$$($(1)_asm_obj_files)) 33$(1)_objs += $$(addprefix $(BUILD_DIR)/,$$($(1)_ASM_obj_files)) 34$(1)_objs += $$(addprefix $(BUILD_DIR)/,$$(patsubst %.c,%.o,$$(CFILES))) 35$(1)_objs += $$(addprefix $(BUILD_DIR)/,$$(patsubst %.cpp,%.o,$$(CPPFILES))) 36$(1)_objs += $$(addprefix $(BUILD_DIR)/,$$(patsubst %.s,%.o,$$(asmFILES))) 37$(1)_objs += $$(addprefix $(BUILD_DIR)/,$$(patsubst %.S,%.o,$$(ASMFILES))) 38endef 39 40## provide the static linked target compile rules. 41## Args: 42## $(1) : the $(MODULE_FOLDER) needed to be compile. 43## $(2) : the $(MODULE_FILE) needed to be generated. 44define eval_libs 45$(2): $$($(1)_objs) 46 @echo "[ AR2 ] $$@ $$^" 47 $$(VER)$$(AR) rD $$@ $$^ 48endef 49 50define eval_elf 51$(2): $$($(1)_objs) 52 @echo "[ LD ] $$@" 53 @test -d $$(dir $$@) || mkdir -p $$(dir $$@) 54 $$(VER)$$(LD) $$($(1)_objs) $$($(1)_extracted_objs) $$(LDFLAGS) --build-id=none \ 55 $$($(1)_LDFLAGS) -o $$@ 56 $$(OBJCOPY) $$@ 57endef 58 59## This function will generate $(1)_extracted_objs variables which are 60## extracted from the $(2) archive file and provided the objects needed to 61## compile to the $(1) module. 62## $(1) : $(MODULE_FOLDER) needed to be compile. 63## $(2) : $(PREBUILD_ARCHIVE) needed to be extracted. 64## $(3) : the $(BUILD_DIR) where target generates. 65define eval_extracted_objs 66$(1)_extracted_objs += $$(addprefix $(3)/,$$(shell $$(AR) -t $(2))) 67endef 68 69## provide an extra archive dependence for the app/driver target 70## and the extraction rule 71## $(1) : the $(BUILD_DIR) where target generates. 72## $(2) : the $(MODULE_FILE) needed to be generated. 73## $(3) : the $(PREBUILD_ARCHIVE) needed to be extracted. 74define eval_extract_ar 75$(1)/.extracted: $(3) 76 @echo "extracting $(3)" 77 @test -d $$(dir $$@) || mkdir -p $$(dir $$@) 78 $$(VER)cd $(1) && $$(AR) -x $(3) 79 @touch $$@ 80$(2): $(1)/.extracted 81endef 82 83GENERAL_OPTIONS := -Wdate-time -Wfloat-equal -Wshadow -Wformat=2 -fsigned-char -fno-strict-aliasing \ 84 -pipe -Wall -Wextra -Werror -fno-common 85 86FILTER_MODULE := open_source openssl bounds_checking_function 87 88# we need function is_filter_module to remove open source files 89is_filter_module = $(strip $(foreach module,$(FILTER_MODULE),$(findstring $(module),$1))) 90 91last_component = $(shell echo $1 | awk -F '//' '{print $$NF}') 92 93# we need function uniq to deduplication 94uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1))) 95 96is_abs_path = $(filter $(shell echo $1 | head -c 1),/) 97 98## compile object file rules: 99$(BUILD_DIR)/%.o: %.cpp 100 @test -d $(dir $@) || mkdir -p $(dir $@) 101 @echo "[ CXX ] $@" 102 $(VER)$(CXX) -MMD -MP -MF $(BUILD_DIR)/$<.d $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $< 103 104$(BUILD_DIR)/%.o: %.cxx 105 @test -d $(dir $@) || mkdir -p $(dir $@) 106 @echo "[ CXX ] $@" 107 $(VER)$(CXX) -MMD -MP -MF $(BUILD_DIR)/$<.d $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $< 108 109$(BUILD_DIR)/%.o: %.c 110 @test -d $(dir $@) || mkdir -p $(dir $@) 111 @echo "[ CC ] $@ " 112 $(VER)$(CC) -MMD -MP -MF $(BUILD_DIR)/$<.d $(if $(call is_filter_module,$(call last_component,$@)), \ 113 $(filter-out -Werror, $(CFLAGS) $(CPPFLAGS)),$(call uniq, \ 114 $(CFLAGS) $(CPPFLAGS) $(GENERAL_OPTIONS))) -c -o $@ $< 115 116$(BUILD_DIR)/%.o: %.S 117 @test -d $(dir $@) || mkdir -p $(dir $@) 118 @echo "[ ASM ] $@" 119 $(VER)$(CC) -MMD -MP -MF $(BUILD_DIR)/$<.d $(CPPFLAGS) $(ASFLAGS) -D__ASM__ -c -o $@ $< 120 121$(BUILD_DIR)/%.o: %.s 122 @test -d $(dir $@) || mkdir -p $(dir $@) 123 @echo "[ asm ] $@" 124 $(VER)$(CC) -MMD -MP -MF $(BUILD_DIR)/$<.d $(CPPFLAGS) $(ASFLAGS) -c -o $@ $< 125 126CPPFLAGS += $(inc-flags) 127CFLAGS += $(flags) $(c-flags) 128CXXFLAGS += $(cxx-flags) 129ASFLAGS += $(asflags) 130 131rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)) 132 133DEPS := $(patsubst %.c,$(BUILD_DIR)/%.c.d,$(call rwildcard,./,*.c)) 134DEPS += $(patsubst %.cxx,%$(BUILD_DIR)/.cxx.d,$(call rwildcard,./,*.cxx)) 135DEPS += $(patsubst %.cpp,$(BUILD_DIR)/%.cpp.d,$(call rwildcard,./,*.cpp)) 136DEPS += $(patsubst %.S,$(BUILD_DIR)/%.S.d,$(call rwildcard,./,*.S)) 137DEPS += $(patsubst %.s,$(BUILD_DIR)/%.s.d,$(call rwildcard,./,*.s)) 138 139ifneq ($(MAKECMDGOALS),clean) 140-include $(DEPS) 141endif 142