Forcing a file to recompile

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hopefully I've chosen the right forum for this question:

All firmware I build includes a version number that can somehow be read by the user at runtime. In case I forget to update the version number, I use the __DATE__ macro to force a new version number at every build. The problem with this is that I must somehow force the file containing my version number to be rebuilt every time make is called (or I hit F7 in AVR Studio).

Is there some way to force GCC or just AVR Studio to always rebuild a file? Or perhaps there's a clever way to achieve my goal without all that mess.

Perferably, this can be done without me having to maintain my own Makefile (thus letting AVR Studio handle it for me).

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With your own Makefile, it can be done. Normally, .o files just depend from
their .c file, plus all the .h files they are including. There is a so-called
inference rule for that (which is even built into make itself, so it doesn't
necessarily appear in your Makefile).

Change that into an explicit rule for that file, and add a depdency that is
always out-of-date (because it does not exist at all):

timestamp.o: timestamp.c force
	$(CC) $(CFLAGS) -c timestamp.c

force:

(N.B.: The spaces before the $(CC) must be a hard TAB character.)

That way, timestamp.o depends on a hypothetical file named force which
does not exist (and *must* never exist) but the rule for that target
named "force" just does nothing, so it will never exist.

If you want to play nicely to your make utility, you could even add

.PHONY: force

That tells GNU make that force is not supposed to be a file at all but
just a dummy target inside the Makefile.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using force will cause a recompile of timestamp.c
even if there is nothing else to do.
If you compile timestamp.c only as part of the link step,
redundant recompiles can be avoided.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I had the same dilemma using __DATE__ and __TIME__ to version stamp a build and simply added a rule that invoked 'touch' to ensure the source that contained them is always rebuilt:

# Eye candy.
# AVR Studio 3.x does not check make's exit code but relies on
# the following magic strings to be generated by the compile job.
begin:
	@echo
	@echo $(MSG_BEGIN)
	@touch main.c

"clunky" but it works.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
I had the same dilemma using __DATE__ and __TIME__ to version stamp a build and simply added a rule that invoked 'touch' to ensure the source that contained them is always rebuilt:

# Eye candy.
# AVR Studio 3.x does not check make's exit code but relies on
# the following magic strings to be generated by the compile job.
begin:
	@echo
	@echo $(MSG_BEGIN)
	@touch main.c

"clunky" but it works.

It's ok if you don't mind spurious recompiles or if
you never do a make when there is nothing to do.
Putting main.c on the link line instead would avoid spurious recompiles.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

my method

file version.c

const char BuildTime[] __attribute__((__progmem__)) = __TIME__;
const char BuildDate[] __attribute__((__progmem__)) = __DATE__;

Makefile

%.elf: $(OBJ)
	@echo
	@echo update build time and date.
	$(CC) -c $(ALL_CFLAGS) version.c -o $(OBJDIR)/version.o
	@echo
	@echo $(MSG_LINKING) $@
	$(CC) $(ALL_CFLAGS) $(OBJDIR)/version.o $^ --output $@ $(LDFLAGS)