Skip to content

Commit 5babc8c

Browse files
committed
[core] Generalize TROOT::GetIncludeDir() using build tree marker
Introducing a new marker file generalizes `TROOT::GetIncludeDir()` to work in all cases for both relocated install and build trees, even if the `CMAKE_INSTALL_INCLUDEDIR` is different from `include`. This follows up on 43ec084, which was incomplete and left ROOT configuration in a broken state, because `ROOTINCDIR` was not defined anymore but still used. The `RConfigure.h` header still defines a few other internal macros with hardcoded paths, and if the suggested mechanism proves to be stable for the include directory, we can also migrate the other macros. They are fragile anyway. For example, they contain relative paths for `gnuinstall=OFF` and absolute paths for `gnuinstall=ON`, which is nowhere documented and breaks relocatable builds. Also, they use `ROOTSYS` in the case of `gnuinstall=OFF`, which we want to migrate away from. The `ROOT::FoundationUtils::GetIncludeDir()` function was also removed and completely absorbed into `TROOT::GetIncludeDir()`, as it was not used in any other place. Also, allow absolute `CMAKE_INSTALL_INCLUDEDIR` paths.
1 parent 92fdae1 commit 5babc8c

6 files changed

Lines changed: 50 additions & 30 deletions

File tree

cmake/modules/RootConfiguration.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,19 @@ endif()
548548
string(REGEX MATCH "__cplusplus[=| ]([0-9]+)" __cplusplus "${__cplusplus_PPout}")
549549
set(__cplusplus ${CMAKE_MATCH_1}L)
550550

551+
# To mark the build tree. Important for automatic resolution of relative paths,
552+
# for example to the include directory. Use custom target to ensure re-creation
553+
# when someone deletes the marker.
554+
set(build_tree_marker "${localruntimedir}/root-build-tree-marker")
555+
add_custom_command(
556+
OUTPUT "${build_tree_marker}"
557+
COMMAND ${CMAKE_COMMAND} -E touch "${build_tree_marker}"
558+
COMMENT "Ensuring that \"${build_tree_marker}\" exists"
559+
)
560+
add_custom_target(ensure_build_tree_marker ALL
561+
DEPENDS "${build_tree_marker}"
562+
)
563+
551564
configure_file(${PROJECT_SOURCE_DIR}/config/RConfigure.in ginclude/RConfigure.h NEWLINE_STYLE UNIX)
552565
install(FILES ${CMAKE_BINARY_DIR}/ginclude/RConfigure.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
553566

config/RConfigure.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#define ROOTPREFIX "@prefix@"
1010
#define ROOTBINDIR "@bindir@"
1111
#define ROOTLIBDIR "@libdir@"
12-
#define ROOTINCDIR "@incdir@"
1312
#define ROOTETCDIR "@etcdir@"
1413
#define ROOTDATADIR "@datadir@"
1514
#define ROOTDOCDIR "@docdir@"

core/base/CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,21 @@ if(core_soversion)
236236
else()
237237
set(full_core_filename "${core_prefix}Core${core_suffix}")
238238
endif()
239+
240+
# Absolue CMAKE_INSTALL_<dir> paths are discouraged in CMake, but some
241+
# packagers use them anyway. So we support it.
242+
if(IS_ABSOLUTE ${CMAKE_INSTALL_INCLUDEDIR})
243+
set(install_includedir_is_absolute 1)
244+
else()
245+
set(install_includedir_is_absolute 0)
246+
endif()
247+
file(TO_NATIVE_PATH "${CMAKE_INSTALL_INCLUDEDIR}" install_includedir_native)
248+
239249
target_compile_options(Core PRIVATE -DLIB_CORE_NAME=${full_core_filename}
240-
-DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
250+
-DINSTALL_INCLUDEDIR=${install_includedir_native}
251+
-DINSTALL_INCLUDEDIR_IS_ABSOLUTE=${install_includedir_is_absolute}
241252
)
253+
add_dependencies(Core ensure_build_tree_marker)
242254

243255
if(PCRE2_FOUND)
244256
target_link_libraries(Core PRIVATE PCRE2::PCRE2)

core/base/src/TROOT.cxx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,9 +1993,7 @@ void TROOT::InitSystem()
19931993
if (gSystem->Init())
19941994
fprintf(stderr, "Fatal in <TROOT::InitSystem>: can't init operating system layer\n");
19951995

1996-
TString rootincludedir = _R_QUOTEVAL_(CMAKE_INSTALL_INCLUDEDIR);
1997-
gSystem->PrependPathName(GetRootSys(), rootincludedir);
1998-
gSystem->SetIncludePath(("-I" + rootincludedir).Data());
1996+
gSystem->SetIncludePath(("-I" + GetIncludeDir()).Data());
19991997

20001998
if (!gSystem->HomeDirectory()) {
20011999
fprintf(stderr, "Fatal in <TROOT::InitSystem>: HOME directory not set\n");
@@ -3098,11 +3096,29 @@ const TString& TROOT::GetSharedLibDir() {
30983096
////////////////////////////////////////////////////////////////////////////////
30993097
/// Get the include directory in the installation. Static utility function.
31003098

3101-
const TString& TROOT::GetIncludeDir() {
3102-
// Avoid returning a reference to a temporary because of the conversion
3103-
// between std::string and TString.
3104-
const static TString includedir = ROOT::FoundationUtils::GetIncludeDir();
3105-
return includedir;
3099+
const TString &TROOT::GetIncludeDir()
3100+
{
3101+
static TString rootincdir;
3102+
3103+
if (!rootincdir.IsNull())
3104+
return rootincdir;
3105+
3106+
const std::string &sep = ROOT::FoundationUtils::GetPathSeparator();
3107+
3108+
// Check if we are in the build tree using the build tree marker file
3109+
const bool isBuildTree = std::filesystem::exists((GetSharedLibDir() + sep + "root-build-tree-marker").Data());
3110+
3111+
if (isBuildTree) {
3112+
rootincdir = GetRootSys() + sep + "include";
3113+
} else {
3114+
#if INSTALL_INCLUDEDIR_IS_ABSOLUTE
3115+
rootincdir = _R_QUOTEVAL_(INSTALL_INCLUDEDIR);
3116+
#else
3117+
rootincdir = GetRootSys() + sep + _R_QUOTEVAL_(INSTALL_INCLUDEDIR);
3118+
#endif
3119+
}
3120+
3121+
return rootincdir;
31063122
}
31073123

31083124
////////////////////////////////////////////////////////////////////////////////

core/foundation/res/ROOT/FoundationUtils.hxx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ namespace FoundationUtils {
6767
///
6868
const std::string& GetRootSys();
6969

70-
///\ returns the include directory in the installation.
71-
///
72-
const std::string& GetIncludeDir();
73-
7470
///\returns the sysconfig directory in the installation.
7571
const std::string& GetEtcDir();
7672

core/foundation/src/FoundationUtils.cxx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,6 @@ const std::string& GetRootSys() {
177177
return rootsys;
178178
}
179179

180-
181-
const std::string& GetIncludeDir() {
182-
#ifdef ROOTINCDIR
183-
if (!IgnorePrefix()) {
184-
const static std::string rootincdir = ROOTINCDIR;
185-
return rootincdir;
186-
}
187-
#endif
188-
static std::string rootincdir;
189-
if (rootincdir.empty()) {
190-
const std::string& sep = GetPathSeparator();
191-
rootincdir = GetRootSys() + sep + "include" + sep;
192-
}
193-
return rootincdir;
194-
}
195-
196180
const std::string& GetEtcDir() {
197181
#ifdef ROOTETCDIR
198182
if (!IgnorePrefix()) {

0 commit comments

Comments
 (0)