Skip to content

Commit

Permalink
Fix LDFLAGS definition and usage (#199)
Browse files Browse the repository at this point in the history
`ld` flags are currently specified incorrectly. For example:
1. `-lnetcdf` is not required since it is a dependency of the netCDF
Fortran library - only `-lnetcdff` is needed.
2. `ld` flags are being split across two variables: `LDFLAGS` and `LD`
when there should only be one (`LD` typically refers to the path to the
`ld` executable). Having the flags split across two variables allows for
the flags to be passed to the compiler in an inconsistent way.

See #198 for more details.

This change removes the `LD` variable and any hard-coded linker flags or
include flags in the Makefile or build script. Linker flags and include
flags are replaced with a query to `pkg-config` so that the correct
flags are used. Using `pkg-config` also insulates the build system from
changes to library versions and dependencies.

~~This PR should ideally be done after #197 is merged so that we can
verify compiled object files have not been altered as a result of this
change.~~

Compiled object files are identical between this branch and the main
branch. However, the executables differ between the two branches due to
changes in the compiler arguments at the linking step.

Regression tests using
[benchcab](https://github.com/CABLE-LSM/benchcab)* (bitwise comparison
of model output via nccmp) show that model output is bitwise identical
between the current branch and the main branch for serial and MPI model
runs.

*executables were built manually for the spatial case (see this related
issue: CABLE-LSM/benchcab#244).

Fixes #198

<!-- readthedocs-preview cable start -->
----
📚 Documentation preview 📚:
https://cable--199.org.readthedocs.build/en/199/

<!-- readthedocs-preview cable end -->
  • Loading branch information
SeanBryan51 authored Feb 28, 2024
1 parent 67a52dc commit 4be9a61
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
9 changes: 3 additions & 6 deletions src/offline/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,23 @@ PROG_MPI = cable-mpi
# from the compiled object files
OBJS := ${LSRC:.F90=.o}

#compiler switches and flags
CINC = -I$(NCMOD)

#suffixes we use
.SUFFIXES:
.SUFFIXES: .F90 .o
#default rules for these suffixes
.F90.o:
$(FC) $(CFLAGS) $(CINC) -c $<
$(FC) $(CFLAGS) -c $<

SUPPRESS_FLAGS := -warn nostderrors -diag-disable 10145

# the first target is the default target
.PHONY: serial
serial: cable_driver.F90 $(OBJS)
$(FC) $(SUPPRESS_FLAGS) $(CFLAGS) $(LDFLAGS) $(LD) -o $(PROG_SERIAL) $^ $(CINC)
$(FC) $(SUPPRESS_FLAGS) $(CFLAGS) -o $(PROG_SERIAL) $^ $(LDFLAGS)

.PHONY: mpi
mpi: cable_mpidrv.F90 cable_mpicommon.o cable_mpimaster.o cable_mpiworker.o pop_mpi.o $(OBJS)
$(FC) $(SUPPRESS_FLAGS) $(CFLAGS) $(LDFLAGS) -o $(PROG_MPI) $^ $(CINC) $(LD)
$(FC) $(SUPPRESS_FLAGS) $(CFLAGS) -o $(PROG_MPI) $^ $(LDFLAGS)

cable_mpicommon.o: cable_mpicommon.F90 $(OBJS)
pop_mpi.o: pop_mpi.F90 cable_mpicommon.o $(OBJS)
Expand Down
29 changes: 17 additions & 12 deletions src/offline/build3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,37 @@ host_gadi()
module purge
module add intel-compiler/2019.5.281
module add netcdf/4.6.3
# This is required so that the netcdf-fortran library is discoverable by
# pkg-config:
prepend_path PKG_CONFIG_PATH "${NETCDF_BASE}/lib/Intel/pkgconfig"

export NCDIR=$NETCDF_ROOT'/lib/Intel'
export NCMOD=$NETCDF_ROOT'/include/Intel'
export CFLAGS='-O2 -fp-model precise '
export LDFLAGS='-L'$NCDIR
export LD='-lnetcdf -lnetcdff'
if ! pkg-config --exists netcdf-fortran; then
echo -e "\nUnable to find netcdf-fortran via pkg-config. Exiting...\n"
exit 1
fi

if [[ $1 = 'mpi' ]]; then
module add intel-mpi/2019.5.281
export FC='mpif90'
else
export FC='ifort'
fi

CFLAGS="-O2 -fp-model precise"

if [[ $1 = 'MGK' ]]; then
export CFLAGS='-O2'
#export NCMOD=$NETCDF_ROOT'/include'
CFLAGS='-O2'
fi

if [[ $1 = 'debug' ]]; then
export CFLAGS='-O0 -traceback -g -fp-model precise -ftz -fpe0'
#export CFLAGS='-O0 -traceback -g -fp-model precise -ftz -fpe0 -check all,noarg_temp_created'
CFLAGS='-O0 -traceback -g -fp-model precise -ftz -fpe0'
#CFLAGS='-O0 -traceback -g -fp-model precise -ftz -fpe0 -check all,noarg_temp_created'
fi

CFLAGS+=" $(pkg-config --cflags netcdf-fortran)"
export CFLAGS
LDFLAGS=$(pkg-config --libs netcdf-fortran)
export LDFLAGS

if [[ $1 = 'mpi' ]]; then
build_build mpi
Expand Down Expand Up @@ -144,9 +152,6 @@ build_status()
echo ''
echo 'Oooops. Something went wrong'
echo ''
echo 'Other than compiler messages, known build issues:'
echo 'Some systems require additional library. '
echo 'e.g. Edit Makefile; add -lnetcdff to LD = ...'
fi
exit
}
Expand Down

0 comments on commit 4be9a61

Please sign in to comment.