-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
129 lines (102 loc) · 4.22 KB
/
Copy pathMakefile
File metadata and controls
129 lines (102 loc) · 4.22 KB
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
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright 2020 RnD Center "ELVEES", JSC
CONFIG_HEADER_PATH := $(CURDIR)/include/config.h
SRC_DIR := $(CURDIR)/src
TARGET := $(SRC_DIR)/ddrinit
CROSS_COMPILE ?=
override CFLAGS += -I$(CURDIR)/include -Wall -ffreestanding -fno-stack-protector -Os \
-fstack-usage -fdump-ipa-cgraph -fdata-sections -ffunction-sections
override LDFLAGS += -nostdlib -nostartfiles -Wl,--gc-sections -Wl,-Map=$(TARGET).map
# Don't traverse above current directory while searching for .git
BUILD_COMMIT := $(shell git log --pretty=tformat:"%h" -n1 .)
ifneq ($(BUILD_COMMIT),)
override CFLAGS += -DVERSION=\"$(BUILD_COMMIT)\"
endif
## Build artifacts
all: $(TARGET).bin $(TARGET).dis
ifneq ($(FRAGMENTS),)
KCONFIG_CONFIG := .config
PLATFORM := $$(sed -n 's/^CONFIG_PLATFORM_\(.*\)=y/\L\1/p' $(KCONFIG_CONFIG))
endif
## Run interactive Python based configurator
menuconfig:
menuconfig Kconfig
## Resolve any unresolved symbols in .config
oldconfig:
oldconfig Kconfig
## Save a minimal configuration file as defconfig
savedefconfig:
savedefconfig
## Apply minimal configuration from configs/<board>_defconfig
%_defconfig:
defconfig $(CURDIR)/configs/$@
ifneq ($(FRAGMENTS),)
KCONFIG_CONFIG=$(KCONFIG_CONFIG) scripts/merge_config.sh \
-m $(KCONFIG_CONFIG) $(foreach f,$(subst :, ,$(FRAGMENTS)), \
$(CURDIR)/fragments/$(PLATFORM)/$(f).fragment)
olddefconfig Kconfig
endif
$(CONFIG_HEADER_PATH): .config
genconfig --header-path $(CONFIG_HEADER_PATH) Kconfig
-include .config
include $(shell find $(SRC_DIR) -name Makefile)
srcs := $(shell for f in $(obj-y); do find $(SRC_DIR) -name $$f; done)
objs := $(srcs:.c=.o)
objs := $(objs:.S=.o)
objs := $(sort $(objs))
ifneq ($(linkfile),)
linkfile := $(shell find $(SRC_DIR) -name '$(linkfile)')
endif
%.o : %.c $(CONFIG_HEADER_PATH)
$(CROSS_COMPILE)gcc -c $(CFLAGS) $< -o $@
%.o : %.S $(CONFIG_HEADER_PATH)
$(CROSS_COMPILE)gcc -c $(CFLAGS) $< -o $@
$(TARGET).ld : $(linkfile) $(CONFIG_HEADER_PATH)
$(CROSS_COMPILE)gcc $(CFLAGS) -E $< -P -o $@
%.bin : %.elf
$(CROSS_COMPILE)objcopy -O binary $< $@
%.dis : %.elf
$(CROSS_COMPILE)objdump -D $< > $@
$(TARGET).elf: $(TARGET).ld $(objs)
$(CROSS_COMPILE)gcc -T$^ $(LDFLAGS) -o $@
## Check stack usage
check-stack: $(TARGET).elf
find . -name '*.cgraph' | grep -v stack-usage-log | xargs cat > stack-usage-log.cgraph; \
find . -name '*.su' | grep -v stack-usage-log | xargs cat > stack-usage-log.su; \
echo -n "stack-usage=" > $(TARGET).info; \
python3 scripts/stack-usage.py --csv stack-usage.csv --json stack-usage.json \
>> $(TARGET).info; \
$(CROSS_COMPILE)nm -t x -S --size-sort --reverse-sort $< >> $(TARGET).info
#TODO: Add desired stack size in defconfig
#TODO: Compare stack size with one defined in defconfig
## Remove build artifacts
clean:
find $(SRC_DIR) -name *.o | xargs $(RM) -f
find . -name *.su | xargs $(RM) -f
find . -name *.cgraph | xargs $(RM) -f
$(RM) -f stack-usage.json stack-usage.csv
$(RM) -f $(TARGET).elf $(TARGET).bin $(TARGET).dis $(TARGET).map $(TARGET).ld $(CONFIG_HEADER_PATH)
## Remove build artifacts and .config file
clean-all: clean
$(RM) -f .config
## Show available targets
help: Makefile
# Inspired by https://gist.github.com/klmr/575726c7e05d8780505a
@echo "$$(tput bold)Available rules:$$(tput sgr0)";echo;sed -ne"/^## /{h;s/.*//;:d" \
-e"H;n;s/^## //;td" -e"s/:.*//;G;s/\\n## /---/;s/\\n/ /g;p;}" \
${MAKEFILE_LIST}|LC_ALL='C' sort -f|awk -F --- -v n=$$(tput cols) -v i=19 -v \
a="$$(tput setaf 6)" -v z="$$(tput sgr0)" '{printf"%s%*s%s ",a,-i,$$1,z; \
m=split($$2,w," ");l=n-i;for(j=1;j<=m;j++){l-=length(w[j])+1;if(l<= 0) \
{l=n-i-length(w[j])-1;printf"\n%*s ",-i," ";}printf"%s ",w[j];}printf"\n";}' \
|more $(shell test $(shell uname) == Darwin && echo '-Xr')
## Install build artifacts to $DESTDIR directory
install:
install -D -m 0755 $(TARGET).bin $(DESTDIR)/ddrinit.bin
install -D -m 0755 $(TARGET).dis $(DESTDIR)/ddrinit.dis
install -D -m 0755 $(TARGET).elf $(DESTDIR)/ddrinit.elf
install -D -m 0755 $(TARGET).map $(DESTDIR)/ddrinit.map
install -D -m 0755 $(TARGET).ld $(DESTDIR)/ddrinit.ld
ifneq ($(wildcard $(TARGET).info),)
install -D -m 0755 $(TARGET).info $(DESTDIR)/ddrinit.info
endif
.PHONY: all help clean clean-all menuconfig oldconfig savedefconfig install check-stack