Skip to content

Commit

Permalink
Add support for shared libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
minhqdao committed Mar 3, 2024
1 parent 04d0ada commit 6af65a4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ build

# Locally generated libraries
*.a
*.so
*.dylib
108 changes: 66 additions & 42 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,62 +1,86 @@
.POSIX:
.SUFFIXES:
.PHONY: all test clean

FC = gfortran
FFLAGS = -O2
AR = ar
ARFLAGS = rcs

NAME = version-f
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)
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))
.PHONY: all static shared test clean

FC := gfortran
FFLAGS := -O2
AR := ar
ARFLAGS := rcs

NAME := version-f
STATIC := lib$(NAME).a

ifeq ($(shell uname), Linux)
SHARED := lib$(NAME).so
else ifeq ($(shell uname), Darwin)
SHARED := lib$(NAME).dylib
endif

SRCDIR := src
TESTDIR := test
EXMPLDIR := example
BUILDDIR := build/Makefile
MODDIR := $(BUILDDIR)/mod
OBJDIR := $(BUILDDIR)/obj
EXEDIRSTATIC := $(BUILDDIR)/exe/static
EXEDIRSHARED := $(BUILDDIR)/exe/shared

ifeq ($(FC),nvfortran)
MODOUT = -module $(MODDIR)
MODOUT := -module $(MODDIR)
else
MODOUT = -J$(MODDIR)
MODOUT := -J$(MODDIR)
endif

ifeq ($(FC),nvfortran)
MODIN = -module $(MODDIR)
MODIN := -module $(MODDIR)
else
MODIN = -I$(MODDIR)
MODIN := -I$(MODDIR)
endif

all: $(STATIC)
SRCS := $(wildcard $(SRCDIR)/*.f90)
TESTSRCS := $(wildcard $(TESTDIR)/*.f90)
EXMPLSRCS := $(wildcard $(EXMPLDIR)/*.f90)
OBJS := $(patsubst $(SRCDIR)/%.f90,$(OBJDIR)/%.o,$(SRCS))
TESTEXESSTATIC := $(patsubst $(TESTDIR)/%.f90,$(EXEDIRSTATIC)/%.out,$(TESTSRCS))
TESTEXESSHARED := $(patsubst $(TESTDIR)/%.f90,$(EXEDIRSHARED)/%.out,$(TESTSRCS))
EXMPLEXESSTATIC := $(patsubst $(EXMPLDIR)/%.f90,$(EXEDIRSTATIC)/%.out,$(EXMPLSRCS))
EXMPLEXESSHARED := $(patsubst $(EXMPLDIR)/%.f90,$(EXEDIRSHARED)/%.out,$(EXMPLSRCS))

all: $(STATIC) $(SHARED)
static: $(STATIC)
shared: $(SHARED)

$(OBJDIR)/%.o: $(SRCDIR)/%.f90
mkdir -p $(MODDIR) $(OBJDIR)
$(FC) $(FFLAGS) $(MODOUT) -c $< -o $@
mkdir -p $(MODDIR) $(OBJDIR)
$(FC) $(FFLAGS) $(MODOUT) -c $< -o $@

$(STATIC): $(OBJS)
$(AR) $(ARFLAGS) $@ $(OBJS)
$(AR) $(ARFLAGS) $@ $(OBJS)

$(SHARED): $(SRCS)
mkdir -p $(MODDIR)
$(FC) $(FFLAGS) -fpic -shared $(MODOUT) -o $@ $<

$(EXEDIRSTATIC)/%.out: $(TESTDIR)/%.f90 $(STATIC)
mkdir -p $(EXEDIRSTATIC)
$(FC) $(FFLAGS) $< $(MODIN) -o $@ $(STATIC)

$(EXEDIRSTATIC)/%.out: $(EXMPLDIR)/%.f90 $(STATIC)
mkdir -p $(EXEDIRSTATIC)
$(FC) $(FFLAGS) $< $(MODIN) -o $@ $(STATIC)

$(EXEDIR)/%.out: $(TESTDIR)/%.f90 $(STATIC)
mkdir -p $(EXEDIR)
$(FC) $(FFLAGS) $< $(MODIN) -o $@ $(STATIC)
$(EXEDIRSHARED)/%.out: $(TESTDIR)/%.f90 $(SHARED)
mkdir -p $(EXEDIRSHARED)
$(FC) $(FFLAGS) $< $(MODIN) -o $@ $(SHARED)

$(EXEDIR)/%.out: $(EXMPLDIR)/%.f90 $(STATIC)
mkdir -p $(EXEDIR)
$(FC) $(FFLAGS) $< $(MODIN) -o $@ $(STATIC)
$(EXEDIRSHARED)/%.out: $(EXMPLDIR)/%.f90 $(SHARED)
mkdir -p $(EXEDIRSHARED)
$(FC) $(FFLAGS) $< $(MODIN) -o $@ $(SHARED)

test: $(TESTEXES) $(EXMPLEXES)
@for f in $(TESTEXES) $(EXMPLEXES); do $$f; done
test: $(TESTEXESSTATIC) $(TESTEXESSHARED) $(EXMPLEXESSTATIC) $(EXMPLEXESSHARED)
@for f in $^; do $$f; done

clean:
rm -rf $(BUILDDIR)
rm -f $(STATIC)
rm -rf $(BUILDDIR)
rm -f $(STATIC)
rm -f $(SHARED)

0 comments on commit 6af65a4

Please sign in to comment.