Skip to content

Commit 7e02d90

Browse files
committed
update docker to build case
1 parent 7fe10ee commit 7e02d90

File tree

3 files changed

+106
-131
lines changed

3 files changed

+106
-131
lines changed

cime_config/atm_musica_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
The URLs and tags provided in this script are read by buildlib.py to build
2+
The URLs and tags provided in this script are read by buildlib to build
33
and install the MUSICA library, as well as to download the MUSICA configuration."
44
"""
55

66
musica_repo_url = "https://github.com/NCAR/musica.git"
7-
musica_tag = "e0c3d219fc8bbfd388cd3c95a3646d2e3aa1e288" # main
7+
musica_tag = "802bc9041f964a7edd5ec896bec8cdc0c90de50b"
88
chemistry_data_repo_url = "https://github.com/NCAR/cam-sima-chemistry-data.git"
99
chemistry_data_tag = "b81cbc2e61c41ecd2d09167d6736daf1bf1be149"

cime_config/buildlib

Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _build_cam():
7979
config.generate_cam_src(gen_indent)
8080

8181
dycore = config.get_value('dyn')
82-
physics_suites = config.get_value('physics_suites')
82+
phys_suites = config.get_value('physics_suites')
8383
reg_dir = config.get_value('reg_dir')
8484
init_dir = config.get_value('init_dir')
8585
phys_dirs_str = config.get_value('phys_dirs')
@@ -125,6 +125,7 @@ def _build_cam():
125125
paths.append(os.path.join(atm_root, "src", "dynamics", "tests",
126126
"initial_conditions"))
127127

128+
# TODO(jiwon); this path is not found
128129
# If using the CMEPS/NUOPC coupler, then add additional path:
129130
if case.get_value("COMP_INTERFACE") == "nuopc":
130131
paths.append(os.path.join(__CIMEROOT, "src", "drivers",
@@ -135,7 +136,7 @@ def _build_cam():
135136
with open(filepath_src, "w", encoding='utf-8') as filepath:
136137
filepath.write("\n".join(paths))
137138
filepath.write("\n")
138-
# End with
139+
# End if
139140

140141
# Move Filepath to the bld directory unless it has not changed
141142
if os.path.isfile(filepath_dst):
@@ -154,17 +155,25 @@ def _build_cam():
154155
if dycore == "mpas":
155156
_setup_mpas(case)
156157

157-
if physics_suites == "musica":
158+
# If the physics suite is MUSICA, build the MUSICA library and configuration
159+
if phys_suites == "musica":
158160
_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)
163162

164163
complib = os.path.join(libroot, "libatm.a")
165164
makefile = os.path.join(casetools, "Makefile")
166165

167166
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+
168177
cmd += f" -f {makefile} {get_standard_makefile_args(case)} "
169178

170179
# Add C Pre-Processor (CPP) definitions, if present:
@@ -175,26 +184,10 @@ def _build_cam():
175184
retcode, out, err = run_cmd(cmd)
176185
_LOGGER.info("%s: \n\n output:\n %s \n\n err:\n\n%s\n", cmd, out, err)
177186
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}")
196187

188+
###############################################################################
197189
def _setup_mpas(case: Case) -> None:
190+
###############################################################################
198191
"""
199192
Setup MPAS build infrastructure.
200193
"""
@@ -222,7 +215,9 @@ def _setup_mpas(case: Case) -> None:
222215
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)
223216
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)
224217

218+
###############################################################################
225219
def _copy2_as_needed(src: str, dst: str) -> None:
220+
###############################################################################
226221
"""
227222
Wrapper around `shutil.copy2`. Copy the file `src` to the file or directory `dst` as needed.
228223
"""
@@ -242,25 +237,34 @@ def _copy2_as_needed(src: str, dst: str) -> None:
242237
# Example scenario: User added some new source code files.
243238
shutil.copy2(src, dst)
244239

240+
###############################################################################
245241
def _build_musica_library(case: Case) -> None:
242+
###############################################################################
243+
"""
244+
Builds and installs the MUSICA library.
245+
246+
Args:
247+
case (Case)
246248
249+
Raises:
250+
Exception: If configuring the CMake MUSICA project fails or
251+
the MUSICA library build fails, an exception is raised.
252+
253+
"""
247254
install_dir_name = "install"
248255
build_type = "Release"
256+
caseroot = os.path.normpath(case.get_value("CASEROOT"))
257+
_clone_and_checkout(musica_repo_url, musica_tag, caseroot)
249258

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")
255260
if os.path.exists(bld_path):
256-
print(f"Removing existing build directory: {bld_path}")
257261
shutil.rmtree(bld_path)
258-
259262
os.makedirs(bld_path)
263+
264+
# To install the target, the working directory must be the build directory.
265+
current_dir = os.getcwd()
260266
os.chdir(bld_path)
261267

262-
# Run cmake to configure the project
263-
print("Configuring the project with CMake...")
264268
command = [
265269
"cmake",
266270
f"-DCMAKE_INSTALL_PREFIX={install_dir_name}",
@@ -269,92 +273,84 @@ def _build_musica_library(case: Case) -> None:
269273
f"-DMUSICA_BUILD_FORTRAN_INTERFACE=ON",
270274
".."
271275
]
272-
print(f"Running command: {' '.join(command)}")
273276
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
274277
if result.returncode != 0:
275278
raise Exception(f"Unable to configure the MUSICA CMake project. Error: {result.stderr}")
276279

277-
# Build the project
278-
print("Building the project...")
279280
command = ["cmake", "--build", ".", "--target", "install"]
280281
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
281282
if result.returncode != 0:
282283
raise Exception(f"Unable to build the MUSICA library. Error: {result.stderr}")
283-
284-
print(f"MUSICA installed to {install_dir_name}")
285284

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")
287287
musica_lib_root = os.path.join(atm_bld_root, "musica", "lib")
288288
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_
327289

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.
328324
"""
329325
musica_config_dir_name = "musica_configurations"
330326

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)
333328

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)
336331
try:
337-
os.rename(original_config_dir, renamed_config_dir)
332+
os.rename(original_dir, renamed_dir)
338333
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.")
340335
except Exception as e:
341336
raise Exception(f"An error occurred while renaming: {e}")
342337

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)
344339
if os.path.exists(musica_config_path):
345340
shutil.rmtree(musica_config_path)
346341

347-
shutil.move(renamed_config_dir, case_root)
348-
342+
shutil.move(renamed_dir, download_dest)
349343

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+
###############################################################################
351347
"""
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.
353349
354350
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.
358354
359355
Raises:
360356
Exception: If the `git clone` or `git checkout` commands fail,
@@ -366,15 +362,19 @@ def _clone_and_checkout(repo_url, tag_name, clone_dest):
366362
if os.path.exists(repo_path):
367363
shutil.rmtree(repo_path)
368364

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)
370367
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}")
372370

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)
374373
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}")
376376

377377
###############################################################################
378378

379379
if __name__ == "__main__":
380-
_build_cam()
380+
_build_cam()

0 commit comments

Comments
 (0)