Ticket #60: makefile

File makefile, 17.1 KB (added by m.hadfield, 17 years ago)

ROMS make file

Line 
1# $Id: makefile 80 2007-07-06 02:30:52Z 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, activate 64-bit compilation:
114
115 USE_LARGE ?= on
116
117# If applicable, link with netCDF 4 libraries
118
119 USE_NETCDF4 ?=
120
121#--------------------------------------------------------------------------
122# We are going to include a file with all the settings that depend on
123# the system and the compiler. We are going to build up the name of the
124# include file using information on both. Set your compiler here from
125# the following list:
126#
127# Operating System Compiler(s)
128#
129# AIX: xlf
130# ALPHA: f90
131# CYGWIN: g95, df, ifort
132# Darwin: f90, xlf
133# IRIX: f90
134# Linux: ftn, ifc, ifort, pgi, path, g95, gfortran
135# SunOS: f95
136# UNICOS-mp: ftn
137# SunOS/Linux: ftn (Cray cross-compiler)
138#
139# Feel free to send us additional rule files to include! Also, be sure
140# to check the appropriate file to make sure it has the right paths to
141# NetCDF and so on.
142#--------------------------------------------------------------------------
143
144 FORT ?= pgi
145
146#--------------------------------------------------------------------------
147# Set directory for executable.
148#--------------------------------------------------------------------------
149
150 BINDIR ?= .
151
152#==========================================================================
153# End of user-defined options. See also the machine-dependent include
154# file being used above.
155#==========================================================================
156
157#--------------------------------------------------------------------------
158# Set directory for temporary objects.
159#--------------------------------------------------------------------------
160
161SCRATCH_DIR ?= Build
162 clean_list := core *.ipo $(SCRATCH_DIR)
163
164ifeq "$(strip $(SCRATCH_DIR))" "."
165 clean_list := core *.o *.oo *.mod *.f90 lib*.a *.bak
166 clean_list += $(CURDIR)/*.ipo
167endif
168ifeq "$(strip $(SCRATCH_DIR))" "./"
169 clean_list := core *.o *.oo *.ipo *.mod *.f90 lib*.a *.bak
170 clean_list += $(CURDIR)/*.ipo
171endif
172
173#--------------------------------------------------------------------------
174# Set Pattern rules.
175#--------------------------------------------------------------------------
176
177%.o: %.F
178
179%.o: %.f90
180 cd $(SCRATCH_DIR); $(FC) -c $(FFLAGS) $(notdir $<)
181
182%.f90: %.F
183 $(CPP) $(CPPFLAGS) $(MY_CPP_FLAGS) $< > $*.f90
184 $(CLEAN) $*.f90
185
186CLEAN := ROMS/Bin/cpp_clean
187
188#--------------------------------------------------------------------------
189# Make functions for putting the temporary files in $(SCRATCH_DIR)
190# DO NOT modify this section; spaces and blank lineas are needed.
191#--------------------------------------------------------------------------
192
193# $(call source-dir-to-binary-dir, directory-list)
194source-dir-to-binary-dir = $(addprefix $(SCRATCH_DIR)/, $(notdir $1))
195
196# $(call source-to-object, source-file-list)
197source-to-object = $(call source-dir-to-bindary-dir, \
198 $(subst .F,.o,$1))
199
200# $(call make-library, library-name, source-file-list)
201define make-library
202 libraries += $(SCRATCH_DIR)/$1
203 sources += $2
204
205 $(SCRATCH_DIR)/$1: $(call source-dir-to-binary-dir, \
206 $(subst .F,.o,$2))
207 $(AR) $(ARFLAGS) $$@ $$^
208 $(RANLIB) $$@
209endef
210
211# $(call f90-source, source-file-list)
212f90-source = $(call source-dir-to-binary-dir, \
213 $(subst .F,.f90,$1))
214
215# $(compile-rules)
216define compile-rules
217 $(foreach f, $(local_src), \
218 $(call one-compile-rule,$(call source-to-object,$f), \
219 $(call f90-source,$f),$f))
220endef
221
222# $(call one-compile-rule, binary-file, f90-file, source-files)
223define one-compile-rule
224 $1: $2 $3
225 cd $$(SCRATCH_DIR); $$(FC) -c $$(FFLAGS) $(notdir $2)
226
227 $2: $3
228 $$(CPP) $$(CPPFLAGS) $$(MY_CPP_FLAGS) $$< > $$@
229 $$(CLEAN) $$@
230
231endef
232
233#--------------------------------------------------------------------------
234# Set ROMS/TOMS executable file name.
235#--------------------------------------------------------------------------
236
237BIN := $(BINDIR)/oceanS
238ifdef USE_DEBUG
239 BIN := $(BINDIR)/oceanG
240else
241 ifdef USE_MPI
242 BIN := $(BINDIR)/oceanM
243 endif
244 ifdef USE_OpenMP
245 BIN := $(BINDIR)/oceanO
246 endif
247endif
248
249#--------------------------------------------------------------------------
250# Set name of module files for netCDF F90 interface. On some platforms
251# these will need to be overridden in the machine-dependent include file.
252#--------------------------------------------------------------------------
253
254 NETCDF_MODFILE := netcdf.mod
255TYPESIZES_MODFILE := typesizes.mod
256
257#--------------------------------------------------------------------------
258# "uname -s" should return the OS or kernel name and "uname -m" should
259# return the CPU or hardware name. In practice the results can be pretty
260# flaky. Run the results through sed to convert "/" and " " to "-",
261# then apply platform-specific conversions.
262#--------------------------------------------------------------------------
263
264OS := $(shell uname -s | sed 's/[\/ ]/-/g')
265OS := $(patsubst CYGWIN_%,CYGWIN,$(OS))
266OS := $(patsubst MINGW%,MINGW,$(OS))
267OS := $(patsubst sn%,UNICOS-sn,$(OS))
268
269CPU := $(shell uname -m | sed 's/[\/ ]/-/g')
270
271SVNREV ?= $(shell svnversion -n .)
272
273ROOTDIR := $(shell pwd)
274
275COMPILERS := ./Compilers
276
277ifndef FORT
278 $(error Variable FORT not set)
279endif
280
281ifneq "$(MAKECMDGOALS)" "clean"
282 include $(COMPILERS)/$(OS)-$(strip $(FORT)).mk
283endif
284
285#--------------------------------------------------------------------------
286# Pass the platform variables to the preprocessor as macros. Convert to
287# valid, upper-case identifiers. If applicable, attach ROMS application
288# CPP option.
289#--------------------------------------------------------------------------
290
291CPPFLAGS += -D$(shell echo ${OS} | tr "-" "_" | tr [a-z] [A-Z])
292CPPFLAGS += -D$(shell echo ${CPU} | tr "-" "_" | tr [a-z] [A-Z])
293CPPFLAGS += -D$(shell echo ${FORT} | tr "-" "_" | tr [a-z] [A-Z])
294
295CPPFLAGS += -D'ROOT_DIR="$(ROOTDIR)"'
296ifdef ROMS_APPLICATION
297 HEADER := $(addsuffix .h,$(shell echo ${ROMS_APPLICATION} | tr [A-Z] [a-z]))
298 CPPFLAGS += -D$(ROMS_APPLICATION)
299 CPPFLAGS += -D'HEADER="$(HEADER)"'
300 ifdef MY_HEADER_DIR
301 CPPFLAGS += -D'ROMS_HEADER="$(MY_HEADER_DIR)/$(HEADER)"'
302 else
303 CPPFLAGS += -D'ROMS_HEADER="$(HEADER)"'
304 endif
305 MDEPFLAGS += -DROMS_HEADER="$(HEADER)"
306 CPPFLAGS += -DNestedGrids=$(NestedGrids)
307endif
308
309ifndef MY_ANALYTICAL_DIR
310 MY_ANALYTICAL_DIR := $(ROOTDIR)/ROMS/Functionals
311endif
312ifeq (,$(findstring ROMS/Functionals,$(MY_ANALYTICAL_DIR)))
313 MY_ANALYTICAL := on
314endif
315CPPFLAGS += -D'ANALYTICAL_DIR="$(MY_ANALYTICAL_DIR)"'
316
317ifdef MY_ANALYTICAL
318 CPPFLAGS += -D'MY_ANALYTICAL="$(MY_ANALYTICAL)"'
319endif
320
321ifdef SVNREV
322 CPPFLAGS += -D'SVN_REV="$(SVNREV)"'
323else
324 SVNREV := $(shell grep Revision ./ROMS/Version | sed 's/.* \([0-9]*\) .*/\1/')
325 CPPFLAGS += -D'SVN_REV="$(SVNREV)"'
326endif
327
328ifdef MY_CPP_FLAGS
329 CPPFLAGS += $(MY_CPP_FLAGS)
330endif
331
332#--------------------------------------------------------------------------
333# Internal macro definitions used to select the code to compile and
334# additional libraries to link. It uses the CPP activated in the
335# header file ROMS/Include/cppdefs.h to determine macro definitions.
336#--------------------------------------------------------------------------
337
338MAKE_MACROS := Compilers/make_macros.mk
339
340MACRO_FLAGS := $(CPPFLAGS) $(MY_CPP_FLAGS)
341
342MACROS := $(shell $(CPP) $(MACRO_FLAGS) Compilers/make_macros.h > \
343 $(MAKE_MACROS); $(CLEAN) $(MAKE_MACROS))
344
345include $(MAKE_MACROS)
346
347#--------------------------------------------------------------------------
348# Build target directories.
349#--------------------------------------------------------------------------
350
351.PHONY: all
352
353all: $(SCRATCH_DIR) $(SCRATCH_DIR)/MakeDepend $(BIN) rm_macros
354
355 modules :=
356ifdef USE_ADJOINT
357 modules += ROMS/Adjoint
358endif
359ifdef USE_REPRESENTER
360 modules += ROMS/Representer
361endif
362ifdef USE_TANGENT
363 modules += ROMS/Tangent
364endif
365 modules += ROMS/Nonlinear \
366 ROMS/Functionals \
367 ROMS/SeaIce \
368 ROMS/Utility \
369 ROMS/Modules
370
371 includes := ROMS/Include
372ifdef USE_ADJOINT
373 includes += ROMS/Adjoint
374endif
375ifdef USE_REPRESENTER
376 includes += ROMS/Representer
377endif
378ifdef USE_TANGENT
379 includes += ROMS/Tangent
380endif
381 includes += ROMS/Nonlinear \
382 ROMS/SeaIce \
383 ROMS/Utility \
384 ROMS/Drivers
385
386ifdef MY_ANALYTICAL
387 includes += $(MY_ANALYTICAL_DIR)
388endif
389 includes += ROMS/Functionals
390
391ifdef MY_HEADER_DIR
392 includes += $(MY_HEADER_DIR)
393endif
394
395ifdef USE_SWAN
396 modules += Waves/SWAN/Src
397 includes += Waves/SWAN/Src
398endif
399
400 modules += Master
401 includes += Master
402
403vpath %.F $(modules)
404vpath %.h $(includes)
405vpath %.f90 $(SCRATCH_DIR)
406vpath %.o $(SCRATCH_DIR)
407
408include $(addsuffix /Module.mk,$(modules))
409
410MDEPFLAGS += $(patsubst %,-I %,$(includes)) --silent --moddir $(SCRATCH_DIR)
411
412CPPFLAGS += $(patsubst %,-I%,$(includes))
413
414ifdef MY_HEADER_DIR
415 CPPFLAGS += -D'HEADER_DIR="$(MY_HEADER_DIR)"'
416else
417 CPPFLAGS += -D'HEADER_DIR="./ROMS/Include"'
418endif
419
420$(SCRATCH_DIR):
421 $(shell $(TEST) -d $(SCRATCH_DIR) || $(MKDIR) $(SCRATCH_DIR) )
422
423#--------------------------------------------------------------------------
424# Add profiling.
425#--------------------------------------------------------------------------
426
427# FFLAGS += -check bounds # ifort
428# FFLAGS += -C # pgi
429# FFLAGS += -xpg # Sun
430# FFLAGS += -pg # g95
431# FFLAGS += -qp # ifort
432# FFLAGS += -Mprof=func,lines # pgi
433# FFLAGS += -Mprof=mpi,lines # pgi
434# FFLAGS += -Mprof=mpi,hwcts # pgi
435# FFLAGS += -Mprof=func # pgi
436
437#--------------------------------------------------------------------------
438# Special CPP macros for mod_strings.F
439#--------------------------------------------------------------------------
440
441$(SCRATCH_DIR)/mod_strings.f90: CPPFLAGS += -DMY_OS='"$(OS)"' \
442 -DMY_CPU='"$(CPU)"' -DMY_FORT='"$(FORT)"' \
443 -DMY_FC='"$(FC)"' -DMY_FFLAGS='"$(FFLAGS)"'
444
445#--------------------------------------------------------------------------
446# ROMS/TOMS libraries.
447#--------------------------------------------------------------------------
448
449MYLIB := libocean.a
450
451.PHONY: libraries
452
453libraries: $(libraries)
454
455#--------------------------------------------------------------------------
456# Target to create ROMS/TOMS dependecies.
457#--------------------------------------------------------------------------
458
459$(SCRATCH_DIR)/$(NETCDF_MODFILE): | $(SCRATCH_DIR)
460 cp -f $(NETCDF_INCDIR)/$(NETCDF_MODFILE) $(SCRATCH_DIR)
461
462$(SCRATCH_DIR)/$(TYPESIZES_MODFILE): | $(SCRATCH_DIR)
463 cp -f $(NETCDF_INCDIR)/$(TYPESIZES_MODFILE) $(SCRATCH_DIR)
464
465$(SCRATCH_DIR)/MakeDepend: makefile \
466 $(SCRATCH_DIR)/$(NETCDF_MODFILE) \
467 $(SCRATCH_DIR)/$(TYPESIZES_MODFILE) \
468 | $(SCRATCH_DIR)
469 $(SFMAKEDEPEND) $(MDEPFLAGS) $(sources) > $(SCRATCH_DIR)/MakeDepend
470 cp -p $(CURDIR)/$(MAKE_MACROS) $(SCRATCH_DIR)
471
472.PHONY: depend
473
474SFMAKEDEPEND := ./ROMS/Bin/sfmakedepend
475
476depend: $(SCRATCH_DIR)
477 $(SFMAKEDEPEND) $(MDEPFLAGS) $(sources) > $(SCRATCH_DIR)/MakeDepend
478
479ifneq "$(MAKECMDGOALS)" "clean"
480 -include $(SCRATCH_DIR)/MakeDepend
481endif
482
483#--------------------------------------------------------------------------
484# Target to create ROMS/TOMS tar file.
485#--------------------------------------------------------------------------
486
487.PHONY: tarfile
488
489tarfile:
490 tar --exclude=".svn" -cvf roms-3_0.tar *
491
492.PHONY: zipfile
493
494zipfile:
495 zip -r roms-3_0.zip *
496
497.PHONY: gzipfile
498
499gzipfile:
500 gzip -v roms-3_0.gzip *
501
502#--------------------------------------------------------------------------
503# Cleaning targets.
504#--------------------------------------------------------------------------
505
506.PHONY: clean
507
508clean:
509 $(RM) -r $(clean_list)
510
511.PHONY: rm_macros
512
513rm_macros:
514 $(RM) -r $(CURDIR)/$(MAKE_MACROS)
515
516#--------------------------------------------------------------------------
517# A handy debugging target. This will allow to print the value of any
518# makefile defined macro (see http://tinyurl.com/8ax3j). For example,
519# to find the value of CPPFLAGS execute:
520#
521# gmake print-CPPFLAGS
522# or
523# make print-CPPFLAGS
524#--------------------------------------------------------------------------
525
526.PHONY: print-%
527
528print-%:
529 @echo $* = $($*)