Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ lib/*.so
/work_*
Makefile.in
mesh_part/build
test/output_pi
src/cvmix_driver/CVMix-*
test/output_pi
.claude/
81 changes: 23 additions & 58 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,65 +201,30 @@ target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_Fort
target_include_directories(${PROJECT_NAME} PUBLIC $<INSTALL_INTERFACE:module/fesom>)
target_link_libraries(${PROJECT_NAME} PRIVATE MPI::MPI_Fortran)

# build CVMix library if CVMIX is ON
if (CVMIX)
# Pull CVMix in via FetchContent and build it as part of FESOM's build tree.
# Offline workflow:
# - point at a manually placed checkout: -DFETCHCONTENT_SOURCE_DIR_CVMIX=/path/to/CVMix-src
# - or pre-populate <build>/_deps/cvmix-src and pass -DFETCHCONTENT_FULLY_DISCONNECTED=ON
if(CVMIX)
message(STATUS "CVMIX: ${CVMIX}")

# read CVMix install and src directories from the download script, so that there
# is only one place where they are defined
execute_process(COMMAND bash ${src_home}/cvmix_driver/download_cvmix.sh --print-src_dir
OUTPUT_VARIABLE CVMIX_DIR_src OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND bash ${src_home}/cvmix_driver/download_cvmix.sh --print-prefix
OUTPUT_VARIABLE CVMIX_DIR_install OUTPUT_STRIP_TRAILING_WHITESPACE)

# set default CVMix installation path
set(CVMIX_ROOT "${src_home}/cvmix_driver/${CVMIX_DIR_install}_${CMAKE_Fortran_COMPILER_ID}")
message(STATUS "set default CVMIX_ROOT=${CVMIX_ROOT}")

# Where library is located --> fix in download_cvmix.sh that it as always installed
# in /lib and not in /lib64
set(CVMIX_LIB "${CVMIX_ROOT}/lib/libcvmix.a")

# Create parent directory early so CMake does not fail on missing include paths
file(MAKE_DIRECTORY ${CVMIX_ROOT})
file(MAKE_DIRECTORY ${CVMIX_ROOT}/include/cvmix)

# Collect CVMix source files so that CMake knows what changes triggers a
# possible rebuild of the CVMix library
file(GLOB_RECURSE CVMIX_SOURCES
"${src_home}/cvmix_driver/${CVMIX_DIR_src}/src/*.F90"
"${src_home}/cvmix_driver/${CVMIX_DIR_src}/src/*/*.F90")

# Add script dependency (rebuild if script changes)
set(CVMIX_DOWNLOAD "${src_home}/cvmix_driver/download_cvmix.sh")

# Custom command: build CVMix if not yet built
add_custom_command(
OUTPUT ${CVMIX_LIB}
COMMAND bash ${src_home}/cvmix_driver/download_cvmix.sh --prefix ${CVMIX_ROOT}
WORKING_DIRECTORY ${src_home}/cvmix_driver
DEPENDS ${CVMIX_SOURCES} ${CVMIX_DOWNLOAD} # trigger rebuild if source or download script change
COMMENT "Downloading and building CVMix into ${CVMIX_ROOT}"
VERBATIM)

# check if ${CVMIX_ROOT}/lib/libcvmix.a exist or not, only if it does not
# exist build library
add_custom_target(cvmix_autobuild DEPENDS ${CVMIX_LIB})

# build cvmix external libray
add_library(cvmix_external STATIC IMPORTED GLOBAL)
set_target_properties(cvmix_external PROPERTIES
IMPORTED_LOCATION ${CVMIX_LIB}
INTERFACE_INCLUDE_DIRECTORIES "${CVMIX_ROOT}/include/cvmix")

# cvmix_autobuild is a custom target that runs your download_cvmix.sh script
# and produces libcvmix.a. cvmix_external is an imported library, meaning CMake
# doesn’t build it — it just points to an existing file (libcvmix.a).
add_dependencies(cvmix_external cvmix_autobuild)

# Link CVMix library to fesom, build chain becomes: fesom → cvmix_external → cvmix_autobuild
target_link_libraries(${PROJECT_NAME} PRIVATE cvmix_external)


include(FetchContent)
set(CVMIX_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(CVMIX_BUILD_DRIVER OFF CACHE BOOL "" FORCE)
FetchContent_Declare(cvmix

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does cmake know where to iinstall/put the CVMix files ? Or where does the clones repo end up ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo will be put into build/_deps/cvmix-src and then built in build/_deps/cvmix-build. It will be added to the main fesom build through add_subdirectory(), that is why cmake can then link it.

GIT_REPOSITORY https://github.com/CVMix/CVMix-src.git
GIT_TAG v1.0.0
)
FetchContent_MakeAvailable(cvmix)

# CVMix's own CMakeLists exposes its module dir only via INSTALL_INTERFACE and
# writes .mod files to ${CMAKE_BINARY_DIR}/modules (top-level build dir, set
# in the subdirectory scope). Expose that path on the BUILD_INTERFACE so
# FESOM's wrapper sources in cvmix_driver/ can find the CVMix .mod files.
target_include_directories(cvmix_static INTERFACE
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/modules>)

target_link_libraries(${PROJECT_NAME} PRIVATE cvmix_static)
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE Fortran)
Expand Down
100 changes: 0 additions & 100 deletions src/cvmix_driver/download_cvmix.sh

This file was deleted.

Loading