-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
173 lines (134 loc) · 4.77 KB
/
Makefile
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
# Copyright (C) 2015 Jeff Ciesielski <[email protected]>
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.
ifeq ($(APP),)
ifeq ($(wildcard app.default),)
$(error APP is not defined. Pass it in as APP= or create a config.mk file)
else
APP = $(shell cat app.default)
endif
else
$(shell echo "$(APP)" > app.default)
endif
ifeq ($(BOARD),)
ifeq ($(wildcard board.default),)
$(error BOARD is not defined. Pass it in as BOARD= or create a config.mk file)
else
BOARD = $(shell cat board.default)
endif
else
$(shell echo "$(BOARD)" > board.default)
endif
APP_PATH := app/$(APP)
LIB_PATH := libs
BOARD_PATH := board/$(BOARD)
APP_INCLUDES += -I$(APP_PATH)
# load the board specific configuration
include $(APP_PATH)/config.mk
include $(BOARD_PATH)/config.mk
ifeq ($(CPU_TYPE),)
$(error CPU_TYPE is not defined, please ensure it is defined in your cpu config.mk)
endif
PREFIX ?= arm-none-eabi-
CC := $(PREFIX)gcc
AS := $(PREFIX)as
CXX := $(PREFIX)g++
AR := $(PREFIX)gcc-ar
NM := $(PREFIX)gcc-nm
OBJCOPY := $(PREFIX)objcopy
OBJDUMP := $(PREFIX)objdump
SIZE := $(PREFIX)size
RANLIB := $(PREFIX)gcc-ranlib
OPENOCD := openocd
DDD := ddd
GDB := $(PREFIX)gdb
NIM := $(shell which nim)
# Grab the nim base include path
NIM_PATHS = $(shell nim dump 2>&1 | grep /lib)
NIM_INCLUDES = $(patsubst %, -I%, $(NIM_PATHS))
LIBS = -lnosys
INCLUDES += $(CPU_INCLUDES) $(BOARD_INCLUDES) $(LIB_INCLUDES) $(APP_INCLUDES) $(NIM_INCLUDES)
INCLUDES += -Icpu/common
CFLAGS = $(INCLUDES) $(CPU_DEFINES) $(BOARD_DEFINES) $(APP_DEFINES) \
$(CPU_FLAGS) -Os -Wall -Wno-address -Wno-pragmas \
-Wno-unused-but-set-variable -fno-common -c -mthumb \
-ffunction-sections -fdata-sections -flto -mcpu=$(CPU_TYPE) \
-MD -std=gnu99
ASFLAGS = -mcpu=$(CPU_TYPE) $(FPU) -g -Wa,--warn
ARFLAGS = rcs
LDFLAGS ?= --specs=nano.specs -lc -lgcc $(LIBS) -mcpu=$(CPU_TYPE) -g -gdwarf-2 \
-L. -Lcpu/common -L$(APP_PATH) -T$(LINK_MEM) \
-nostartfiles -Wl,--gc-sections -mthumb -mcpu=$(CPU_TYPE) \
-msoft-float -ffunction-sections -fdata-sections -flto -Os -Wl,-Map,$(TARGET).map
# Be silent per default, but 'make V=1' will show all compiler calls.
ifneq ($(V),1)
Q := @
# Do not print "Entering directory ...".
MAKEFLAGS += --no-print-directory
# Redirect stdout/stderr for chatty tools
NOOUT = 1> /dev/null 2> /dev/null
endif
# This file is dynamically created based on the libraries in the libs/ folder
-include libs.mk
LIB_C_FILES += $(wildcard libs/newlib_stubs/*.c)
LIB_INCLUDES += -Ilibs/rtos_nim
APP_O_FILES = $(APP_C_FILES:.c=.o) $(APP_S_FILES:.s=.o)
CPU_O_FILES = $(CPU_C_FILES:.c=.o) $(CPU_S_FILES:.s=.o)
LIB_O_FILES = $(LIB_C_FILES:.c=.o) $(LIB_S_FILES:.s=.o)
ifeq ($(TARGET),)
$(error TARGET is not defined, please define it in your applications config.mk)
endif
LIB_CONFIGS = $(wildcard $(LIB_PATH)/*.mk)
NIM_IMPORTS = --import:newlib
NIMFLAGS = --compile_only
NIMFLAGS += -d:StandaloneHeapSize=4
NIMFLAGS += -d:release
NIMFLAGS += -p=libs/narm_runtime
NIMFLAGS += -p=libs/narmos $(NIM_IMPORTS)
all: $(TARGET).bin
libs.mk: Makefile
@printf " Generating library includes\n"
@( $(foreach L,$(LIB_CONFIGS),echo 'include $L';) ) >$@
$(TARGET).bin: $(TARGET).elf
@printf " OBJCOPY $(subst $(shell pwd)/,,$(@))\n"
$(Q)$(OBJCOPY) -Obinary $< $@
nimcache:
@printf " Regenerating Nim cache\n"
$(Q)nim c $(NIMFLAGS) $(APP_PATH)/main.nim
$(TARGET).elf: nimcache libs.mk $(APP_O_FILES) $(CPU_O_FILES) $(LIB_O_FILES)
@printf " LD $(subst $(shell pwd)/,,$(@))\n"
$(Q)$(CC) -o $@ $(APP_O_FILES) $(LIB_O_FILES) $(CPU_O_FILES) $(LDFLAGS)
$(Q)$(SIZE) $(TARGET).elf
%.nim: %.c
@printf " NIMC $(subst $(shell pwd)/,,$(@))\n"
%.o: %.c
@printf " CC $(subst $(shell pwd)/,,$(@))\n"
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
%.o: %.s
@printf " AS $(subst $(shell pwd)/,,$(@))\n"
$(Q)$(CC) $(ASFLAGS) -c -o $@ $<
clean:
$(Q)rm -f \
$(APP_D_FILES) \
$(APP_O_FILES) \
$(CPU_D_FILES) \
$(CPU_O_FILES) \
$(LIB_D_FILES) \
$(LIB_O_FILES) \
$(TARGET).bin $(TARGET).elf \
$(TARGET).map \
$(LIBS_ALL) \
libs.mk \
$(APP_DIR)/nimcache
st-flash: $(TARGET).bin
sudo st-flash write $(TARGET).bin 0x08000000
debug: $(TARGET).elf
$(OPENOCD) -f $(BOARD_PATH)/board.ocd
flash: $(TARGET).elf
$(OPENOCD) -f $(BOARD_PATH)/board.ocd -c "program $(TARGET).elf verify" -c "reset run" -c "exit"
ddd: $(TARGET).elf
$(DDD) --eval-command="target remote localhost:3333" --debugger $(GDB) $(TARGET).elf
gdb: $(TARGET).elf
$(GDB) -ex "target ext localhost:3333" -ex "mon reset halt" -ex "mon arm semihosting enable" $(TARGET).elf
.PHONY: clean st-flash debug flash ddd gdb nimcache