From 6af65a44a3b6199f492f8f9fc81576920c7a0707 Mon Sep 17 00:00:00 2001 From: minhqdao Date: Mon, 4 Mar 2024 02:08:51 +0300 Subject: [PATCH] Add support for shared libraries --- .gitignore | 2 + Makefile | 108 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 68 insertions(+), 42 deletions(-) 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..53a6bb5 100644 --- a/Makefile +++ b/Makefile @@ -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)