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