Skip to content

Commit

Permalink
Improved generation location and updated install step (#354)
Browse files Browse the repository at this point in the history
* Moved all generated header files to <build_dir>/headers and included them in the install step
* Removed cfloat.h from rte_types.h
  • Loading branch information
makepath-alex authored Feb 12, 2025
1 parent b556481 commit 0c51fa2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
12 changes: 9 additions & 3 deletions cbind_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def fortran_to_c(fortran_code):
subroutine_pattern = re.compile(
r"subroutine\s+(\w+)\s*" r"\((.*?)\)\s*&?\s*" r'bind\(C,\s*name="(.*?)"\)',
r"subroutine\s+(\w+)\s*" r"\((.*?)\)\s*&?\s*" r'bind\s*\(C,\s*name="(.*?)"\)',
re.DOTALL | re.IGNORECASE,
)
var_pattern = re.compile(
Expand Down Expand Up @@ -89,6 +89,12 @@ def fortran_to_c(fortran_code):

def extract_between(text, start_str, end_str):
start = text.find(start_str)
endOfStart = start + len(start_str)

# Error checking for function with same preffix-name
if start != -1 and not (text[endOfStart] == " " or text[endOfStart] == "("):
start = text.find(start_str, endOfStart)

if start == -1:
return None
end = text.find(end_str, start + len(start_str))
Expand Down Expand Up @@ -116,10 +122,10 @@ def extract_cbinds(fortran_code):
)

if subroutine_code:
res = subroutine_code.find("bind(C, name=")
res = re.search(r'bind\s*\(\s*C\s*,\s*name\s*=\s*"', subroutine_code)

# Skip subroutine if not binded
if res == -1:
if not res:
continue
else:
print("Invalid subroutine declaration! exiting...")
Expand Down
17 changes: 9 additions & 8 deletions rrtmgp-kernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ if(BUILD_C_HEADERS)
get_target_property(RRTMGPKERNELS_SOURCES rrtmgpkernels SOURCES)

if(RRTMGPKERNELS_SOURCES)
set(RRTMGP_HEADER ${CMAKE_BINARY_DIR}/headers/rrtmgp_kernels.h)
# Convert sources to absolute paths
set(ABSOLUTE_RRTMGPKERNELS_SOURCES "")

Expand All @@ -53,32 +54,32 @@ if(BUILD_C_HEADERS)
endforeach()

add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/api/rrtmgp_kernels.h
OUTPUT ${RRTMGP_HEADER}
COMMAND
${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cbind_generator.py -if
${ABSOLUTE_RRTMGPKERNELS_SOURCES} -of
${CMAKE_CURRENT_SOURCE_DIR}/api/rrtmgp_kernels.h
${ABSOLUTE_RRTMGPKERNELS_SOURCES} -of ${RRTMGP_HEADER}
COMMENT "Generating RRTMGPKernels' C bindings header"
DEPENDS ${ABSOLUTE_RRTMGPKERNELS_SOURCES}
)

add_custom_target(
rrtmgpkernels_header
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/api/rrtmgp_kernels.h
DEPENDS ${RRTMGP_HEADER}
COMMENT
"Ensure rrtmgpkernels_header is built before rrtmgpkernels can use its output"
)
# Ensure that rrtmgpkernels depends on rrtmgpkernels_header
add_dependencies(rrtmgpkernels rrtmgpkernels_header)

if(KERNEL_MODE STREQUAL "extern")
# Ensure that rrtmgpkernels depends on rrtmgpkernels_header
add_dependencies(rrtmgpkernels rrtmgpkernels_header)

target_sources(
rrtmgpkernels
PRIVATE # cmake-format: sort
api/rrtmgp_kernels.h
${RRTMGP_HEADER}
)
endif()

install(FILES ${RRTMGP_HEADER} TYPE INCLUDE)
else()
message(WARNING "No sources found for rrtmgpkernels!")
endif()
Expand Down
23 changes: 13 additions & 10 deletions rte-kernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ target_include_directories(rtekernels PRIVATE ${CMAKE_Fortran_MODULE_DIRECTORY})
if(BUILD_C_HEADERS)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/api/rte_types.h.in
${CMAKE_CURRENT_SOURCE_DIR}/api/rte_types.h
${CMAKE_BINARY_DIR}/headers/rte_types.h
@ONLY
)

get_target_property(RTEKERNELS_SOURCES rtekernels SOURCES)

if(RTEKERNELS_SOURCES)
set(RTE_HEADER ${CMAKE_BINARY_DIR}/headers/rte_kernels.h)
# Convert sources to absolute paths
set(ABSOLUTE_RTEKERNELS_SOURCES "")

Expand All @@ -71,34 +72,36 @@ if(BUILD_C_HEADERS)
endforeach()

add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/api/rte_kernels.h
OUTPUT ${RTE_HEADER}
COMMAND
${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cbind_generator.py -if
${ABSOLUTE_RTEKERNELS_SOURCES} -of
${CMAKE_CURRENT_SOURCE_DIR}/api/rte_kernels.h
${ABSOLUTE_RTEKERNELS_SOURCES} -of ${RTE_HEADER}
COMMENT "Generating RTEKernels' C bindings header"
DEPENDS ${ABSOLUTE_RTEKERNELS_SOURCES}
)

add_custom_target(
rtekernels_header
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/api/rte_kernels.h
DEPENDS ${RTE_HEADER}
COMMENT
"Ensure rtekernels_header is built before rtekernels can use its output"
)
# Ensure that rtekernels depends on rtekernels_header
add_dependencies(rtekernels rtekernels_header)

if(KERNEL_MODE STREQUAL "extern")
# Ensure that rtekernels depends on rtekernels_header
add_dependencies(rtekernels rtekernels_header)

target_sources(
rtekernels
PRIVATE # cmake-format: sort
api/rte_kernels.h
api/rte_types.h
${CMAKE_BINARY_DIR}/headers/rte_types.h
${RTE_HEADER}
)
endif()

install(FILES ${RTE_HEADER} TYPE INCLUDE)
else()
message(WARNING "No sources found for rtekernels!")
endif()

install(FILES ${CMAKE_BINARY_DIR}/headers/rte_types.h TYPE INCLUDE)
endif()
1 change: 0 additions & 1 deletion rte-kernels/api/rte_types.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ This `.h` file defines C-compatible Boolean and floating-point types.
*/
#pragma once

#include <cfloat.h> // Ensure FLT_EPSILON and DBL_EPSILON are available
#include <stdbool.h>

// Define a C-compatible Boolean type
Expand Down
12 changes: 6 additions & 6 deletions rte-kernels/mo_rte_solver_kernels.F90
Original file line number Diff line number Diff line change
Expand Up @@ -922,14 +922,14 @@ subroutine lw_source_2str(ncol, nlay, top_at_1, &
integer, intent(in) :: ncol, nlay
logical(wl), intent(in) :: top_at_1
real(wp), dimension(ncol ), intent(in) :: sfc_emis, sfc_src
real(wp), dimension(ncol, nlay), intent(in) :: lay_source, & ! Planck source at layer center
tau, & ! Optical depth (tau)
gamma1, gamma2,& ! Coupling coefficients
rdif, tdif ! Layer reflectance and transmittance
real(wp), dimension(ncol, nlay), intent(in) :: lay_source ! Planck source at layer center
real(wp), dimension(ncol, nlay), intent(in) :: tau ! Optical depth (tau)
real(wp), dimension(ncol, nlay), intent(in) :: gamma1, gamma2 ! Coupling coefficients
real(wp), dimension(ncol, nlay), intent(in) :: rdif, tdif ! Layer reflectance and transmittance
real(wp), dimension(ncol, nlay+1), target, &
intent(in) :: lev_source ! Planck source at layer edges
intent(in) :: lev_source ! Planck source at layer edges
real(wp), dimension(ncol, nlay), intent(out) :: source_dn, source_up
real(wp), dimension(ncol ), intent(out) :: source_sfc ! Source function for upward radation at surface
real(wp), dimension(ncol ), intent(out) :: source_sfc ! Source function for upward radation at surface

integer :: icol, ilay
real(wp) :: Z, Zup_top, Zup_bottom, Zdn_top, Zdn_bottom
Expand Down

0 comments on commit 0c51fa2

Please sign in to comment.