Compiling optiboot within the arduino environment.

http://code.google.com/p/arduino/issues/detail?id=487

a couple of people are interested in changing or patching the optiboot version that comes with the arduino IDE, but are distressed by the apparent need to install a new toolchain to do so. This shouldn't really be necessary; the IDE includes all the tools that are needed; it's just accessing them from the right place that is annoying.

The above issue include diffs to the optiboot "Makefile", as well as a short shell script (for macos) and a .bat file (for windows) that will allow one to rebuild optiboot by connecting to the .../hardware/arduino/bootloaders/optiboot directory and typing "./omake [names]" or "omake [names]" in a terminal/command window. You shouldn't have to have any other developer tools (other than Arduino) installed... Linux users are on their own (but should have fewer problems since installing Arduino usually means installing the other tools in "standard" places rather than the "special" places that they show up on Mac/Windows.)

I'm especially happy with the omake/omake.bat hack :slight_smile:

omake:

echo ../../../tools/avr/bin/make OS=macosx ENV=arduino $*
../../../tools/avr/bin/make OS=macosx ENV=arduino $*

omake.bat:

..\..\..\tools\avr\utils\bin\make OS=windows ENV=arduino %*

Makefile.diffs

--- Makefile-	2010-12-24 11:55:50.000000000 -0800
+++ Makefile	2011-02-27 00:48:10.000000000 -0800
@@ -19,6 +19,13 @@
 # program name should not be changed...
 PROGRAM    = optiboot
 
+# Uncomment this next lines to build within the arduino environment,
+# using the arduino-included avrgcc toolset (mac and pc)
+# ENV ?= arduino
+# OS ?= macosx
+# OS ?= windows
+
+
 # enter the parameters for the avrdude isp tool
 ISPTOOL	   = stk500v2
 ISPPORT	   = usb
@@ -27,6 +34,27 @@
 MCU_TARGET = atmega168
 LDSECTION  = --section-start=.text=0x3e00
 
+ifeq ($(ENV), arduino)
+# For Arduino, we assume that we're connected to the optiboot directory
+# included with the arduino distribution, which means that the full set
+# of avr-tools are "right up there" in standard places.
+TOOLROOT = ../../../tools
+GCCROOT = $(TOOLROOT)/avr/bin/
+AVRDUDE_CONF = -C$(TOOLROOT)/avr/etc/avrdude.conf
+else
+GCCROOT =
+AVRDUDE_CONF =
+endif
+
+ifeq ($(OS), windows)
+# On windows, SOME of the tool paths will need to have backslashes instead
+# of forward slashes (because they use windows cmd.exe for execution instead
+# of a unix/mingw shell?)
+fixpath = $(subst /,\,$1)
+else
+fixpath = $(1)
+endif
+
 # the efuse should really be 0xf8; since, however, only the lower
 # three bits of that byte are used on the atmega168, avrdude gets
 # confused if you specify 1's for the higher bits, see:
@@ -37,10 +65,13 @@
 # lock it), but since the high two bits of the lock byte are
 # unused, avrdude would get confused.
 
-ISPFUSES    = avrdude -c $(ISPTOOL) -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \
--e -u -U lock:w:0x3f:m -U efuse:w:0x$(EFUSE):m -U hfuse:w:0x$(HFUSE):m -U lfuse:w:0x$(LFUSE):m
-ISPFLASH    = avrdude -c $(ISPTOOL) -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \
--U flash:w:$(PROGRAM)_$(TARGET).hex -U lock:w:0x0f:m
+ISPFUSES    = $(GCCROOT)avrdude $(AVRDUDE_CONF) -c $(ISPTOOL) \
+              -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \
+              -e -u -U lock:w:0x3f:m -U efuse:w:0x$(EFUSE):m \
+              -U hfuse:w:0x$(HFUSE):m -U lfuse:w:0x$(LFUSE):m
+ISPFLASH    = $(GCCROOT)avrdude $(AVRDUDE_CONF) -c $(ISPTOOL) \
+              -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \
+              -U flash:w:$(PROGRAM)_$(TARGET).hex -U lock:w:0x0f:m
 
 STK500 = "C:\Program Files\Atmel\AVR Tools\STK500\Stk500.exe"
 STK500-1 = $(STK500) -e -d$(MCU_TARGET) -pf -vf -if$(PROGRAM)_$(TARGET).hex \
@@ -53,15 +84,15 @@
 DEFS       = 
 LIBS       =
 
-CC         = avr-gcc
+CC         = $(GCCROOT)avr-gcc
 
 # Override is only needed by avr-lib build system.
 
 override CFLAGS        = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) -DF_CPU=$(AVR_FREQ) $(DEFS)
-override LDFLAGS       = -Wl,$(LDSECTION) -Wl,--relax -nostartfiles
+override LDFLAGS       = -Wl,$(LDSECTION) -Wl,--relax -Wl,--gc-sections -nostartfiles
 
-OBJCOPY        = avr-objcopy
-OBJDUMP        = avr-objdump
+OBJCOPY        = $(GCCROOT)avr-objcopy
+OBJDUMP        = $(call fixpath,$(GCCROOT)avr-objdump)
 
 # 20MHz clocked platforms
 #

Thanks very much for making this patch! I have now been able to compile Optiboot with the IDE 0022 tools running under Windows 7, and get a .hex file which matches the one in the Optiboot repository. I did have to make a couple of small adjustments to the diff file (see below) to match the current version of the Makefile, and add the '-nostdlib' flag to LDFLAGS (as explained in Optiboot issue #1).

And if I add the fix at Google Code Archive - Long-term storage for Google Code Project Hosting. I get a hex file which matches that in your excellent Optifix sketch.

The next things I will try are your size-reduction patch from Google Code Archive - Long-term storage for Google Code Project Hosting., and an 8MHz version of Optifix.

cheers,

--- westfw-Makefile.diff	Tue Mar 22 03:28:25 2011
+++ tim-Makefile.diff	Tue Mar 22 03:17:06 2011
@@ -72,13 +70,13 @@
  # Override is only needed by avr-lib build system.
  
  override CFLAGS        = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) -DF_CPU=$(AVR_FREQ) $(DEFS)
--override LDFLAGS       = -Wl,$(LDSECTION) -Wl,--relax -nostartfiles
-+override LDFLAGS       = -Wl,$(LDSECTION) -Wl,--relax -Wl,--gc-sections -nostartfiles
+-override LDFLAGS       = -Wl,$(LDSECTION) -Wl,--relax -nostartfiles -Wl,--gc-sections
++override LDFLAGS       = -Wl,$(LDSECTION) -Wl,--relax -nostartfiles -Wl,--gc-sections -nostdlib
  
 -OBJCOPY        = avr-objcopy
 -OBJDUMP        = avr-objdump
 +OBJCOPY        = $(GCCROOT)avr-objcopy
 +OBJDUMP        = $(call fixpath,$(GCCROOT)avr-objdump)
  
- # 20MHz clocked platforms
- #
+ # Test platforms
+ # Virtual boot block test

I have now been able to compile Optiboot with the IDE 0022 tools running under Windows 7, and get a .hex file which matches the one in the Optiboot repository. I did have to make a couple of small adjustments to the diff file (see below) to match the current version of the Makefile, and add the '-nostdlib' flag to LDFLAGS (as explained in Optiboot issue #1).

Thanks for the confirmation. Wish I had thought of comparing the .hex files; that would have been a simple test.
I didn't include the -nostdlib patch because it had already been made to the repository, and I wanted this patch to be the bits for changing the toolchain paths only. But I should have mentioned it...

this is excatly what i needed.... but could be nice with some examples..

ie how do i build an bootloader for an sanguinololu with a 644p running at 20 Mhz and for a 1284p also running at 20 Mhz

also using ide 1.6.8 so will have to see what paths should be

It stopped working in the post 1.5 ides, because they no longer include make.