From 28e1b8e8ff011828f3efd4483fa655dec1a11c01 Mon Sep 17 00:00:00 2001
From: Sylvester Joosten <sylvester.joosten@gmail.com>
Date: Mon, 4 Jan 2021 18:14:27 -0600
Subject: [PATCH] Resolve "Issue with hard-coded root build-dir"

---
 .rootlogon.C | 37 +++++++++++++++++++++++++++++++++++++
 rootlogon.C  | 12 ------------
 2 files changed, 37 insertions(+), 12 deletions(-)
 create mode 100644 .rootlogon.C
 delete mode 100644 rootlogon.C

diff --git a/.rootlogon.C b/.rootlogon.C
new file mode 100644
index 00000000..18fbc713
--- /dev/null
+++ b/.rootlogon.C
@@ -0,0 +1,37 @@
+#include <fmt/core.h>
+
+void _rootlogon() {
+  // ensure we have format loaded. Should be able to move to C++20 fmt library
+  // some point in the future
+  R__LOAD_LIBRARY(libfmt);
+
+  // check if we requested a special build directory through the environment,
+  const char*       build_dir_from_env = gSystem->Getenv("ROOT_BUILD_TMPDIR");
+  const std::string build_dir = build_dir_from_env ? build_dir_from_env : "$HOME/.root_build_dir";
+
+  // ensure the build directory exists to avoid crashes due a directory
+  // note: in principle this allows us to execute arbitrary code, but seeing
+  //       as this can only be manipulated through the shell environment in the
+  //       first place this should not be a safety concern.
+  const std::string mkdir = fmt::format("mkdir -p {}", build_dir);
+  gSystem->Exec(mkdir.c_str());
+
+  // Communicate what build directory we are using
+  fmt::print("---> Setting ACLiC build directory to: {}\n\n", build_dir);
+  gSystem->SetBuildDir(build_dir.c_str());
+
+  // Ensure we have our environment setup
+  gROOT->ProcessLine(".include /usr/local/include/hcana");
+  gROOT->ProcessLine(".include SCRIPTS/include");
+  gROOT->ProcessLine(".include .");
+
+  // Some basic plotting setup
+  gROOT->SetStyle("Plain");
+  gStyle->SetPalette(1);
+
+  // Load extra ROOT libraries, TODO check if this is still
+  // needed in the newer ROOT versions we are using.
+  gSystem->Load("libTree");
+  gSystem->Load("libTreePlayer");
+  gSystem->Load("libHist");
+}
diff --git a/rootlogon.C b/rootlogon.C
deleted file mode 100644
index 2f19a5a3..00000000
--- a/rootlogon.C
+++ /dev/null
@@ -1,12 +0,0 @@
-void rootlogon() {
-  gSystem->SetBuildDir("$HOME/.root_build_dir");
-  gROOT->ProcessLine(".include /usr/local/include/hcana");
-  gROOT->ProcessLine(".include SCRIPTS/include");
-  gROOT->ProcessLine(".include .");
-  R__LOAD_LIBRARY(libfmt)
-  gROOT->SetStyle("Plain");
-  gStyle->SetPalette(1);
-  gSystem->Load("libTree");
-  gSystem->Load("libTreePlayer");
-  gSystem->Load("libHist");
-}
-- 
GitLab