From 59cc4ff43f1825dd2c6e856b5e0d21a63b7f7443 Mon Sep 17 00:00:00 2001 From: minhqdao Date: Sat, 2 Mar 2024 00:40:58 +0300 Subject: [PATCH 1/5] Add special targets --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index c0a17ed..5022a8f 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +.POSIX: +.SUFFIXES: + FC = gfortran FFLAGS = -O2 AR = ar From 9b76748ca242e541cab8fe2bdc3a8076b841d30d Mon Sep 17 00:00:00 2001 From: minhqdao Date: Sat, 2 Mar 2024 00:43:12 +0300 Subject: [PATCH 2/5] Shorten by removing dashes --- Makefile | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 5022a8f..29b4549 100644 --- a/Makefile +++ b/Makefile @@ -9,24 +9,24 @@ ARFLAGS = rcs NAME = version-f FILENAME = version_f SRC = src/$(FILENAME).f90 -TEST-SRC = test/version_f_test.f90 +TESTSRC = test/version_f_test.f90 STATIC = lib$(NAME).a -BUILD-DIR = build/Makefile -MOD-DIR = $(BUILD-DIR)/mod -OBJ-DIR = $(BUILD-DIR)/obj -TEST-DIR = $(BUILD-DIR)/test +BUILDDIR = build/Makefile +MODDIR = $(BUILDDIR)/mod +OBJDIR = $(BUILDDIR)/obj +TESTDIR = $(BUILDDIR)/test ifeq ($(FC),nvfortran) - MOD-OUTPUT = -module $(MOD-DIR) + MOD-OUTPUT = -module $(MODDIR) else - MOD-OUTPUT = -J$(MOD-DIR) + MOD-OUTPUT = -J$(MODDIR) endif ifeq ($(FC),nvfortran) - MOD-INPUT = -module $(MOD-DIR) + MOD-INPUT = -module $(MODDIR) else - MOD-INPUT = -I$(MOD-DIR) + MOD-INPUT = -I$(MODDIR) endif .PHONY: all test clean @@ -34,15 +34,15 @@ endif all: $(STATIC) $(STATIC): $(SRC) - mkdir -p $(MOD-DIR) $(OBJ-DIR) - $(FC) $(FFLAGS) -c $(SRC) $(MOD-OUTPUT) -o $(OBJ-DIR)/$(FILENAME).o - $(AR) $(ARFLAGS) $(STATIC) $(OBJ-DIR)/$(FILENAME).o + mkdir -p $(MODDIR) $(OBJDIR) + $(FC) $(FFLAGS) -c $(SRC) $(MODOUTPUT) -o $(OBJDIR)/$(FILENAME).o + $(AR) $(ARFLAGS) $(STATIC) $(OBJDIR)/$(FILENAME).o -test: $(TEST-SRC) $(STATIC) - mkdir -p $(TEST-DIR) - $(FC) $(FFLAGS) $(TEST-SRC) $(MOD-INPUT) -o $(TEST-DIR)/test.out $(STATIC) - $(TEST-DIR)/test.out +test: $(TESTSRC) $(STATIC) + mkdir -p $(TESTDIR) + $(FC) $(FFLAGS) $(TESTSRC) $(MODINPUT) -o $(TESTDIR)/test.out $(STATIC) + $(TESTDIR)/test.out clean: - rm -rf $(BUILD-DIR) + rm -rf $(BUILDDIR) rm -f $(STATIC) From 1b488e412b8e963287f7198568e60b1d6485a313 Mon Sep 17 00:00:00 2001 From: minhqdao Date: Sat, 2 Mar 2024 00:48:01 +0300 Subject: [PATCH 3/5] Shorten further and fix forgotten ones --- Makefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 29b4549..1cc206e 100644 --- a/Makefile +++ b/Makefile @@ -18,15 +18,15 @@ OBJDIR = $(BUILDDIR)/obj TESTDIR = $(BUILDDIR)/test ifeq ($(FC),nvfortran) - MOD-OUTPUT = -module $(MODDIR) + MODOUT = -module $(MODDIR) else - MOD-OUTPUT = -J$(MODDIR) + MODOUT = -J$(MODDIR) endif ifeq ($(FC),nvfortran) - MOD-INPUT = -module $(MODDIR) + MODIN = -module $(MODDIR) else - MOD-INPUT = -I$(MODDIR) + MODIN = -I$(MODDIR) endif .PHONY: all test clean @@ -35,12 +35,12 @@ all: $(STATIC) $(STATIC): $(SRC) mkdir -p $(MODDIR) $(OBJDIR) - $(FC) $(FFLAGS) -c $(SRC) $(MODOUTPUT) -o $(OBJDIR)/$(FILENAME).o + $(FC) $(FFLAGS) -c $(SRC) $(MODOUT) -o $(OBJDIR)/$(FILENAME).o $(AR) $(ARFLAGS) $(STATIC) $(OBJDIR)/$(FILENAME).o test: $(TESTSRC) $(STATIC) mkdir -p $(TESTDIR) - $(FC) $(FFLAGS) $(TESTSRC) $(MODINPUT) -o $(TESTDIR)/test.out $(STATIC) + $(FC) $(FFLAGS) $(TESTSRC) $(MODIN) -o $(TESTDIR)/test.out $(STATIC) $(TESTDIR)/test.out clean: From 1c8f01cce17d442251ae11b92327fdccf10e721b Mon Sep 17 00:00:00 2001 From: minhqdao Date: Sat, 2 Mar 2024 01:59:09 +0300 Subject: [PATCH 4/5] Use automatic variables --- Makefile | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 1cc206e..9d100c6 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ .POSIX: .SUFFIXES: +.PHONY: all test clean FC = gfortran FFLAGS = -O2 @@ -7,15 +8,19 @@ AR = ar ARFLAGS = rcs NAME = version-f -FILENAME = version_f -SRC = src/$(FILENAME).f90 -TESTSRC = test/version_f_test.f90 STATIC = lib$(NAME).a +SRCDIR = src +TESTDIR = test BUILDDIR = build/Makefile MODDIR = $(BUILDDIR)/mod OBJDIR = $(BUILDDIR)/obj -TESTDIR = $(BUILDDIR)/test +EXEDIR = $(BUILDDIR)/exe + +SRCS := $(wildcard $(SRCDIR)/*.f90) +TESTSRCS := $(wildcard $(TESTDIR)/*.f90) +OBJS := $(patsubst $(SRCDIR)/%.f90,$(OBJDIR)/%.o,$(SRCS)) +TESTOBJS := $(patsubst $(TESTDIR)/%.f90,$(EXEDIR)/%.out,$(TESTSRCS)) ifeq ($(FC),nvfortran) MODOUT = -module $(MODDIR) @@ -29,19 +34,21 @@ else MODIN = -I$(MODDIR) endif -.PHONY: all test clean - all: $(STATIC) -$(STATIC): $(SRC) +$(OBJDIR)/%.o: $(SRCDIR)/%.f90 mkdir -p $(MODDIR) $(OBJDIR) - $(FC) $(FFLAGS) -c $(SRC) $(MODOUT) -o $(OBJDIR)/$(FILENAME).o - $(AR) $(ARFLAGS) $(STATIC) $(OBJDIR)/$(FILENAME).o + $(FC) $(FFLAGS) $(MODOUT) -c $< -o $@ + +$(STATIC): $(OBJS) + $(AR) $(ARFLAGS) $@ $(OBJS) + +$(EXEDIR)/%.out: $(TESTDIR)/%.f90 $(STATIC) + mkdir -p $(EXEDIR) + $(FC) $(FFLAGS) $< $(MODIN) -o $@ $(STATIC) -test: $(TESTSRC) $(STATIC) - mkdir -p $(TESTDIR) - $(FC) $(FFLAGS) $(TESTSRC) $(MODIN) -o $(TESTDIR)/test.out $(STATIC) - $(TESTDIR)/test.out +test: $(TESTOBJS) + $(TESTOBJS) clean: rm -rf $(BUILDDIR) From 92ab5a40751ddbabe0d361e2c8e93e179f24b157 Mon Sep 17 00:00:00 2001 From: minhqdao Date: Sat, 2 Mar 2024 02:21:45 +0300 Subject: [PATCH 5/5] Add examples and run all executables --- Makefile | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 9d100c6..70b2022 100644 --- a/Makefile +++ b/Makefile @@ -12,15 +12,18 @@ STATIC = lib$(NAME).a SRCDIR = src TESTDIR = test +EXMPLDIR = example BUILDDIR = build/Makefile MODDIR = $(BUILDDIR)/mod OBJDIR = $(BUILDDIR)/obj EXEDIR = $(BUILDDIR)/exe -SRCS := $(wildcard $(SRCDIR)/*.f90) -TESTSRCS := $(wildcard $(TESTDIR)/*.f90) -OBJS := $(patsubst $(SRCDIR)/%.f90,$(OBJDIR)/%.o,$(SRCS)) -TESTOBJS := $(patsubst $(TESTDIR)/%.f90,$(EXEDIR)/%.out,$(TESTSRCS)) +SRCS = $(wildcard $(SRCDIR)/*.f90) +TESTSRCS = $(wildcard $(TESTDIR)/*.f90) +EXMPLSRCS = $(wildcard $(EXMPLDIR)/*.f90) +OBJS = $(patsubst $(SRCDIR)/%.f90,$(OBJDIR)/%.o,$(SRCS)) +TESTEXES = $(patsubst $(TESTDIR)/%.f90,$(EXEDIR)/%.out,$(TESTSRCS)) +EXMPLEXES = $(patsubst $(EXMPLDIR)/%.f90,$(EXEDIR)/%.out,$(EXMPLSRCS)) ifeq ($(FC),nvfortran) MODOUT = -module $(MODDIR) @@ -47,8 +50,12 @@ $(EXEDIR)/%.out: $(TESTDIR)/%.f90 $(STATIC) mkdir -p $(EXEDIR) $(FC) $(FFLAGS) $< $(MODIN) -o $@ $(STATIC) -test: $(TESTOBJS) - $(TESTOBJS) +$(EXEDIR)/%.out: $(EXMPLDIR)/%.f90 $(STATIC) + mkdir -p $(EXEDIR) + $(FC) $(FFLAGS) $< $(MODIN) -o $@ $(STATIC) + +test: $(TESTEXES) $(EXMPLEXES) + @for f in $(TESTEXES) $(EXMPLEXES); do $$f; done clean: rm -rf $(BUILDDIR)