6c9133f8375d7369c6bccdf9c4101a4fa47b9f6e
[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 MCU ?= $(error Makefile MCU value not set.)
68 TARGET ?= $(error Makefile TARGET value not set.)
69 ARCH ?= $(error Makefile ARCH value not set.)
70 SRC ?= $(error Makefile SRC value not set.)
71 F_USB ?= $(error Makefile F_USB value not set.)
72 LUFA_PATH ?= $(error Makefile LUFA_PATH value not set.)
73
74 # Default values of user-supplied variables
75 BOARD ?= NONE
76 OPTIMIZATION ?= s
77 F_CPU ?=
78 C_STANDARD ?= gnu99
79 CPP_STANDARD ?= gnu++98
80 C_FLAGS ?=
81 CPP_FLAGS ?=
82 ASM_FLAGS ?=
83 CC_FLAGS ?=
84
85 # Convert input source file list to differentiate them by type
86 C_SOURCE = $(filter %.c, $(SRC))
87 CPP_SOURCE = $(filter %.cpp, $(SRC))
88 ASM_SOURCE = $(filter %.S, $(SRC))
89
90 # Convert input source filenames into a list of required output object files
91 OBJECT_FILES = $(filter %.o, $(C_SOURCE:%.c=%.o) $(CPP_SOURCE:%.cpp=%.o) $(ASM_SOURCE:%.S=%.o))
92
93 # Create a list of flags to pass to the compiler
94 ifeq ($(ARCH), AVR8)
95 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
96 CROSS = avr-
97 else ifeq ($(ARCH), XMEGA)
98 CC_FLAGS += -mmcu=$(MCU) -gdwarf-2 -fshort-enums -fno-inline-small-functions -fpack-struct
99 CROSS = avr-
100 else ifeq ($(ARCH), UC3)
101 CC_FLAGS += -mpart=$(MCU) -g3 -masm-addr-pseudos
102 CROSS = avr32-
103 else
104 $(error Unsupported architecture.)
105 endif
106 CC_FLAGS += -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
107 CC_FLAGS += -I. -I$(LUFA_PATH)/..
108 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
109 ifneq ($(F_CPU),)
110 CC_FLAGS += -DF_CPU=$(F_CPU)UL
111 endif
112
113 # Additional language specific compiler flags
114 C_FLAGS += -Wstrict-prototypes
115
116 # Create a list of flags to pass to the linker
117 LD_FLAGS += -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections -lm
118 ifneq ($(F_CPU), UC3)
119 LD_FLAGS += -Wl,--relax
120 endif
121
122 # Create a list of unknown source file types, if any are found throw an error
123 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
124 ifneq ($(UNKNOWN_SOURCE),)
125 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
126 endif
127
128 # Determine flags to pass to the size utility based on its reported features
129 SIZE_MCU_FLAG = $(shell avr-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
130 SIZE_FORMAT_FLAG = $(shell avr-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
131
132
133 begin:
134 @echo ""
135 @echo $(MSG_BUILD_BEGIN)
136
137 end:
138 @echo $(MSG_BUILD_END)
139 @echo ""
140
141 checksource:
142 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
143 if [ -f $$f ]; then \
144 echo "Found Source File: $$f" ; \
145 else \
146 echo "Source File Not Found: $$f" ; \
147 fi; \
148 done
149
150 size:
151 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
152 @if test -f $(TARGET).elf; then \
153 avr-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
154 fi
155
156 .PHONY: begin hex lss end size
157 all: begin hex end size
158
159 elf: $(TARGET).elf
160 hex: $(TARGET).hex $(TARGET).eep
161 lss: $(TARGET).lss
162
163 %.o: %.c
164 @echo $(MSG_COMPILE_CMD) Compiling C file \"$^\"
165 $(CROSS)gcc -c $(CC_FLAGS) $(C_FLAGS) -O$(OPTIMIZATION) -std=$(C_STANDARD) $< -o $@
166
167 %.o: %.cpp
168 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$^\"
169 $(CROSS)gcc -c $(CC_FLAGS) $(CPP_FLAGS) -O$(OPTIMIZATION) -std=$(CPP_STANDARD) -x c++ $< -o $@
170
171 %.o: %.S
172 @echo $(MSG_COMPILE_CMD) Assembling \"$^\"
173 $(CROSS)gcc -c $(CC_FLAGS) $(ASM_FLAGS) -x assembler-with-cpp $< -o $@
174
175 .PRECIOUS : $(OBJECT_FILES)
176 %.elf: $(OBJECT_FILES)
177 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
178 $(CROSS)gcc $^ $(CC_FLAGS) $(LD_FLAGS) -o $@
179
180 %.hex: %.elf
181 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$@\"
182 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
183
184 %.eep: %.elf
185 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$@\"
186 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
187
188 %.lss: %.elf
189 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$@\"
190 $(CROSS)objdump -h -S -z $< > $@
191
192 clean:
193 @echo $(MSG_REMOVE_CMD) Removing object files \"$(notdir $(OBJECT_FILES))\"
194 rm -f $(OBJECT_FILES)
195 @echo $(MSG_REMOVE_CMD) Removing output files \"$(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss\"
196 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss