3f9d446e33257e39ca07bac2bcb3f98464dedaad
[pub/USBasp.git] / LUFA / Build / lufa.build.in
1 #
2 # LUFA Library
3 # Copyright (C) Dean Camera, 2012.
4 #
5 # dean [at] fourwalledcubicle [dot] com
6 # www.lufa-lib.org
7 #
8
9 LUFA_BUILD_MODULES += BUILD
10 LUFA_BUILD_TARGETS += size checksource all elf hex clean
11
12 # -----------------------------------------------------------------------------
13 # LUFA Compiler Buildsystem Makefile Module.
14 # -----------------------------------------------------------------------------
15 # DESCRIPTION:
16 # Provides a set of targets to build a C, C++ and/or Assembly application
17 # via the AVR-GCC compiler.
18 # -----------------------------------------------------------------------------
19 # TARGETS:
20 #
21 # size - List application size
22 # checksource - Check existance of listed input source files
23 # all - Build application and list size
24 # elf - Build application ELF debug object file
25 # hex - Build application HEX object files
26 # clean - Remove output files
27 #
28 # MANDATORY PARAMETERS:
29 #
30 # TARGET - Application name
31 # ARCH - Device architecture name
32 # MCU - Microcontroller device model name
33 # SRC - List of input source files (.c, .cpp/.c++, .S)
34 # F_USB - Speed of the input clock of the USB controller
35 # in Hz
36 # LUFA_PATH - Path to the LUFA library core
37 #
38 # OPTIONAL PARAMETERS:
39 #
40 # BOARD - LUFA board hardware
41 # OPTIMIZATION - Optimization level
42 # C_STANDARD - C Language Standard to use
43 # CPP_STANDARD - C++ Language Standard to use
44 # F_CPU - Speed of the CPU, in Hz
45 # CC_FLAGS - Flags to pass to the compiler
46 # LD_FLAGS - Flags to pass to the linker
47 #
48 # -----------------------------------------------------------------------------
49
50 # Output Messages
51 MSG_BUILD_BEGIN = Begin compilation of project \"$(TARGET)\"...
52 MSG_BUILD_END = Finished building project \"$(TARGET)\"...
53 MSG_COMPILE_CMD = ' [CC] :'
54 MSG_REMOVE_CMD = ' [RM] :'
55 MSG_LINKER_CMD = ' [LNK] :'
56 MSG_SIZE_CMD = ' [SIZE] :'
57 MSG_OBJCPY_CMD = ' [OBJCPY] :'
58 MSG_OBJDMP_CMD = ' [OBJDMP] :'
59
60 # Sanity check the user MCU, TARGET, ARCH, SRC, F_USB and LUFA_PATH makefile options
61 ifeq ($(TARGET),)
62 $(error Makefile TARGET value not set.)
63 endif
64 ifeq ($(ARCH),)
65 $(error Makefile ARCH value not set.)
66 endif
67 ifeq ($(MCU),)
68 $(error Makefile MCU value not set.)
69 endif
70 ifeq ($(SRC),)
71 $(error Makefile SRC value not set.)
72 endif
73 ifeq ($(F_USB),)
74 $(error Makefile F_USB value not set.)
75 endif
76 ifeq ($(LUFA_PATH),)
77 $(error Makefile LUFA_PATH value not set.)
78 endif
79
80 # Default values of user-supplied variables
81 BOARD ?= NONE
82 OPTIMIZATION ?= s
83 F_CPU ?=
84 C_STANDARD ?= c99
85 CPP_STANDARD ?= c++98
86
87 # Convert input source file list to differentiate them by type
88 C_SOURCE = $(filter %.c, $(SRC))
89 CPP_SOURCE = $(filter %.cpp, $(SRC))
90 ASM_SOURCE = $(filter %.S, $(SRC))
91
92 # Convert input source filenames into a list of required output object files
93 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
94
95 # Create a list of flags to pass to the compiler
96 ifeq ($(ARCH),AVR8)
97 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
98 CROSS = avr-
99 else ifeq ($(ARCH),XMEGA)
100 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
101 CROSS = avr-
102 else ifeq ($(ARCH),UC3)
103 CC_FLAGS += -mpart=$(MCU) -g3 -masm-addr-pseudos
104 CROSS = avr32-
105 endif
106
107 CC_FLAGS += -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
108 CC_FLAGS += -Wall -Wstrict-prototypes
109 CC_FLAGS += -I. -I$(LUFA_PATH)/.. -pipe
110 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
111 ifneq ($(F_CPU),)
112 CC_FLAGS += -DF_CPU=$(F_CPU)UL
113 endif
114
115 # Create a list of flags to pass to the linker
116 LD_FLAGS += -Wl,-Map=$(TARGET).map,--cref -Wl,--relax -Wl,--gc-sections -lm
117
118 # Create a list of unknown source file types, if any are found throw an error
119 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
120 ifneq ($(UNKNOWN_SOURCE),)
121 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
122 endif
123
124 # Determine flags to pass to the size utility based on its reported features
125 SIZE_MCU_FLAG = $(shell avr-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
126 SIZE_FORMAT_FLAG = $(shell avr-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
127
128
129 begin:
130 @echo ""
131 @echo $(MSG_BUILD_BEGIN)
132
133 end:
134 @echo $(MSG_BUILD_END)
135 @echo ""
136
137 checksource:
138 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
139 if [ -f $$f ]; then \
140 echo "Found Source File: $$f" ; \
141 else \
142 echo "Source File Not Found: $$f" ; \
143 fi; \
144 done
145
146 size:
147 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
148 @if test -f $(TARGET).elf; then \
149 avr-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
150 fi
151
152 .PHONY: begin hex lss end size
153 all: begin hex end size
154
155 elf: $(TARGET).elf
156 hex: $(TARGET).hex $(TARGET).eep
157 lss: $(TARGET).lss
158
159 %.o: %.c
160 @echo $(MSG_COMPILE_CMD) Compiling C file \"$^\"
161 $(CROSS)gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) --std=$(C_STANDARD) $< -o $@
162
163 %.o: %.cpp
164 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$^\"
165 $(CROSS)gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) --std=$(CPP_STANDARD) -x c++ $< -o $@
166
167 %.o: %.S
168 @echo $(MSG_COMPILE_CMD) Assembling \"$^\"
169 $(CROSS)gcc -c $(CC_FLAGS) -x assembler-with-cpp $< -o $@
170
171 .PRECIOUS : $(OBJECT_FILES)
172 %.elf: $(OBJECT_FILES)
173 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
174 $(CROSS)gcc $^ $(CC_FLAGS) $(LD_FLAGS) -o $@
175
176 %.hex: %.elf
177 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$@\"
178 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
179
180 %.eep: %.elf
181 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$@\"
182 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
183
184 %.lss: %.elf
185 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$@\"
186 $(CROSS)objdump -h -S -z $< > $@
187
188 clean:
189 @echo $(MSG_REMOVE_CMD) Removing object files \"$(OBJECT_FILES)\"
190 rm -f $(OBJECT_FILES)
191 @echo $(MSG_REMOVE_CMD) Removing output files \"$(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss\"
192 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss