You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Kaleidoscope/_Makefile.master

704 lines
24 KiB

#
# Version 1.0.0
#
# This work is based on the following original.
# it includes many fixes and enhancements.
# Origin URL https://github.com/xxxajk/Arduino_Makefile_master
#
# Installation note.
# Place this file in Arduino/Arduino_Makefile_master/
# Even better:
# just 'git pull' from your Arduino directory (where your sketches are)
#
# Copyright 2011 Alan Burlison, alan@bleaklow.com. All rights reserved.
# Use is subject to license terms.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY ALAN BURLISON "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL ALAN BURLISON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Makefile for building Arduino projects outside of the Arduino environment
#
# This makefile should be included into a per-project Makefile of the following
# form:
#
# ----------
# BOARD = mega
# PORT = /dev/term/0
# THIRD_PARTY_HARDWARE = ../hardware/Sanguino
# INC_DIRS = ../common
# LIB_DIRS = ../libraries/Task ../../libraries/VirtualWire
# include ../Arduino_Makefile_master/Makefile.master
# ----------
#
# Where:
# BOARD : Arduino board type, from boards.txt
# BOARD_SUB : If the board is part a sub menu introduced in Arduino 1.5.x, then you must set this as well
# PORT : USB port
# THIRD_PARTY_HARDWARE : Path to third party hardware folder
# INC_DIRS : List pf directories containing header files
# LIB_DIRS : List of directories containing library source
#
# Before using this Makefile you can adjust the following macros to suit
# your environment, either by editing this file directly or by defining them in
# the Makefile that includes this one, in which case they will override the
# definitions below:
# ARD_REV : arduino software revision, e.g. 0017, 0018
# ARD_HOME : installation directory of the Arduino software.
# AVR_BIN : location of compiler binaries
# AVRDUDE : location of avrdude executable
# AVRDUDE_CONF : location of avrdude configuration file
# PROGRAMMER : avrdude programmer type
# MON_SPEED : serial monitor speed
# EXTRA_FLAGS : any extra flags that should be passed to the compilers
# FIND : The GNU or system find utility
# UNIQ : The GNU or system uniq utility
# DIRNAME : The GNU or system dirname utlity
# SED : The GNU or system sed utility
# GREP : The GNU or system grep utility
# WHICH : The GNU or system which utility
#
# Global configuration.
MON_SPEED ?= 115200
GCS ?= -Wl,--relax,--gc-sections
# Optimiser flags.
OPT_FLAGS ?= -Os -freorder-blocks -fno-inline-small-functions -fno-exceptions -ffunction-sections -fdata-sections -MMD
### Nothing below here should require editing. ###
MKDIR = mkdir -p
RM = rm -fr
MV = mv -f
FIND ?= find
UNIQ ?= uniq
DIRNAME ?= dirname
SED ?= sed
GREP ?= grep
WHICH ?= which
# Check for the required definitions.
ifndef BOARD
$(error BOARD not defined)
endif
ifndef PORT
$(error PORT not defined)
endif
# Set default values if ARD_HOME is not defined.
PLATFORM = $(shell uname -s)
ifndef ARD_HOME
ifeq "$(PLATFORM)" "SunOS"
ARD_HOME = /opt/Arduino
else ifeq "$(PLATFORM)" "Linux"
ARD_HOME = $(shell $(DIRNAME) `$(WHICH) arduino 2> /dev/null || echo /opt/Arduino/arduino`)
else ifeq "$(PLATFORM)" "Darwin"
ARD_HOME = /Applications/Arduino.app
else
$(error Please define ARD_HOME)
endif
endif
# Make if easier for Mac users, so they only have to define the path to the application.
ifneq ($(wildcard $(ARD_HOME)/Contents/Resources/Java/hardware/tools/avr/bin/avrdude),)
ARD_HOME := $(ARD_HOME)/Contents/Resources/Java
endif
# ARD_HOME set, does it exist?
ifeq ($(wildcard $(ARD_HOME)),)
$(error $(ARD_HOME) does not exist)
endif
# Read revision from revisions.txt.
ifndef ARD_REV
VERSION = $(shell $(SED) -n 's/ARDUINO \(.*\)/\1/p' < $(ARD_HOME)/revisions.txt | cut -d " " -f1 | head -1) # Extract version number - it is in the following format: ARDUINO x.x.x
ARD_REV = $(subst .,,$(VERSION)) # Remove punctuation marks
endif
ifndef ARD_REV
$(error Please define ARD_REV)
endif
# Automatically locate AVR and ARM tools.
ifneq ($(wildcard $(ARD_HOME)/hardware/tools/avr/bin/avrdude),)
AVR_BIN ?= $(ARD_HOME)/hardware/tools/avr/bin
ARM_BIN ?= $(ARD_HOME)/hardware/tools/arm-none-eabi/bin
AVRDUDE ?= $(AVR_BIN)/avrdude
AVRDUDE_CONF ?= $(ARD_HOME)/hardware/tools/avr/etc/avrdude.conf
TEENSY_LOADER_CLI ?= $(AVR_BIN)/teensy_loader_cli
else ifneq ($(wildcard $(ARD_HOME)/hardware/tools/avrdude),)
AVR_BIN ?= $(ARD_HOME)/hardware/tools/avr/bin
ARM_BIN ?= $(ARD_HOME)/hardware/tools/arm-none-eabi/bin
AVRDUDE ?= $(ARD_HOME)/hardware/tools/avrdude
AVRDUDE_CONF ?= $(ARD_HOME)/hardware/tools/avrdude.conf
TEENSY_LOADER_CLI ?= $(ARD_HOME)/hardware/tools/teensy_loader_cli
else ifneq ($(wildcard $(ARD_HOME)/build/linux/work/hardware/tools/avrdude),)
AVR_BIN ?= $(ARD_HOME)/build/linux/work/hardware/tools/avr/bin
ARM_BIN ?= $(ARD_HOME)/build/linux/work/hardware/tools/arm-none-eabi/bin
AVRDUDE ?= $(ARD_HOME)/build/linux/work/hardware/tools/avrdude
AVRDUDE_CONF ?= $(ARD_HOME)/build/linux/work/hardware/tools/avrdude.conf
TEENSY_LOADER_CLI ?= $(ARD_HOME)/build/linux/work/hardware/tools/teensy_loader_cli
endif
# Version-specific settings.
ifdef THIRD_PARTY_HARDWARE # Check if a third party hardware add-on is used.
ARD_BOARDS = $(THIRD_PARTY_HARDWARE)/boards.txt
ifdef BOARD_SUB
CORE := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.core=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef CORE
CORE = $(shell $(SED) -n 's/$(BOARD)\.build\.core=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifeq "$(CORE)" "arduino:arduino" # Check if it is using the standard Arduino core - supported since 1.5.x
ARD_SRC_DIR = $(ARD_HOME)/hardware/arduino/avr/cores/arduino
else
ARD_SRC_DIR = $(THIRD_PARTY_HARDWARE)/cores/$(CORE)
endif
ifdef BOARD_SUB
VARIANT := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.variant=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef VARIANT
VARIANT = $(shell $(SED) -n 's/$(BOARD)\.build\.variant=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef VARIANT
$(error Unknown board '$(BOARD)')
endif
INTERNAL_LIB_DIRS += $(THIRD_PARTY_HARDWARE)/variants/$(VARIANT)
else
ifneq ($(wildcard $(ARD_HOME)/hardware/arduino/avr/boards.txt),)
ARD_BOARDS = $(ARD_HOME)/hardware/arduino/avr/boards.txt
TEENSY_BOARDS = $(ARD_HOME)/hardware/teensy/boards.txt
TEENSY_BASE = $(ARD_HOME)/hardware/teensy/cores/
ARD_SRC_DIR = $(ARD_HOME)/hardware/arduino/avr/cores/arduino
ifdef BOARD_SUB
VARIANT := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.variant=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef VARIANT
VARIANT = $(shell $(SED) -n 's/$(BOARD)\.build\.variant=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef VARIANT
$(error Unknown board '$(BOARD)')
endif
INTERNAL_LIB_DIRS += $(ARD_HOME)/hardware/arduino/avr/variants/$(VARIANT)
else ifneq ($(wildcard $(ARD_HOME)/hardware/arduino/boards.txt),)
ARD_BOARDS = $(ARD_HOME)/hardware/arduino/boards.txt
TEENSY_BOARDS = $(ARD_HOME)/hardware/teensy/boards.txt
TEENSY_BASE = $(ARD_HOME)/hardware/teensy/cores/
ARD_SRC_DIR = $(ARD_HOME)/hardware/arduino/cores/arduino
VARIANT = $(shell $(SED) -n 's/$(BOARD)\.build\.variant=\(.*\)/\1/p' < $(ARD_BOARDS))
ifndef VARIANT
$(error Unknown board '$(BOARD)')
endif
INTERNAL_LIB_DIRS += $(ARD_HOME)/hardware/arduino/variants/$(VARIANT)
else ifneq ($(wildcard $(ARD_HOME)/hardware/boards.txt),)
ARD_BOARDS = $(ARD_HOME)/hardware/boards.txt
ARD_SRC_DIR = $(ARD_HOME)/hardware/cores/arduino
endif
endif
ifndef ARD_BOARDS
$(error Can not locate boards.txt)
endif
# Platform-specific settings.
ifeq "$(PLATFORM)" "SunOS"
define run-monitor
gnome-terminal -t '$(BOARD) $(PORT)' \
-e 'env -i tip -$(MON_SPEED) $(PORT)' &
endef
define kill-monitor
- pkill -f 'tip.*$(PORT)'
endef
else ifeq "$(PLATFORM)" "Linux"
define run-monitor
screen $(PORT) $(MON_SPEED)
endef
define kill-monitor
- pkill -f 'screen.*$(PORT)'
endef
else ifeq "$(PLATFORM)" "Darwin"
define run-monitor
screen $(PORT) $(MON_SPEED)
endef
define kill-monitor
- pkill -f 'screen.*$(PORT)'
endef
else
$(error Unknown platform $(PLATFORM))
endif
# Standard macros.
SKETCH = $(notdir $(CURDIR))
BUILD_DIR = build
VPATH = $(LIB_DIRS)
# Macros derived from boards.txt.
ifdef BOARD_SUB
MCU := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.mcu=\(.*\)/\1/p' < $(ARD_BOARDS))
F_CPU := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.f_cpu=\(.*\)/\1/p' < $(ARD_BOARDS))
UPLOAD_SPEED := $(shell $(SED) -n 's/$(BOARD_SUB)\.upload\.speed=\(.*\)/\1/p' < $(ARD_BOARDS))
BUILD_BOARD := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.board=\(.*\)/\1/p' < $(ARD_BOARDS))
BOARD_EXTRA_FLAGS := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.extra_flags=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
# Check if the board is part of a sub menu.
ifndef MCU
MCU := $(shell $(SED) -n 's/$(BOARD)\.build\.mcu=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef F_CPU
F_CPU := $(shell $(SED) -n 's/$(BOARD)\.build\.f_cpu=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef UPLOAD_SPEED
UPLOAD_SPEED := $(shell $(SED) -n 's/$(BOARD)\.upload\.speed=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef BUILD_BOARD
BUILD_BOARD := $(shell $(SED) -n 's/$(BOARD)\.build\.board=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef BOARD_EXTRA_FLAGS
BOARD_EXTRA_FLAGS := $(shell $(SED) -n 's/$(BOARD)\.build\.extra_flags=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
# No board yet. Check for teensy boards.
ifndef MCU
ifneq ($(wildcard $(TEENSY_BOARDS)),)
USB_BUILD_DEFINE ?= USB_SERIAL # Can be set to the following: USB_SERIAL, USB_HID, USB_SERIAL_HID, USB_DISK, USB_DISK_SDFLASH, USB_MIDI, USB_RAWHID or USB_FLIGHTSIM
USE_LAYOUT ?= LAYOUT_US_ENGLISH
ARD_BOARDS = $(TEENSY_BOARDS)
BUILD_CORE = $(shell $(SED) -n 's/$(BOARD)\.build\.core=\(.*\)/\1/p' < $(ARD_BOARDS))
ARD_SRC_DIR = $(TEENSY_BASE)$(BUILD_CORE)
ifdef BOARD_SUB
CPU := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.cpu=\(.*\)/\1/p' < $(ARD_BOARDS))
MCU := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.mcu=\(.*\)/\1/p' < $(ARD_BOARDS))
F_CPU := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.f_cpu=\(.*\)/\1/p' < $(ARD_BOARDS))
ifndef F_CPU
F_CPU := $(shell $(SED) -n 's/$(BOARD_SUB)\.menu\.speed\..*\.build\.f_cpu=\(.*\)/\1/p' < $(ARD_BOARDS) | head --lines=1)
endif
UPLOAD_SPEED := $(shell $(SED) -n 's/$(BOARD_SUB)\.upload\.speed=\(.*\)/\1/p' < $(ARD_BOARDS))
BUILD_BOARD := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.board=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
# Check if the board is part of a sub menu.
ifndef CPU
CPU := $(shell $(SED) -n 's/$(BOARD)\.build\.cpu=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef MCU
MCU := $(shell $(SED) -n 's/$(BOARD)\.build\.mcu=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef F_CPU
F_CPU := $(shell $(SED) -n 's/$(BOARD)\.build\.f_cpu=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef F_CPU
F_CPU := $(shell $(SED) -n 's/$(BOARD)\.menu\.speed\..*\.build\.f_cpu=\(.*\)/\1/p' < $(ARD_BOARDS) | head --lines=1)
endif
ifndef UPLOAD_SPEED
UPLOAD_SPEED := $(shell $(SED) -n 's/$(BOARD)\.upload\.speed=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef BUILD_BOARD
BUILD_BOARD := $(shell $(SED) -n 's/$(BOARD)\.build\.board=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
endif
endif
# We still have not found this board.
ifndef MCU
$(error Unknown board '$(BOARD)')
endif
CPU_MCU = $(MCU)
#
# NOTE: for 1.0.5
#
# Pull in any regular extra C flags
#
#
EXT_C_FLAGS := $(shell $(SED) -n 's/$(BOARD)\.build\.option[^=]*=\(.*\)/\1/p' < $(ARD_BOARDS))
ifdef EXT_C_FLAGS
EXT_C_FLAGS += $(shell $(SED) -n 's/$(BOARD)\.menu\.usb\.serial\.build\.define[^=]*=\(.*\)/\1/p' < $(ARD_BOARDS))
EXT_CPP_FLAGS += $(shell $(SED) -n 's/$(BOARD)\.build\.cppoption[^=]*=\(.*\)/\1/p' < $(ARD_BOARDS)) $(EXT_C_FLAGS)
EXT_LINK_T_FLAG := $(shell $(SED) -n 's/$(BOARD)\.build\.linkscript=\(.*\)/\1/p' < $(ARD_BOARDS))
ifdef EXT_LINK_T_FLAG
EXT_LINK_FLAGS += -T$(ARD_SRC_DIR)/$(EXT_LINK_T_FLAG)
endif
EXT_LINK_FLAGS += $(shell $(SED) -n 's/$(BOARD)\.build\.linkoption[^=]*=\(.*\)/\1/p' < $(ARD_BOARDS))
EXT_LINK_FLAGS += $(shell $(SED) -n 's/$(BOARD)\.build\.additionalobject[^=]*=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
# Add extracted variable to compiler flags.
#
ifdef GIT_VERSION
EXTRA_FLAGS += -DVERSION=\"$(GIT_VERSION)\"
endif
ifdef BUILD_BOARD
EXTRA_FLAGS += -DARDUINO_$(BUILD_BOARD)
endif
ifdef USB_BUILD_DEFINE
EXTRA_FLAGS += -D$(USB_BUILD_DEFINE)
endif
ifdef USE_LAYOUT
EXTRA_FLAGS += -D$(USE_LAYOUT)=1
endif
ifdef BOARD_EXTRA_FLAGS
ifdef BOARD_SUB
USB_VID := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.vid=\(.*\)/\1/p' < $(ARD_BOARDS))
USB_PID := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.pid=\(.*\)/\1/p' < $(ARD_BOARDS))
USB_MANUFACTURER := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.usb_manufacturer=\(.*\)/\1/p' < $(ARD_BOARDS))
USB_PRODUCT := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.usb_product=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef USB_VID
USB_VID := $(shell $(SED) -n 's/$(BOARD)\.build\.vid=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef USB_PID
USB_PID := $(shell $(SED) -n 's/$(BOARD)\.build\.pid=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef USB_MANUFACTURER
USB_MANUFACTURER := $(shell $(SED) -n 's/$(BOARD)\.build\.usb_manufacturer=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef USB_PRODUCT
USB_PRODUCT := $(shell $(SED) -n 's/$(BOARD)\.build\.usb_product=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifdef USB_VID
USB_FLAGS = -DUSB_VID=$(USB_VID)
endif
ifdef USB_PID
USB_FLAGS += -DUSB_PID=$(USB_PID)
endif
ifdef USB_MANUFACTURER
USB_FLAGS += '-DUSB_MANUFACTURER=$(USB_MANUFACTURER)'
endif
ifdef USB_PRODUCT
USB_FLAGS += '-DUSB_PRODUCT=$(USB_PRODUCT)'
endif
BOARD_EXTRA_FLAGS := $(subst {build.usb_flags},$(USB_FLAGS),$(BOARD_EXTRA_FLAGS)) # Subst {build.usb_flags} with -DUSB_VID={build.vid} -DUSB_PID={build.pid} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}'
EXTRA_FLAGS += $(BOARD_EXTRA_FLAGS)
endif
# Read programmer type from boards.txt.
ifndef PROGRAMMER
ifdef BOARD_SUB
PROGRAMMER := $(shell $(SED) -n 's/$(BOARD_SUB)\.upload\.protocol=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef PROGRAMMER
PROGRAMMER := $(shell $(SED) -n 's/$(BOARD)\.upload\.protocol=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef PROGRAMMER
PROGRAMMER = arduino # Defaults to arduino upload protocol
endif
endif
# extract arch
# Arduino architecture:
ifdef BOARD_SUB
BUILD_ARCH := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.arch=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef BUILD_ARCH
BUILD_ARCH := $(shell $(SED) -n 's/$(BOARD)\.build\.arch=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
# Teensy architecture:
ifndef BUILD_ARCH
ifdef BOARD_SUB
BUILD_ARCH := $(shell $(SED) -n 's/$(BOARD_SUB)\.build\.architecture=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef BUILD_ARCH
BUILD_ARCH := $(shell $(SED) -n 's/$(BOARD)\.build\.architecture=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
endif
# default is avr
ifndef BUILD_ARCH
BUILD_ARCH = avr
endif
ifdef BUILD_ARCH
EXTRA_FLAGS += -DARDUINO_ARCH_$(shell echo $(BUILD_ARCH) | tr a-z A-Z)
endif
ifeq "$(BUILD_ARCH)" "arm-none-eabi"
USED_BIN = $(ARM_BIN)
endif
USED_TARGET = $(BUILD_ARCH)
# If we don't know what target bin dirs, use the default
USED_BIN ?= $(AVR_BIN)
# ...and if the target is not set, default to avr
USED_TARGET ?= avr
#$(error bin = $(USED_BIN), target = $(USED_TARGET))
# Build tools.
CC = $(USED_BIN)/$(USED_TARGET)-gcc
CXX = $(USED_BIN)/$(USED_TARGET)-g++
CXXFILT = $(USED_BIN)/$(USED_TARGET)-c++filt
OBJCOPY = $(USED_BIN)/$(USED_TARGET)-objcopy
OBJDUMP = $(USED_BIN)/$(USED_TARGET)-objdump
AR = $(USED_BIN)/$(USED_TARGET)-ar
SIZE = $(USED_BIN)/$(USED_TARGET)-size
NM = $(USED_BIN)/$(USED_TARGET)-nm
ifndef LIB_DIRS
# Automatically add user libraries and Arduino libraries.
# Note: This code is dumb! It does not follow conditionals in the code.
# This will also include multiple versions of a USER library.
# The effect of this is that it may compile unwanted code.
# It does seem to work OK dispite this.
#
# TO-DO: Scan user libraries for interdependence?
# TO-DO: Scan Arduino libraries for interdependence?
# Step 1: Gather the primary directories
PRILIB_DIRS = $(shell for i in `$(FIND) . \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.ino' \) -exec $(GREP) '\#include.*<.*>' \{\} \; 2>/dev/null | $(SED) 's/>.*$$//g' | $(SED) 's/.*<//g' ` ; { $(FIND) ../libraries -maxdepth 2 -name $$i -exec $(DIRNAME) \{\} \; ;} 2>/dev/null | $(UNIQ) )
PRILIB_DIRS += $(shell for i in `$(FIND) . \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.ino' \) -exec $(GREP) '\#include.*<.*>' \{\} \; 2>/dev/null| $(SED) 's/\.h.*>.*$$//g' | $(SED) 's/.*<//g' ` ; { $(FIND) $(ARD_HOME)/libraries/$$i -maxdepth 2 -name $$i.h -exec $(DIRNAME) \{\} \; ;} 2>/dev/null | $(UNIQ) )
PRILIB_DIRS += $(shell for i in `$(FIND) . \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.ino' \) -exec $(GREP) '\#include.*<.*>' \{\} \; 2>/dev/null| $(SED) 's/\.h.*>.*$$//g' | $(SED) 's/.*<//g' ` ; { $(FIND) $(ARD_HOME)/hardware/arduino/$(BUILD_ARCH)/libraries/$$i -maxdepth 2 -name $$i.h -exec $(DIRNAME) \{\} \; ;} 2>/dev/null | $(UNIQ) )
# Step 2: Gather any 'utility' subdirectories.
# This stupidity brought to you by the developers of the Arduino 'IDE', and
# by the library developers that don't know how to use include properly.
UTLLIB_DIRS=$(shell for i in $(PRILIB_DIRS) ; { $(FIND) $$i -name 'utility' -print ; } )
# Step 3: Combine everything
LIB_DIRS = $(PRILIB_DIRS) $(UTLLIB_DIRS)
endif
# These always should come LAST so that user libraries may over-ride
# This is how the IDE does it too.
LIB_DIRS += $(INTERNAL_LIB_DIRS)
# Compiler flags.
INC_FLAGS = \
$(addprefix -I,$(LIB_DIRS)) $(addprefix -I,$(INC_DIRS)) -I$(ARD_SRC_DIR)
ifdef CPU
ARD_MCP += -mcpu=$(CPU)
else
ARD_MCP += -mmcu=$(MCU)
endif
ARD_FLAGS += -DF_CPU=$(F_CPU) -DARDUINO=$(ARD_REV) $(ARD_MCP)
C_CXX_FLAGS = -g -w -Wa,-adhlns=$(BUILD_DIR)/$*.lst $(EXTRA_FLAGS) -DUSING_MAKEFILE=1 -Wall -Wextra -Wformat=2 -Wuninitialized -Wshadow -Wconversion
C_FLAGS = \
-std=gnu99 -Wstrict-prototypes -Wno-old-style-declaration $(C_CXX_FLAGS) $(EXT_C_FLAGS)
CXX_FLAGS = \
$(C_CXX_FLAGS) $(EXT_CPP_FLAGS)
# Build parameters.
IMAGE = $(BUILD_DIR)/$(SKETCH)
ARD_C_SRC = $(wildcard $(ARD_SRC_DIR)/*.c)
ARD_CXX_SRC = $(wildcard $(ARD_SRC_DIR)/*.cpp)
ARD_C_OBJ = $(patsubst %.c,%.o,$(notdir $(ARD_C_SRC)))
ARD_CXX_OBJ = $(patsubst %.cpp,%.o,$(notdir $(ARD_CXX_SRC)))
ARD_LIB = arduino
ARD_AR = $(BUILD_DIR)/lib$(ARD_LIB).a
ARD_AR_OBJ = $(ARD_AR)($(ARD_C_OBJ) $(ARD_CXX_OBJ))
ARD_LD_FLAG = -l$(ARD_LIB)
# Sketch libraries.
LIB_C_SRC = $(foreach ld,$(LIB_DIRS),$(wildcard $(ld)/*.c))
LIB_CXX_SRC = $(foreach ld,$(LIB_DIRS),$(wildcard $(ld)/*.cpp))
LIB_SRC = $(LIB_C_SRC) $(LIB_CXX_SRC)
ifneq "$(strip $(LIB_C_SRC) $(LIB_CXX_SRC))" ""
LIB_C_OBJ = $(patsubst %.c,%.o,$(notdir $(LIB_C_SRC)))
LIB_CXX_OBJ = $(patsubst %.cpp,%.o,$(notdir $(LIB_CXX_SRC)))
LIB_LIB = library
LIB_AR = $(BUILD_DIR)/lib$(LIB_LIB).a
LIB_AR_OBJ = $(LIB_AR)($(LIB_C_OBJ) $(LIB_CXX_OBJ))
LIB_LD_FLAG = -l$(LIB_LIB)
endif
# Sketch INO source.
SKT_PDE_SRC = $(wildcard *.ino)
ifneq "$(strip $(SKT_PDE_SRC))" ""
SKT_PDE_OBJ = $(BUILD_DIR)/$(SKETCH)_ino.o
endif
# C and C++ source.
SKT_C_SRC = $(wildcard *.c)
SKT_CXX_SRC = $(wildcard *.cpp)
ifneq "$(strip $(SKT_C_SRC) $(SKT_CXX_SRC))" ""
SKT_C_OBJ = $(patsubst %.c,%.o,$(SKT_C_SRC))
SKT_CXX_OBJ = $(patsubst %.cpp,%.o,$(SKT_CXX_SRC))
SKT_LIB = sketch
SKT_AR = $(BUILD_DIR)/lib$(SKT_LIB).a
SKT_AR_OBJ = $(SKT_AR)/($(SKT_C_OBJ) $(SKT_CXX_OBJ))
SKT_LD_FLAG = -l$(SKT_LIB)
endif
# Common rule bodies.
define run-cc
$(CC) -c $(C_FLAGS) $(OPT_FLAGS) $(ARD_FLAGS) $(INC_FLAGS) \
-MD -MT '$@($%)' -MF $(@D)/.$(@F)_$*.dep $< -o $(BUILD_DIR)/$%
@ $(AR) rc $@ $(BUILD_DIR)/$%
@ $(RM) $(BUILD_DIR)/$%
@ $(CXXFILT) < $(BUILD_DIR)/$*.lst > $(BUILD_DIR)/$*.lst.tmp
@ $(MV) $(BUILD_DIR)/$*.lst.tmp $(BUILD_DIR)/$*.lst
endef
define run-cxx
$(CXX) -c $(CXX_FLAGS) $(OPT_FLAGS) $(ARD_FLAGS) $(INC_FLAGS) \
-MD -MT '$@($%)' -MF $(@D)/.$(@F)_$*.dep $< -o $(BUILD_DIR)/$%
@ $(AR) rc $@ $(BUILD_DIR)/$%
@ $(RM) $(BUILD_DIR)/$%
@ $(CXXFILT) < $(BUILD_DIR)/$*.lst > $(BUILD_DIR)/$*.lst.tmp
@ $(MV) $(BUILD_DIR)/$*.lst.tmp $(BUILD_DIR)/$*.lst
endef
define run-avrdude
$(AVRDUDE) -V -C$(AVRDUDE_CONF) -p$(MCU) -c$(PROGRAMMER) -P$(PORT) -b$(UPLOAD_SPEED) -D -Uflash:w:$(IMAGE).hex:i
endef
define run-teensy_loader_cli
$(TEENSY_LOADER_CLI) -mmcu=$(CPU_MCU) -w $(IMAGE).hex
endef
ifdef BOARD_SUB
USE_1200BPS_TOUCH := $(shell $(SED) -n 's/$(BOARD_SUB)\.upload\.use_1200bps_touch=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifndef USE_1200BPS_TOUCH
USE_1200BPS_TOUCH := $(shell $(SED) -n 's/$(BOARD)\.upload\.use_1200bps_touch=\(.*\)/\1/p' < $(ARD_BOARDS))
endif
ifeq "$(PROGRAMMER)" "halfkay"
define run-uploader
$(TEENSY_LOADER_CLI) -mmcu=$(CPU_MCU) -v -w $(IMAGE).hex
endef
else ifeq "$(USE_1200BPS_TOUCH)" "true"
ifeq "$(PLATFORM)" "Darwin"
STTY_ARGS := -f $(PORT) 1200
else
STTY_ARGS := -F $(PORT) 1200
endif
define run-uploader
stty $(STTY_ARGS)
sleep 2
$(AVRDUDE) -V -C$(AVRDUDE_CONF) -p$(MCU) -c$(PROGRAMMER) -P$(PORT) -b$(UPLOAD_SPEED) -D -Uflash:w:$(IMAGE).hex:i
endef
else
define run-uploader
$(AVRDUDE) -V -C$(AVRDUDE_CONF) -p$(MCU) -c$(PROGRAMMER) -P$(PORT) -b$(UPLOAD_SPEED) -D -Uflash:w:$(IMAGE).hex:i
endef
endif
# Rules.
.PHONY : all clean upload monitor upload_monitor upmonitor
all : $(BUILD_DIR) $(IMAGE).hex
clean :
$(RM) $(BUILD_DIR)
$(RM) *~ *.bak *.BAK *.Bak *.orig
$(RM) */*~ */*.bak */*.BAK */*.Bak */*.orig
$(RM) */*/*~ */*/*.bak */*/*.BAK */*/*.Bak */*/*.orig
sync
$(BUILD_DIR) :
$(MKDIR) $@
$(SKT_PDE_OBJ) : $(SKT_PDE_SRC)
cat $(SKT_PDE_SRC) > $(BUILD_DIR)/$(SKETCH)_ino.cpp
cd $(BUILD_DIR) && $(CXX) -c $(subst build/,,$(CXX_FLAGS)) \
$(OPT_FLAGS) $(ARD_FLAGS) -I.. \
$(patsubst -I..%,-I../..%,$(INC_FLAGS)) \
$(SKETCH)_ino.cpp -o $(@F)
(%.o) : $(ARD_SRC_DIR)/%.c
$(run-cc)
(%.o) : $(ARD_SRC_DIR)/%.cpp
$(run-cxx)
(%.o) : %.c
$(run-cc)
(%.o) : %.cpp
$(run-cxx)
$(BUILD_DIR)/%.d : %.c
$(run-cc-d)
$(BUILD_DIR)/%.d : %.cpp
$(run-cxx-d)
#-Wl,--gc-sections
# $(AR) d $(BUILD_DIR)/lib$(LIB_LIB).a malloc.o
$(IMAGE).hex : $(ARD_AR_OBJ) $(LIB_AR_OBJ) $(SKT_AR_OBJ) $(SKT_PDE_OBJ)
$(CC) -Os $(GCS) $(ARD_MCP) -L$(BUILD_DIR) $(SKT_PDE_OBJ) \
$(SKT_LD_FLAG) $(LIB_LD_FLAG) $(ARD_LD_FLAG) $(EXT_LINK_FLAGS)\
-lm -o $(IMAGE).elf
$(OBJCOPY) -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load \
--no-change-warnings --change-section-lma .eeprom=0 $(IMAGE).elf \
$(IMAGE).eep
$(OBJCOPY) -O ihex -R .eeprom $(IMAGE).elf $(IMAGE).hex
$(OBJDUMP) -h -S $(IMAGE).elf | $(CXXFILT) -t > $(IMAGE).lst
$(SIZE) $(IMAGE).elf
#
# This only works on the avr, sorry
# $(SIZE) -C --mcu=$(MCU) $(IMAGE).elf
#
upload : all
$(kill-monitor)
$(run-uploader)
# $(AVRDUDE) -V -C$(AVRDUDE_CONF) -p$(MCU) -c$(PROGRAMMER) -P$(PORT) \
# -b$(UPLOAD_SPEED) -D -Uflash:w:$(IMAGE).hex:i
monitor :
$(kill-monitor)
$(run-monitor)
upload_monitor : upload monitor
upmonitor: upload_monitor
-include $(wildcard $(BUILD_DIR)/.*.dep))