From 4641acc691e7b252c5b0ca8d9afed18a003b8813 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Sun, 30 Sep 2012 14:01:30 +0000
Subject: [PATCH] Add LINKER_RELAXATIONS option to the LUFA BUILD build system
 module.

---
 LUFA/Build/lufa_build.mk                  | 38 ++++++++++++++---------
 LUFA/DoxygenPages/BuildSystem.txt         |  7 +++++
 LUFA/DoxygenPages/LUFAPoweredProjects.txt |  2 ++
 3 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/LUFA/Build/lufa_build.mk b/LUFA/Build/lufa_build.mk
index 04596c51b..ce76b60df 100644
--- a/LUFA/Build/lufa_build.mk
+++ b/LUFA/Build/lufa_build.mk
@@ -9,7 +9,7 @@
 LUFA_BUILD_MODULES         += BUILD
 LUFA_BUILD_TARGETS         += size symbol-sizes all lib elf hex lss clean mostlyclean
 LUFA_BUILD_MANDATORY_VARS  += TARGET ARCH MCU SRC F_USB LUFA_PATH
-LUFA_BUILD_OPTIONAL_VARS   += BOARD OPTIMIZATION C_STANDARD CPP_STANDARD F_CPU C_FLAGS CPP_FLAGS ASM_FLAGS CC_FLAGS LD_FLAGS OBJDIR OBJECT_FILES DEBUG_TYPE DEBUG_LEVEL
+LUFA_BUILD_OPTIONAL_VARS   += BOARD OPTIMIZATION C_STANDARD CPP_STANDARD F_CPU C_FLAGS CPP_FLAGS ASM_FLAGS CC_FLAGS LD_FLAGS OBJDIR OBJECT_FILES DEBUG_TYPE DEBUG_LEVEL LINKER_RELAXATIONS
 LUFA_BUILD_PROVIDED_VARS   += 
 LUFA_BUILD_PROVIDED_MACROS += 
 
@@ -60,6 +60,9 @@ LUFA_BUILD_PROVIDED_MACROS +=
 #    CC_FLAGS                  - Common flags to pass to the C/C++ compiler and
 #                                assembler
 #    LD_FLAGS                  - Flags to pass to the linker
+#    LINKER_RELAXATIONS        - Enable or disable linker relaxations to
+#                                decrease binary size (note: can cause link
+#                                failures on systems with an unpatched binutils)
 #    OBJDIR                    - Directory for the output object and dependency
 #                                files; if equal to ".", the output files will
 #                                be generated in the same folder as the sources
@@ -86,19 +89,20 @@ ERROR_IF_EMPTY   ?= $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $
 ERROR_IF_NONBOOL ?= $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))
 
 # Default values of optionally user-supplied variables
-BOARD           ?= NONE
-OPTIMIZATION    ?= s
-F_CPU           ?=
-C_STANDARD      ?= gnu99
-CPP_STANDARD    ?= gnu++98
-C_FLAGS         ?=
-CPP_FLAGS       ?=
-ASM_FLAGS       ?=
-CC_FLAGS        ?=
-OBJDIR          ?= .
-OBJECT_FILES    ?=
-DEBUG_FORMAT    ?= dwarf-2
-DEBUG_LEVEL     ?= 2
+BOARD              ?= NONE
+OPTIMIZATION       ?= s
+F_CPU              ?=
+C_STANDARD         ?= gnu99
+CPP_STANDARD       ?= gnu++98
+C_FLAGS            ?=
+CPP_FLAGS          ?=
+ASM_FLAGS          ?=
+CC_FLAGS           ?=
+OBJDIR             ?= .
+OBJECT_FILES       ?=
+DEBUG_FORMAT       ?= dwarf-2
+DEBUG_LEVEL        ?= 2
+LINKER_RELAXATIONS ?= Y
 
 # Sanity check user supplied values
 $(foreach MANDATORY_VAR, $(LUFA_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
@@ -114,6 +118,7 @@ $(call ERROR_IF_EMPTY, CPP_STANDARD)
 $(call ERROR_IF_EMPTY, OBJDIR)
 $(call ERROR_IF_EMPTY, DEBUG_FORMAT)
 $(call ERROR_IF_EMPTY, DEBUG_LEVEL)
+$(call ERROR_IF_NONBOOL, LINKER_RELAXATIONS)
 
 # Determine the utility prefix to use for the selected architecture
 ifeq ($(ARCH), AVR8)
@@ -194,7 +199,10 @@ BASE_CPP_FLAGS := -x c++ -O$(OPTIMIZATION) -std=$(CPP_STANDARD)
 BASE_ASM_FLAGS := -x assembler-with-cpp
 
 # Create a list of flags to pass to the linker
-BASE_LD_FLAGS := -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections -Wl,--relax 
+BASE_LD_FLAGS := -lm -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
+ifeq ($(LINKER_RELAXATIONS), Y)
+   BASE_LD_FLAGS += -Wl,--relax
+endif
 ifeq ($(ARCH), AVR8)
    BASE_LD_FLAGS += -mmcu=$(MCU)
 else ifeq ($(ARCH), XMEGA)
diff --git a/LUFA/DoxygenPages/BuildSystem.txt b/LUFA/DoxygenPages/BuildSystem.txt
index 57a01d944..3ee524ba3 100644
--- a/LUFA/DoxygenPages/BuildSystem.txt
+++ b/LUFA/DoxygenPages/BuildSystem.txt
@@ -188,6 +188,13 @@
  *    <td>Flags to pass to the linker, after the automatically generated flags.</td>
  *   </tr>
  *   <tr>
+ *    <td><tt>LINKER_RELAXATIONS</tt></td>
+ *    <td>Enables or disables linker relaxations when linking the application binary. This can reduce the total size
+ *        of the application by replacing full \c CALL instructions with smaller \c RCALL instructions where possible.
+ *        \note On some unpatched versions of binutils, this can cause link failures in some circumstances. If you
+ *              receive a link error <tt>relocation truncated to fit: R_AVR_13_PCREL</tt>, disable this setting.</td>
+ *   </tr>
+ *   <tr>
  *    <td><tt>OBJDIR</tt></td>
  *    <td>Directory to place the generated object and dependency files. If set to "." the same folder as the source file will be used.
  *        \note When this option is enabled, all source filenames <b>must</b> be unique.</td>
diff --git a/LUFA/DoxygenPages/LUFAPoweredProjects.txt b/LUFA/DoxygenPages/LUFAPoweredProjects.txt
index f45643299..99a0f26aa 100644
--- a/LUFA/DoxygenPages/LUFAPoweredProjects.txt
+++ b/LUFA/DoxygenPages/LUFAPoweredProjects.txt
@@ -148,6 +148,7 @@
  *  \li CFFA3000, a CompactFlash interface for the Apple II: http://www.dreher.net/CFforAppleII
  *  \li Digital Survey Instruments Magnetometer and Pointer: http://www.digitalsurveyinstruments.com/
  *  \li FinchRobot, a robot designed for educational use: http://www.finchrobot.com/
+ *  \li Flysight, a GPS logger for wingsuit pilots: http://flysight.ca/
  *  \li HummingBird Kit, a robotics learning platform: http://www.hummingbirdkit.com/
  *  \li Penguino, an Arduino Board With On-Board LUFA Powered Debugger/Programmer: http://wiki.icy.com.au/PenguinoAVR
  *  \li PIR-1, an IR control interface for consumer electronics: http://www.promixis.com/pir-1.php
@@ -160,6 +161,7 @@
  *  \li Retrode, a USB Games Console Cartridge Reader: http://www.retrode.org
  *  \li RFI21.1EU UHF RFID reader: http://www.metra.cz/rfid/uhf-rfid-ctecky/rfi21-1eu-uhf-rfid-ctecka.htm
  *  \li SmartCardDetective, a Smart Card analysis tool: http://www.smartcarddetective.com/
+ *  \li TimelapsePlus, a digital camera time lapse tool: https://github.com/timelapseplus/TimelapsePlus-Firmware
  *  \li USBTINY-MKII, an AVRISP-MKII Clone AVR Programmer: http://tom-itx.dyndns.org:81/~webpage/boards/USBTiny_Mkii/USBTiny_Mkii_index.php
  *  \li UDS18B20 USB Temperature sensor: http://toughlog.org/uds18b20/
  *  \li VMeter, a USB MIDI touch strip controller: http://www.vmeter.net/
-- 
GitLab