[libcxx-commits] [libcxx] 13fa4fc - [libc++] Tidy up how %T and %t are created during configuration checks

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 23 06:51:38 PST 2021


Author: Louis Dionne
Date: 2021-11-23T09:51:22-05:00
New Revision: 13fa4fcfe70eaf5fdef6f8520fe134ac1d9955b1

URL: https://github.com/llvm/llvm-project/commit/13fa4fcfe70eaf5fdef6f8520fe134ac1d9955b1
DIFF: https://github.com/llvm/llvm-project/commit/13fa4fcfe70eaf5fdef6f8520fe134ac1d9955b1.diff

LOG: [libc++] Tidy up how %T and %t are created during configuration checks

Instead of having ad-hoc cleanup in various places, handle all creation
and removal of temporary files and directories inside _makeConfigTest.

As a fly-by, also remove testPrefix since we don't keep any source file
around anymore. Setting a prefix for the files is hence not useful anymore.

Differential Revision: https://reviews.llvm.org/D114390

Added: 
    

Modified: 
    libcxx/utils/libcxx/test/dsl.py

Removed: 
    


################################################################################
diff  --git a/libcxx/utils/libcxx/test/dsl.py b/libcxx/utils/libcxx/test/dsl.py
index 1bbd543e21d03..a9d45f2b5a598 100644
--- a/libcxx/utils/libcxx/test/dsl.py
+++ b/libcxx/utils/libcxx/test/dsl.py
@@ -11,6 +11,7 @@
 import pipes
 import platform
 import re
+import shutil
 import tempfile
 
 import libcxx.test.format
@@ -60,9 +61,6 @@ def _executeScriptInternal(test, commands):
     params={})
   _, tmpBase = libcxx.test.format._getTempPaths(test)
   execDir = os.path.dirname(test.getExecPath())
-  for d in (execDir, os.path.dirname(tmpBase)):
-    if not os.path.exists(d):
-      os.makedirs(d)
   res = lit.TestRunner.executeScriptInternal(test, litConfig, tmpBase, parsedCommands, execDir)
   if isinstance(res, lit.Test.Result): # Handle failure to parse the Lit test
     res = ('', res.output, 127, None)
@@ -79,19 +77,31 @@ def _executeScriptInternal(test, commands):
 
   return (out, err, exitCode, timeoutInfo)
 
-def _makeConfigTest(config, testPrefix=''):
+def _makeConfigTest(config):
+  # Make sure the support directories exist, which is needed to create
+  # the temporary file %t below.
   sourceRoot = os.path.join(config.test_exec_root, '__config_src__')
   execRoot = os.path.join(config.test_exec_root, '__config_exec__')
+  for supportDir in (sourceRoot, execRoot):
+    if not os.path.exists(supportDir):
+      os.makedirs(supportDir)
+
+  # Create a dummy test suite and single dummy test inside it. As part of
+  # the Lit configuration, automatically do the equivalent of 'mkdir %T'
+  # and 'rm -r %T' to avoid cluttering the build directory.
   suite = lit.Test.TestSuite('__config__', sourceRoot, execRoot, config)
-  if not os.path.exists(sourceRoot):
-    os.makedirs(sourceRoot)
-  tmp = tempfile.NamedTemporaryFile(dir=sourceRoot, delete=False, suffix='.cpp',
-                                    prefix=testPrefix)
+  tmp = tempfile.NamedTemporaryFile(dir=sourceRoot, delete=False, suffix='.cpp')
   tmp.close()
   pathInSuite = [os.path.relpath(tmp.name, sourceRoot)]
   class TestWrapper(lit.Test.Test):
-    def __enter__(self):       return self
-    def __exit__(self, *args): os.remove(tmp.name)
+    def __enter__(self):
+      testDir, _ = libcxx.test.format._getTempPaths(self)
+      os.makedirs(testDir)
+      return self
+    def __exit__(self, *args):
+      testDir, _ = libcxx.test.format._getTempPaths(self)
+      shutil.rmtree(testDir)
+      os.remove(tmp.name)
   return TestWrapper(suite, pathInSuite, config)
 
 @_memoizeExpensiveOperation(lambda c, s: (c.substitutions, c.environment, s))
@@ -106,11 +116,10 @@ def sourceBuilds(config, source):
     with open(test.getSourcePath(), 'w') as sourceFile:
       sourceFile.write(source)
     out, err, exitCode, timeoutInfo = _executeScriptInternal(test, ['%{build}'])
-    _executeScriptInternal(test, ['rm %t.exe'])
     return exitCode == 0
 
- at _memoizeExpensiveOperation(lambda c, p, args=None, testPrefix='': (c.substitutions, c.environment, p, args))
-def programOutput(config, program, args=None, testPrefix=''):
+ at _memoizeExpensiveOperation(lambda c, p, args=None: (c.substitutions, c.environment, p, args))
+def programOutput(config, program, args=None):
   """
   Compiles a program for the test target, run it on the test target and return
   the output.
@@ -121,24 +130,20 @@ def programOutput(config, program, args=None, testPrefix=''):
   """
   if args is None:
     args = []
-  with _makeConfigTest(config, testPrefix=testPrefix) as test:
+  with _makeConfigTest(config) as test:
     with open(test.getSourcePath(), 'w') as source:
       source.write(program)
-    try:
-      _, _, exitCode, _ = _executeScriptInternal(test, ['%{build}'])
-      if exitCode != 0:
-        return None
-
-      out, err, exitCode, _ = _executeScriptInternal(test, ["%{{run}} {}".format(' '.join(args))])
-      if exitCode != 0:
-        return None
+    _, _, exitCode, _ = _executeScriptInternal(test, ['%{build}'])
+    if exitCode != 0:
+      return None
 
-      actualOut = re.search("# command output:\n(.+)\n$", out, flags=re.DOTALL)
-      actualOut = actualOut.group(1) if actualOut else ""
-      return actualOut
+    out, err, exitCode, _ = _executeScriptInternal(test, ["%{{run}} {}".format(' '.join(args))])
+    if exitCode != 0:
+      return None
 
-    finally:
-      _executeScriptInternal(test, ['rm %t.exe'])
+    actualOut = re.search("# command output:\n(.+)\n$", out, flags=re.DOTALL)
+    actualOut = actualOut.group(1) if actualOut else ""
+    return actualOut
 
 @_memoizeExpensiveOperation(lambda c, f: (c.substitutions, c.environment, f))
 def hasCompileFlag(config, flag):
@@ -181,8 +186,7 @@ def hasAnyLocale(config, locales):
       return 1;
     }
   """
-  return programOutput(config, program, args=[pipes.quote(l) for l in locales],
-                       testPrefix="check_locale_" + locales[0]) is not None
+  return programOutput(config, program, args=[pipes.quote(l) for l in locales]) is not None
 
 @_memoizeExpensiveOperation(lambda c, flags='': (c.substitutions, c.environment, flags))
 def compilerMacros(config, flags=''):


        


More information about the libcxx-commits mailing list