2021-12-15 02:30:48 +01:00
|
|
|
# Load the configure script variables
|
|
|
|
-include configure.mk
|
|
|
|
|
2021-11-18 16:02:53 +01:00
|
|
|
##
|
|
|
|
## Directories
|
|
|
|
##
|
|
|
|
|
|
|
|
# Structure:
|
|
|
|
# gray - top level
|
|
|
|
# ├── src - source code
|
2021-12-15 02:30:58 +01:00
|
|
|
# ├── doc - documentation
|
2023-12-18 00:52:11 +01:00
|
|
|
# ├── tests - integration tests
|
2021-11-18 16:02:53 +01:00
|
|
|
# └── build - build artifacts
|
|
|
|
# ├── bin - binaries
|
|
|
|
# ├── lib - libraries
|
2022-04-29 01:52:50 +02:00
|
|
|
# ├── obj - objects
|
|
|
|
# └── inc - files to #include
|
2021-11-18 16:02:53 +01:00
|
|
|
SRCDIR = src
|
|
|
|
BUILDDIR = build
|
2021-12-15 02:30:58 +01:00
|
|
|
SHAREDIR = build/share
|
2021-11-18 16:02:53 +01:00
|
|
|
BINDIR = build/bin
|
|
|
|
LIBDIR = build/lib
|
|
|
|
OBJDIR = build/obj
|
2022-04-29 01:52:50 +02:00
|
|
|
INCDIR = build/inc
|
2021-12-15 02:30:58 +01:00
|
|
|
|
2022-05-03 19:56:55 +02:00
|
|
|
PREFIX ?= /usr
|
2021-12-15 02:31:16 +01:00
|
|
|
|
2021-12-15 02:30:58 +01:00
|
|
|
# Directories that need to be created
|
2022-04-29 01:52:50 +02:00
|
|
|
DIRS = $(BINDIR) $(OBJDIR) $(INCDIR) $(LIBDIR) $(SHAREDIR)
|
2021-11-18 16:02:53 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
## Files
|
|
|
|
##
|
|
|
|
|
|
|
|
# All Fortran source files
|
|
|
|
SOURCES = $(wildcard $(SRCDIR)/*.f90)
|
|
|
|
|
2022-04-29 01:52:50 +02:00
|
|
|
# All Fortran object
|
2021-11-18 16:02:53 +01:00
|
|
|
OBJECTS = $(patsubst $(SRCDIR)/%.f90,$(OBJDIR)/%.o,$(SOURCES))
|
|
|
|
|
|
|
|
# Generated makefiles describing object dependencies
|
|
|
|
DEPS = $(OBJECTS:o=d)
|
|
|
|
|
|
|
|
# Fortran modules to be bundled into libraries
|
|
|
|
MODULES = $(filter-out $(OBJDIR)/main%,$(OBJECTS))
|
|
|
|
|
|
|
|
# Build outputs
|
|
|
|
GRAY = $(BINDIR)/gray
|
2023-10-17 11:05:36 +02:00
|
|
|
LIBGRAY = $(LIBDIR)/libgray.so
|
2021-12-15 02:30:55 +01:00
|
|
|
BINARIES = $(GRAY)
|
2023-10-17 11:05:36 +02:00
|
|
|
LIBRARIES = $(LIBGRAY)
|
2021-11-18 16:02:53 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
## Git information (used in the version string)
|
|
|
|
##
|
|
|
|
|
|
|
|
# Short hash of the latest commit
|
|
|
|
GIT_REV ?= $(shell git rev-parse --short HEAD)
|
|
|
|
|
|
|
|
# Whether the worktree and the latest commit differs
|
|
|
|
GIT_DIRTY ?= $(shell test -n "$$(git status --porcelain)" && echo "-dirty")
|
|
|
|
|
|
|
|
##
|
|
|
|
## Fortran compiler and flags
|
|
|
|
##
|
|
|
|
|
|
|
|
# Note: can't use ?= for FC because GNU Make defaults to f77
|
|
|
|
FC = gfortran
|
|
|
|
LD = gfortran
|
2024-01-24 15:10:40 +01:00
|
|
|
FFLAGS += -J$(OBJDIR) -I$(INCDIR) -ffree-line-length-none -fPIC -frecursive
|
2023-10-17 11:05:36 +02:00
|
|
|
LDFLAGS += -L$(LIBDIR)
|
2021-12-15 02:31:16 +01:00
|
|
|
CPPFLAGS += -DREVISION=\"$(GIT_REV)$(GIT_DIRTY)\" -DPREFIX=\"$(PREFIX)\"
|
2021-11-18 16:02:53 +01:00
|
|
|
|
2023-10-13 13:58:47 +02:00
|
|
|
ifndef DETERMINISTIC
|
|
|
|
ifdef AR_DEFAULT_DETERMINISTIC
|
|
|
|
# Write timestamps to allow partial updates
|
|
|
|
ARFLAGS = crU
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2021-11-18 16:02:53 +01:00
|
|
|
# Static build options
|
|
|
|
ifdef STATIC
|
2023-10-17 11:05:36 +02:00
|
|
|
LIBGRAY := $(LIBGRAY:so=a)
|
2021-11-18 16:02:53 +01:00
|
|
|
LIBRARIES := $(LIBRARIES:so=a)
|
2023-11-25 18:11:52 +01:00
|
|
|
ifndef SEMISTATIC
|
2021-11-18 16:02:53 +01:00
|
|
|
LDFLAGS += -static
|
2023-11-25 18:11:52 +01:00
|
|
|
endif
|
2023-10-17 11:05:36 +02:00
|
|
|
else
|
|
|
|
LDFLAGS += -Wl,-rpath '$$ORIGIN/../lib/'
|
2021-11-18 16:02:53 +01:00
|
|
|
endif
|
|
|
|
|
2021-12-15 02:31:06 +01:00
|
|
|
# Debug build options
|
|
|
|
ifdef DEBUG
|
|
|
|
FFLAGS += -ggdb -O0 -Wall -Wunused-parameter
|
|
|
|
# Tricks to detect uninitialised memory
|
|
|
|
FFLAGS += -finit-integer=7777777 -finit-real=snan -ffpe-trap=invalid
|
2023-10-20 13:42:06 +02:00
|
|
|
else
|
|
|
|
# Optimization level 3
|
|
|
|
FFLAGS += -O3
|
2021-12-15 02:31:06 +01:00
|
|
|
endif
|
|
|
|
|
2021-11-18 16:02:53 +01:00
|
|
|
##
|
|
|
|
## Targets
|
|
|
|
##
|
|
|
|
|
2023-12-18 00:52:11 +01:00
|
|
|
.PHONY: all clean check install docs
|
2021-11-18 16:02:53 +01:00
|
|
|
|
2023-10-17 11:01:51 +02:00
|
|
|
# Don't update archives in parallel, it's unsupported
|
|
|
|
.NOTPARALLEL: $(LIBDIR)/libgray.a
|
|
|
|
|
2022-11-17 19:31:58 +01:00
|
|
|
all: $(BINARIES) $(LIBRARIES)
|
2021-11-18 16:02:53 +01:00
|
|
|
|
|
|
|
# Remove all generated files
|
2012-06-21 14:38:29 +02:00
|
|
|
clean:
|
2021-11-18 16:02:53 +01:00
|
|
|
rm -r $(BUILDDIR)
|
|
|
|
|
2023-12-18 00:52:11 +01:00
|
|
|
# Run tests
|
|
|
|
check: $(GRAY)
|
|
|
|
python -Bm tests --binary $^
|
|
|
|
|
2021-12-15 02:30:58 +01:00
|
|
|
# Install libraries, binaries and documentation
|
2022-05-02 10:51:16 +02:00
|
|
|
install: $(BINARIES) $(LIBRARIES) $(SHAREDIR)/doc $(SHAREDIR)/gray.1
|
2023-11-26 18:51:38 +01:00
|
|
|
mkdir -p $(PREFIX)/{bin,lib,share/{doc/res,man/man1}}
|
|
|
|
install -m555 -t $(PREFIX)/bin $(BINDIR)/*
|
|
|
|
install -m555 -t $(PREFIX)/lib $(LIBDIR)/*
|
|
|
|
install -m644 -t $(PREFIX)/share/doc $(SHAREDIR)/doc/manual.*
|
|
|
|
install -m644 -t $(PREFIX)/share/doc/res $(SHAREDIR)/doc/res/*
|
|
|
|
install -m644 -t $(PREFIX)/share/man/man1 $(SHAREDIR)/gray.1
|
2021-11-18 16:02:53 +01:00
|
|
|
|
2022-11-17 19:31:58 +01:00
|
|
|
# dependencies
|
|
|
|
$(OBJDIR)/%.o: $(OBJDIR)/%.d
|
|
|
|
|
2021-11-18 16:02:53 +01:00
|
|
|
# GRAY binary
|
2023-10-17 11:05:36 +02:00
|
|
|
$(GRAY): $(OBJDIR)/main.o $(LIBGRAY) | $(BINDIR)
|
|
|
|
$(LD) $(LDFLAGS) -o '$@' $< -lgray
|
2021-11-18 16:02:53 +01:00
|
|
|
|
|
|
|
# GRAY shared library
|
2023-10-17 11:05:36 +02:00
|
|
|
$(LIBDIR)/libgray.so: $(MODULES) | $(LIBDIR)
|
2021-12-15 02:31:18 +01:00
|
|
|
$(LD) -shared $(LDFLAGS) -o '$@' $^
|
2021-11-18 16:02:53 +01:00
|
|
|
|
|
|
|
# GRAY static library
|
2023-10-17 11:05:36 +02:00
|
|
|
$(LIBDIR)/libgray.a($(MODULES)): | $(LIBDIR)
|
|
|
|
$(LIBDIR)/libgray.a: $(LIBDIR)/libgray.a($(MODULES))
|
2021-11-18 16:02:53 +01:00
|
|
|
|
2022-09-27 18:30:03 +02:00
|
|
|
# GRAY static library when compiling from JETTO
|
|
|
|
-include ../include.mk
|
2023-10-17 11:05:36 +02:00
|
|
|
$(JLIBDIR)/libgray.a: $(LIBDIR)/libgray.a
|
2023-11-26 18:51:38 +01:00
|
|
|
mkdir -p $(LIBDIR)
|
|
|
|
install -m755 '$<' '$@'
|
2022-09-27 18:30:03 +02:00
|
|
|
|
2021-12-15 02:30:58 +01:00
|
|
|
# All documentation
|
2022-05-02 10:51:16 +02:00
|
|
|
docs: $(SHAREDIR)/gray.1 $(SHAREDIR)/doc
|
|
|
|
|
|
|
|
$(SHAREDIR)/doc: | $(SHAREDIR)
|
2021-12-15 02:31:17 +01:00
|
|
|
+make -C doc
|
2021-12-22 23:40:05 +01:00
|
|
|
cp -r doc/build/* $(SHAREDIR)
|
2021-12-15 02:30:58 +01:00
|
|
|
|
2021-12-15 02:31:17 +01:00
|
|
|
# Generated man pages
|
|
|
|
$(SHAREDIR)/gray.1: $(GRAY) doc/gray.1 | $(SHAREDIR)
|
|
|
|
help2man '$<' -i doc/gray.1 -N -n 'beam-tracing code for EC waves' > '$@'
|
|
|
|
|
2021-11-18 16:02:53 +01:00
|
|
|
# Visualise the dependency graph
|
|
|
|
# Note: requires makefile2graph and graphviz
|
|
|
|
graph.svg: Makefile
|
|
|
|
make -Bdn \
|
|
|
|
| make2graph -b \
|
|
|
|
| awk -F'( -> )| ;|[[]' ' \
|
|
|
|
(/label/ && !/(\.o|bin\/gray)/) {bad[$$1]=1; next} \
|
|
|
|
/->/ {if (bad[$$1] || bad[$$2]) next} \
|
|
|
|
{print $$0}' \
|
|
|
|
| sed 's/label/fontsize=30,label/g; s/\.o//' \
|
2021-12-15 02:31:18 +01:00
|
|
|
| dot -Tsvg -o '$@'
|
2021-11-18 16:02:53 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
## Generic rules
|
|
|
|
##
|
|
|
|
|
|
|
|
# Rebuild everything if the makefile changed
|
|
|
|
.EXTRA_PREREQS += Makefile
|
|
|
|
|
|
|
|
# Automatic generation of the Fortran prerequisites
|
|
|
|
# Note 1: this is needed because gfortran -M flag doesn't properly work;
|
|
|
|
# Note 2: this assumes matching module/file names and no circular
|
|
|
|
# dependencies (ie. the dependency graph is a DAG);
|
|
|
|
$(OBJDIR)/%.d: $(SRCDIR)/%.f90 | $(OBJDIR)
|
2021-12-15 02:31:18 +01:00
|
|
|
@printf '$(@:d=o): $< \\\n' > '$@'
|
2021-12-22 17:43:23 +01:00
|
|
|
@grep -vE '^[[:blank:]]*use.*(intrinsic|iso_)' '$<' | \
|
|
|
|
sed -nE 's@^[[:blank:]]*use[[:blank:]]+'\
|
|
|
|
'([^,[:blank:]&]+).*@$(OBJDIR)/\1.o \\@p' | \
|
2021-12-15 02:31:18 +01:00
|
|
|
sort -u >> '$@'
|
2022-10-28 13:32:56 +02:00
|
|
|
@sed -nE 's@^#include "(.+)"@$(INCDIR)/\1@p' '$<' >> '$@'
|
2021-11-18 16:02:53 +01:00
|
|
|
|
|
|
|
# Load the generated rules
|
2023-10-20 16:59:13 +02:00
|
|
|
-include $(DEPS)
|
2021-11-18 16:02:53 +01:00
|
|
|
|
|
|
|
# Compile a generic Fortran source
|
2022-04-29 01:52:50 +02:00
|
|
|
$(OBJDIR)/%.o: $(SRCDIR)/%.f90 | $(OBJDIR) $(INCDIR)
|
2021-12-15 02:31:18 +01:00
|
|
|
$(FC) -cpp $(CPPFLAGS) $(FFLAGS) -c '$<' -o '$@'
|
2021-11-18 16:02:53 +01:00
|
|
|
|
2024-01-29 01:08:28 +01:00
|
|
|
# Generate Fortran code from awk script
|
|
|
|
$(INCDIR)/%.inc: $(SRCDIR)/%.awk | $(INCDIR)
|
|
|
|
awk -f '$<' > '$@'
|
2022-04-29 01:52:50 +02:00
|
|
|
|
2021-11-18 16:02:53 +01:00
|
|
|
# Create directories
|
|
|
|
$(DIRS):
|
2021-12-15 02:31:18 +01:00
|
|
|
mkdir -p '$@'
|