From 7d685c76e92dbfef716f9a1b8a8fe8235e359dd2 Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Thu, 22 Jan 2026 20:50:03 +0100 Subject: [PATCH] [CMake] Don't set ROOTSYS in rootcling_stage1 During build time, the build directory path has to be fixed anyway, so we might as well hardcode it in the `rootcling_stage1` executable. This implements a suggestion by @pcanal in this comment: https://github.com/root-project/root/pull/20828#discussion_r2682980144 It's one more step towards completely avoiding the need for the `ROOTSYS` environment variable. --- core/dictgen/res/rootcling_impl.h | 4 - core/dictgen/src/rootcling_impl.cxx | 76 ------------------- core/rootcling_stage1/CMakeLists.txt | 1 + .../rootcling_stage1/src/rootcling_stage1.cxx | 12 +-- 4 files changed, 3 insertions(+), 90 deletions(-) diff --git a/core/dictgen/res/rootcling_impl.h b/core/dictgen/res/rootcling_impl.h index 9b80abd3c86d5..9f34163fe4e98 100644 --- a/core/dictgen/res/rootcling_impl.h +++ b/core/dictgen/res/rootcling_impl.h @@ -30,10 +30,6 @@ namespace RootCling { void (*fAddEnumToROOTFile)(const char *tdname) = nullptr; bool (*fCloseStreamerInfoROOTFile)(bool writeEmptyRootPCM) = nullptr; }; - - struct TROOTSYSSetter { - TROOTSYSSetter(); - }; } // namespace RootCling } // namespace Internal } // namespace ROOT diff --git a/core/dictgen/src/rootcling_impl.cxx b/core/dictgen/src/rootcling_impl.cxx index 36a34c956ec19..765f49a808f35 100644 --- a/core/dictgen/src/rootcling_impl.cxx +++ b/core/dictgen/src/rootcling_impl.cxx @@ -149,15 +149,6 @@ static llvm::cl::OptionCategory gRootclingOptions("rootcling common options"); //////////////////////////////////////////////////////////////////////////////// -void SetRootSys(); - -ROOT::Internal::RootCling::TROOTSYSSetter::TROOTSYSSetter() { - // rootcling's libCore needs "our" ROOTSYS: - SetRootSys(); -}; - -//////////////////////////////////////////////////////////////////////////////// - void EmitStreamerInfo(const char *normName) { if (gDriverConfig->fAddStreamerInfoToROOTFile) @@ -469,73 +460,6 @@ bool IsSelectionFile(const char *filename) return ROOT::TMetaUtils::IsLinkdefFile(filename) || IsSelectionXml(filename); } -//////////////////////////////////////////////////////////////////////////////// -/// Set the ROOTSYS env var based on the executable location. - -void SetRootSys() -{ - const char *exepath = GetExePath(); - if (exepath && *exepath) { -#if !defined(_WIN32) - char *ep = new char[PATH_MAX]; - if (!realpath(exepath, ep)) { - fprintf(stderr, "rootcling: error getting realpath of rootcling!"); - strlcpy(ep, exepath, PATH_MAX); - } -#else - int nche = strlen(exepath) + 1; - char *ep = new char[nche]; - strlcpy(ep, exepath, nche); -#endif - char *s; - - if ((s = strrchr(ep, '/'))) { - // $ROOTSYS/bin/rootcling - int removesubdirs = 2; - if (!strncmp(s + 1, "rootcling_stage1.exe", 20)) { - // $ROOTSYS/bin/rootcling_stage1.exe - removesubdirs = 2; - gBuildingROOT = true; - } else if (!strncmp(s + 1, "rootcling_stage1", 16)) { - // $ROOTSYS/core/rootcling_stage1/src/rootcling_stage1 - removesubdirs = 4; - gBuildingROOT = true; - } - for (int i = 1; s && i < removesubdirs; ++i) { - *s = 0; - s = strrchr(ep, '/'); - } - if (s) *s = 0; - } else { - // There was no slashes at all let now change ROOTSYS - delete [] ep; - return; - } - - if (!gBuildingROOT) { - delete [] ep; - return; // don't mess with user's ROOTSYS. - } - - int ncha = strlen(ep) + 10; - char *env = new char[ncha]; - snprintf(env, ncha, "ROOTSYS=%s", ep); - - if (gDriverConfig) { - // After the putenv below, gRootDir might point to the old ROOTSYS - // entry, i.e. to deleted memory. Update it. - const char** pRootDir = gDriverConfig->fPRootDir; - if (pRootDir) { - *pRootDir = env + 8; - } - } - - putenv(env); - // intentionally not call delete [] env, while GLIBC keep use pointer - delete [] ep; - } -} - //////////////////////////////////////////////////////////////////////////////// /// Check whether the `#pragma` line contains expectedTokens (0-terminated array). diff --git a/core/rootcling_stage1/CMakeLists.txt b/core/rootcling_stage1/CMakeLists.txt index 961292d3691b3..b333c14fe0893 100644 --- a/core/rootcling_stage1/CMakeLists.txt +++ b/core/rootcling_stage1/CMakeLists.txt @@ -35,6 +35,7 @@ ROOT_EXECUTABLE(rootcling_stage1 src/rootcling_stage1.cxx $ LIBRARIES ${CLING_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT} ${ROOT_ATOMIC_LIBS} NOINSTALL) +target_compile_options(rootcling_stage1 PRIVATE -DCMAKE_BINARY_DIR="${CMAKE_BINARY_DIR}") target_include_directories(rootcling_stage1 PRIVATE ${CMAKE_SOURCE_DIR}/core/foundation/inc # for RConfig.hxx diff --git a/core/rootcling_stage1/src/rootcling_stage1.cxx b/core/rootcling_stage1/src/rootcling_stage1.cxx index 6f4071149646f..2b120296e0e13 100644 --- a/core/rootcling_stage1/src/rootcling_stage1.cxx +++ b/core/rootcling_stage1/src/rootcling_stage1.cxx @@ -21,21 +21,13 @@ extern "C" { // force compiler to emit symbol for function above static void (*dlsymaddr)() = &usedToIdentifyRootClingByDlSym; -ROOT::Internal::RootCling::TROOTSYSSetter gROOTSYSSetter; - static const char *GetIncludeDir() { - auto renv = std::getenv("ROOTSYS"); - if (!renv) - return nullptr; - static std::string incdir = std::string(renv) + "/include"; + static std::string incdir = CMAKE_BINARY_DIR "/include"; return incdir.c_str(); } static const char *GetEtcDir() { - auto renv = std::getenv("ROOTSYS"); - if (!renv) - return nullptr; - static std::string etcdir = std::string(renv) + "/etc"; + static std::string etcdir = CMAKE_BINARY_DIR "/etc"; return etcdir.c_str(); }