From 82d80a0aed3b6747be3596bfa19e3c75de3ac5c0 Mon Sep 17 00:00:00 2001
From: Axel Naumann <Axel.Naumann@cern.ch>
Date: Thu, 12 Jan 2017 14:13:07 +0100
Subject: [PATCH] Set gRootDir for rootcling -rootbuild; putenv ROOTSYS for
 stage1.

---
 core/dictgen/res/rootcling_impl.h   |  1 +
 core/dictgen/src/rootcling_impl.cxx | 21 ++++++++++++++++-----
 main/src/rootcling.cxx              |  1 +
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/core/dictgen/res/rootcling_impl.h b/core/dictgen/res/rootcling_impl.h
index f86e971ba7f..cbd1133cf92 100644
--- a/core/dictgen/res/rootcling_impl.h
+++ b/core/dictgen/res/rootcling_impl.h
@@ -20,6 +20,7 @@ namespace RootCling {
    struct DriverConfig {
       bool fBuildingROOTStage1 = false;
       std::string fLLVMResourceDir;
+      const char** fPRootDir = nullptr;
 
       // Function that might (rootcling) or might not (rootcling_stage1) be there.
       const char ** * (*fTROOT__GetExtraInterpreterArgs)() = nullptr;
diff --git a/core/dictgen/src/rootcling_impl.cxx b/core/dictgen/src/rootcling_impl.cxx
index 304cbbb5bb4..f775ac382c4 100644
--- a/core/dictgen/src/rootcling_impl.cxx
+++ b/core/dictgen/src/rootcling_impl.cxx
@@ -716,11 +716,22 @@ void SetRootSys()
          // There was no slashes at all let now change ROOTSYS
          return;
       }
-      int ncha = strlen(ep) + 10;
-      char *env = new char[ncha];
-      snprintf(env, ncha, "ROOTSYS=%s", ep);
-      putenv(env);
-      delete [] ep;
+      if (gDriverConfig) {
+         const char** pRootDir = gDriverConfig->fPRootDir;
+         if (gBuildingROOT && pRootDir && !strcmp(*pRootDir, ep)) {
+            *pRootDir = ep; // leaks...
+         } else {
+            delete [] ep;
+         }
+      } else {
+         // stage 1.
+         int ncha = strlen(ep) + 10;
+         char *env = new char[ncha];
+         snprintf(env, ncha, "ROOTSYS=%s", ep);
+         putenv(env);
+         delete [] ep;
+      }
+
    }
 }
 
diff --git a/main/src/rootcling.cxx b/main/src/rootcling.cxx
index 1e2e6c93c2a..833176e7209 100644
--- a/main/src/rootcling.cxx
+++ b/main/src/rootcling.cxx
@@ -33,6 +33,7 @@ int main(int argc, char **argv)
 #endif
 
    config.fBuildingROOTStage1 = false;
+   config.fPRootDir = &gRootDir;
    config.fTROOT__GetExtraInterpreterArgs = &TROOT__GetExtraInterpreterArgs;
    config.fTCling__GetInterpreter = &TCling__GetInterpreter;
    config.fInitializeStreamerInfoROOTFile = &InitializeStreamerInfoROOTFile;
-- 
GitLab