@@ -79,7 +79,7 @@ def _build_cam():
79
79
config .generate_cam_src (gen_indent )
80
80
81
81
dycore = config .get_value ('dyn' )
82
- physics_suites = config .get_value ('physics_suites' )
82
+ phys_suites = config .get_value ('physics_suites' )
83
83
reg_dir = config .get_value ('reg_dir' )
84
84
init_dir = config .get_value ('init_dir' )
85
85
phys_dirs_str = config .get_value ('phys_dirs' )
@@ -125,6 +125,7 @@ def _build_cam():
125
125
paths .append (os .path .join (atm_root , "src" , "dynamics" , "tests" ,
126
126
"initial_conditions" ))
127
127
128
+ # TODO(jiwon); this path is not found
128
129
# If using the CMEPS/NUOPC coupler, then add additional path:
129
130
if case .get_value ("COMP_INTERFACE" ) == "nuopc" :
130
131
paths .append (os .path .join (__CIMEROOT , "src" , "drivers" ,
@@ -135,7 +136,7 @@ def _build_cam():
135
136
with open (filepath_src , "w" , encoding = 'utf-8' ) as filepath :
136
137
filepath .write ("\n " .join (paths ))
137
138
filepath .write ("\n " )
138
- # End with
139
+ # End if
139
140
140
141
# Move Filepath to the bld directory unless it has not changed
141
142
if os .path .isfile (filepath_dst ):
@@ -154,17 +155,25 @@ def _build_cam():
154
155
if dycore == "mpas" :
155
156
_setup_mpas (case )
156
157
157
- if physics_suites == "musica" :
158
+ # If the physics suite is MUSICA, build the MUSICA library and configuration
159
+ if phys_suites == "musica" :
158
160
_build_musica_library (case )
159
- _download_musica_configuration (case )
160
- # clone_dest = os.path.normpath(case.get_value("CASEROOT"))
161
- # bld_path = os.path.normpath(os.path.join(clone_dest, "musica", "build", "install", "include"))
162
- # paths.append(bld_path)
161
+ _download_musica_configuration (caseroot )
163
162
164
163
complib = os .path .join (libroot , "libatm.a" )
165
164
makefile = os .path .join (casetools , "Makefile" )
166
165
167
166
cmd = f"{ gmake } complib -j { gmake_j } MODEL=cam COMPLIB={ complib } "
167
+
168
+ if phys_suites == "musica" :
169
+ cmd += ' USER_INCLDIR="' \
170
+ f'-I{ os .path .join (bldroot , "musica" , "include" , "micm" )} ' \
171
+ f'-I{ os .path .join (bldroot , "musica" , "include" , "musica" )} ' \
172
+ f'-I{ os .path .join (bldroot , "musica" , "include" , "musica" , "micm" )} ' \
173
+ f'-I{ os .path .join (bldroot , "musica" , "include" , "musica" , "tuvx" )} ' \
174
+ f'-I{ os .path .join (bldroot , "musica" , "include" , "musica" , "fortran" )} ' \
175
+ '"'
176
+
168
177
cmd += f" -f { makefile } { get_standard_makefile_args (case )} "
169
178
170
179
# Add C Pre-Processor (CPP) definitions, if present:
@@ -175,26 +184,10 @@ def _build_cam():
175
184
retcode , out , err = run_cmd (cmd )
176
185
_LOGGER .info ("%s: \n \n output:\n %s \n \n err:\n \n %s\n " , cmd , out , err )
177
186
expect (retcode == 0 , f"Command { cmd } failed with rc={ retcode } " )
178
-
179
-
180
- # JIWON
181
- if physics_suites == "musica" :
182
- clone_dest = os .path .normpath (case .get_value ("CASEROOT" ))
183
- # bld_path = os.path.normpath(os.path.join(clone_dest, "musica", "build", "install", "include"))
184
- bld_path = os .path .normpath (os .path .join (case .get_value ("EXEROOT" ), "atm" , "obj" , "musica" , "include" ))
185
- cmd = f"{ gmake } complib -j { gmake_j } MODEL=cam USER_INCLDIR={ bld_path } "
186
- cmd += f" -f { makefile } { get_standard_makefile_args (case )} "
187
-
188
- # Add C Pre-Processor (CPP) definitions, if present:
189
- if config .cpp_defs :
190
- ccpp_defs_str = ' ' .join (config .cpp_defs )
191
- cmd += f" USER_CPPDEFS='{ ccpp_defs_str } '"
192
-
193
- retcode , out , err = run_cmd (cmd )
194
- _LOGGER .info ("%s: \n \n output:\n %s \n \n err:\n \n %s\n " , cmd , out , err )
195
- expect (retcode == 0 , f"Command { cmd } failed with rc={ retcode } " )
196
187
188
+ ###############################################################################
197
189
def _setup_mpas (case : Case ) -> None :
190
+ ###############################################################################
198
191
"""
199
192
Setup MPAS build infrastructure.
200
193
"""
@@ -222,7 +215,9 @@ def _setup_mpas(case: Case) -> None:
222
215
shutil .copytree (os .path .normpath (os .path .join (mpas_dycore_src_root , os .pardir , os .pardir , "assets" )), mpas_dycore_bld_root , copy_function = _copy2_as_needed , dirs_exist_ok = True )
223
216
shutil .copytree (os .path .normpath (os .path .join (mpas_dycore_src_root , os .pardir , os .pardir , "driver" )), os .path .join (mpas_dycore_bld_root , "driver" ), copy_function = _copy2_as_needed , dirs_exist_ok = True )
224
217
218
+ ###############################################################################
225
219
def _copy2_as_needed (src : str , dst : str ) -> None :
220
+ ###############################################################################
226
221
"""
227
222
Wrapper around `shutil.copy2`. Copy the file `src` to the file or directory `dst` as needed.
228
223
"""
@@ -242,25 +237,34 @@ def _copy2_as_needed(src: str, dst: str) -> None:
242
237
# Example scenario: User added some new source code files.
243
238
shutil .copy2 (src , dst )
244
239
240
+ ###############################################################################
245
241
def _build_musica_library (case : Case ) -> None :
242
+ ###############################################################################
243
+ """
244
+ Builds and installs the MUSICA library.
245
+
246
+ Args:
247
+ case (Case)
246
248
249
+ Raises:
250
+ Exception: If configuring the CMake MUSICA project fails or
251
+ the MUSICA library build fails, an exception is raised.
252
+
253
+ """
247
254
install_dir_name = "install"
248
255
build_type = "Release"
256
+ caseroot = os .path .normpath (case .get_value ("CASEROOT" ))
257
+ _clone_and_checkout (musica_repo_url , musica_tag , caseroot )
249
258
250
- clone_dest = os .path .normpath (case .get_value ("CASEROOT" ))
251
- _clone_and_checkout (musica_repo_url , musica_tag , clone_dest )
252
-
253
- bld_path = os .path .normpath (os .path .join (clone_dest , "musica" , "build" ))
254
- # Ensure the build directory is clean
259
+ bld_path = os .path .join (caseroot , "musica" , "build" )
255
260
if os .path .exists (bld_path ):
256
- print (f"Removing existing build directory: { bld_path } " )
257
261
shutil .rmtree (bld_path )
258
-
259
262
os .makedirs (bld_path )
263
+
264
+ # To install the target, the working directory must be the build directory.
265
+ current_dir = os .getcwd ()
260
266
os .chdir (bld_path )
261
267
262
- # Run cmake to configure the project
263
- print ("Configuring the project with CMake..." )
264
268
command = [
265
269
"cmake" ,
266
270
f"-DCMAKE_INSTALL_PREFIX={ install_dir_name } " ,
@@ -269,92 +273,84 @@ def _build_musica_library(case: Case) -> None:
269
273
f"-DMUSICA_BUILD_FORTRAN_INTERFACE=ON" ,
270
274
".."
271
275
]
272
- print (f"Running command: { ' ' .join (command )} " )
273
276
result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
274
277
if result .returncode != 0 :
275
278
raise Exception (f"Unable to configure the MUSICA CMake project. Error: { result .stderr } " )
276
279
277
- # Build the project
278
- print ("Building the project..." )
279
280
command = ["cmake" , "--build" , "." , "--target" , "install" ]
280
281
result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
281
282
if result .returncode != 0 :
282
283
raise Exception (f"Unable to build the MUSICA library. Error: { result .stderr } " )
283
-
284
- print (f"MUSICA installed to { install_dir_name } " )
285
284
286
- atm_bld_root = os .path .normpath (os .path .join (case .get_value ("EXEROOT" ), "atm" , "obj" ))
285
+ # Create symlinks pointing to the MUSICA libraries in the ATM build directory
286
+ atm_bld_root = os .path .join (case .get_value ("EXEROOT" ), "atm" , "obj" )
287
287
musica_lib_root = os .path .join (atm_bld_root , "musica" , "lib" )
288
288
os .makedirs (musica_lib_root , exist_ok = True )
289
- musica_install_path = os .path .join (bld_path , install_dir_name , "lib64" )
290
-
291
- root , _ , files = next (os .walk (musica_install_path , topdown = True ))
292
- for f in files :
293
- symlink_force (os .path .join (root , f ), os .path .join (musica_lib_root , f ))
294
-
295
- src_dir = os .path .join (bld_path , install_dir_name , "include" )
296
- dest_dir = os .path .join (atm_bld_root , "musica" , "include" )
297
- # os.makedirs(dest_dir, exist_ok=True)
298
- # Walk through the source directory
299
- # for root, dirs, files in os.walk(src_dir):
300
- # # Calculate the relative path from the source directory
301
- # rel_path = os.path.relpath(root, src_dir)
302
- # # Define the corresponding path in the destination
303
- # dest_root = os.path.join(dest_dir, rel_path)
304
-
305
- # # Create the destination directory structure if it doesn't exist
306
- # os.makedirs(dest_root, exist_ok=True)
307
-
308
- # # Create symlinks for all files in the current directory
309
- # for file in files:
310
- # src_file = os.path.join(root, file)
311
- # dest_file = os.path.join(dest_root, file)
312
-
313
- # # Create a symlink if it doesn't already exist
314
- # if not os.path.exists(dest_file):
315
- # os.symlink(src_file, dest_file)
316
- # try:
317
- # os.symlink(src_file, dest_file)
318
- # print(f"Created symlink: {dest_file} -> {src_file}")
319
- # except OSError as e:
320
- # print(f"Error creating symlink for {src_file}: {e}")
321
- # else:
322
- # print(f"Symlink already exists: {dest_file}")
323
-
324
-
325
- def _download_musica_configuration (case : Case ) -> None :
326
- """_summary_
327
289
290
+ installed_lib_path = os .path .join (bld_path , install_dir_name , "lib64" )
291
+ root , _ , files = next (os .walk (installed_lib_path , topdown = True ))
292
+ for file in files :
293
+ symlink_force (os .path .join (root , file ), os .path .join (musica_lib_root , file ))
294
+
295
+ # Create symlinks pointing to the MUSICA include files in the ATM build directory
296
+ musica_include_root = os .path .join (atm_bld_root , "musica" , "include" )
297
+ os .makedirs (musica_include_root , exist_ok = True )
298
+
299
+ installed_include_path = os .path .join (bld_path , install_dir_name , "include" )
300
+ for root , _ , files in os .walk (installed_include_path ):
301
+ rel_path = os .path .relpath (root , installed_include_path )
302
+ dest_dir = os .path .join (musica_include_root , rel_path )
303
+
304
+ os .makedirs (dest_dir , exist_ok = True )
305
+ for file in files :
306
+ symlink_force (os .path .join (root , file ), os .path .join (dest_dir , file ))
307
+
308
+ os .chdir (current_dir )
309
+
310
+ ###############################################################################
311
+ def _download_musica_configuration (download_dest : str ) -> None :
312
+ ###############################################################################
313
+ """
314
+ Downloads the MUSICA configuration and renames the configuration
315
+ directory to match the name in the MUSICA-CCPP configuration.
316
+
317
+ Args:
318
+ download_dest: destination where configuration will be downloaded.
319
+
320
+ Raises:
321
+ Exception: If the directory to be renamed is not found or
322
+ any other exceptions occur during the renaming process,
323
+ an exception is raised with the error message.
328
324
"""
329
325
musica_config_dir_name = "musica_configurations"
330
326
331
- case_root = os .path .normpath (case .get_value ("CASEROOT" ))
332
- _clone_and_checkout (chemistry_data_repo_url , chemistry_data_tag , case_root )
327
+ _clone_and_checkout (chemistry_data_repo_url , chemistry_data_tag , download_dest )
333
328
334
- original_config_dir = os .path .join (case_root , "cam-sima-chemistry-data" , "mechanisms" )
335
- renamed_config_dir = os .path .join (case_root , "cam-sima-chemistry-data" , musica_config_dir_name )
329
+ original_dir = os .path .join (download_dest , "cam-sima-chemistry-data" , "mechanisms" )
330
+ renamed_dir = os .path .join (download_dest , "cam-sima-chemistry-data" , musica_config_dir_name )
336
331
try :
337
- os .rename (original_config_dir , renamed_config_dir )
332
+ os .rename (original_dir , renamed_dir )
338
333
except FileNotFoundError :
339
- raise Exception (f"The directory { original_config_dir } was not found." )
334
+ raise Exception (f"The directory { original_dir } was not found." )
340
335
except Exception as e :
341
336
raise Exception (f"An error occurred while renaming: { e } " )
342
337
343
- musica_config_path = os .path .join (case_root , musica_config_dir_name )
338
+ musica_config_path = os .path .join (download_dest , musica_config_dir_name )
344
339
if os .path .exists (musica_config_path ):
345
340
shutil .rmtree (musica_config_path )
346
341
347
- shutil .move (renamed_config_dir , case_root )
348
-
342
+ shutil .move (renamed_dir , download_dest )
349
343
350
- def _clone_and_checkout (repo_url , tag_name , clone_dest ):
344
+ ###############################################################################
345
+ def _clone_and_checkout (repo_url : str , tag_name : str , clone_dest : str ):
346
+ ###############################################################################
351
347
"""
352
- Clones a Git repository from the provided URL and checks out a specific branch.
348
+ Clones a Git repository from the URL and checks out a specific branch.
353
349
354
350
Args:
355
- repo_url (str) : The URL of the Git repository to clone
356
- tag_name (str) : The tag name to check out
357
- clone_dest (str) : destination where the repository will be cloned. (absolute path)
351
+ repo_url: The URL of the Git repository to clone
352
+ tag_name: The tag name to check out
353
+ clone_dest: destination where the repository will be cloned.
358
354
359
355
Raises:
360
356
Exception: If the `git clone` or `git checkout` commands fail,
@@ -366,15 +362,19 @@ def _clone_and_checkout(repo_url, tag_name, clone_dest):
366
362
if os .path .exists (repo_path ):
367
363
shutil .rmtree (repo_path )
368
364
369
- result = subprocess .run (["git" , "clone" , repo_url , repo_path ], stderr = subprocess .PIPE , text = True )
365
+ result = subprocess .run (["git" , "clone" , repo_url , repo_path ],
366
+ stderr = subprocess .PIPE , text = True )
370
367
if result .returncode != 0 :
371
- raise Exception (f"Unable to clone the repository: { repo_url } . Error: { result .stderr } " )
368
+ raise Exception (f"Unable to clone the repository: { repo_url } . \
369
+ Error: { result .stderr } " )
372
370
373
- result = subprocess .run (["git" , "-C" , repo_path , "checkout" , tag_name ], stderr = subprocess .PIPE , text = True )
371
+ result = subprocess .run (["git" , "-C" , repo_path , "checkout" , tag_name ],
372
+ stderr = subprocess .PIPE , text = True )
374
373
if result .returncode != 0 :
375
- raise Exception (f"Unable to checkout the branch: { tag_name } . Error: { result .stderr } " )
374
+ raise Exception (f"Unable to checkout the branch: { tag_name } . \
375
+ Error: { result .stderr } " )
376
376
377
377
###############################################################################
378
378
379
379
if __name__ == "__main__" :
380
- _build_cam ()
380
+ _build_cam ()
0 commit comments