158eb24c0821d968b92bed09758ee80a00aa7b2f
[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 ?= gnu99
85 CPP_STANDARD ?= gnu++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 CC_FLAGS += -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections
107 CC_FLAGS += -Wall -Wstrict-prototypes
108 CC_FLAGS += -I. -I$(LUFA_PATH)/..
109 CC_FLAGS += -DARCH=ARCH_$(ARCH) -DBOARD=BOARD_$(BOARD) -DF_USB=$(F_USB)UL
110 ifneq ($(F_CPU),)
111 CC_FLAGS += -DF_CPU=$(F_CPU)UL
112 endif
113
114 # Create a list of flags to pass to the linker
115 LD_FLAGS += -Wl,-Map=$(TARGET).map,--cref -Wl,--relax -Wl,--gc-sections -lm
116
117 # Create a list of unknown source file types, if any are found throw an error
118 UNKNOWN_SOURCE = $(filter-out $(C_SOURCE) $(CPP_SOURCE) $(ASM_SOURCE), $(SRC))
119 ifneq ($(UNKNOWN_SOURCE),)
120 $(error Unknown input source formats: $(UNKNOWN_SOURCE))
121 endif
122
123 # Determine flags to pass to the size utility based on its reported features
124 SIZE_MCU_FLAG = $(shell avr-size --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
125 SIZE_FORMAT_FLAG = $(shell avr-size --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
126
127
128 begin:
129 @echo ""
130 @echo $(MSG_BUILD_BEGIN)
131
132 end:
133 @echo $(MSG_BUILD_END)
134 @echo ""
135
136 checksource:
137 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
138 if [ -f $$f ]; then \
139 echo "Found Source File: $$f" ; \
140 else \
141 echo "Source File Not Found: $$f" ; \
142 fi; \
143 done
144
145 size:
146 @echo $(MSG_SIZE_CMD) Determining size of \"$(TARGET).elf\"
147 @if test -f $(TARGET).elf; then \
148 avr-size $(SIZE_MCU_FLAG) $(SIZE_FORMAT_FLAG) $(TARGET).elf ; 2>/dev/null; \
149 fi
150
151 .PHONY: begin hex lss end size
152 all: begin hex end size
153
154 elf: $(TARGET).elf
155 hex: $(TARGET).hex $(TARGET).eep
156 lss: $(TARGET).lss
157
158 %.o: %.c
159 @echo $(MSG_COMPILE_CMD) Compiling C file \"$^\"
160 $(CROSS)gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) -std=$(C_STANDARD) $< -o $@
161
162 %.o: %.cpp
163 @echo $(MSG_COMPILE_CMD) Compiling C++ file \"$^\"
164 $(CROSS)gcc -c $(CC_FLAGS) -O$(OPTIMIZATION) -std=$(CPP_STANDARD) -x c++ $< -o $@
165
166 %.o: %.S
167 @echo $(MSG_COMPILE_CMD) Assembling \"$^\"
168 $(CROSS)gcc -c $(CC_FLAGS) -x assembler-with-cpp $< -o $@
169
170 .PRECIOUS : $(OBJECT_FILES)
171 %.elf: $(OBJECT_FILES)
172 @echo $(MSG_LINKER_CMD) Linking object files into \"$@\"
173 $(CROSS)gcc $^ $(CC_FLAGS) $(LD_FLAGS) -o $@
174
175 %.hex: %.elf
176 @echo $(MSG_OBJCPY_CMD) Extracting HEX file data from \"$@\"
177 $(CROSS)objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
178
179 %.eep: %.elf
180 @echo $(MSG_OBJCPY_CMD) Extracting EEP file data from \"$@\"
181 $(CROSS)objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@ || exit 0
182
183 %.lss: %.elf
184 @echo $(MSG_OBJDMP_CMD) Extracting LSS file data from \"$@\"
185 $(CROSS)objdump -h -S -z $< > $@
186
187 clean:
188 @echo $(MSG_REMOVE_CMD) Removing object files \"$(OBJECT_FILES)\"
189 rm -f $(OBJECT_FILES)
190 @echo $(MSG_REMOVE_CMD) Removing output files \"$(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss\"
191 rm -f $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(TARGET).lss