1# Extra make rules for components containing ULP coprocessor code.
2#
3# ULP program(s) gets built and linked into the application.
4# Steps taken here are explained in docs/ulp.rst
5
6# Define names for files generated at different stages
7ULP_ELF := $(ULP_APP_NAME).elf
8ULP_MAP := $(ULP_ELF:.elf=.map)
9ULP_SYM := $(ULP_ELF:.elf=.sym)
10ULP_BIN := $(ULP_ELF:.elf=.bin)
11ULP_EXPORTS_LD := $(ULP_ELF:.elf=.ld)
12ULP_EXPORTS_HEADER := $(ULP_ELF:.elf=.h)
13ULP_LD_SCRIPT := $(ULP_ELF:.elf=.common.ld)
14
15ULP_OBJECTS := $(notdir $(ULP_S_SOURCES:.S=.ulp.o))
16ULP_DEP := $(notdir $(ULP_S_SOURCES:.S=.ulp.d)) $(ULP_LD_SCRIPT:.ld=.d)
17ULP_PREPROCESSED := $(notdir $(ULP_S_SOURCES:.S=.ulp.pS))
18ULP_LISTINGS := $(notdir $(ULP_S_SOURCES:.S=.ulp.lst))
19
20ULP_PREPROCESSOR_ARGS := \
21	$(addprefix -I ,$(COMPONENT_INCLUDES)) \
22	$(addprefix -I ,$(COMPONENT_EXTRA_INCLUDES)) \
23	-I$(COMPONENT_PATH) -D__ASSEMBLER__
24
25-include $(ULP_DEP)
26
27# Check the assembler version
28include $(IDF_PATH)/components/ulp/toolchain_ulp_version.mk
29# $(ULP_AS) --version output might be localized, for example the first line could be
30# "Ensamblador (GNU Binutils) 2.28.51-esp-20191205 de GNU" instead of
31# "GNU assembler (GNU Binutils) 2.28.51-esp-20191205".
32ULP_AS_VER := $(shell $(ULP_AS) --version | sed -E -n 's/.+ \(GNU Binutils\) ([a-z0-9\.-]+)( .*)?/\1/gp')
33
34$(info Building ULP app $(ULP_APP_NAME))
35$(info ULP assembler version: $(ULP_AS_VER))
36
37ifeq (,$(findstring $(ULP_AS_VER), $(SUPPORTED_ULP_ASSEMBLER_VERSION)))
38$(info WARNING: ULP assembler version $(ULP_AS_VER) is not supported.)
39$(info Expected to see version: $(SUPPORTED_ULP_ASSEMBLER_VERSION))
40$(info Please check ESP-IDF ULP setup instructions and update the toolchain, or proceed at your own risk.)
41endif
42
43# Preprocess LD script used to link ULP program
44$(ULP_LD_SCRIPT): $(ULP_LD_TEMPLATE)
45	$(summary) CPP $(patsubst $(PWD)/%,%,$(CURDIR))/$@
46	$(CC) $(CPPFLAGS) -MT $(ULP_LD_SCRIPT) -E -P -xc -o $@ $(ULP_PREPROCESSOR_ARGS) $<
47
48# Generate preprocessed assembly files.
49# To inspect these preprocessed files, add a ".PRECIOUS: %.ulp.pS" rule.
50%.ulp.pS: $(COMPONENT_PATH)/ulp/%.S
51	$(summary) CPP $(patsubst $(PWD)/%,%,$<)
52	$(CC) $(CPPFLAGS) -MT $(patsubst %.ulp.pS,%.ulp.o,$@) -E -P -xc -o $@ $(ULP_PREPROCESSOR_ARGS) $<
53
54# Compiled preprocessed files into object files.
55%.ulp.o: %.ulp.pS
56	$(summary) ULP_AS $(patsubst $(PWD)/%,%,$(CURDIR))/$@
57	$(ULP_AS) -al=$(patsubst %.ulp.o,%.ulp.lst,$@) -o $@ $<
58
59# Link object files and generate map file
60$(ULP_ELF): $(ULP_OBJECTS) $(ULP_LD_SCRIPT)
61	$(summary) ULP_LD $(patsubst $(PWD)/%,%,$(CURDIR))/$@
62	$(ULP_LD) -o $@ -A elf32-esp32ulp -Map=$(ULP_MAP) -T $(ULP_LD_SCRIPT) $(ULP_OBJECTS)
63
64# Dump the list of global symbols in a convenient format.
65$(ULP_SYM): $(ULP_ELF)
66	$(ULP_NM) -g -f posix $< > $@
67
68# Dump the binary for inclusion into the project
69$(COMPONENT_BUILD_DIR)/$(ULP_BIN): $(ULP_ELF)
70	$(summary) ULP_BIN $(patsubst $(PWD)/%,%,$@)
71	$(ULP_OBJCOPY) -O binary $< $@
72
73# Left and right side of the rule are the same, but the right side
74# is given as an absolute path.
75# (Make can not resolve such things automatically)
76$(ULP_EXPORTS_HEADER): $(COMPONENT_BUILD_DIR)/$(ULP_EXPORTS_HEADER)
77
78# Artificial intermediate target to trigger generation of .h and .ld files.
79.INTERMEDIATE: $(COMPONENT_NAME)_ulp_mapgen_intermediate
80
81$(COMPONENT_BUILD_DIR)/$(ULP_EXPORTS_HEADER)\
82$(COMPONENT_BUILD_DIR)/$(ULP_EXPORTS_LD): $(COMPONENT_NAME)_ulp_mapgen_intermediate
83
84# Convert the symbols list into a header file and linker export script.
85$(COMPONENT_NAME)_ulp_mapgen_intermediate: $(ULP_SYM)
86	$(summary) ULP_MAPGEN $(patsubst $(PWD)/%,%,$(CURDIR))/$<
87	$(ULP_MAP_GEN) -s $(ULP_SYM) -o $(ULP_EXPORTS_LD:.ld=)
88
89# Building the component separately from the project should result in
90# ULP files being built.
91build: $(COMPONENT_BUILD_DIR)/$(ULP_EXPORTS_HEADER) \
92	$(COMPONENT_BUILD_DIR)/$(ULP_EXPORTS_LD) \
93	$(COMPONENT_BUILD_DIR)/$(ULP_BIN)
94
95# Objects listed as being dependent on $(ULP_EXPORTS_HEADER) must also
96# depend on $(ULP_SYM), to order build steps correctly.
97$(ULP_EXP_DEP_OBJECTS) : $(ULP_EXPORTS_HEADER) $(ULP_SYM)
98
99# Finally, set all the variables processed by the build system.
100COMPONENT_EXTRA_CLEAN += $(ULP_OBJECTS) \
101			$(ULP_LD_SCRIPT) \
102			$(ULP_PREPROCESSED) \
103			$(ULP_ELF) $(ULP_BIN) \
104			$(ULP_MAP) $(ULP_SYM) \
105			$(ULP_EXPORTS_LD) \
106			$(ULP_EXPORTS_HEADER) \
107			$(ULP_DEP) \
108			$(ULP_LISTINGS)
109
110COMPONENT_EMBED_FILES += $(COMPONENT_BUILD_DIR)/$(ULP_BIN)
111COMPONENT_ADD_LDFLAGS += -l$(COMPONENT_NAME) -T $(COMPONENT_BUILD_DIR)/$(ULP_EXPORTS_LD)
112COMPONENT_EXTRA_INCLUDES += $(COMPONENT_BUILD_DIR)
113