diff --git a/.gitignore b/.gitignore index 4c27c27..1cab72c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ build # Locally generated libraries *.a +*.so +*.dylib diff --git a/Makefile b/Makefile index 70b2022..eda114f 100644 --- a/Makefile +++ b/Makefile @@ -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)