Ticket #52: makefile

File makefile, 16.3 KB (added by m.hadfield, 17 years ago)
Line 
1# $Id: makefile 76 2007-06-11 22:52:45Z 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# Set name of module files for netCDF F90 interface. On some platforms
253# these will need to be overridden in the machine-dependent include file.
254#--------------------------------------------------------------------------
255
256 NETCDF_MODFILE := netcdf.mod
257 TYPESIZES_MODFILE := typesizes.mod
258
259#--------------------------------------------------------------------------
260# "uname -s" should return the OS or kernel name and "uname -m" should
261# return the CPU or hardware name. In practice the results can be pretty
262# flaky. Run the results through sed to convert "/" and " " to "-",
263# then apply platform-specific conversions.
264#--------------------------------------------------------------------------
265
266OS := $(shell uname -s | sed 's/[\/ ]/-/g')
267OS := $(patsubst CYGWIN_%,CYGWIN,$(OS))
268OS := $(patsubst MINGW%,MINGW,$(OS))
269OS := $(patsubst sn%,UNICOS-sn,$(OS))
270
271CPU := $(shell uname -m | sed 's/[\/ ]/-/g')
272
273SVNREV := $(shell svnversion -n .)
274
275ROOTDIR := $(shell pwd)
276
277COMPILERS := ./Compilers
278
279ifndef FORT
280 $(error Variable FORT not set)
281endif
282
283ifneq "$(MAKECMDGOALS)" "clean"
284 include $(COMPILERS)/$(OS)-$(strip $(FORT)).mk
285endif
286
287#--------------------------------------------------------------------------
288# Pass the platform variables to the preprocessor as macros. Convert to
289# valid, upper-case identifiers. If applicable, attach ROMS application
290# CPP option.
291#--------------------------------------------------------------------------
292
293CPPFLAGS += -D$(shell echo ${OS} | tr "-" "_" | tr [a-z] [A-Z])
294CPPFLAGS += -D$(shell echo ${CPU} | tr "-" "_" | tr [a-z] [A-Z])
295CPPFLAGS += -D$(shell echo ${FORT} | tr "-" "_" | tr [a-z] [A-Z])
296
297CPPFLAGS += -D'ROOT_DIR="$(ROOTDIR)"'
298ifdef ROMS_APPLICATION
299 HEADER := $(addsuffix .h,$(shell echo ${ROMS_APPLICATION} | tr [A-Z] [a-z]))
300 CPPFLAGS += -D$(ROMS_APPLICATION)
301 CPPFLAGS += -D'HEADER="$(HEADER)"'
302 ifdef MY_HEADER_DIR
303 CPPFLAGS += -D'ROMS_HEADER="$(MY_HEADER_DIR)/$(HEADER)"'
304 else
305 CPPFLAGS += -D'ROMS_HEADER="$(HEADER)"'
306 endif
307 MDEPFLAGS += -DROMS_HEADER="$(HEADER)"
308 CPPFLAGS += -DNestedGrids=$(NestedGrids)
309endif
310
311ifndef MY_ANALYTICAL_DIR
312 MY_ANALYTICAL_DIR := $(ROOTDIR)/ROMS/Functionals
313endif
314ifeq (,$(findstring ROMS/Functionals,$(MY_ANALYTICAL_DIR)))
315 MY_ANALYTICAL := on
316endif
317CPPFLAGS += -D'ANALYTICAL_DIR="$(MY_ANALYTICAL_DIR)"'
318
319ifdef MY_ANALYTICAL
320 CPPFLAGS += -D'MY_ANALYTICAL="$(MY_ANALYTICAL)"'
321endif
322
323ifdef SVNREV
324 CPPFLAGS += -D'SVN_REV="$(SVNREV)"'
325else
326 SVNREV := $(shell grep Revision ./ROMS/Version | sed 's/.* \([0-9]*\) .*/\1/')
327 CPPFLAGS += -D'SVN_REV="$(SVNREV)"'
328endif
329
330ifdef MY_CPPP_FLAGS
331 CPPFLAGS += $(MY_CPP_FLAGS)
332endif
333
334#--------------------------------------------------------------------------
335# Build target directories.
336#--------------------------------------------------------------------------
337
338.PHONY: all
339
340all: $(SCRATCH_DIR) $(SCRATCH_DIR)/MakeDepend $(BIN)
341
342modules := ROMS/Adjoint \
343 ROMS/Representer \
344 ROMS/Tangent \
345 ROMS/Nonlinear \
346 ROMS/Functionals \
347 ROMS/SeaIce \
348 ROMS/Utility \
349 ROMS/Modules
350
351includes := ROMS/Include \
352 ROMS/Adjoint \
353 ROMS/Nonlinear \
354 ROMS/Representer \
355 ROMS/Tangent \
356 ROMS/SeaIce \
357 ROMS/Utility \
358 ROMS/Drivers
359
360ifdef MY_ANALYTICAL
361 includes += $(MY_ANALYTICAL_DIR)
362endif
363 includes += ROMS/Functionals
364
365ifdef MY_HEADER_DIR
366 includes += $(MY_HEADER_DIR)
367endif
368
369ifdef SWAN_COUPLE
370 modules += Waves/SWAN/Src
371 includes += Waves/SWAN/Src
372endif
373
374modules += Master
375includes += Master
376
377vpath %.F $(modules)
378vpath %.h $(includes)
379vpath %.f90 $(SCRATCH_DIR)
380vpath %.o $(SCRATCH_DIR)
381
382include $(addsuffix /Module.mk,$(modules))
383
384MDEPFLAGS += $(patsubst %,-I %,$(includes)) --silent --moddir $(SCRATCH_DIR)
385
386CPPFLAGS += $(patsubst %,-I%,$(includes))
387
388ifdef MY_HEADER_DIR
389 CPPFLAGS += -D'HEADER_DIR="$(MY_HEADER_DIR)"'
390else
391 CPPFLAGS += -D'HEADER_DIR="./ROMS/Include"'
392endif
393
394$(SCRATCH_DIR):
395 $(shell $(TEST) -d $(SCRATCH_DIR) || $(MKDIR) $(SCRATCH_DIR) )
396
397#--------------------------------------------------------------------------
398# Add profiling.
399#--------------------------------------------------------------------------
400
401# FFLAGS += -check bounds # ifort
402# FFLAGS += -C # pgi
403# FFLAGS += -xpg # Sun
404# FFLAGS += -pg # g95
405# FFLAGS += -qp # ifort
406# FFLAGS += -Mprof=func,lines # pgi
407# FFLAGS += -Mprof=mpi,lines # pgi
408# FFLAGS += -Mprof=mpi,hwcts # pgi
409# FFLAGS += -Mprof=func # pgi
410
411#--------------------------------------------------------------------------
412# Special CPP macros for mod_strings.F
413#--------------------------------------------------------------------------
414
415$(SCRATCH_DIR)/mod_strings.f90: CPPFLAGS += -DMY_OS='"$(OS)"' \
416 -DMY_CPU='"$(CPU)"' -DMY_FORT='"$(FORT)"' \
417 -DMY_FC='"$(FC)"' -DMY_FFLAGS='"$(FFLAGS)"'
418
419#--------------------------------------------------------------------------
420# ROMS/TOMS libraries.
421#--------------------------------------------------------------------------
422
423MYLIB := libocean.a
424
425.PHONY: libraries
426
427libraries: $(libraries)
428
429#--------------------------------------------------------------------------
430# Target to create ROMS/TOMS dependecies.
431#--------------------------------------------------------------------------
432
433$(SCRATCH_DIR)/$(NETCDF_MODFILE): | $(SCRATCH_DIR)
434 cp -f $(NETCDF_INCDIR)/$(NETCDF_MODFILE) $(SCRATCH_DIR)
435
436$(SCRATCH_DIR)/$(TYPESIZES_MODFILE): | $(SCRATCH_DIR)
437 cp -f $(NETCDF_INCDIR)/$(TYPESIZES_MODFILE) $(SCRATCH_DIR)
438
439$(SCRATCH_DIR)/MakeDepend: makefile \
440 $(SCRATCH_DIR)/$(NETCDF_MODFILE) \
441 $(SCRATCH_DIR)/$(TYPESIZES_MODFILE) \
442 | $(SCRATCH_DIR)
443 $(SFMAKEDEPEND) $(MDEPFLAGS) $(sources) > $(SCRATCH_DIR)/MakeDepend
444
445.PHONY: depend
446
447SFMAKEDEPEND := ./ROMS/Bin/sfmakedepend
448
449depend: $(SCRATCH_DIR)
450 $(SFMAKEDEPEND) $(MDEPFLAGS) $(sources) > $(SCRATCH_DIR)/MakeDepend
451
452ifneq "$(MAKECMDGOALS)" "clean"
453 -include $(SCRATCH_DIR)/MakeDepend
454endif
455
456#--------------------------------------------------------------------------
457# Target to create ROMS/TOMS tar file.
458#--------------------------------------------------------------------------
459
460.PHONY: tarfile
461
462tarfile:
463 tar --exclude=".svn" -cvf roms-3_0.tar *
464
465.PHONY: zipfile
466
467zipfile:
468 zip -r roms-3_0.zip *
469
470.PHONY: gzipfile
471
472gzipfile:
473 gzip -v roms-3_0.gzip *
474
475#--------------------------------------------------------------------------
476# Cleaning targets.
477#--------------------------------------------------------------------------
478
479.PHONY: clean
480
481clean:
482 $(RM) -r $(clean_list)
483
484#--------------------------------------------------------------------------
485# A handy debugging target. This will allow to print the value of any
486# makefile defined macro (see http://tinyurl.com/8ax3j). For example,
487# to find the value of CPPFLAGS execute:
488#
489# gmake print-CPPFLAGS
490# or
491# make print-CPPFLAGS
492#--------------------------------------------------------------------------
493
494.PHONY: print-%
495
496print-%:
497 @echo $* = $($*)