@@ -32,6 +32,7 @@ from CIME.buildlib import parse_input
32
32
from CIME .build import get_standard_makefile_args
33
33
from CIME .Tools .standard_script_setup import check_minimum_python_version
34
34
from CIME .locked_files import lock_file , unlock_file
35
+ from CIME .XML .env_build import EnvBuild
35
36
#pylint: enable=wrong-import-position
36
37
37
38
check_minimum_python_version (3 , 7 ) #CAM requires version 3.7 or greater
@@ -180,7 +181,7 @@ def _build_cam():
180
181
musica_install_path = _build_musica (caseroot )
181
182
182
183
cam_linked_libs = case .get_value ("CAM_LINKED_LIBS" )
183
- musica_libs = "-lmusica-fortran -lmusica -lyaml-cpp"
184
+ musica_libs = _get_musica_libs ( caseroot )
184
185
if not musica_libs in cam_linked_libs :
185
186
_set_musica_lib_path (musica_install_path , caseroot )
186
187
@@ -249,6 +250,60 @@ def _copy2_as_needed(src: str, dst: str) -> None:
249
250
# Example scenario: User added some new source code files.
250
251
shutil .copy2 (src , dst )
251
252
253
+ ###############################################################################
254
+ def _cmake_default_args (caseroot ):
255
+ ###############################################################################
256
+ # Returns a dictionary of CMake variables based on the Macros.cmake file for
257
+ # the build.
258
+
259
+ build = EnvBuild (case_root = caseroot )
260
+ with Case (caseroot ) as case :
261
+ macro_path = os .path .abspath (os .path .join (caseroot , "cmake_macros" , "" ))
262
+ args = "-DCONVERT_TO_MAKE=ON "
263
+ args += f"-DCASEROOT={ caseroot } "
264
+ args += f"-DCOMPILER={ build .get_value ('COMPILER' )} "
265
+ args += f"-DOS={ build .get_value ('OS' )} "
266
+ args += f"-DMACH={ case .get_value ('MACH' )} "
267
+ args += "-DCMAKE_C_COMPILER_WORKS=1 "
268
+ args += "-DCMAKE_Fortran_COMPILER_WORKS=1 "
269
+ args += "-DCMAKE_CXX_COMPILER_WORKS=1 "
270
+ args += f"-DDEBUG={ build .get_value ('DEBUG' )} "
271
+ cmd = f"cmake { args } ."
272
+ rc , out , err = run_cmd (cmd , combine_output = True , from_dir = macro_path )
273
+ expect (rc == 0 , "Command {} failed with rc={} out={} err={}" .format (cmd , rc , out , err ))
274
+
275
+ arg_dict = {}
276
+ for line in out .splitlines ():
277
+ if ":=" in line :
278
+ key , val = line .split (":=" )
279
+ arg_dict [key .replace ('CIME_SET_MAKEFILE_VAR' ,'' ).strip ()] = val .strip ()
280
+
281
+ return arg_dict
282
+
283
+ ###############################################################################
284
+ def _get_musica_libs (caseroot : str ) -> str :
285
+ ###############################################################################
286
+ """
287
+ Returns the MUSICA libraries to be linked to CAM.
288
+
289
+ Args:
290
+ caseroot: CASEROOT where the xmlchange command is located
291
+
292
+ Raises:
293
+ Exception: If the subprocess for the xmlquery command fails,
294
+ an exception is raised with the error message.
295
+
296
+ Returns:
297
+ musica_libs: MUSICA libraries to be linked to CAM
298
+ """
299
+ build = EnvBuild (case_root = caseroot )
300
+ if build .get_value ("DEBUG" ):
301
+ musica_libs = "-lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cppd -lstdc++"
302
+ else :
303
+ musica_libs = "-lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cpp -lstdc++"
304
+
305
+ return musica_libs
306
+
252
307
###############################################################################
253
308
def _build_musica (clone_dest : str ) -> str :
254
309
###############################################################################
@@ -266,19 +321,29 @@ def _build_musica(clone_dest: str) -> str:
266
321
musica_install_path: path to the MUSICA installation directory
267
322
"""
268
323
_clone_and_checkout (MUSICA_REPO_URL , MUSICA_TAG , clone_dest )
324
+ build = EnvBuild (case_root = clone_dest )
325
+ arg_dict = _cmake_default_args (clone_dest )
269
326
270
327
bld_path = os .path .join (clone_dest , "musica" , "build" )
271
328
if os .path .exists (bld_path ):
272
329
shutil .rmtree (bld_path )
273
330
os .makedirs (bld_path )
274
-
275
331
install_dir = "install"
332
+
333
+ with Case (clone_dest ) as case :
334
+ if build .get_value ("DEBUG" ):
335
+ cmake_build_type = "Debug"
336
+ else :
337
+ cmake_build_type = "Release"
338
+
276
339
command = [
277
340
"cmake" ,
278
341
f"-D CMAKE_INSTALL_PREFIX={ install_dir } " ,
279
- "-D CMAKE_BUILD_TYPE=Release" ,
342
+ f"-D CMAKE_BUILD_TYPE={ cmake_build_type } " ,
343
+ f"-D CMAKE_Fotran_FLAGS={ arg_dict ['FFLAGS' ]} " ,
280
344
"-D MUSICA_ENABLE_TESTS=OFF" ,
281
345
"-D MUSICA_BUILD_FORTRAN_INTERFACE=ON" ,
346
+ "-D MUSICA_SET_MICM_DEFAULT_VECTOR_SIZE=128" ,
282
347
".."
283
348
]
284
349
try :
@@ -365,14 +430,14 @@ def _set_musica_lib_path(musica_install_path: str, caseroot: str) -> None:
365
430
an exception is raised with the error message.
366
431
"""
367
432
368
- unlock_file ( "env_build.xml" , caseroot )
433
+ musica_libs = _get_musica_libs ( caseroot )
369
434
370
435
command = [
371
436
"./xmlchange" ,
372
437
"--append" ,
373
438
# The libraries must be on the same line because CIME flags an
374
439
# error for multi-character arguments preceded by a single dash
375
- f"CAM_LINKED_LIBS=-L{ os .path .join (musica_install_path , 'lib64' )} -lmusica-fortran -lmusica -lyaml-cpp "
440
+ f"CAM_LINKED_LIBS=-L{ os .path .join (musica_install_path , 'lib64' )} { musica_libs } "
376
441
]
377
442
try :
378
443
subprocess .run (command , cwd = caseroot , stdout = subprocess .PIPE ,
@@ -385,8 +450,6 @@ def _set_musica_lib_path(musica_install_path: str, caseroot: str) -> None:
385
450
except OSError as e :
386
451
raise OSError ("An error occurred while executing the 'xmlchange' command." ) from e
387
452
388
- lock_file ("env_build.xml" , caseroot )
389
-
390
453
###############################################################################
391
454
def _clone_and_checkout (repo_url : str , tag_name : str , clone_dest : str ) -> None :
392
455
###############################################################################
0 commit comments