1################################################
2### Compiles the libraries, by Vili Forsell
3################################################
4
5##### MACROS
6
7# Paths:
8MODULE_PATH =./src/GridDataMapper/
9GEN_PATH =./src/Generators/
10POST_PATH =./src/Postprocess/
11
12TMP_PATH =./tmp/
13BACKUP_PATH =./Backup/
14BIN_PATH =./bin/
15SRC_PATH =./src/
16TEST_PATH =./tests/
17DOC_PATH =./doc/
18
19VPATH = $(MODULE_PATH)
20
21# Work directory; chosen to be called "data" and assumed to contain mesh files, ELMER_STARTINFO, case.sif, etc.
22WORK_DIRECTORY = ./data/
23
24ROOT_FILES = Makefile README
25
26# The modules used by the main program
27SOLVER_SRC = NetCDFGeneralUtils.f90 NetCDFGridUtils.f90 NetCDFInterpolate.f90 CustomTimeInterpolation.f90 MapperUtils.f90
28SOLVER_OBJS =  $(SOLVER_SRC:.f90=.o)
29SOLVER_MAIN = GridDataMapper.f90 cs2cs_interface.c
30SOLVER_OUTFILE = GridLib.so
31
32GEN_SRC = $(wildcard $(GEN_PATH)*.f90) $(wildcard $(GEN_PATH)*.cc)
33GEN_OBJS = $(addsuffix .o, $(basename $(shell find $(GEN_PATH) -name *.cc )) $(basename $(shell find $(GEN_PATH) -name *.f90)) $(basename $(shell find $(GEN_PATH) -name *.c)) )
34GEN_MAIN = $(addsuffix .o, $(basename $(GEN_SRC)))
35# outputs to the file names themselves
36
37POST_SRC = $(wildcard $(POST_PATH)*.f90) $(wildcard $(POST_PATH)*.cc)
38POST_OBJS = $(addsuffix .o, $(basename $(shell find $(POST_PATH) -name *.cc )) $(basename $(shell find $(POST_PATH) -name *.f90)) $(basename $(shell find $(POST_PATH) -name *.c)) )
39POST_MAIN = $(addsuffix .o, $(basename $(POST_SRC)))
40# outputs to the file names of the root themselves
41
42ALL_HEADERS = $(shell find $(SRC_PATH) -name *.hh) $(shell find $(SRC_PATH) -name *.h)
43HEADER_PATHS = $(sort $(dir $(ALL_HEADERS))) # sort removes duplicates
44
45# Compilers
46COMPILER_F90 = elmerf90
47COMPILER_F90_2 = gfortran
48COMPILER_CXX = g++
49COMPILER_C = gcc
50
51# Includes NetCDF and such from the include directory
52INCLUDE_FLAGS = -I/usr/include $(foreach hdr, $(HEADER_PATHS), -I$(hdr))
53# NetCDF library path and flags to catch the necessary headers
54LIBRARY_FLAGS = -L/usr/lib -lnetcdff -lnetcdf -lproj $(addprefix -L, $(TMP_PATH))
55# The shared library (.so) for using in Elmer via a Solver Input File
56
57# Packaging macros for easily transporting latest backups elsewhere
58PACKET = GridMap.tar
59TEST_PACKET = Tests
60TAR_FLAGS = cf
61
62##### RULES
63
64# Builds the shared library for Elmer
65solver: $(SOLVER_OBJS)
66	@echo COMPILES THE GRID_DATA_MAPPER
67	@$(COMPILER_F90) $(INCLUDE_FLAGS) $(LIBRARY_FLAGS) -J$(TMP_PATH) $(addprefix $(TMP_PATH), $^) $(addprefix $(MODULE_PATH), $(SOLVER_MAIN)) -o $(addprefix $(BIN_PATH), $(SOLVER_OUTFILE))
68
69generator: $(GEN_OBJS)
70	@echo COMPILES THE DATA/TEST GENERATORS
71	@$(foreach file, $(notdir $(wildcard $(GEN_PATH)*.f90)), $(if $(file),$(COMPILER_F90_2) $(INCLUDE_FLAGS) $(LIBRARY_FLAGS) $(addprefix $(TMP_PATH), $(addsuffix .o, $(basename $(file)))) -o $(addprefix $(BIN_PATH), $(basename $(file)));))
72	@$(foreach file, $(notdir $(wildcard $(GEN_PATH)*.cc)), $(if $(file), $(COMPILER_CXX) $(addprefix $(TMP_PATH), $(addsuffix .o, $(basename $(file)))) -o $(addprefix $(BIN_PATH), $(basename $(file)));))
73	@$(foreach file, $(notdir $(wildcard $(GEN_PATH)*.c)), $(if $(file), $(COMPILER_C) $(addprefix $(TMP_PATH), $(addsuffix .o, $(basename $(file)))) -o $(addprefix $(BIN_PATH), $(basename $(file)));))
74
75postprocessor: $(POST_OBJS)
76	@echo COMPILES THE POSTPROCESSOR
77	@$(foreach file, $(notdir $(POST_MAIN)), $(COMPILER_CXX) $(addprefix $(TMP_PATH), $(notdir $(POST_OBJS))) -o $(addprefix $(BIN_PATH), $(basename $(file)));)
78
79%.o: %.f90
80	@$(COMPILER_F90) -c $(INCLUDE_FLAGS) $(LIBRARY_FLAGS) $? -J$(TMP_PATH) -o $(addprefix $(TMP_PATH), $(notdir $@))
81
82%.o: %.cc
83	@$(COMPILER_CXX) -c $(INCLUDE_FLAGS) $(LIBRARY_FLAGS) $? -J$(TMP_PATH) -o $(addprefix $(TMP_PATH), $(notdir $@))
84
85%.o: %.c
86	@$(COMPILER_C) -c $(INCLUDE_FLAGS) $(LIBRARY_FLAGS) $? -J$(TMP_PATH) -o $(addprefix $(TMP_PATH), $(notdir $@))
87
88# Cleans extra stuff
89clean:
90	@rm $(TMP_PATH)* $(BIN_PATH)*
91
92# Saves the work in a backup location
93save:	pack
94	@echo Saving files
95	@- @cp -r $(DOC_PATH) $(SRC_PATH) $(ROOT_FILES) $(BACKUP_PATH)
96
97# Too large to copy just as files; so they're compressed first
98save_tests:
99	@echo Packing all tests
100	$(foreach folder, $(wildcard $(TEST_PATH)*), tar $(TAR_FLAGS) $(addprefix $(BACKUP_PATH), $(TEST_PACKET)_$(notdir $(folder)).tar) $(folder);)
101
102# Packs the work for transportation
103pack:
104	@echo Packing development files
105	@tar $(TAR_FLAGS) $(addprefix $(BACKUP_PATH), $(PACKET)) $(SRC_PATH) $(DOC_PATH) $(ROOT_FILES)
106