Makefile patch for better dependency generation

Hi,

I’m using the Makefile that is kindly provided by the arduino source distribution.

I am using a slightly modified version that has two advantages:

  1. it generates dependencies automatically

  2. it correctly deals with sources from different directories (gcc -M ignores relative paths)

Inline is a patch against the Makefile from 0015 (be aware of tab issues).

--- Makefile.old      2009-04-29 13:55:23.000000000 +0200
+++ Makefile      2009-04-29 13:57:36.000000000 +0200
@@ -1,4 +1,4 @@
-# Arduino 0011 Makefile
+# Arduino 0015 Makefile
 # Arduino adaptation by mellis, eighthave, oli.keller
 #
 # This makefile allows you to build sketches from the command line
@@ -35,7 +35,7 @@
 # $Id$
 
 TARGET = $(notdir $(CURDIR))
-INSTALL_DIR = /Users/dmellis/Source/arduino/trunk/build/macosx/build/work
+INSTALL_DIR = ../../..
 PORT = /dev/tty.usb*
 UPLOAD_RATE = 19200
 AVRDUDE_PROGRAMMER = stk500v1
@@ -223,6 +223,13 @@
       $(CC) -c $(ALL_ASFLAGS) $< -o $@
 
 
+# Automatic dependencies
+%.d: %.c
+      $(CC) -M $(ALL_CFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@
+
+%.d: %.cpp
+      $(CXX) -M $(ALL_CXXFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@
+
 
 # Target: clean project.
 clean:
@@ -230,15 +237,7 @@
       applet/$(TARGET).map applet/$(TARGET).sym applet/$(TARGET).lss applet/core.a \
       $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d)
 
-depend:
-      if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
-      then \
-            sed -e '/^# DO NOT DELETE/,$d' $(MAKEFILE) > \
-                  $(MAKEFILE).$$ && \
-            $(MV) $(MAKEFILE).$$ $(MAKEFILE); \
-      fi
-      echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
-            >> $(MAKEFILE); \
-      $(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)
+.PHONY:      all build elf hex eep lss sym program coff extcoff clean applet_files sizebefore sizeafter
 
-.PHONY:      all build elf hex eep lss sym program coff extcoff clean depend applet_files sizebefore sizeafter
+include $(SRC:.c=.d)
+include $(CXXSRC:.cpp=.d)

See http://www.ipp.mpg.de/~dpc/gmake/make_42.html for a discussion of automatic dependencies.

very nice, but I am not sure if the new IDE uses the makefile at all.

but I am not sure if the new IDE uses the makefile at all.

It doesn't, the make file is for people who don't use the IDE.

--Phil.

I patched the Makefile; the changes will be in the next release.

Ah, nice, thanks.

Hey,guys! when I use make command by windows commandline for a simple testing program,there occurs some errors as follows:

E:\arduino code\test1>make makefile:242:../../arduino-0017/hardware/cores/arduino/pins_arduino.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring_analog.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring_digital.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring_pulse.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring_serial.d:No such file or directory make:*** No rule to make target '../../arduino-0017/hardware/cores/arduino/wiring_serial.d'.Stop.

it seems some .d files are not generated,have you ever happened to this? Regards! --Icing

when I use make command by windows commandline for a simple testing program,there occurs some errors as follows:

E:\arduino code\test1>make makefile:242:../../arduino-0017/hardware/cores/arduino/pins_arduino.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring_analog.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring_digital.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring_pulse.d:No such file or directory makefile:242:../../arduino-0017/hardware/cores/arduino/wiring_serial.d:No such file or directory make:*** No rule to make target '../../arduino-0017/hardware/cores/arduino/wiring_serial.d'.Stop.

it seems some .d files are not generated,have you ever happened to this? Regards!

This was fixed in Arduino 0017, I think. In older versions, this happened on the first invocation of make (but not on subsequent ones).

It's harmless: The dependency files are generated as needed and they just don't exist in a fresh install or project.

  • Lars

This was fixed in Arduino 0017, I think.

I just did an install of 0017 (clean install / new to Arduino) and this issue still seems to be there. I addressed using this method:

http://code.google.com/p/arduino/issues/detail?id=65#c3

It's harmless:

For me, it caused critical errors to never be displayed. It may cause processing to stop.

I am still working through the "hidden" errors.

This was fixed in Arduino 0017, I think.

I just did an install of 0017 (clean install / new to Arduino) and this issue still seems to be there. I addressed using this method:

http://code.google.com/p/arduino/issues/detail?id=65#c3

I know David Mellis applied a second patch, I thought it was 0017, but I could be wrong about the version.

The fix is very simple.

Replace:

include $(SRC:.c=.d)
include $(CXXSRC:.cpp=.d)

with:

-include $(SRC:.c=.d)
-include $(CXXSRC:.cpp=.d)

But as far as I understand you, you already did this.