makefile (1626B)
1 CC = avr-gcc 2 CFLAGS = -Os -DF_CPU=12000000 -flto 3 CPPFLAGS = -std=c++14 4 TARGET_ARCH = -mmcu=atmega644p 5 6 DEPFLAGS = -MT $@ -MMD -MP -MF .$*.d 7 8 COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c 9 10 # Create our phony targets to avoid files interfering 11 .PHONY: clean install check 12 13 # Generate a list of sources and objects 14 C_SOURCES = $(wildcard *.c) 15 CPP_SOURCES = $(wildcard *.cpp) 16 ASM_SOURCES = $(wildcard *.s) 17 OBJECTS = $(C_SOURCES:%.c=.%.o) $(CPP_SOURCES:%.cpp=.%.o) $(ASM_SOURCES:%.s=.%.o) 18 19 program.hex: program.elf 20 @ avr-objcopy -O ihex program.elf program.hex 21 22 # The final target, with dependancies generated from the sources list 23 program.elf: $(OBJECTS) 24 @ $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -Wl,--gc-sections -o $@ $^ 25 26 # The install target 27 install: program.hex 28 @ avrdude -c usbasp -p m644p -D -U $< 29 30 verify: program.hex 31 @ avrdude -c usbasp -p m644p -n -U flash:v:$< 32 33 # Clean up everything that we generate 34 clean: 35 @ rm -f program.hex program.elf .*.o .*.d 36 37 # Delete the built-in rules for building object files from .c files... 38 %.o : %.c 39 # ...so that our rule is used instead. 40 .%.o : %.c .%.d makefile 41 @ $(COMPILE.c) $(OUTPUT_OPTION) $< 42 43 %.o : %.cpp 44 .%.o : %.cpp .%.d makefile 45 @ $(COMPILE.c) $(OUTPUT_OPTION) $< 46 47 %.o : %.s 48 .%.o : %.s .%.d makefile 49 @ avr-as --MD .$*.d -c -o $@ $< 50 51 # Create a pattern rule with an empty recipe, so that 52 # `make` won’t fail if the dependency file doesn’t exist. 53 .%.d: ; 54 55 # Mark the dependency files precious, so they won’t be 56 # automatically deleted as intermediate files. 57 .PRECIOUS: .%.d 58 59 # Include all of the dependency files 60 include $(wildcard .*.d)