Ticket #50: makefile

File makefile, 16.0 KB (added by m.hadfield, 17 years ago)
Line 
1# $Id: makefile 75 2007-06-07 01:01:18Z arango $
2#::::::::::::::::::::::::::::::::::::::::::::::::::::: Hernan G. Arango :::
3# Copyright (c) 2002-2007 The ROMS/TOMS Group Kate Hedstrom :::
4# Licensed under a MIT/X style license :::
5# See License_ROMS.txt :::
6#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
7# :::
8# ROMS/TOMS Framework Master Makefile :::
9# :::
10# This makefile is designed to work only with GNU Make version 3.80 or :::
11# higher. It can be used in any architecture provided that there is a :::
12# machine/compiler rules file in the "Compilers" subdirectory. You :::
13# may need to modify the rules file to specify the correct path for :::
14# the NetCDF and ARPACK libraries. The ARPACK library is only used in :::
15# the Generalized Stability Theory analysis and Laczos algorithm. :::
16# :::
17# If appropriate, the USER needs to modify the macro definitions in :::
18# in user-defined section below. To activate an option set the macro :::
19# to "on". For example, if you want to compile with debugging options :::
20# set: :::
21# :::
22# USE_DEBUG := on :::
23# :::
24# Otherwise, leave macro definition blank. :::
25# :::
26# The USER needs to provide a value for the macro FORT. Choose the :::
27# appropriate value from the list below. :::
28# :::
29#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
30
31NEED_VERSION := 3.80 3.81
32$(if $(filter $(MAKE_VERSION),$(NEED_VERSION)),, \
33 $(error This makefile requires one of GNU make version $(NEED_VERSION).))
34
35#--------------------------------------------------------------------------
36# Initialize some things.
37#--------------------------------------------------------------------------
38
39 sources :=
40 libraries :=
41
42#==========================================================================
43# Start of user-defined options. In some macro definitions below: "on" or
44# any other string means TRUE while blank (or spaces) is FALSE.
45#==========================================================================
46#
47# The CPP option defining a particular application is specified below.
48# See header file "ROMS/Include/cppdefs.h" for all available idealized
49# and realistic applications CPP flags. For example, to activate the
50# upwelling test case (UPWELLING) set:
51#
52# ROMS_APPLICATION ?= UPWELLING
53#
54# Notice that this makefile will include the associated application header
55# file, which is located either in the "ROMS/Include" or MY_HEADER_DIR
56# directory. This makefile is designed to search in both directories.
57# The only constrain is that the application CPP option must be unique
58# and header file name is the lowercase value of ROMS_APPLICATION with
59# the .h extension. For example, the upwelling application includes the
60# "upwelling.h" header file.
61
62ROMS_APPLICATION ?= UPWELLING
63
64# If application header files is not located in "ROMS/Include",
65# provide an alternate directory FULL PATH.
66
67MY_HEADER_DIR ?=
68
69# If your application requires analytical expressions and they are not
70# located in "ROMS/Functionals", provide an alternate directory.
71# Notice that a set analytical expressions templates can be found in
72# "User/Functionals".
73
74MY_ANALYTICAL_DIR ?=
75
76# Sometimes it is desirable to activate one or more CPP options to
77# run different variants of the same application without modifying
78# its header file. If this is the case, specify such options here
79# using the -D syntax. For example, to write time-averaged fields
80# set:
81#
82# MY_CPP_FLAGS ?= -DAVERAGES
83#
84
85MY_CPP_FLAGS ?=
86
87# Set number of ROMS nested and/or composed grid. Currently, only
88# one grid is supported. This option will be available in the near
89# future.
90
91 NestedGrids ?= 1
92
93# Activate debugging compiler options:
94
95 USE_DEBUG ?=
96
97# If parallel applications, use at most one of these definitions
98# (leave both definitions blank in serial applications):
99
100 USE_MPI ?=
101 USE_OpenMP ?=
102
103# If distributed-memory, turn on compilation via the script "mpif90".
104# This is needed in some Linux operating systems. In some systems with
105# native MPI libraries the compilation does not require MPICH type
106# scripts. This macro is also convient when there are several fortran
107# compiliers (ifort, pgf90, pathf90) in the system that use mpif90.
108# In this, case the user need to select the desired compiler below and
109# turn on both USE_MPI and USE_MPIF90 macros.
110
111 USE_MPIF90 ?=
112
113# If applicable, compile with the ARPACK library (GST analysis):
114
115 USE_ARPACK ?=
116
117# If applicable, activate 64-bit compilation:
118
119 USE_LARGE ?= on
120
121# If applicable, activate Coupling to SWAN wave model.
122
123 SWAN_COUPLE ?=
124
125#--------------------------------------------------------------------------
126# We are going to include a file with all the settings that depend on
127# the system and the compiler. We are going to build up the name of the
128# include file using information on both. Set your compiler here from
129# the following list:
130#
131# Operating System Compiler(s)
132#
133# AIX: xlf
134# ALPHA: f90
135# CYGWIN: g95, df, ifort
136# Darwin: f90, xlf
137# IRIX: f90
138# Linux: ftn, ifc, ifort, pgi, path, g95, gfortran
139# SunOS: f95
140# UNICOS-mp: ftn
141# SunOS/Linux: ftn (Cray cross-compiler)
142#
143# Feel free to send us additional rule files to include! Also, be sure
144# to check the appropriate file to make sure it has the right paths to
145# NetCDF and so on.
146#--------------------------------------------------------------------------
147
148 FORT ?= pgi
149
150#--------------------------------------------------------------------------
151# Set directory for executable.
152#--------------------------------------------------------------------------
153
154 BINDIR ?= .
155
156#==========================================================================
157# End of user-defined options. See also the machine-dependent include
158# file being used above.
159#==========================================================================
160
161#--------------------------------------------------------------------------
162# Set directory for temporary objects.
163#--------------------------------------------------------------------------
164
165SCRATCH_DIR ?= Build
166 clean_list := core $(SCRATCH_DIR)
167
168ifeq "$(strip $(SCRATCH_DIR))" "."
169 clean_list := core *.o *.oo *.mod *.f90 lib*.a *.bak
170endif
171ifeq "$(strip $(SCRATCH_DIR))" "./"
172 clean_list := core *.o *.oo *.mod *.f90 lib*.a *.bak
173endif
174
175#--------------------------------------------------------------------------
176# Set Pattern rules.
177#--------------------------------------------------------------------------
178
179%.o: %.F
180
181%.o: %.f90
182 cd $(SCRATCH_DIR); $(FC) -c $(FFLAGS) $(notdir $<)
183
184%.f90: %.F
185 $(CPP) $(CPPFLAGS) $(MY_CPP_FLAGS) $< > $*.f90
186 $(CLEAN) $*.f90
187
188CLEAN := ROMS/Bin/cpp_clean
189
190#--------------------------------------------------------------------------
191# Make functions for putting the temporary files in $(SCRATCH_DIR)
192# DO NOT modify this section; spaces and blank lineas are needed.
193#--------------------------------------------------------------------------
194
195# $(call source-dir-to-binary-dir, directory-list)
196source-dir-to-binary-dir = $(addprefix $(SCRATCH_DIR)/, $(notdir $1))
197
198# $(call source-to-object, source-file-list)
199source-to-object = $(call source-dir-to-bindary-dir, \
200 $(subst .F,.o,$1))
201
202# $(call make-library, library-name, source-file-list)
203define make-library
204 libraries += $(SCRATCH_DIR)/$1
205 sources += $2
206
207 $(SCRATCH_DIR)/$1: $(call source-dir-to-binary-dir, \
208 $(subst .F,.o,$2))
209 $(AR) $(ARFLAGS) $$@ $$^
210 $(RANLIB) $$@
211endef
212
213# $(call f90-source, source-file-list)
214f90-source = $(call source-dir-to-binary-dir, \
215 $(subst .F,.f90,$1))
216
217# $(compile-rules)
218define compile-rules
219 $(foreach f, $(local_src), \
220 $(call one-compile-rule,$(call source-to-object,$f), \
221 $(call f90-source,$f),$f))
222endef
223
224# $(call one-compile-rule, binary-file, f90-file, source-files)
225define one-compile-rule
226 $1: $2 $3
227 cd $$(SCRATCH_DIR); $$(FC) -c $$(FFLAGS) $(notdir $2)
228
229 $2: $3
230 $$(CPP) $$(CPPFLAGS) $$(MY_CPP_FLAGS) $$< > $$@
231 $$(CLEAN) $$@
232
233endef
234
235#--------------------------------------------------------------------------
236# Set ROMS/TOMS executable file name.
237#--------------------------------------------------------------------------
238
239BIN := $(BINDIR)/oceanS
240ifdef USE_DEBUG
241 BIN := $(BINDIR)/oceanG
242else
243 ifdef USE_MPI
244 BIN := $(BINDIR)/oceanM
245 endif
246 ifdef USE_OpenMP
247 BIN := $(BINDIR)/oceanO
248 endif
249endif
250
251#--------------------------------------------------------------------------
252# "uname -s" should return the OS or kernel name and "uname -m" should
253# return the CPU or hardware name. In practice the results can be pretty
254# flaky. Run the results through sed to convert "/" and " " to "-",
255# then apply platform-specific conversions.
256#--------------------------------------------------------------------------
257
258OS := $(shell uname -s | sed 's/[\/ ]/-/g')
259OS := $(patsubst CYGWIN_%,CYGWIN,$(OS))
260OS := $(patsubst MINGW%,MINGW,$(OS))
261OS := $(patsubst sn%,UNICOS-sn,$(OS))
262
263CPU := $(shell uname -m | sed 's/[\/ ]/-/g')
264
265SVNREV := $(shell svnversion -n .)
266
267ROOTDIR := $(shell pwd)
268
269COMPILERS := ./Compilers
270
271ifndef FORT
272 $(error Variable FORT not set)
273endif
274
275ifneq "$(MAKECMDGOALS)" "clean"
276 include $(COMPILERS)/$(OS)-$(strip $(FORT)).mk
277endif
278
279#--------------------------------------------------------------------------
280# Pass the platform variables to the preprocessor as macros. Convert to
281# valid, upper-case identifiers. If applicable, attach ROMS application
282# CPP option.
283#--------------------------------------------------------------------------
284
285CPPFLAGS += -D$(shell echo ${OS} | tr "-" "_" | tr [a-z] [A-Z])
286CPPFLAGS += -D$(shell echo ${CPU} | tr "-" "_" | tr [a-z] [A-Z])
287CPPFLAGS += -D$(shell echo ${FORT} | tr "-" "_" | tr [a-z] [A-Z])
288
289CPPFLAGS += -D'ROOT_DIR="$(ROOTDIR)"'
290ifdef ROMS_APPLICATION
291 HEADER := $(addsuffix .h,$(shell echo ${ROMS_APPLICATION} | tr [A-Z] [a-z]))
292 CPPFLAGS += -D$(ROMS_APPLICATION)
293 CPPFLAGS += -D'HEADER="$(HEADER)"'
294 ifdef MY_HEADER_DIR
295 CPPFLAGS += -D'ROMS_HEADER="$(MY_HEADER_DIR)/$(HEADER)"'
296 else
297 CPPFLAGS += -D'ROMS_HEADER="$(HEADER)"'
298 endif
299 MDEPFLAGS += -DROMS_HEADER="$(HEADER)"
300 CPPFLAGS += -DNestedGrids=$(NestedGrids)
301endif
302
303ifndef MY_ANALYTICAL_DIR
304 MY_ANALYTICAL_DIR := $(ROOTDIR)/ROMS/Functionals
305endif
306ifeq (,$(findstring ROMS/Functionals,$(MY_ANALYTICAL_DIR)))
307 MY_ANALYTICAL := on
308endif
309CPPFLAGS += -D'ANALYTICAL_DIR="$(MY_ANALYTICAL_DIR)"'
310
311ifdef MY_ANALYTICAL
312 CPPFLAGS += -D'MY_ANALYTICAL="$(MY_ANALYTICAL)"'
313endif
314
315ifdef SVNREV
316 CPPFLAGS += -D'SVN_REV="$(SVNREV)"'
317else
318 SVNREV := $(shell grep Revision ./ROMS/Version | sed 's/.* \([0-9]*\) .*/\1/')
319 CPPFLAGS += -D'SVN_REV="$(SVNREV)"'
320endif
321
322ifdef MY_CPPP_FLAGS
323 CPPFLAGS += $(MY_CPP_FLAGS)
324endif
325
326#--------------------------------------------------------------------------
327# Build target directories.
328#--------------------------------------------------------------------------
329
330.PHONY: all
331
332all: $(SCRATCH_DIR) $(SCRATCH_DIR)/MakeDepend $(BIN)
333
334modules := ROMS/Adjoint \
335 ROMS/Representer \
336 ROMS/Tangent \
337 ROMS/Nonlinear \
338 ROMS/Functionals \
339 ROMS/SeaIce \
340 ROMS/Utility \
341 ROMS/Modules
342
343includes := ROMS/Include \
344 ROMS/Adjoint \
345 ROMS/Nonlinear \
346 ROMS/Representer \
347 ROMS/Tangent \
348 ROMS/SeaIce \
349 ROMS/Utility \
350 ROMS/Drivers
351
352ifdef MY_ANALYTICAL
353 includes += $(MY_ANALYTICAL_DIR)
354endif
355 includes += ROMS/Functionals
356
357ifdef MY_HEADER_DIR
358 includes += $(MY_HEADER_DIR)
359endif
360
361ifdef SWAN_COUPLE
362 modules += Waves/SWAN/Src
363 includes += Waves/SWAN/Src
364endif
365
366modules += Master
367includes += Master
368
369vpath %.F $(modules)
370vpath %.h $(includes)
371vpath %.f90 $(SCRATCH_DIR)
372vpath %.o $(SCRATCH_DIR)
373
374include $(addsuffix /Module.mk,$(modules))
375
376MDEPFLAGS += $(patsubst %,-I %,$(includes)) --silent --moddir $(SCRATCH_DIR)
377
378CPPFLAGS += $(patsubst %,-I%,$(includes))
379
380ifdef MY_HEADER_DIR
381 CPPFLAGS += -D'HEADER_DIR="$(MY_HEADER_DIR)"'
382else
383 CPPFLAGS += -D'HEADER_DIR="./ROMS/Include"'
384endif
385
386$(SCRATCH_DIR):
387 $(shell $(TEST) -d $(SCRATCH_DIR) || $(MKDIR) $(SCRATCH_DIR) )
388
389#--------------------------------------------------------------------------
390# Add profiling.
391#--------------------------------------------------------------------------
392
393# FFLAGS += -check bounds # ifort
394# FFLAGS += -C # pgi
395# FFLAGS += -xpg # Sun
396# FFLAGS += -pg # g95
397# FFLAGS += -qp # ifort
398# FFLAGS += -Mprof=func,lines # pgi
399# FFLAGS += -Mprof=mpi,lines # pgi
400# FFLAGS += -Mprof=mpi,hwcts # pgi
401# FFLAGS += -Mprof=func # pgi
402
403#--------------------------------------------------------------------------
404# Special CPP macros for mod_strings.F
405#--------------------------------------------------------------------------
406
407$(SCRATCH_DIR)/mod_strings.f90: CPPFLAGS += -DMY_OS='"$(OS)"' \
408 -DMY_CPU='"$(CPU)"' -DMY_FORT='"$(FORT)"' \
409 -DMY_FC='"$(FC)"' -DMY_FFLAGS='"$(FFLAGS)"'
410
411#--------------------------------------------------------------------------
412# ROMS/TOMS libraries.
413#--------------------------------------------------------------------------
414
415MYLIB := libocean.a
416
417.PHONY: libraries
418
419libraries: $(libraries)
420
421#--------------------------------------------------------------------------
422# Target to create ROMS/TOMS dependecies.
423#--------------------------------------------------------------------------
424
425$(SCRATCH_DIR)/MakeDepend: makefile
426 $(shell $(TEST) -d $(SCRATCH_DIR) || $(MKDIR) $(SCRATCH_DIR) )
427 $(shell $(TEST) -e $(NETCDF_INCDIR)/netcdf.mod && cp -f $(NETCDF_INCDIR)/netcdf.mod $(SCRATCH_DIR) )
428 $(shell $(TEST) -e $(NETCDF_INCDIR)/NETCDF.mod && cp -f $(NETCDF_INCDIR)/NETCDF.mod $(SCRATCH_DIR) )
429 $(shell $(TEST) -e $(NETCDF_INCDIR)/typesizes.mod && cp -f $(NETCDF_INCDIR)/typesizes.mod $(SCRATCH_DIR) )
430 $(shell $(TEST) -e $(NETCDF_INCDIR)/TYPESIZES.mod && cp -f $(NETCDF_INCDIR)/TYPESIZES.mod $(SCRATCH_DIR) )
431 $(SFMAKEDEPEND) $(MDEPFLAGS) $(sources) > $(SCRATCH_DIR)/MakeDepend
432
433.PHONY: depend
434
435SFMAKEDEPEND := ./ROMS/Bin/sfmakedepend
436
437depend: $(SCRATCH_DIR)
438 $(SFMAKEDEPEND) $(MDEPFLAGS) $(sources) > $(SCRATCH_DIR)/MakeDepend
439
440ifneq "$(MAKECMDGOALS)" "clean"
441 -include $(SCRATCH_DIR)/MakeDepend
442endif
443
444#--------------------------------------------------------------------------
445# Target to create ROMS/TOMS tar file.
446#--------------------------------------------------------------------------
447
448.PHONY: tarfile
449
450tarfile:
451 tar --exclude=".svn" -cvf roms-3_0.tar *
452
453.PHONY: zipfile
454
455zipfile:
456 zip -r roms-3_0.zip *
457
458.PHONY: gzipfile
459
460gzipfile:
461 gzip -v roms-3_0.gzip *
462
463#--------------------------------------------------------------------------
464# Cleaning targets.
465#--------------------------------------------------------------------------
466
467.PHONY: clean
468
469clean:
470 $(RM) -r $(clean_list)
471
472#--------------------------------------------------------------------------
473# A handy debugging target. This will allow to print the value of any
474# makefile defined macro (see http://tinyurl.com/8ax3j). For example,
475# to find the value of CPPFLAGS execute:
476#
477# gmake print-CPPFLAGS
478# or
479# make print-CPPFLAGS
480#--------------------------------------------------------------------------
481
482.PHONY: print-%
483
484print-%:
485 @echo $* = $($*)