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