From ac912ea25ebd7be972a9731c6ba524084461988b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Tue, 24 Mar 2026 05:43:53 +0100 Subject: [PATCH] Support nested build directories in sibling search MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The inline sibling search logic used get_filename_component(NAME) to extract the build directory name, which only returned the last path component. This broke sibling discovery for nested build directories like "builds/debug" — the search would look for "../opm-common/debug" instead of "../opm-common/builds/debug". Use file(RELATIVE_PATH) to compute the full relative path from PROJECT_SOURCE_DIR to PROJECT_BINARY_DIR, then apply that same relative path to the sibling module's source tree. This correctly handles build directories at any depth. The previous strategies (prefix-based naming and common build root) are kept as fallbacks for out-of-source builds. This is the inline copy of the logic in opm-common/cmake/Modules/OpmSiblingSearch.cmake, duplicated here because opm-common must be located before its cmake modules become available via find_package. --- CMakeLists.txt | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2dc6e3e7449..02121ff95b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -908,26 +908,35 @@ if(CONVERT_CUDA_TO_HIP) set(USE_GPU_BRIDGE OFF) endif() +# NOTE: Inline sibling search: duplicated from opm-common/cmake/Modules/OpmSiblingSearch.cmake +# because we need to locate opm-common before find_package(opm-common) makes that module available. +# NOTE: If you modify this logic, you must also modify the logic in opm-grid/CMakeLists.txt and in +# opm-common/cmake/Modules/OpmSiblingSearch.cmake. +# NOTE: See description of logic in opm-common/cmake/Modules/OpmSiblingSearch.cmake for more details. if(SIBLING_SEARCH AND NOT opm-common_DIR) - # guess the sibling dir - get_filename_component(_leaf_dir_name ${PROJECT_BINARY_DIR} NAME) - get_filename_component(_parent_full_dir ${PROJECT_BINARY_DIR} DIRECTORY) - get_filename_component(_parent_dir_name ${_parent_full_dir} NAME) - #Try if / is used - get_filename_component(_modules_dir ${_parent_full_dir} DIRECTORY) - if(IS_DIRECTORY ${_modules_dir}/opm-common/${_leaf_dir_name}) - set(opm-common_DIR ${_modules_dir}/opm-common/${_leaf_dir_name}) - set(opm-grid_DIR ${_modules_dir}/opm-grid/${_leaf_dir_name}) + # Strategy 1: Compute relative path from source to build dir, apply to sibling. + # Handles both flat (repo/build) and nested (repo/builds/debug) layouts. + get_filename_component(_source_parent "${PROJECT_SOURCE_DIR}" DIRECTORY) + file(RELATIVE_PATH _build_rel "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}") + set(_sibling_common "${_source_parent}/opm-common/${_build_rel}") + set(_sibling_grid "${_source_parent}/opm-grid/${_build_rel}") + if(NOT _build_rel MATCHES "^\\.\\." AND IS_DIRECTORY "${_sibling_common}") + set(opm-common_DIR "${_sibling_common}") + if(IS_DIRECTORY "${_sibling_grid}") + set(opm-grid_DIR "${_sibling_grid}") + endif() else() - string(REPLACE ${PROJECT_NAME} opm-common _opm_common_leaf ${_leaf_dir_name}) + # Fallback strategies for out-of-source or non-standard layouts + get_filename_component(_leaf_dir_name "${PROJECT_BINARY_DIR}" NAME) + get_filename_component(_parent_full_dir "${PROJECT_BINARY_DIR}" DIRECTORY) + string(REPLACE "${PROJECT_NAME}" opm-common _opm_common_leaf "${_leaf_dir_name}") if(NOT _leaf_dir_name STREQUAL _opm_common_leaf - AND IS_DIRECTORY ${_parent_full_dir}/${_opm_common_leaf}) - # We are using build directories named - set(opm-common_DIR ${_parent_full_dir}/${_opm_common_leaf}) - string(REPLACE ${PROJECT_NAME} opm-grid _opm_grid_leaf ${_leaf_dir_name}) - set(opm-grid_DIR ${_parent_full_dir}/${_opm_grid_leaf}) - elseif(IS_DIRECTORY ${_parent_full_dir}/opm-common) - # All modules are in a common build dir + AND IS_DIRECTORY "${_parent_full_dir}/${_opm_common_leaf}") + set(opm-common_DIR "${_parent_full_dir}/${_opm_common_leaf}") + string(REPLACE "${PROJECT_NAME}" opm-grid _opm_grid_leaf "${_leaf_dir_name}") + set(opm-grid_DIR "${_parent_full_dir}/${_opm_grid_leaf}") + elseif(IS_DIRECTORY "${_parent_full_dir}/opm-common" AND + EXISTS "${_parent_full_dir}/opm-common/CMakeCache.txt") set(opm-common_DIR "${_parent_full_dir}/opm-common") set(opm-grid_DIR "${_parent_full_dir}/opm-grid") endif()