Skip to content

Commit

Permalink
[libc++] Pickle substitutions to pass them to dsl.sh.py
Browse files Browse the repository at this point in the history
This is less brittle than hand-picking the substitutions that we
pass to the test, since a config could theorically use non-base
substitutions as well (such as defining %{flags} in terms of another
substitution like %{include}).

Also, print the decoded substitutions, which makes it much easier
to debug the test when it fails.

Differential Revision: https://reviews.llvm.org/D111179
  • Loading branch information
ldionne committed Oct 5, 2021
1 parent 54a8a0d commit d51f57c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
26 changes: 9 additions & 17 deletions libcxx/test/libcxx/selftest/dsl/dsl.sh.py
Expand Up @@ -10,18 +10,12 @@

# Note: We prepend arguments with 'x' to avoid thinking there are too few
# arguments in case an argument is an empty string.
# RUN: %{python} %s x%S \
# RUN: x%T \
# RUN: x%{escaped_exec} \
# RUN: x%{escaped_cxx} \
# RUN: x%{escaped_flags} \
# RUN: x%{escaped_compile_flags} \
# RUN: x%{escaped_link_flags}
# END.
# RUN: %{python} %s x%S x%T x%{substitutions}

import base64
import copy
import os
import pickle
import platform
import subprocess
import sys
Expand All @@ -40,9 +34,14 @@
# Steal some parameters from the config running this test so that we can
# bootstrap our own TestingConfig.
args = list(map(lambda s: s[1:], sys.argv[1:8])) # Remove the leading 'x'
SOURCE_ROOT, EXEC_PATH, EXEC, CXX, FLAGS, COMPILE_FLAGS, LINK_FLAGS = args
SOURCE_ROOT, EXEC_PATH, SUBSTITUTIONS = args
sys.argv[1:8] = []

# Decode the substitutions.
SUBSTITUTIONS = pickle.loads(base64.b64decode(SUBSTITUTIONS))
for s, sub in SUBSTITUTIONS:
print("Substitution '{}' is '{}'".format(s, sub))

class SetupConfigs(unittest.TestCase):
"""
Base class for the tests below -- it creates a fake TestingConfig.
Expand All @@ -69,14 +68,7 @@ def setUp(self):
self.config.test_source_root = SOURCE_ROOT
self.config.test_exec_root = EXEC_PATH
self.config.recursiveExpansionLimit = 10
base64Decode = lambda s: lit.util.to_string(base64.b64decode(s))
self.config.substitutions = [
('%{cxx}', base64Decode(CXX)),
('%{flags}', base64Decode(FLAGS)),
('%{compile_flags}', base64Decode(COMPILE_FLAGS)),
('%{link_flags}', base64Decode(LINK_FLAGS)),
('%{exec}', base64Decode(EXEC))
]
self.config.substitutions = copy.deepcopy(SUBSTITUTIONS)

def getSubstitution(self, substitution):
"""
Expand Down
16 changes: 7 additions & 9 deletions libcxx/test/libcxx/selftest/dsl/lit.local.cfg
Expand Up @@ -5,16 +5,14 @@
# substituting the directory. This way, the test itself can populate %T as it
# sees fit, and %{exec} will respect it.
#
# To solve this problem, we add base64 encoded versions of substitutions just
# in this directory. We then base64-decode them from the tests when we need to.
# Another option would be to have a way to prevent expansion in Lit itself.
import base64
import lit.util
# To solve this problem, we pickle the substitutions and base64 encode that
# to pass it to the test, and we decode and unpickle the substitutions from
# within the test.
import base64, lit.util, pickle
base64Encode = lambda s: lit.util.to_string(base64.b64encode(lit.util.to_bytes(s)))
escaped = [(k.replace('%{', '%{escaped_'), base64Encode(v)) for (k, v) in config.substitutions]
config.substitutions.extend(escaped)
escapedSubstitutions = base64Encode(pickle.dumps(config.substitutions))
config.substitutions.append(('%{substitutions}', escapedSubstitutions))

# The tests in this directory need to run Python
import pipes
import sys
import pipes, sys
config.substitutions.append(('%{python}', pipes.quote(sys.executable)))

0 comments on commit d51f57c

Please sign in to comment.