Skip to content

Instantly share code, notes, and snippets.

@floswald
Created July 16, 2020 11:52
Show Gist options
  • Save floswald/f79e654a64ca4f75d1e617d28679425f to your computer and use it in GitHub Desktop.
Save floswald/f79e654a64ca4f75d1e617d28679425f to your computer and use it in GitHub Desktop.
#
#
# To build a target, enter:
#
# make <target>
#
# Targets:
#
# all - builds exe, dbg
# clean - deletes all .o, .a, binaries, in source/ directory
# cleanall- deletes all .o, .a, binaries, in all dependencies as well
# tests - builds unit tests
# exe - optimized application
# dbg - application with debug symbols
# dbg.knitro - debug version of knitro
# knitro - optimised knitro
#
# Options:
#
# USE_MPI - defaults to 0 (no MPI). If defined to 1, build
# application with MPI libraries for execution on
# multiple processors
#
# USE_DIAG - defaults to 0 (no diagnostic printing). Set to 1
# on the command line to enable diagnostic printing.
#
# Example:
#
# To build an optimized application with MPI:
#
# make USE_MPI=1 exe
#
# To build a optimized application for one processor (sequential):
#
# make exe
#
# To build a sequential debug application:
#
# make dbg
#
.SUFFIXES: .a .o .f95 .f90 .mod
#------------------------------------------
# Select Macros for current computer system
#------------------------------------------
# Select correct Macros based on platform in use
include ../config/LoadLocalDefs.inc
SRC_DIR = $(shell pwd)
# include $(PFUNIT)/include/base.mk
#----------------------------
# Compilers and archive tools
#----------------------------
# Fortran and C Compiler
FC = $(LOCAL_FC)
CC = ${LOCAL_CC}
# Linker: use fortran to link
FL = $(LOCAL_FL)
# Archive tools -- use GNU by default
AR = $(LOCAL_AR)
RANLIB = $(LOCAL_RANLIB)
AR_FLAGS = $(LOCAL_AR_FLAGS)
#-----------------------------------------------------------------------------------------
# Define libraries
#
# USE_MPI
# USE_MPI = 0 NO MPI libaries (default)
# USE_MPI = 1 USE MPI LIBRARIES
#-----------------------------------------------------------------------------------------
ifeq ($(USE_MPI),1)
LOCAL_FL_LIBS += $(LOCAL_MPI_LIBS)
LOCAL_FC_FLAGS += $(LOCAL_MPI_FLAGS)
LOCAL_FC_DEBUG_FLAGS += $(LOCAL_MPI_DEBUG_FLAGS)
FC = $(LOCAL_MPI_FC)
LOCAL_CC_LIBS += $(LOCAL_MPI_LIBS)
LOCAL_CC_FLAGS += $(LOCAL_MPI_FLAGS)
LOCAL_CC_DEBUG_FLAGS += $(LOCAL_MPI_DEBUG_FLAGS)
CC += $(LOCAL_MPI_CC)
endif
LOCAL_FC_FLAGS += -DUSE_NAG=$(USE_NAG)
LOCAL_FC_DEBUG_FLAGS += -DUSE_NAG=$(USE_NAG)
# -----------------
# Library Locations
# -----------------
NR_DIR = $(LOCAL_NR_DIR)
TOOLS_DIR = $(LOCAL_TOOLS_DIR)
NAG_INTERFACE_BLOCKS = ${LOCAL_NAG_BLOCKS}
MKL_DIR = ${LOCAL_MKL_DIR}
KNITRO_DIR = ${LOCAL_KNITRO_DIR}
# Summarize Libraries to build
LIBS = $(NR_DIR)/libnr.a $(TOOLS_DIR)/libtools.a
LIBS_D = $(NR_DIR)/libnr_d.a $(TOOLS_DIR)/libtools_d.a
#----------------------------------
# Define compiler and linker flags
#----------------------------------
# fortran
FC_FLAGS = $(LOCAL_FC_FLAGS) -I${MKL_DIR} -I$(NR_DIR) -I$(TOOLS_DIR) -I${NAG_INTERFACE_BLOCKS} -DHOST_ID=$(HOST_ID)
FC_DEBUG_FLAGS = $(LOCAL_FC_DEBUG_FLAGS) -I${MKL_DIR} -I$(NR_DIR) -I$(TOOLS_DIR) -I${NAG_INTERFACE_BLOCKS} -DHOST_ID=$(HOST_ID)
FC_TEST_FLAGS = $(LOCAL_FC_FLAGS) -I${MKL_DIR} -I$(NR_DIR) -I$(TOOLS_DIR) -I${NAG_INTERFACE_BLOCKS} -DHOST_ID=$(HOST_ID) -DUSE_PFUNIT=1
# add commandline args to flags
ifneq ($(USE_DIAG),)
FC_FLAGS += -DUSE_DIAG=$(USE_DIAG)
FC_DEBUG_FLAGS += -DUSE_DIAG=$(USE_DIAG)
FC_TEST_FLAGS += -DUSE_DIAG=$(USE_DIAG)
endif
# c-compiler
CC_FLAGS = $(LOCAL_CC_FLAGS)
CC_DEBUG_FLAGS = $(LOCAL_CC_DEBUG_FLAGS)
# Fortran linker flags
FL_LIBS = $(LOCAL_FL_LIBS)
CC_LIBS = ${LOCAL_CC_LIBS}
FL_LIBS += -L$(NR_DIR) -L$(TOOLS_DIR)
FL_LIBS_TEST += $(FL_LIBS) -lnr -ltools -L$(SRC_DIR) -L$(TEST_DIR) -L$(PFUNIT)/lib -lpfunit
# Add the following line to FC_FLAGS to use profiling data to
# improve compiler optimization:
# -prof-use -prof-dir ./profdata
#
# Add the following line to FC_FLAGS to generate profile data
#
# -prof-genx -prof-dir ./profdata
# ------------
# Knitro setup
# ------------
# KNITRO_DIR must be set in config!
ifeq ($(KNITRO_DIR),)
$(error KNITRO_DIR must be set in config!)
endif
ifeq ($(KNITRORELEASE),)
$(error KNITRORELEASE must be set in config!)
endif
# Libraries to link knitro with
KNITRO_LIBS = $(LOCAL_KNITRO_LIBS)
# KNITRO_LIBS= ${KNITRO_DIR}/lib/libknitro${KNITRORELEASE}.a
# -----------------
# Source file setup
# -----------------
# fortran source/object files common to all executables
ifeq ($(USE_NAG),1)
COMMON_SOURCES = \
GlobalModule.f90 \
OutputModule.f90 \
StructuralModule.f90 \
ConstraintModule.f90
else
COMMON_SOURCES = \
GlobalModule.f90 \
OutputModule.f90 \
StructuralModule.f90 \
nag_dummy.f90 \
ConstraintModule.f90
endif
COMMON_OBJECTS = $(patsubst %.f90, %.o, $(COMMON_SOURCES))
# Sources for main executable
MAIN_SOURCES = main.f90
MAIN_OBJECTS = $(patsubst %.f90, %.o, $(MAIN_SOURCES))
# Sources/objects for knitro executable
KNITRO_SOURCES = KnitroModule.f90 \
KnitroWrappers.f90 \
main_knitro.f90
KNITRO_OBJECTS = $(patsubst %.f90, %.o, $(KNITRO_SOURCES))
# Application Name -- what to build
TGT = housing
# separate filenames for debug objects
DEBUG_OBJS = $(patsubst %.o, %_d.o, $(COMMON_OBJECTS) $(MAIN_OBJECTS))
DEBUG_KNITRO = $(patsubst %.o, %_k.o, $(COMMON_OBJECTS) $(KNITRO_OBJECTS))
# NOTE: This order of single and double quotes is crucial
# because any other order will cause mpiifort to fail because
# its preprocessor is weaker than ifort's. In particular, it
# seems to only take one pass and have trouble with quoting...
CC_FLAGS += -DAPP_NAME='"$(TGT)"'
CC_DEBUG_FLAGS += -DAPP_NAME='"$(TGT)"'
FC_FLAGS += -DAPP_NAME='"$(TGT)"'
FC_DEBUG_FLAGS += -DAPP_NAME='"$(TGT)"'
FC_TEST_FLAGS += -DAPP_NAME='"$(TGT)"' -I$(PFUNIT)/mod
#----------------------------------------------------------
# export variables to be available in lower level makefiles
#----------------------------------------------------------
export FC
export CC
export AR
export RANLIB
export AR_FLAGS
export NR_DIR
export TOOLS_DIR
export MKL_DIR
export SRC_DIR
export CC_LIBS
export FL_LIBS
export FL_LIBS_TEST
export CC_FLAGS
export CC_DEBUG_FLAGS
export FC_FLAGS
export FC_DEBUG_FLAGS
export FC_TEST_FLAGS
export SRC_DIR
export KNITRO_DIR
export OBJ_EXT = .o
#-----------------------------------------------
# Targets - these are objects which can be built
#-----------------------------------------------
tests: $(TGT).tests
dbg: $(TGT).dbg
exe: $(TGT).exe
dbg.knitro: $(TGT).dbg.knitro
knitro: $(TGT).knitro
all.knitro: dbg.knitro knitro
all: dbg exe knitro dbg.knitro
cleanall: cleantools cleannr clean cleantests
#-----------------------------
# Rule to build libraries
# The codes $(patsubst ...) adds a prefix to
# each source file defining the path where it is stored
# This section also builds the libraries (libnr,libtools) if they have not yet already been built.
#-----------------------------
$(NR_DIR)/libnr.a:
make -C $(NR_DIR) libnr.a
$(NR_DIR)/libnr_d.a:
make -C $(NR_DIR) libnr_d.a
$(TOOLS_DIR)/libtools.a:
make -C $(TOOLS_DIR) libtools.a
$(TOOLS_DIR)/libtools_d.a:
make -C $(TOOLS_DIR) libtools_d.a
SUT:
make -C $(TEST_DIR) testSuites.inc
make -C $(TEST_DIR) tests
#----------------------------
# Rules to build application
#----------------------------
$(TGT).dbg : $(LIBS_D) $(DEBUG_OBJS)
$(FC) $(FC_DEBUG_FLAGS) $^ -o $@ \
$(NR_DIR)/libnr_d.a $(TOOLS_DIR)/libtools_d.a \
$(FL_LIBS)
$(TGT).exe : $(LIBS) $(COMMON_OBJECTS) $(MAIN_OBJECTS)
$(FC) $(FC_FLAGS) $^ -o $@ \
$(FL_LIBS)
# builds the tests
$(TGT).tests : $(LIBS) $(COMMON_OBJECTS) SUT
$(FC) $(FC_TEST_FLAGS) -o $@ -I$(PFUNIT)/mod -I$(PFUNIT)/include \
$(PFUNIT)/include/driver.F90 -I$(TEST_DIR) -I$(TOOLS_DIR) \
$(TEST_DIR)/*$(OBJ_EXT) $(SRC_DIR)/*$(OBJ_EXT) $(FL_LIBS_TEST)
# build version with knitro
$(TGT).dbg.knitro: $(LIBS_D) $(DEBUG_KNITRO) knitro_fortran_k.o $(KNITRO_LIBS)
$(FC) $(FC_DEBUG_FLAGS) $^ -o $@ \
$(NR_DIR)/libnr_d.a $(TOOLS_DIR)/libtools_d.a \
$(CC_LIBS) $(FL_LIBS)
$(TGT).knitro: $(LIBS) $(COMMON_OBJECTS) $(KNITRO_OBJECTS) knitro_fortran.o $(KNITRO_LIBS)
$(FC) $(FC_FLAGS) $^ -o $@ $(CC_LIBS) $(FL_LIBS)
# compile C code for knitro
knitro_fortran_k.o: knitro_fortran.c $(KNITRO_DIR)/include/knitro.h
$(CC) $(CC_DEBUG_FLAGS) -c -I$(KNITRO_DIR)/include $< -o $@
knitro_fortran.o: knitro_fortran.c $(KNITRO_DIR)/include/knitro.h
$(CC) $(CC_FLAGS) -c -I$(KNITRO_DIR)/include $< -o $@
#-----------------------------
# clean up any messes which have been created
#----------------------------
clean:
rm -rf *.o $(TGT) *.mod a.out *.a *.dbg *.exe *.tests *.knitro housing.dbg.knitro.dSYM
cleantools:
cd $(TOOLS_DIR) ; make clean
cleannr:
cd $(NR_DIR) ; make clean
cleantests:
make -C $(TEST_DIR) clean
# -------------
# Pattern Rules
# -------------
$(COMMON_OBJECTS) : %.o : %.f90
$(FC) $(FC_FLAGS) -c $< -o $@
$(MAIN_OBJECTS) : %.o : %.f90
$(FC) $(FC_FLAGS) -c $< -o $@
$(KNITRO_OBJECTS) : %.o : %.f90
$(FC) $(FC_FLAGS) -c $< -o $@
$(DEBUG_OBJS) : %_d.o : %.f90
$(FC) $(FC_DEBUG_FLAGS) -c $< -o $@
$(DEBUG_KNITRO) : %_k.o : %.f90
$(FC) $(FC_DEBUG_FLAGS) -c $< -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment