Ticket #32: makefile

File makefile, 15.2 KB (added by m.hadfield, 17 years ago)
Line 
1# $Id: makefile 62 2007-05-16 01:36:06Z 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# Set number of ROMS nested and/or composed grid. Currently, only
77# one grid is supported. This option will be available in the near
78# future.
79
80 NestedGrids ?= 1
81
82# Activate debugging compiler options:
83
84 USE_DEBUG ?=
85
86# If parallel applications, use at most one of these definitions
87# (leave both definitions blank in serial applications):
88
89 USE_MPI ?=
90 USE_OpenMP ?=
91
92# If distributed-memory, turn on compilation via the script "mpif90".
93# This is needed in some Linux operating systems. In some systems with
94# native MPI libraries the compilation does not require MPICH type
95# scripts. This macro is also convient when there are several fortran
96# compiliers (ifort, pgf90, pathf90) in the system that use mpif90.
97# In this, case the user need to select the desired compiler below and
98# turn on both USE_MPI and USE_MPIF90 macros.
99
100 USE_MPIF90 ?=
101
102# If applicable, compile with the ARPACK library (GST analysis):
103
104 USE_ARPACK ?=
105
106# If applicable, activate 64-bit compilation:
107
108 USE_LARGE ?= on
109
110# If applicable, activate Coupling to SWAN wave model.
111
112 SWAN_COUPLE ?=
113
114#--------------------------------------------------------------------------
115# We are going to include a file with all the settings that depend on
116# the system and the compiler. We are going to build up the name of the
117# include file using information on both. Set your compiler here from
118# the following list:
119#
120# Operating System Compiler(s)
121#
122# AIX: xlf
123# ALPHA: f90
124# CYGWIN: g95, df, ifort
125# Darwin: f90, xlf
126# IRIX: f90
127# Linux: ftn, ifc, ifort, pgi, path, g95, gfortran
128# SunOS: f95
129# UNICOS-mp: ftn
130# SunOS/Linux: ftn (Cray cross-compiler)
131#
132# Feel free to send us additional rule files to include! Also, be sure
133# to check the appropriate file to make sure it has the right paths to
134# NetCDF and so on.
135#--------------------------------------------------------------------------
136
137 FORT ?= pgi
138
139#--------------------------------------------------------------------------
140# Set directory for executable.
141#--------------------------------------------------------------------------
142
143 BINDIR ?= .
144
145#==========================================================================
146# End of user-defined options. See also the machine-dependent include
147# file being used above.
148#==========================================================================
149
150#--------------------------------------------------------------------------
151# Set directory for temporary objects.
152#--------------------------------------------------------------------------
153
154SCRATCH_DIR ?= Build
155 clean_list := core $(SCRATCH_DIR)
156
157ifeq "$(strip $(SCRATCH_DIR))" "."
158 clean_list := core *.o *.oo *.mod *.f90 lib*.a *.bak
159endif
160ifeq "$(strip $(SCRATCH_DIR))" "./"
161 clean_list := core *.o *.oo *.mod *.f90 lib*.a *.bak
162endif
163
164#--------------------------------------------------------------------------
165# Set Pattern rules.
166#--------------------------------------------------------------------------
167
168%.o: %.F
169
170%.o: %.f90
171 cd $(SCRATCH_DIR); $(FC) -c $(FFLAGS) $(notdir $<)
172
173%.f90: %.F
174 $(CPP) $(CPPFLAGS) $(CPP_APP_FLAGS) $< > $*.f90
175 $(CLEAN) $*.f90
176
177CLEAN := ROMS/Bin/cpp_clean
178
179#--------------------------------------------------------------------------
180# Make functions for putting the temporary files in $(SCRATCH_DIR)
181# DO NOT modify this section; spaces and blank lineas are needed.
182#--------------------------------------------------------------------------
183
184# $(call source-dir-to-binary-dir, directory-list)
185source-dir-to-binary-dir = $(addprefix $(SCRATCH_DIR)/, $(notdir $1))
186
187# $(call source-to-object, source-file-list)
188source-to-object = $(call source-dir-to-bindary-dir, \
189 $(subst .F,.o,$1))
190
191# $(call make-library, library-name, source-file-list)
192define make-library
193 libraries += $(SCRATCH_DIR)/$1
194 sources += $2
195
196 $(SCRATCH_DIR)/$1: $(call source-dir-to-binary-dir, \
197 $(subst .F,.o,$2))
198 $(AR) $(ARFLAGS) $$@ $$^
199 $(RANLIB) $$@
200endef
201
202# $(call f90-source, source-file-list)
203f90-source = $(call source-dir-to-binary-dir, \
204 $(subst .F,.f90,$1))
205
206# $(compile-rules)
207define compile-rules
208 $(foreach f, $(local_src), \
209 $(call one-compile-rule,$(call source-to-object,$f), \
210 $(call f90-source,$f),$f))
211endef
212
213# $(call one-compile-rule, binary-file, f90-file, source-files)
214define one-compile-rule
215 $1: $2 $3
216 cd $$(SCRATCH_DIR); $$(FC) -c $$(FFLAGS) $(notdir $2)
217
218 $2: $3
219 $$(CPP) $$(CPPFLAGS) $$(CPP_APP_FLAGS) $$< > $$@
220 $$(CLEAN) $$@
221
222endef
223
224#--------------------------------------------------------------------------
225# Set ROMS/TOMS executable file name.
226#--------------------------------------------------------------------------
227
228BIN := $(BINDIR)/oceanS
229ifdef USE_DEBUG
230 BIN := $(BINDIR)/oceanG
231else
232 ifdef USE_MPI
233 BIN := $(BINDIR)/oceanM
234 endif
235 ifdef USE_OpenMP
236 BIN := $(BINDIR)/oceanO
237 endif
238endif
239
240#--------------------------------------------------------------------------
241# "uname -s" should return the OS or kernel name and "uname -m" should
242# return the CPU or hardware name. In practice the results can be pretty
243# flaky. Run the results through sed to convert "/" and " " to "-",
244# then apply platform-specific conversions.
245#--------------------------------------------------------------------------
246
247OS := $(shell uname -s | sed 's/[\/ ]/-/g')
248OS := $(patsubst CYGWIN_%,CYGWIN,$(OS))
249OS := $(patsubst MINGW%,MINGW,$(OS))
250OS := $(patsubst sn%,UNICOS-sn,$(OS))
251
252CPU := $(shell uname -m | sed 's/[\/ ]/-/g')
253
254SVNREV := $(shell svnversion -n .)
255
256ROOTDIR := $(shell pwd)
257
258COMPILERS := ./Compilers
259
260ifndef FORT
261 $(error Variable FORT not set)
262endif
263
264ifneq "$(MAKECMDGOALS)" "clean"
265 include $(COMPILERS)/$(OS)-$(strip $(FORT)).mk
266endif
267
268#--------------------------------------------------------------------------
269# Pass the platform variables to the preprocessor as macros. Convert to
270# valid, upper-case identifiers. If applicable, attach ROMS application
271# CPP option.
272#--------------------------------------------------------------------------
273
274CPPFLAGS += -D$(shell echo ${OS} | tr "-" "_" | tr [a-z] [A-Z])
275CPPFLAGS += -D$(shell echo ${CPU} | tr "-" "_" | tr [a-z] [A-Z])
276CPPFLAGS += -D$(shell echo ${FORT} | tr "-" "_" | tr [a-z] [A-Z])
277
278CPPFLAGS += -D'ROOT_DIR="$(ROOTDIR)"'
279ifdef ROMS_APPLICATION
280 HEADER := $(addsuffix .h,$(shell echo ${ROMS_APPLICATION} | tr [A-Z] [a-z]))
281 CPPFLAGS += -D$(ROMS_APPLICATION)
282 CPPFLAGS += -D'HEADER="$(HEADER)"'
283 ifdef MY_HEADER_DIR
284 CPPFLAGS += -D'ROMS_HEADER="$(MY_HEADER_DIR)/$(HEADER)"'
285 else
286 CPPFLAGS += -D'ROMS_HEADER="$(HEADER)"'
287 endif
288 MDEPFLAGS += -DROMS_HEADER="$(HEADER)"
289 CPPFLAGS += -DNestedGrids=$(NestedGrids)
290endif
291
292ifndef MY_ANALYTICAL_DIR
293 MY_ANALYTICAL_DIR := $(ROOTDIR)/ROMS/Functionals
294endif
295ifeq (,$(findstring ROMS/Functionals,$(MY_ANALYTICAL_DIR)))
296 MY_ANALYTICAL := on
297endif
298CPPFLAGS += -D'ANALYTICAL_DIR="$(MY_ANALYTICAL_DIR)"'
299
300ifdef MY_ANALYTICAL
301 CPPFLAGS += -D'MY_ANALYTICAL="$(MY_ANALYTICAL)"'
302endif
303
304ifdef SVNREV
305 CPPFLAGS += -D'SVN_REV="$(SVNREV)"'
306else
307 SVNREV := $(shell grep Revision ./ROMS/Version | sed 's/.* \([0-9]*\) .*/\1/')
308 CPPFLAGS += -D'SVN_REV="$(SVNREV)"'
309endif
310
311#--------------------------------------------------------------------------
312# Build target directories.
313#--------------------------------------------------------------------------
314
315.PHONY: all
316
317all: $(SCRATCH_DIR) $(SCRATCH_DIR)/MakeDepend $(BIN)
318
319modules := ROMS/Adjoint \
320 ROMS/Representer \
321 ROMS/Tangent \
322 ROMS/Nonlinear \
323 ROMS/Functionals \
324 ROMS/SeaIce \
325 ROMS/Utility \
326 ROMS/Modules
327
328includes := ROMS/Include \
329 ROMS/Adjoint \
330 ROMS/Nonlinear \
331 ROMS/Representer \
332 ROMS/Tangent \
333 ROMS/SeaIce \
334 ROMS/Utility \
335 ROMS/Drivers
336
337ifdef MY_ANALYTICAL
338 includes += $(MY_ANALYTICAL_DIR)
339endif
340 includes += ROMS/Functionals
341
342ifdef MY_HEADER_DIR
343 includes += $(MY_HEADER_DIR)
344endif
345
346ifdef SWAN_COUPLE
347 modules += Waves/SWAN/Src
348 includes += Waves/SWAN/Src
349endif
350
351modules += Master
352includes += Master
353
354vpath %.F $(modules)
355vpath %.h $(includes)
356vpath %.f90 $(SCRATCH_DIR)
357vpath %.o $(SCRATCH_DIR)
358
359include $(addsuffix /Module.mk,$(modules))
360
361MDEPFLAGS += $(patsubst %,-I %,$(includes)) --silent --moddir $(SCRATCH_DIR)
362
363CPPFLAGS += $(patsubst %,-I%,$(includes))
364
365ifdef MY_HEADER_DIR
366 CPPFLAGS += -D'HEADER_DIR="$(MY_HEADER_DIR)"'
367else
368 CPPFLAGS += -D'HEADER_DIR="./ROMS/Include"'
369endif
370
371$(SCRATCH_DIR):
372 $(shell $(TEST) -d $(SCRATCH_DIR) || $(MKDIR) $(SCRATCH_DIR) )
373
374#--------------------------------------------------------------------------
375# Add profiling.
376#--------------------------------------------------------------------------
377
378# FFLAGS += -check bounds # ifort
379# FFLAGS += -C # pgi
380# FFLAGS += -xpg # Sun
381# FFLAGS += -pg # g95
382# FFLAGS += -qp # ifort
383# FFLAGS += -Mprof=func,lines # pgi
384# FFLAGS += -Mprof=mpi,lines # pgi
385# FFLAGS += -Mprof=mpi,hwcts # pgi
386# FFLAGS += -Mprof=func # pgi
387
388#--------------------------------------------------------------------------
389# Special CPP macros for mod_strings.F
390#--------------------------------------------------------------------------
391
392$(SCRATCH_DIR)/mod_strings.f90: CPPFLAGS += -DMY_OS='"$(OS)"' \
393 -DMY_CPU='"$(CPU)"' -DMY_FORT='"$(FORT)"' \
394 -DMY_FC='"$(FC)"' -DMY_FFLAGS='"$(FFLAGS)"'
395
396#--------------------------------------------------------------------------
397# ROMS/TOMS libraries.
398#--------------------------------------------------------------------------
399
400MYLIB := libocean.a
401
402.PHONY: libraries
403
404libraries: $(libraries)
405
406#--------------------------------------------------------------------------
407# Target to create ROMS/TOMS dependecies.
408#--------------------------------------------------------------------------
409
410$(SCRATCH_DIR)/MakeDepend: makefile
411 $(shell $(TEST) -d $(SCRATCH_DIR) || $(MKDIR) $(SCRATCH_DIR) )
412 $(SFMAKEDEPEND) $(MDEPFLAGS) $(sources) > $(SCRATCH_DIR)/MakeDepend
413
414.PHONY: depend
415
416SFMAKEDEPEND := ./ROMS/Bin/sfmakedepend
417
418depend: $(SCRATCH_DIR)
419 $(SFMAKEDEPEND) $(MDEPFLAGS) $(sources) > $(SCRATCH_DIR)/MakeDepend
420
421ifneq "$(MAKECMDGOALS)" "clean"
422 -include $(SCRATCH_DIR)/MakeDepend
423endif
424
425#--------------------------------------------------------------------------
426# Target to create ROMS/TOMS tar file.
427#--------------------------------------------------------------------------
428
429.PHONY: tarfile
430
431tarfile:
432 tar --exclude=".svn" -cvf roms-3_0.tar *
433
434.PHONY: zipfile
435
436zipfile:
437 zip -r roms-3_0.zip *
438
439.PHONY: gzipfile
440
441gzipfile:
442 gzip -v roms-3_0.gzip *
443
444#--------------------------------------------------------------------------
445# Cleaning targets.
446#--------------------------------------------------------------------------
447
448.PHONY: clean
449
450clean:
451 $(RM) -r $(clean_list)
452
453#--------------------------------------------------------------------------
454# A handy debugging target. This will allow to print the value of any
455# makefile defined macro (see http://tinyurl.com/8ax3j). For example,
456# to find the value of CPPFLAGS execute:
457#
458# gmake print-CPPFLAGS
459# or
460# make print-CPPFLAGS
461#--------------------------------------------------------------------------
462
463.PHONY: print-%
464
465print-%:
466 @echo $* = $($*)