forked from ESCOMP/CAM
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuildlib
More file actions
executable file
·367 lines (301 loc) · 16 KB
/
Copy pathbuildlib
File metadata and controls
executable file
·367 lines (301 loc) · 16 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
"""
create the cam library
"""
# pylint: disable=multiple-imports, wrong-import-position, wildcard-import
# pylint: disable=unused-wildcard-import, bad-whitespace, too-many-locals
# pylint: disable=invalid-name
import sys, os, filecmp, shutil, re
from glob import glob
_CIMEROOT = os.environ.get("CIMEROOT")
if _CIMEROOT is None:
raise SystemExit("ERROR: must set CIMEROOT environment variable")
_LIBDIR = os.path.join(_CIMEROOT, "CIME", "Tools")
sys.path.append(_LIBDIR)
from standard_script_setup import *
from CIME.case import Case
from CIME.utils import run_sub_or_cmd, expect, run_cmd
from CIME.utils import import_from_file
from CIME.buildlib import parse_input
from CIME.build import get_standard_makefile_args
logger = logging.getLogger(__name__)
###############################################################################
def _build_fms(caseroot, libroot, bldroot):
###############################################################################
with Case(caseroot) as case:
# Only need FMS for fv3 dycore
cam_dycore = case.get_value("CAM_DYCORE")
srcroot = case.get_value("SRCROOT")
if cam_dycore == "fv3":
# first check for the external FMS library and build it
# Check to see if some other component built it already
fmsbuildlib = os.path.join(srcroot, "libraries", "FMS", "buildlib")
librootfms = os.path.join(libroot, "libfms.a")
if not os.path.exists(librootfms):
if case.get_value("DEBUG"):
strdebug = "debug"
else:
strdebug = "nodebug"
if case.get_value("BUILD_THREADED"):
strthread = "threads"
else:
strthread = "nothreads"
mpilib = case.get_value("MPILIB")
sharedpath = os.path.join(case.get_value("COMPILER"), mpilib,
strdebug, strthread)
slr = os.path.abspath(case.get_value("SHAREDLIBROOT"))
fmsbuildroot = os.path.join(slr, sharedpath)
fmsinstallpath = os.path.join(fmsbuildroot, "FMS")
install_libfms = os.path.join(fmsinstallpath, "libfms.a")
if not os.path.exists(install_libfms):
if not os.path.exists(fmsbuildlib):
#todo: call checkout_externals to get this component
expect(False, "FMS external not found")
else:
stat, _, err = run_cmd(f"{fmsbuildlib} {fmsbuildroot} {fmsinstallpath} {caseroot}", verbose=True)
expect(stat==0, f"FMS build Failed {err}")
if os.path.exists(install_libfms):
shutil.copy(install_libfms, libroot)
###############################################################################
def _build_cam(caseroot, libroot, bldroot):
###############################################################################
with Case(caseroot, read_only=False) as case:
srcroot = case.get_value("SRCROOT")
# -------------------------------------------------------
# Call cam's buildcpp
# -------------------------------------------------------
testpath = os.path.join(srcroot, "components", "cam")
if os.path.exists(testpath):
srcroot = testpath
cmd = os.path.join(os.path.join(srcroot, "cime_config", "buildcpp"))
logger.info(" ...calling cam buildcpp to set build time options")
try:
buildcpp = import_from_file("buildcpp", cmd)
cam_cppdefs = buildcpp.buildcpp(case)
except:
raise RuntimeError("CAM's 'buildcpp' script failed to run properly.")
#--------------------------------------------------------
# Check if RTTOV is being requested by the user,
# and if so, set a needed environment variable
# and update the CAM_LINKED_LIBS list:
#--------------------------------------------------------
env_vars = ''
config_opts = case.get_value("CAM_CONFIG_OPTS")
if "-rttov" in config_opts:
# RTTOV source directory (currently hardcoded to a specific ACL-controlled directory on Derecho)
rttov_src_dir = "/glade/campaign/cesm/community/amwg/rttov/RTTOV"
# Set environment variable needed by COSP Makefile
env_vars = f"env RTTOV=true RTTOV_SRC_DIR={rttov_src_dir} "
# Run shell script to build RTTOV in ACL-controlled directory
rttov_command = "./compile_RTTOV.sh"
_run_cmd(rttov_command, rttov_src_dir)
# Get path to HDF5 library
hdf5_lib_path = os.environ.get("NCAR_LDFLAGS_HDF5")
# Add HDF5 library to CAM linked library list:
linked_libs = case.get_value("CAM_LINKED_LIBS",
subgroup="build_component_cam")
hdf5_link_opts = f" -L{hdf5_lib_path}"
hdf5_link_opts += " -lhdf5_fortran -lhdf5_hl_fortran -lhdf5"
case.set_value("CAM_LINKED_LIBS",
linked_libs + hdf5_link_opts)
with Case(caseroot) as case:
casetools = case.get_value("CASETOOLS")
srcroot = case.get_value("SRCROOT")
gmake_j = case.get_value("GMAKE_J")
gmake = case.get_value("GMAKE")
mach = case.get_value("MACH")
config_opts = case.get_value("CAM_CONFIG_OPTS")
user_incldir = None
cam_dycore = case.get_value("CAM_DYCORE")
if cam_dycore == "fv3":
slr = os.path.abspath(case.get_value("SHAREDLIBROOT"))
compiler = case.get_value("COMPILER")
mpilib = case.get_value("MPILIB")
debug = "debug" if case.get_value("DEBUG") else "nodebug"
threaded = "threads" if case.get_value("BUILD_THREADED") or case.get_value("FORCE_BUILD_SMP") else "nothreads"
comp_interface = case.get_value("COMP_INTERFACE")
fmsbuilddir = os.path.join(
slr, compiler, mpilib, debug, threaded, "FMS")
user_incldir = '"-I{} -I{} -I{}"'.format(
os.path.join(srcroot, "libraries", "FMS", "src", "include"),
os.path.join(srcroot, "libraries", "FMS", "src", "mpp", "include"),
fmsbuilddir)
# -------------------------------------------------------
# Filepath is created in caseroot/camconf by the call
# to buildcpp - this needs to be copied to bldroot
# -------------------------------------------------------
filesrc = os.path.join(caseroot, "Buildconf", "camconf", "Filepath")
filedst = os.path.join(bldroot, "Filepath_tmp")
shutil.copy(filesrc, filedst)
filedst = os.path.join(bldroot, "Filepath")
filedst_tmp = os.path.join(bldroot, "Filepath_tmp")
if os.path.isfile(filedst):
if not filecmp.cmp(filedst_tmp, filedst):
shutil.move(filedst_tmp, filedst)
else:
shutil.move(filedst_tmp, filedst)
# -------------------------------------------------------
# fms is needed by fv3 and should have been built by the framework
# -------------------------------------------------------
if cam_dycore == "fv3":
libfms = os.path.join(fmsbuilddir, "libfms.a")
expect(os.path.isfile(libfms), "FMS library not found {}".format(libfms))
shutil.copy(libfms, libroot)
# build the library
# -------------------------------------------------------
complib = os.path.join(libroot, "libatm.a")
makefile = os.path.join(casetools, "Makefile")
cmd = "{}{} complib -j {} COMP_NAME=cam COMPLIB={} -f {} {} ".format(
env_vars, gmake, gmake_j, complib, makefile,
get_standard_makefile_args(case)
)
if cam_cppdefs:
cmd += " USER_CPPDEFS='{}'".format(cam_cppdefs)
if user_incldir:
cmd += " USER_INCLDIR={}".format(user_incldir)
rc, out, err = run_cmd(cmd)
logger.info("%s: \n\n output:\n %s \n\n err:\n\n%s\n", cmd, out, err)
expect(rc == 0, "Command %s failed with rc=%s" % (cmd, rc))
###############################################################################
def _run_cmd(command, working_dir):
###############################################################################
rc, out, err = run_cmd(command, from_dir=working_dir, verbose=True)
expect(rc == 0, f"Command {command} failed with rc={rc} out={out} err={err}")
###############################################################################
def _cmake_default_args(caseroot):
###############################################################################
# Returns a dictionary of CMake variables based on the Macros.cmake file for
# the build.
with Case(caseroot) as case:
macro_path = os.path.abspath(os.path.join(caseroot, "cmake_macros", ""))
args = "-DCONVERT_TO_MAKE=ON "
args += f"-DCASEROOT={caseroot} "
args += f"-DCOMPILER={case.get_value('COMPILER')} "
args += f"-DOS={case.get_value('OS')} "
args += f"-DMACH={case.get_value('MACH')} "
args += "-DCMAKE_C_COMPILER_WORKS=1 "
args += "-DCMAKE_Fortran_COMPILER_WORKS=1 "
args += "-DCMAKE_CXX_COMPILER_WORKS=1 "
args += f"-DDEBUG={case.get_value('DEBUG')} "
cmd = f"cmake {args} ."
rc, out, err = run_cmd(cmd, combine_output=True, from_dir=macro_path)
expect(rc == 0, f"Command {cmd} failed with rc={rc} out={out} err={err}")
arg_dict = {}
for line in out.splitlines():
if ":=" in line:
key, val = line.split(":=")
arg_dict[key.replace('CIME_SET_MAKEFILE_VAR','').strip()] = val.strip()
return arg_dict
###############################################################################
def _build_tuvx(caseroot, libroot, bldroot):
###############################################################################
# Builds the TUV-x library and updates the case variables used to set the
# include paths and linked libraries
with Case(caseroot, read_only=False) as case:
config_opts = case.get_value("CAM_CONFIG_OPTS")
# Remove any TUV-x link options set by a previous build, so that
# re-building the case does not accumulate duplicate or stale
# options in CAM_LINKED_LIBS:
linked_libs = case.get_value("CAM_LINKED_LIBS",
subgroup="build_component_cam")
tuvx_link_opts = " -ltuvx -lyaml-cpp"
new_linked_libs = re.sub(r"\s*-ltuvx -lyaml-cpp", "", linked_libs)
if "-tuvx" in config_opts:
bldpath = os.path.join(bldroot, "tuv-x")
if not os.path.exists(bldpath):
os.makedirs(bldpath)
srcpath = os.path.abspath(os.path.join(case.get_value("SRCROOT"), \
"libraries", "tuv-x", ""))
logger.info("Building TUV-x in {} from source in {}\n".format(bldpath, srcpath))
arg_dict = _cmake_default_args(caseroot)
cmake_args = "-DCMAKE_VERBOSE_MAKEFILE=ON "
if case.get_value("MPILIB") != "mpi-serial":
cmake_args += "-DTUVX_ENABLE_MPI:BOOL=TRUE "
cmake_args += "-DCMAKE_BUILD_TYPE=Debug "
cmake_args += "-DCMAKE_Fortran_COMPILER=mpif90 "
cmake_args += "-DCMAKE_C_COMPILER=mpicc "
cmake_args += "-DCMAKE_CXX_COMPILER=mpicxx "
cmake_args += "-DCMAKE_Fortran_COMPILER_WORKS=1 "
cmake_args += "-DCMAKE_C_COMPILER_WORKS=1 "
cmake_args += "-DCMAKE_CXX_COMPILER_WORKS=1 "
cmake_args += "-DCMAKE_DISABLE_FIND_PACKAGE_yaml-cpp=1 "
if (case.get_value("MACH") == "izumi") :
cmake_args += f"-DCMAKE_PREFIX_PATH={arg_dict['NETCDF_PATH']} "
if (case.get_value("MACH") == "izumi") and (case.get_value('COMPILER') == "nag") :
cmake_args += "-DCMAKE_Fortran_FLAGS='-C=all -g ' "
else :
cmake_args += f"-DCMAKE_Fortran_FLAGS='{arg_dict['FFLAGS']}' "
cmake_args += f"-DCMAKE_INSTALL_PREFIX='{libroot}' "
cmake_args += "-DTUVX_ENABLE_TESTS=OFF "
cmake_args += "-DTUVX_ENABLE_COVERAGE=OFF "
cmake_args += "-DTUVX_BUILD_CLI=OFF "
cmake_args += f"-DTUVX_INSTALL_INCLUDE_DIR='{_tuvx_include_dir(libroot)}' "
cmake_args += f"-DTUVX_INSTALL_MOD_DIR='{_tuvx_include_dir(libroot)}' "
cmake_args += srcpath
_run_cmd(f"cmake {cmake_args}", bldpath)
_run_cmd(case.get_value('GMAKE'), bldpath)
_run_cmd(f"{case.get_value('GMAKE')} install", bldpath)
# add TUV-x to include paths
incldir = os.environ.get('USER_INCLDIR')
if incldir is None:
incldir = ''
os.environ['USER_INCLDIR'] = incldir + \
f" -I{_tuvx_include_dir(libroot)} "
# create symlink to library in folder CIME expects libraries to be in
dst = os.path.join(libroot, "libtuvx.a")
if os.path.isfile(dst):
os.remove(dst)
os.symlink(_tuvx_lib_path(libroot), dst)
dst = os.path.join(libroot, "libyaml-cpp.a")
if os.path.isfile(dst):
os.remove(dst)
os.symlink(_yaml_cpp_lib_path(libroot), dst)
# Add TUVX to CAM linked library list:
new_linked_libs = tuvx_link_opts.strip() + " " + new_linked_libs
if new_linked_libs != linked_libs:
case.set_value("CAM_LINKED_LIBS", new_linked_libs)
###############################################################################
def _tuvx_include_dir(libroot):
###############################################################################
# Returns the path to the TUV-x include directory
coreinc = os.path.join(libroot, "include")
expect(os.path.exists(coreinc), f"TUV-x include directory not found at {coreinc}")
return coreinc
###############################################################################
def _yaml_cpp_lib_path(libroot):
###############################################################################
# Returns the path to the yaml-cpp library
corelib = os.path.join(libroot, "lib64", "libyaml-cpp.a")
if not os.path.exists(corelib):
corelib = os.path.join(libroot, "lib64", "libyaml-cppd.a")
if not os.path.exists(corelib):
corelib = os.path.join(libroot, "lib", "libyaml-cpp.a")
expect(os.path.exists(corelib), f"yaml-cpp library not found at {corelib}")
return corelib
###############################################################################
def _tuvx_lib_path(libroot):
###############################################################################
# Returns the path to the TUV-x library
corelib = os.path.join(_tuvx_install_dir(libroot), "lib64", "libtuvx.a")
if not os.path.exists(corelib):
corelib = os.path.join(_tuvx_install_dir(libroot), "lib", "libtuvx.a")
expect(os.path.exists(corelib), f"TUV-x library not found at {corelib}")
return corelib
###############################################################################
def _tuvx_install_dir(libroot):
###############################################################################
# Returns the path to the TUV-x install directory
corepaths = glob(os.path.join(libroot, "tuvx*"))
expect(len(corepaths)>0, f"TUV-x not found at {libroot}")
expect(len(corepaths)<2, f"Multiple TUV-x versions found at {libroot}")
expect(os.path.exists(corepaths[0]), f"TUV-x install directory not found at {corepaths[0]}")
return corepaths[0]
###############################################################################
def _main_func():
caseroot, libroot, bldroot = parse_input(sys.argv)
_build_tuvx(caseroot, libroot, bldroot)
_build_fms(caseroot, libroot, bldroot)
_build_cam(caseroot, libroot, bldroot)
###############################################################################
if __name__ == "__main__":
_main_func()