Skip to content

Commit

Permalink
Merge pull request #33 from minhqdao/build-shared-libraries
Browse files Browse the repository at this point in the history
Add support for shared libraries
  • Loading branch information
minhqdao authored Mar 5, 2024
2 parents 04d0ada + 604857e commit b8b7047
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 46 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: 62 additions & 46 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,62 +1,78 @@
.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))

ifeq ($(FC),nvfortran)
MODOUT = -module $(MODDIR)
else
MODOUT = -J$(MODDIR)
.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
LDFLAGS := -Wl,-rpath=.
else ifeq ($(shell uname), Darwin)
SHARED := lib$(NAME).dylib
endif

ifeq ($(FC),nvfortran)
MODIN = -module $(MODDIR)
SRCDIR := src
TESTDIR := test
EXMPLDIR := example
BUILDDIR := build/Makefile
MODDIR := $(BUILDDIR)/mod
OBJDIR := $(BUILDDIR)/obj
EXEDIR := $(BUILDDIR)/exe

SRCS := $(wildcard $(SRCDIR)/*.f90)
OBJS := $(patsubst $(SRCDIR)/%.f90,$(OBJDIR)/%.o,$(SRCS))
EXESRCS := $(foreach dir,$(TESTDIR) $(EXMPLDIR),$(wildcard $(dir)/*.f90))
EXESSTATIC := $(patsubst %.f90,$(EXEDIR)/%_static.out,$(notdir $(EXESRCS)))
EXESSHARED := $(patsubst %.f90,$(EXEDIR)/%_shared.out,$(notdir $(EXESRCS)))

ifeq ($(FC),gfortran)
MODIN := -I$(MODDIR)
MODOUT := -J$(MODDIR)
else
MODIN = -I$(MODDIR)
MODIN := -module $(MODDIR)
MODOUT := $(MODIN)
endif

all: $(STATIC)
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) $@ $<

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

$(EXEDIR):
mkdir -p $(EXEDIR)

$(EXEDIR)/%_static.out: $(TESTDIR)/%.f90 $(STATIC) | $(EXEDIR)
$(FC) $(FFLAGS) $(MODIN) -o $@ $^

$(EXEDIR)/%_static.out: $(EXMPLDIR)/%.f90 $(STATIC) | $(EXEDIR)
$(FC) $(FFLAGS) $(MODIN) -o $@ $^

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

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

test: $(TESTEXES) $(EXMPLEXES)
@for f in $(TESTEXES) $(EXMPLEXES); do $$f; done
test: $(EXESSTATIC) $(EXESSHARED)
@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 b8b7047

Please sign in to comment.