Skip to content

Instantly share code, notes, and snippets.

@triffid
Last active June 1, 2020 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save triffid/b4ccdc0bd00c6ef5d79355aafcb19766 to your computer and use it in GitHub Desktop.
Save triffid/b4ccdc0bd00c6ef5d79355aafcb19766 to your computer and use it in GitHub Desktop.
Yet another arduino Makefile
MCU_TARGET=atmega328p
ARDUINO_DIR=/usr/share/arduino
ARDUINO_HARDWARE:=$(ARDUINO_DIR)/hardware/arduino
ARDUINO_CORE:=$(ARDUINO_HARDWARE)/avr/cores/arduino
ARDUINO_VARIANT:=$(ARDUINO_HARDWARE)/avr/variants/standard
ARDUINO_CORE_LIBRARIES=Wire SPI
ARDUINO_LIBDIRS=Adafruit_MLX90614_Library
ARCH=avr
AVRDUDECONF := /etc/avrdude.conf
PROGPORT := /dev/arduino
PROGBAUD := 115200
PROGID := arduino
APPBAUD := 9600
# shouldn't need to touch things below here
CFLAGS+=-DF_CPU=16000000UL
PROJECT:=$(notdir $(PWD))
O:=build
PREFIX=$(if $(ARCH),$(ARCH)-,)
AVRDUDE=avrdude
AS=$(PREFIX)gcc
CC=$(PREFIX)gcc
CXX=$(PREFIX)g++
LD=$(PREFIX)g++
SRCPATHS=$(ARDUINO_CORE) $(ARDUINO_VARIANT) $(wildcard $(ARDUINO_HARDWARE)/avr/libraries/*/src) $(ARDUINO_LIBDIRS)
CFLAGS+=-Os
CFLAGS+=-mmcu=$(MCU_TARGET) -DARDUINO=185
CFLAGS+=-funsigned-char -fno-stack-protector
CFLAGS+=-ffunction-sections -fdata-sections -Wl,--gc-sections
CFLAGS+=$(patsubst %,-I%,$(SRCPATHS))
VPATH=$(foreach dir,$(SRCPATHS),$(shell find -H $(dir) -type d))
CXXSRC=$(wildcard *.cpp) $(foreach dir,$(SRCPATHS),$(shell find -H $(dir) -name '*.cpp'))
CSRC=$(wildcard *.c) $(foreach dir,$(SRCPATHS),$(shell find -H $(dir) -name '*.c'))
ASRC=$(wildcard *.S) $(foreach dir,$(SRCPATHS),$(shell find -H $(dir) -name '*.S'))
INO=$(PROJECT).ino
OBJS= $(patsubst %.ino,$(O)/%.ino.o, $(INO)) \
$(patsubst %.cpp,$(O)/%.cpp.o, $(notdir $(CXXSRC))) \
$(patsubst %.c,$(O)/%.c.o, $(notdir $(CSRC))) \
$(patsubst %.S,$(O)/%.S.o, $(notdir $(ASRC)))
# comment if your ino contains int main()
OBJ=$(OBJS)
# uncomment if your ino contains int main()
# OBJ=$(patsubst $(O)/main.cpp.o,,$(OBJS))
.PHONY: all clean functionsbysize flash console
all: $(O)/$(PROJECT).hex
clean:
rm -r $(O)
functionsbysize: $(OBJ)
@$(PREFIX)objdump -h $^ | grep '\.text\.' | perl -ne '/\.text\.(\S+)\s+([0-9a-f]+)/ && printf "%u\t%s\n", eval("0x$$2"), $$1;' | sort -n
flash: $(O)/$(PROJECT).hex
@#stty $(PROGBAUD) raw ignbrk hup < $(PROGPORT)
@#sleep 0.1
@#@stty $(PROGBAUD) raw ignbrk hup < $(PROGPORT)
$(AVRDUDE) -c$(PROGID) -b$(PROGBAUD) -p$(MCU_TARGET) -P$(PROGPORT) -C$(AVRDUDECONF) -U flash:w:$<
@#stty $(APPBAUD) raw ignbrk -hup -echo ixoff < $(PROGPORT)
console: $(PROGPORT)
@stty $(APPBAUD) raw ignbrk hup < $(PROGPORT)
@( cat <&3 & cat >&3; trap "kill -9 $!" INT; kill %%; ) 3<>$(PROGPORT)
$(O):
@mkdir -p "$@"
$(O)/$(PROJECT).elf: $(OBJ) | $(O)
@echo " LINK " "$@"
@$(LD) $(CFLAGS) -o $@ $^
$(O)/%.cpp.o: %.cpp | $(O)
@echo " CXX " "$@"
@$(CXX) $(CFLAGS) -c -o $@ $<
$(O)/%.c.o: %.c | $(O)
@echo " CC " "$@"
@$(CC) $(CFLAGS) -c -o $@ $<
$(O)/%.S.o: %.S | $(O)
@echo " ASM " "$@"
@$(AS) $(CFLAGS) -c -o $@ $<
$(O)/%.ino.o: %.ino | $(O)
@echo " INO " "$@"
@( echo '#include <Arduino.h>'; cat "$^"; ) | $(CXX) $(CFLAGS) -x c++ -c -o $@ -
%.hex: %.elf
@echo " HEX " "$@"
@$(PREFIX)objcopy -j .text -j .data -O ihex $< $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment