@@ -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 -lmechanism_configuration -lyaml-cpp -lstdc++"
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,64 @@ 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
+ unlock_file ("env_build.xml" , caseroot )
260
+ build = EnvBuild (case_root = caseroot )
261
+ with Case (caseroot ) as case :
262
+ macro_path = os .path .abspath (os .path .join (caseroot , "cmake_macros" , "" ))
263
+ args = "-DCONVERT_TO_MAKE=ON "
264
+ args += "-DCASEROOT={} " .format (caseroot )
265
+ args += "-DCOMPILER={} " .format (build .get_value ("COMPILER" ))
266
+ args += "-DOS={} " .format (build .get_value ("OS" ))
267
+ args += "-DMACH={} " .format (case .get_value ("MACH" ))
268
+ args += "-DCMAKE_C_COMPILER_WORKS=1 "
269
+ args += "-DCMAKE_Fortran_COMPILER_WORKS=1 "
270
+ args += "-DCMAKE_CXX_COMPILER_WORKS=1 "
271
+ args += "-DDEBUG={} " .format (build .get_value ("DEBUG" ))
272
+ cmd = "cmake {} ." .format (args )
273
+ rc , out , err = run_cmd (cmd , combine_output = True , from_dir = macro_path )
274
+ expect (rc == 0 , "Command {} failed with rc={} out={} err={}" .format (cmd , rc , out , err ))
275
+
276
+ arg_dict = {}
277
+ for line in out .splitlines ():
278
+ if ":=" in line :
279
+ key , val = line .split (":=" )
280
+ arg_dict [key .replace ('CIME_SET_MAKEFILE_VAR' ,'' ).strip ()] = val .strip ()
281
+
282
+ unlock_file ("env_build.xml" , caseroot )
283
+ return arg_dict
284
+
285
+ ###############################################################################
286
+ def _get_musica_libs (caseroot : str ) -> str :
287
+ ###############################################################################
288
+ """
289
+ Returns the MUSICA libraries to be linked to CAM.
290
+
291
+ Args:
292
+ caseroot: CASEROOT where the xmlchange command is located
293
+
294
+ Raises:
295
+ Exception: If the subprocess for the xmlquery command fails,
296
+ an exception is raised with the error message.
297
+
298
+ Returns:
299
+ musica_libs: MUSICA libraries to be linked to CAM
300
+ """
301
+ unlock_file ("env_build.xml" , caseroot )
302
+ build = EnvBuild (case_root = caseroot )
303
+ if build .get_value ("DEBUG" ):
304
+ musica_libs = "-lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cppd -lstdc++"
305
+ else :
306
+ musica_libs = "-lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cpp -lstdc++"
307
+ lock_file ("env_build.xml" , caseroot )
308
+
309
+ return musica_libs
310
+
252
311
###############################################################################
253
312
def _build_musica (clone_dest : str ) -> str :
254
313
###############################################################################
@@ -266,20 +325,29 @@ def _build_musica(clone_dest: str) -> str:
266
325
musica_install_path: path to the MUSICA installation directory
267
326
"""
268
327
_clone_and_checkout (MUSICA_REPO_URL , MUSICA_TAG , clone_dest )
328
+ build = EnvBuild (case_root = clone_dest )
329
+ arg_dict = _cmake_default_args (clone_dest )
269
330
270
331
bld_path = os .path .join (clone_dest , "musica" , "build" )
271
332
if os .path .exists (bld_path ):
272
333
shutil .rmtree (bld_path )
273
334
os .makedirs (bld_path )
274
-
275
335
install_dir = "install"
336
+
337
+ with Case (clone_dest ) as case :
338
+ if build .get_value ("DEBUG" ):
339
+ cmake_build_type = "Debug"
340
+ else :
341
+ cmake_build_type = "Release"
342
+
276
343
command = [
277
344
"cmake" ,
278
345
f"-D CMAKE_INSTALL_PREFIX={ install_dir } " ,
279
- "-D CMAKE_BUILD_TYPE=Release" ,
346
+ f"-D CMAKE_BUILD_TYPE={ cmake_build_type } " ,
347
+ f"-D CMAKE_Fotran_FLAGS={ arg_dict ['FFLAGS' ]} " ,
280
348
"-D MUSICA_ENABLE_TESTS=OFF" ,
281
349
"-D MUSICA_BUILD_FORTRAN_INTERFACE=ON" ,
282
- "-D MICM_DEFAULT_VECTOR_SIZE =128" ,
350
+ "-D MUSICA_SET_MICM_DEFAULT_VECTOR_SIZE =128" ,
283
351
".."
284
352
]
285
353
try :
@@ -366,14 +434,14 @@ def _set_musica_lib_path(musica_install_path: str, caseroot: str) -> None:
366
434
an exception is raised with the error message.
367
435
"""
368
436
369
- unlock_file ( "env_build.xml" , caseroot )
437
+ musica_libs = _get_musica_libs ( caseroot )
370
438
371
439
command = [
372
440
"./xmlchange" ,
373
441
"--append" ,
374
442
# The libraries must be on the same line because CIME flags an
375
443
# error for multi-character arguments preceded by a single dash
376
- f"CAM_LINKED_LIBS=-L{ os .path .join (musica_install_path , 'lib64' )} -lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cpp -lstdc++ "
444
+ f"CAM_LINKED_LIBS=-L{ os .path .join (musica_install_path , 'lib64' )} { musica_libs } "
377
445
]
378
446
try :
379
447
subprocess .run (command , cwd = caseroot , stdout = subprocess .PIPE ,
@@ -386,8 +454,6 @@ def _set_musica_lib_path(musica_install_path: str, caseroot: str) -> None:
386
454
except OSError as e :
387
455
raise OSError ("An error occurred while executing the 'xmlchange' command." ) from e
388
456
389
- lock_file ("env_build.xml" , caseroot )
390
-
391
457
###############################################################################
392
458
def _clone_and_checkout (repo_url : str , tag_name : str , clone_dest : str ) -> None :
393
459
###############################################################################
0 commit comments