Skip to content

Commit

Permalink
Makefile: make Fortran object files depend on their dependency files
Browse files Browse the repository at this point in the history
When 'make' is invoked on the CICE Makefile, the first thing it does is
to try to make the included dependency files (*.d) (which are in fact
Makefiles themselves) [1], in alphabetical order.

The rule to make the dep files have the dependency generator, 'makdep',
as a prerequisite, so when processing the first dep file, make notices
'makdep' does not exist and proceeds to build it. If for whatever reason
this compilation fails, make will then proceed to the second dep file,
notice that it recently tried and failed to build its dependency
'makdep', give up on the second dep file, proceed to the third, and so
on.

In the end, no dep file is produced. Make then restarts itself and
proceeds to build the code, which of course fails catastrophically
because the Fortran source files are not compiled in the right order
because the dependency files are missing.

To avoid that, add a dependency on the dep file to the rules that make
the object file out of the Fortran source files. Since old-fashioned
suffix rules cannot have their own prerequisites [2], migrate the rules
for the Fortran source files to use pattern rules [3] instead. While at
it, also migrate the rule for the C source files.

With this new dependency, the builds abort early, before trying to
compile the Fortran sources, making it easier to understand what
has gone wrong.

Since we do not use suffix rules anymore, remove the '.SUFFIXES' lines.

[1] https://www.gnu.org/software/make/manual/html_node/Remaking-Makefiles.html
[2] https://www.gnu.org/software/make/manual/html_node/Suffix-Rules.html
[3] https://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html#Pattern-Rules
  • Loading branch information
phil-blain committed Nov 25, 2021
1 parent 2ccb8f2 commit 673679a
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions configuration/scripts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ MODDIR:= -I.
RM := rm
AR := ar

.SUFFIXES:
.SUFFIXES: .F90 .F .c .o

.PHONY: all cice libcice targets target db_files db_flags clean realclean helloworld calchk sumchk bcstchk
all: $(EXEC)

Expand Down Expand Up @@ -167,13 +164,13 @@ libcice: $(OBJS)
@ echo "$(AR) -r $(EXEC) $(OBJS)"
$(AR) -r $(EXEC) $(OBJS)

.c.o:
%.o : %.c
$(CC) $(CFLAGS) $(CPPDEFS) $(INCLDIR) $<

.F.o:
%.o : %.F %.d
$(FC) -c $(FFLAGS) $(FIXEDFLAGS) $(CPPDEFS) $(INCLDIR) $<

.F90.o:
%.o : %.F90 %.d
$(FC) -c $(FFLAGS) $(FREEFLAGS) $(CPPDEFS) $(MODDIR) $(INCLDIR) $<

clean:
Expand Down

0 comments on commit 673679a

Please sign in to comment.