From bd2ce09b5c34f9b38a5f1691be3b6768ae1d9ab3 Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Wed, 18 Feb 2026 12:43:22 +0200 Subject: [PATCH] Use `TemporaryDirectory` for `g++` version detection When MadAnalysis5 is installed in a common (shared) directory so that multiple people can use it, the installation directory might not be writable by the current user. In this case, detecting the version of the C++ standard supported by the compiler will invariably fail, since MA cannot create the `cxxtest.cc` file in the installation directory. This changes the code to use a temporary directory for building the test program. --- madanalysis/system/detect_gpp.py | 46 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/madanalysis/system/detect_gpp.py b/madanalysis/system/detect_gpp.py index fa7b55ea..fa83f19e 100644 --- a/madanalysis/system/detect_gpp.py +++ b/madanalysis/system/detect_gpp.py @@ -1,24 +1,24 @@ ################################################################################ -# +# # Copyright (C) 2012-2025 Jack Araz, Eric Conte & Benjamin Fuks # The MadAnalysis development team, email: -# +# # This file is part of MadAnalysis 5. # Official website: -# +# # MadAnalysis 5 is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. -# +# # MadAnalysis 5 is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with MadAnalysis 5. If not, see -# +# ################################################################################ @@ -27,6 +27,7 @@ import glob import os import sys +from tempfile import TemporaryDirectory import re import platform from shell_command import ShellCommand @@ -55,10 +56,10 @@ def __init__(self, archi_info, user_info, session_info, debug): def PrintDisableMessage(self): self.logger.warning('g++ compiler not found. Please install it before using MadAnalysis 5.') - + def AutoDetection(self): msg='' - + # Which result = ShellCommand.Which('g++',all=False,mute=True) if len(result)==0: @@ -70,19 +71,20 @@ def AutoDetection(self): # Check C++ version try: - with open(os.path.join(self.archi_info.ma5dir, "cxxtest.cc"), 'w') as f: - f.write("int main() { return 0; }\n") - command = lambda cxx_version: [ - f"g++ -std=c++{cxx_version} " - f"{os.path.join(self.archi_info.ma5dir, 'cxxtest.cc')} " - f"-o {os.path.join(self.archi_info.ma5dir, 'cxxtest')}" - ] - for version in [11,14]: # ,17,20]: for the future - result = ShellCommand.Execute(command(version), self.archi_info.ma5dir, shell=True) - if result: - setattr(self.archi_info, "cpp"+str(version), True) - os.remove(os.path.join(self.archi_info.ma5dir, "cxxtest.cc")) - os.remove(os.path.join(self.archi_info.ma5dir, "cxxtest")) + with TemporaryDirectory() as tmpdir: + with open(os.path.join(tmpdir, "cxxtest.cc"), 'w') as f: + f.write("int main() { return 0; }\n") + command = lambda cxx_version: [ + f"g++ -std=c++{cxx_version} " + f"{os.path.join(tmpdir, 'cxxtest.cc')} " + f"-o {os.path.join(tmpdir, 'cxxtest')}" + ] + for version in [11,14]: # ,17,20]: for the future + result = ShellCommand.Execute(command(version), tmpdir, shell=True) + if result: + setattr(self.archi_info, "cpp"+str(version), True) + os.remove(os.path.join(tmpdir, "cxxtest.cc")) + os.remove(os.path.join(tmpdir, "cxxtest")) except Exception as err: self.logger.debug(f"Unexpected {err}, {type(err)}") @@ -144,7 +146,7 @@ def ExtractInfo(self): for path in paths: if os.path.isdir(path): self.library_paths.append(os.path.normpath(path)) - + if self.debug: self.logger.debug(" search path for headers:") for line in self.header_paths: