Skip to content

Commit

Permalink
Patch mom_cap.F90 source file on-the-fly instead of adding a full cop…
Browse files Browse the repository at this point in the history
…y of the patched file.
  • Loading branch information
micaeljtoliveira committed Aug 31, 2023
1 parent a22dbee commit 6570fef
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 2,748 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ project(ACCESS-OM3

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(${CMAKE_SOURCE_DIR}/cmake/FortranLib.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/AddPatchedSource.cmake)

# Configurations to build
list(APPEND KNOWN_CONFIGURATIONS MOM6 CICE6 WW3 MOM6-CICE6 CICE6-WW3 MOM6-WW3 MOM6-CICE6-WW3)
Expand Down
1 change: 1 addition & 0 deletions MOM6/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ target_link_libraries(mom6 PUBLIC share
FMS::fms_r8
esmf
NetCDF::NetCDF_Fortran)
add_patched_source(mom6 MOM6/config_src/drivers/nuopc_cap/mom_cap.F90)
2,747 changes: 0 additions & 2,747 deletions MOM6/extra_sources/mom_cap.F90

This file was deleted.

1 change: 0 additions & 1 deletion MOM6/mom6_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ list(APPEND mom6_src_files
MOM6/config_src/infra/FMS1/MOM_io_infra.F90
MOM6/config_src/infra/FMS1/MOM_time_manager.F90

extra_sources/mom_cap.F90
MOM6/config_src/drivers/nuopc_cap/ocn_comp_NUOPC.F90
MOM6/config_src/drivers/nuopc_cap/mom_cap_methods.F90
MOM6/config_src/drivers/nuopc_cap/mom_cap_time.F90
Expand Down
11 changes: 11 additions & 0 deletions MOM6/patches/mom_cap.F90.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- MOM6/config_src/drivers/nuopc_cap/mom_cap.F90 2023-08-31 10:49:33.115826000 +1000
+++ MOM6/config_src/drivers/nuopc_cap/mom_cap.F90.new 2023-08-31 10:50:34.446781000 +1000
@@ -1672,7 +1672,7 @@
! determine restart filename
call ESMF_ClockGetNextTime(clock, MyTime, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
- call ESMF_TimeGet (MyTime, yy=year, mm=month, dd=day, h=hour, m=minute, s=seconds, rc=rc )
+ call ESMF_TimeGet (MyTime, yy=year, mm=month, dd=day, s=seconds, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return

if (cesm_coupled) then
22 changes: 22 additions & 0 deletions cmake/AddPatchedSource.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Apply a patch to a source file and add the resulting file to the sources of a
# given target.
function(add_patched_source TARGET FILE)
# This function makes the following assumptions:
# - patches are stored in ${CMAKE_CURRENT_SOURCE_DIR}/patches
# - all patches to a given file are kept in the same patch file
# - patch files are named ${FILE}.patch

get_filename_component(filename ${FILE} NAME)

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${filename}
COMMAND ${CMAKE_COMMAND}
-Din_file:FILEPATH=${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
-Dpatch_file:FILEPATH=${CMAKE_CURRENT_SOURCE_DIR}/patches/${filename}.patch
-Dout_file:FILEPATH=${CMAKE_CURRENT_BINARY_DIR}/${filename}
-P ${CMAKE_SOURCE_DIR}/cmake/PatchFile.cmake
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
)

target_sources(${TARGET} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${filename})
endfunction(add_patched_source)
30 changes: 30 additions & 0 deletions cmake/PatchFile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# use GNU Patch from any platform

if(WIN32)
# prioritize Git Patch on Windows as other Patches may be very old and incompatible.
find_package(Git)
if(Git_FOUND)
get_filename_component(GIT_DIR ${GIT_EXECUTABLE} DIRECTORY)
get_filename_component(GIT_DIR ${GIT_DIR} DIRECTORY)
endif()
endif()

find_program(PATCH
NAMES patch
HINTS ${GIT_DIR}
PATH_SUFFIXES usr/bin
)

if(NOT PATCH)
message(FATAL_ERROR "Did not find GNU Patch")
endif()

execute_process(COMMAND ${PATCH} ${in_file} --input=${patch_file} --output=${out_file} --ignore-whitespace
TIMEOUT 15
COMMAND_ECHO STDOUT
RESULT_VARIABLE ret
)

if(NOT ret EQUAL 0)
message(FATAL_ERROR "Failed to apply patch ${patch_file} to ${in_file} with ${PATCH}")
endif()

0 comments on commit 6570fef

Please sign in to comment.