This is the mail archive of the crossgcc@sources.redhat.com mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more information.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Makefile best practices for embedded development


Ken Rose writes:
 > When the project I was working 
 > on encountered a similar problem, we modified our assembler to generate 
 > dependencies.  We weren't using gas, so that doesn't help you.  This 
 > does seem like a good thing for gas (or gasp?) to do, though.

Newer gcc's have much better (IMO, YMMV) support for generating dependency
information.  Typical use is to generate dependencies at build time and put
them into separate files that are included by the Makefile.
It's a chicken-and-egg situation that is resolved by the fact that
one begins with a clean directory (so everything needs to be built from
scratch anyway) and "-include" is used to include the dependency files
(so make doesn't complain if they don't exist).
I've seen it work extremely well in practice with two caveats:
dependencies on machine-generated files still need to be explicitly stated,
and if the provided defaults don't work you need to remember to
explicitly add the -Wp,-M,-MP,-MT,$(*F).o,-MF,.deps/$(@F).p

The .p suffix is historical, one might prefer .d.

CC1_DEPENDENCY and AS_DEPENDENCY are there so that your files get
automatically rebuilt if the compiler changes.  One might wish to
remove them depending on context.


e.g. add this to your Makefile (NOTE: requires GNU make):

# NOTE: It is important to include the file extension in the
# dependence-file names so that files with the same stem
# but different extensions don't step on each other.

%.o: %.c $(CC1_DEPENDENCY) $(AS_DEPENDENCY)
	$(CC_FOR_TARGET) $(ALL_CFLAGS_FOR_TARGET) -Wp,-M,-MP,-MT,$(*F).o,-MF,.deps/$(@F).p -c $<

%.o: %.S $(AS_DEPENDENCY)
	$(CC_FOR_TARGET) $(ALL_SFLAGS_FOR_TARGET) -Wp,-M,-MP,-MT,$(*F).o,-MF,.deps/$(@F).p -c $<

# Utility rule to clean out .deps dirs.
clean: clean-my-dot-deps
clean-my-dot-deps:
	rm -f .deps/*

# This actually includes the dependencies that were generated during the
# previous build by the default rules (e.g. .c->.o).

DEP_FILES = $(shell mkdir -p .deps >&/dev/null) $(wildcard .deps/*)
-include $(DEP_FILES)

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sources.redhat.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]