Skip to content

Commit cb2b4a9

Browse files
committed
[core] Use shared library dir as anchor in GetIncludeDir()
Using the shared library directory as the anchor to resolve the resource directories like `GetIncludeDir()` is more direct and robust than using `GetRootSys()`, which loses its meaning in some installations (e.g. what should it be when ROOT is installed by the package manager? Just `/`?). Most importantly, this change lifts some more constraint from how the install tree can be reorganized after the fact. One can for example move the include and library directory around as wanted, as long as their relative paths don't change. This fixes the Python wheels, which were broken by commit a5b1ed9. As of commit a5b1ed9, ROOT became smarter about finding the include path in the install tree, inferring the correct relative path from $ROOTSYS and the CMAKE_INSTALL_INCLUDEDIR variable at build time. Before, that only happened for gnuinstall=ON, but now it always happens. However, for the Python wheel, we are breaking the assumptions that ROOT makes by moving around directories in the install tree. This commit fixes that situation.
1 parent a996981 commit cb2b4a9

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

core/base/CMakeLists.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,19 @@ set(full_core_filename $<TARGET_FILE_NAME:Core>)
233233
# Absolue CMAKE_INSTALL_<dir> paths are discouraged in CMake, but some
234234
# packagers use them anyway. So we support it.
235235
if(IS_ABSOLUTE ${CMAKE_INSTALL_INCLUDEDIR})
236-
set(install_includedir_is_absolute 1)
236+
set(install_lib_to_include "${CMAKE_INSTALL_INCLUDEDIR}")
237237
else()
238-
set(install_includedir_is_absolute 0)
238+
if(IS_ABSOLUTE ${CMAKE_INSTALL_LIBDIR})
239+
set(libdir "${CMAKE_INSTALL_LIBDIR}")
240+
else()
241+
set(libdir "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
242+
endif()
243+
file(RELATIVE_PATH install_lib_to_include "${libdir}" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}")
244+
unset(libdir)
239245
endif()
240-
file(TO_NATIVE_PATH "${CMAKE_INSTALL_INCLUDEDIR}" install_includedir_native)
241246

242247
target_compile_options(Core PRIVATE -DLIB_CORE_NAME=${full_core_filename}
243-
-DINSTALL_INCLUDEDIR=${install_includedir_native}
244-
-DINSTALL_INCLUDEDIR_IS_ABSOLUTE=${install_includedir_is_absolute}
248+
-DINSTALL_LIB_TO_INCLUDE="${install_lib_to_include}"
245249
)
246250
add_dependencies(Core ensure_build_tree_marker)
247251

core/base/src/TROOT.cxx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,21 +3182,27 @@ const TString &TROOT::GetIncludeDir()
31823182
if (!rootincdir.IsNull())
31833183
return rootincdir;
31843184

3185-
const std::string &sep = ROOT::FoundationUtils::GetPathSeparator();
3185+
namespace fs = std::filesystem;
3186+
3187+
// The shared library directory can be found automatically, because the
3188+
// libCore is loaded by definition when using TROOT. It's used as the anchor
3189+
// to resolve the ROOT include directory, using the correct relative path
3190+
// for either the build or install tree.
3191+
fs::path libPath = GetSharedLibDir().Data();
31863192

31873193
// Check if we are in the build tree using the build tree marker file
3188-
const bool isBuildTree = std::filesystem::exists((GetSharedLibDir() + sep + "root-build-tree-marker").Data());
3194+
const bool isBuildTree = fs::exists(libPath / "root-build-tree-marker");
31893195

3190-
if (isBuildTree) {
3191-
rootincdir = GetRootSys() + sep + "include";
3192-
} else {
3193-
#if INSTALL_INCLUDEDIR_IS_ABSOLUTE
3194-
rootincdir = _R_QUOTEVAL_(INSTALL_INCLUDEDIR);
3195-
#else
3196-
rootincdir = GetRootSys() + sep + _R_QUOTEVAL_(INSTALL_INCLUDEDIR);
3197-
#endif
3196+
fs::path includePath = isBuildTree ? "../include" : INSTALL_LIB_TO_INCLUDE;
3197+
3198+
// The INSTALL_LIB_TO_INCLUDE might already be absolute
3199+
if (!includePath.is_absolute()) {
3200+
includePath = libPath / includePath;
31983201
}
31993202

3203+
// Normalize to get rid of the "../" in relative paths
3204+
rootincdir += includePath.lexically_normal().native().c_str();
3205+
32003206
return rootincdir;
32013207
}
32023208

core/base/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if(ROOT_NEED_STDCXXFS)
2020
target_link_libraries(CoreBaseTests PRIVATE stdc++fs)
2121
endif()
2222
target_compile_options(CoreBaseTests PRIVATE -DEXPECTED_SHARED_LIBRARY_DIR="${localruntimedir}")
23+
target_compile_options(CoreBaseTests PRIVATE -DEXPECTED_INCLUDE_DIR="${CMAKE_BINARY_DIR}/include")
2324

2425
ROOT_ADD_GTEST(CoreErrorTests TErrorTests.cxx LIBRARIES Core)
2526

core/base/test/TROOTTests.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ TEST(TROOT, GetSharedLibDir)
4141

4242
EXPECT_EQ(libDir, libDirRef);
4343
}
44+
45+
TEST(TROOT, GetIncludeDir)
46+
{
47+
namespace fs = std::filesystem;
48+
49+
fs::path includeDir = gROOT->GetIncludeDir().Data();
50+
fs::path includeDirRef = EXPECTED_INCLUDE_DIR;
51+
52+
EXPECT_EQ(includeDir, includeDirRef);
53+
}

0 commit comments

Comments
 (0)