-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmk.fortran
More file actions
340 lines (288 loc) · 15.5 KB
/
mk.fortran
File metadata and controls
340 lines (288 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
ifndef __mk_fortran_included
__mk_fortran_included := true
include $(dir $(lastword $(MAKEFILE_LIST)))mk.compilation
#-- set/modify public definitions --
mk_PARAMETERS += FC FC_MOD_DIR FC_PP_DEFINES FC_INCLUDE_DIRS FC_FLAGS FC_CFLAGS FC_LFLAGS FC_LIBRARY_DIRS FC_LIBRARIES FC_LINK_OTHER
mk_OPT_CLASSES += FC
mk_TARGET_list += fpp,*
mk_FILE_TYPES += $(__fc_file_types)
mk_OUTPUT_DIRS += $(FC_MOD_DIR)
mk_CLEARED_FILES += $(mk_BUILD_DIR)/*.o $(mk_BUILD_DIR)/*.mod $(mk_BUILD_DIR)/lib*
__FCs := gfortran ifort
__builtin_FCs := $(foreach c,$(__FCs),$(if $(shell which $c $(mk_ignoreErrors)),$c,-$c))
__builtin_ARCHs := 64 32
mk_FC_list = $(call mk_selective,$(FC_list) $(__builtin_FCs))
mk_ARCH_list = $(call mk_selective,$(ARCH_list) $(__builtin_ARCHs))
cmd_FC_version = $(shell $(fc) $(fc_version) 2>&1 | sed -ne 's/\([0-9]\+\.[0-9.]\+\)/\1/p')
cmd_depends += cmd_FC_depends
regex_export ?= ^\s*[cC!]_PROC_EXPORT(\(\w*\)) \L\1
# search regex ---^ ^--- replace pattern: symbol as lowercase!
ifeq ($(strip $(mk_FC_list)),)
$(error could not find any compiler on your system. Searched for $(__FCs))
endif
# $(cmd_LINK,OUT_FILE,OBJECTS)
define cmd_LINK
$(call fc_cmd_link.$(mk_OUT_TYPE),$1,$2)
endef
__have_python=$(shell which python >/dev/null 2>&1 && echo 0 || echo 1 )
ifeq ($(__have_python), 1)
$(error python not found in PATH, but it is necessary to create dependency lists to compile Fortran sources)
endif
# $(cmd_FC_depends,DEPS_FILE,SOURCE_LIST)
define cmd_FC_depends
$(eval ___fc_src := $(call mk_file_list,filter,$(__fc_file_types),$2))
$(eval ___tmp := $(mk_BUILD_DIR)/.mk.FC_depends)
$(if $(___fc_src),\
$(call mk_write_file,$(___tmp),$(___fc_src)) \
python $(__mk_root)fort_deps.py -o $1 --append --in=$(___tmp) --objdir=$(mk_BUILD_DIR) --fpp="$(__fc_cmd_fpp) {f}" -j 20 -v ;\
rm -f $(___tmp))
endef
#-- user parameters --
FC_doc ?= used fortran compiler: $(call mk_opt_set,FC)
FC = $(firstword $(mk_FC_list))# < override default on most systems: f77
mk_FC = $(call mk_get,FC)
FC_PP_DEFINES_doc ?= list of fortran preprocessor defines
mk_FC_PP_DEFINES = $(call mk_get,FC_PP_DEFINES)
FC_INCLUDE_DIRS_doc ?= fortran include directories
mk_FC_INCLUDE_DIRS = $(call mk_get,FC_INCLUDE_DIRS)
FC_MOD_DIR_doc ?= output directory for generated fortran module files
FC_MOD_DIR_default = $(mk_BUILD_DIR)
mk_FC_MOD_DIR = $(call mk_get,FC_MOD_DIR)
FC_FLAGS_doc ?= flags, applied to any fortran compile and link command
FC_FLAGS.% ?= $(fc_m)$(mk_ARCH)
FC_FLAGS.shared ?= $(fc_pic)
mk_FC_FLAGS = $(call mk_get,FC_FLAGS)
FC_CFLAGS_doc ?= fortran compile flags, applied to any file
FC_CFLAGS.debug ?= $(fc_g) $(fc_trapuv)
FC_CFLAGS.release ?= $(fc_O2)
mk_FC_CFLAGS = $(call mk_get,FC_CFLAGS)
FC_LFLAGS_doc ?= fortran link flags
FC_LFLAGS.exe ?= $(fl_rpath)'$$ORIGIN'
FC_LFLAGS.shared ?= $(fl_rpath)'$$ORIGIN' $(fl_no_undef) $(fl_soname)$(mk_OUT_FILE_NAME)$(mk_MAYOR)
mk_FC_LFLAGS = $(call mk_get,FC_LFLAGS)
FC_LIBRARIES_doc ?= list of linked libraries
mk_FC_LIBRARIES = $(call mk_get,FC_LIBRARIES)
FC_LIBRARY_DIRS_doc ?= list of library directories
mk_FC_LIBRARY_DIRS = $(call mk_get,FC_LIBRARY_DIRS)
FC_LINK_OTHER_doc ?= list of other link input files
mk_FC_LINK_OTHER = $(call mk_get,FC_LINK_OTHER)
#-- exposed compiler switches --
fc = $(__fc_$(mk_FC))# < compiler call
fc_version = $(__fc_version_$(mk_FC))# < compiler version string
fc_c = $(__fc_c_$(mk_FC))# < compile only (no linking)
fc_g = $(__fc_g_$(mk_FC))# < compile with debug info
fc_O0 = $(__fc_O0_$(mk_FC))# < optimization level O0
fc_O1 = $(__fc_O1_$(mk_FC))# < optimization level O1
fc_O2 = $(__fc_O2_$(mk_FC))# < optimization level O2
fc_O3 = $(__fc_O3_$(mk_FC))# < optimization level O3
fc_o = $(__fc_o_$(mk_FC))# < output file
fc_m = $(__fc_m_$(mk_FC))# < architecture
fc_omp = $(__fc_omp_$(mk_FC))# < enable OpenMP extensions
fc_save = $(__fc_save_$(mk_FC))# < place local variables in static memory
fc_Wall = $(__fc_Wall_$(mk_FC))# < warn all
fc_Wsrc_cut = $(__fc_Wsrc_cut_$(mk_FC))# < warn if source lines are truncated
fc_Wimplicit = $(__fc_Wimplicit_$(mk_FC))# < warn/raise error on implicit declarations
fc_Winterfaces = $(__fc_Winterfaces_$(mk_FC))# < warn/raise error on interfaces mismatch
fc_Wnointerfaces = $(__fc_Wnointerfaces_$(mk_FC))# < do NOT warn/raise error on interfaces mismatch
fc_consistency = $(__fc_consistency_$(mk_FC))# < Ensures math library functions to produce consistent results across different microarchitectural (ifort-only!)
fc_backtrace = $(__fc_backtrace_$(mk_FC))# < generate backtrace on serious runtime errorsA
fc_fpscomp = $(call __fc_fpscomp_$(mk_FC),$1)# < runtime compatibility flags ... (ifort-only!)
# valid arguments: none, [no]filesfromcmd, [no]general, [no]ioformat,
# all, [no]libs, [no]ldio_spacing, [no]logicals
fc_fpmodel = $(call __fc_fpmodel_$(mk_FC),$1)# < flags controlling semantics of floating-point calculations (ifort-only!)
# valid arguments: precise, fast, strict, source
fc_fpspeculation = $(call __fc_fpspeculation_$(mk_FC),$1)# < Tells the compiler the mode in which to speculate on floating-point operations. (ifort-only!)
# valid arguments: fast, safe, strict, off (default: fast)
fc_fpe = $(call __fc_fpe_$(mk_FC),$1)# < set fpe-trap mode: $(call fc_fpe,{0,1,3})
# 0: traps invalid, zero, overflow AND (ifort-only:) flush-to-zero
# 1: no traps, (ifort-only:) flush-to-zero
# 3: no traps
fc_trapuv = $(__fc_trapuv_$(mk_FC))# < trap uninitialized variables by initializing them to "unusual" values
fc_check_all = $(__fc_check_all_$(mk_FC))
fc_realloc_lhs = $(__fc_realloc_lhs_$(mk_FC))
fc_fpp = $(__fc_fpp_$(mk_FC))# < enable preprocessor
fc_fpp77 = $(__fc_fpp77_$(mk_FC))# < enable preprocessor f77 style
fc_fpp95 = $(__fc_fpp95_$(mk_FC))# < enable preprocessor f95 style
fc_form = $(call __fc_form_$(mk_FC),$1,$2)# < set input format: $(call fc_form,{free,fixed},{n,none})
fc_E = $(__fc_E_$(mk_FC))# < preprocess only - implies fc_fpp
fc_D = $(__fc_D_$(mk_FC))# < define preprocessor symbol
fc_I = $(__fc_I_$(mk_FC))# < include directory
fc_L = $(__fc_L_$(mk_FC))# < link directory
fc_l = $(__fc_l_$(mk_FC))# < link library
fc_mod = $(__fc_mod_$(mk_FC))# < directory for module output
fc_pic = $(__fc_pic_$(mk_FC))# < generate position independent code (pic) for shared libraries
fc_threads = $(__fc_threads_$(mk_FC))# < enable threading support
#
fl = $(__fl_$(mk_FC))# < linker call
fl_soname = $(__fl_soname_$(mk_FC))# < set soname
fl_static = $(__fl_static_$(mk_FC))# < force static linking
fl_rpath = $(__fl_rpath_$(mk_FC))# < burn library path
fl_no_undef = $(__fl_no_undef_$(mk_FC))# < force error reports for unresolved symbols
fl_arch_on = $(__fl_arch_on_$(mk_FC))# < force linker to include whole archives
fl_arch_off = $(__fl_arch_off_$(mk_FC))# < disable including whole archives
fl_archives = $(fl_arch_on) $1 $(fl_arch_off)# < option frame for linking whole archives
fl_v_script = $(__fl_v_script_$(mk_FC))# < version script
fl_exports = $(if $(mk_EXPORTS),$(fl_v_script)$(mk_V_SCRIPT),)
fl_dynamic = $(__fl_dynamic_$(mk_FC))# < add all symbols to dynamic symbol table (>> stacktrace)
#-- targets --
fpp,*_doc = preprocess file to stdout. Directories have to be separated by \\\\, e.g. make fpp,src\\\\main.f90
fpp,%:
$(mk_MAKE) --quiet $(subst \,/,$*)
$(call __fc_cmd_fpp_of,$(subst \,/,$*))
#-- define compiler specifics --
# GNU Fortran compiler
__fc_gfortran := gfortran
__fc_version_gfortran := --version
__fc_c_gfortran := -c
__fc_g_gfortran := -ggdb -g
__fc_O0_gfortran := -O0
__fc_O1_gfortran := -O1
__fc_O2_gfortran := -O2
__fc_O3_gfortran := -O3
__fc_o_gfortran := -o
__fc_m_gfortran := -m
__fc_omp_gfortran := -fopenmp
__fc_save_gfortran := -fno-automatic
__fc_Wall_gfortran := -Wall
__fc_Wsrc_cut_gfortran := -Wline-truncation
__fc_Wimplicit_gfortran := -fimplicit-none
__fc_consistency_gfortran :=
__fc_Winterfaces_gfortran :=
__fc_Wnointerfaces_gfortran :=
__fc_backtrace_gfortran := -fbacktrace
__fc_fpscomp_gfortran :=
__fc_fpmodel_gfortran :=
__fc_fpspeculation_gfortran :=
__fc_fpe_0_gfortran := -ffpe-trap=invalid,zero,overflow
__fc_fpe_1_gfortran :=
__fc_fpe_3_gfortran :=
__fc_fpe_gfortran = $(__fc_fpe_$1_gfortran)
__fc_trapuv_gfortran := -finit-real=snan -finit-integer=-1 -finit-character=0 -finit-logical=false
__fc_check_all_gfortran := -fcheck=pointer,bounds
__fc_realloc_lhs_gfortran :=
__fc_fpp_gfortran := -cpp
__fc_fpp77_gfortran := -x f77-cpp-input
__fc_fpp95_gfortran := -x f95-cpp-input
__fc_form_gfortran = -f$1-line-length-$2
__fc_E_gfortran := -E $(__fc_fpp_gfortran)
__fc_D_gfortran := -D
__fc_I_gfortran := -I
__fc_L_gfortran := -L
__fc_l_gfortran := -l
__fc_mod_gfortran := -J
__fc_pic_gfortran := -shared -fPIC
__fc_threads_gfortran := -pthread
#
__fl_gfortran := gfortran
__fl_soname_gfortran := -Wl,-soname,
__fl_static_gfortran := -Wl,-static
__fl_rpath_gfortran := -Wl,-rpath,
__fl_no_undef_gfortran := -Wl,--no-undefined
__fl_arch_on_gfortran := -Wl,--whole-archive
__fl_arch_off_gfortran := -Wl,--no-whole-archive
__fl_v_script_gfortran := -Wl,--version-script=
__fl_dynamic_gfortran := -rdynamic
# Intel Fortran compiler
__fc_ifort := ifort
__fc_version_ifort := -V
__fc_c_ifort := -c
__fc_g_ifort := -g
__fc_O0_ifort := -O0
__fc_O1_ifort := -O1
__fc_O2_ifort := -O2
__fc_O3_ifort := -O3
__fc_o_ifort := -o
__fc_m_ifort := -m
__fc_omp_ifort := -qopenmp
__fc_save_ifort := -save
__fc_Wall_ifort := -warn
__fc_Wsrc_cut_ifort := -warn truncated_source
__fc_Wimplicit_ifort := -warn declarations
__fc_Winterfaces_ifort := -warn interfaces
__fc_Wnointerfaces_ifort := -warn nointerfaces
__fc_consistency_ifort := -fimf-arch-consistency=true
__fc_backtrace_ifort := -traceback
__fc_fpscomp_ifort = -fpscomp $(call mk_csv,$1)
__fc_fpmodel_ifort = -fp-model $1
__fc_fpspeculation_ifort = -fp-speculation $1
__fc_fpe_ifort = -fpe$1 #< CAUTION: -fpe-all=0 sounds more sensible here - BUT leads to nasty FP-Exceptions!
__fc_trapuv_ifort := -ftrapuv
__fc_check_all_ifort := -check pointer,bounds,uninit,format,output_conversion
__fc_realloc_lhs_ifort := -assume realloc_lhs
__fc_fpp_ifort := -fpp -allow nofpp-comments
__fc_fpp77_ifort := $(__fc_fpp_ifort)
__fc_fpp95_ifort := $(__fc_fpp_ifort)
__fc_form_ifort_fixed := -fixed
__fc_form_ifort_free := -nofixed
__fc_form_ifort_none := -noextend-source
__fc_form_ifort_0 := -noextend-source
__fc_form_ifort = $(__fc_form_ifort_$1) $(or $(__fc_form_ifort_$2),-extend-source $2)
__fc_E_ifort := -E $(__fc_fpp_ifort)
__fc_D_ifort := -D
__fc_I_ifort := -I
__fc_L_ifort := -L
__fc_l_ifort := -l
__fc_mod_ifort := -module
__fc_pic_ifort := -shared -fPIC
__fc_threads_ifort := -threads
#
__fl_ifort := ifort
__fl_soname_ifort := -Wl,-soname=
__fl_static_ifort := -Wl,-Bstatic
__fl_rpath_ifort := -Wl,-rpath=
__fl_no_undef_ifort := -Wl,--no-undefined
__fl_arch_on_ifort := -Wl,--whole-archive
__fl_arch_off_ifort := -Wl,--no-whole-archive
__fl_v_script_ifort := -Wl,--version-script=
__fl_dynamic_ifort := -rdynamic
#-- derive private flaglists --
__fc_file_types := f95 F95 f90 F90 for f F
__fc_pp_defines = $(mk_FC_PP_DEFINES:%=$(fc_D)%)
__fc_include_dirs = $(patsubst %,$(fc_I)%,$(filter-out $(mk_FC_MOD_DIR),$(mk_BUILD_DIR) $(mk_FC_INCLUDE_DIRS)))
__fc_ppflags = $(__fc_include_dirs) $(__fc_pp_defines)
__fc_cflags = $(__fc_ppflags) $(fc_mod) $(mk_FC_MOD_DIR) $(mk_FC_FLAGS) $(mk_FC_CFLAGS)
fc_cflags_of = $(or $(call mk_get,$(basename $1)),$(__fc_cflags) $(call mk_fileType_opts,$1))
fc_all_flags_of = $(call fc_cflags_of,$(subst \\,/,$1))
__fc_cmd_fpp = $(mk_FC) $(fc_E) $(__fc_ppflags)
__fc_cmd_fpp_of = $(mk_FC) $(fc_E) $(call fc_cflags_of,$(notdir $1)) $1
__fc_cmd_compile = $(fc) $(fc_c) $(call fc_cflags_of,$(notdir $<)) $< $(fc_o) $@
__fc_library_dirs = $(call uniq,$(patsubst %,$(fc_L)%,$(mk_BUILD_DIR) $(mk_FC_LIBRARY_DIRS)))
__fc_libraries = $(patsubst %,$(fc_l)%,$(mk_FC_LIBRARIES))
__fc_lflags = $(mk_FC_LFLAGS) $(mk_FC_FLAGS) $(__fc_library_dirs)
__fc_object_archive = $(mk_BUILD_DIR)/.mk.objects.a
define fc_copy_mods
cp -a $(mk_FC_MOD_DIR)/*[!_].mod $(mk_OUT_DIR) $(mk_ignoreErrors)
endef
define fc_cmd_link.exe
$(call mk_create_archive,$(__fc_object_archive),$2)
$(call mk_logged_cmd,$(fl) $(fc_o) $1 $(__fc_lflags) $(call fl_archives,$(__fc_object_archive)) $(__fc_libraries) $(mk_FC_LINK_OTHER))
$(call mk_collect_so,$(mk_FC_LIBRARY_DIRS),$(mk_FC_LIBRARIES))
endef
define fc_cmd_link.shared
$(call mk_create_version_script)
$(call mk_create_archive,$(__fc_object_archive),$2)
$(call mk_logged_cmd,$(fl) $(fc_o) $1 $(__fc_lflags) $(call fl_archives,$(__fc_object_archive)) $(__fc_libraries) $(mk_FC_LINK_OTHER))
$(call mk_symlink_so,$1)
$(call fc_copy_mods)
$(call mk_collect_so,$(mk_FC_LIBRARY_DIRS),$(mk_FC_LIBRARIES))
endef
define fc_cmd_link.static
$(call mk_create_archive,$1,$2,$(mk_BUILD_DIR),$(mk_FC_LIBRARIES))
$(call fc_copy_mods)
$(call mk_collect_so,$(mk_FC_LIBRARY_DIRS),$(mk_FC_LIBRARIES))
endef
$(mk_BUILD_DIR)/%.o: %.f95
@$(call mk_logged_cmd,$(__fc_cmd_compile))
$(mk_BUILD_DIR)/%.o: %.F95
@$(call mk_logged_cmd,$(__fc_cmd_compile))
$(mk_BUILD_DIR)/%.o: %.f90
@$(call mk_logged_cmd,$(__fc_cmd_compile))
$(mk_BUILD_DIR)/%.o: %.F90
@$(call mk_logged_cmd,$(__fc_cmd_compile))
$(mk_BUILD_DIR)/%.o: %.for
@$(call mk_logged_cmd,$(__fc_cmd_compile))
$(mk_BUILD_DIR)/%.o: %.f
@$(call mk_logged_cmd,$(__fc_cmd_compile))
$(mk_BUILD_DIR)/%.o: %.F
@$(call mk_logged_cmd,$(__fc_cmd_compile))
endif # __mk_fortran_included