Skip to content

Commit a16ba6f

Browse files
committed
Reland "Make it possible for lit.site.cfg to contain relative paths, and use it for llvm and clang"
The problem on Windows was that the \b in "..\bin" was interpreted as an escape sequence. Use r"" strings to prevent that. This reverts commit ab11b9e, with raw strings in the lit.site.cfg.py.in files. Differential Revision: https://reviews.llvm.org/D77184
1 parent fd4d075 commit a16ba6f

File tree

9 files changed

+121
-34
lines changed

9 files changed

+121
-34
lines changed

clang/test/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,30 @@ configure_lit_site_cfg(
3535
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
3636
MAIN_CONFIG
3737
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
38+
PATHS
39+
"LLVM_SOURCE_DIR"
40+
"LLVM_BINARY_DIR"
41+
"LLVM_TOOLS_DIR"
42+
"LLVM_LIBS_DIR"
43+
"SHLIBDIR"
44+
"LLVM_LIT_TOOLS_DIR"
45+
"CLANG_BINARY_DIR"
46+
"CLANG_SOURCE_DIR"
47+
"CLANG_TOOLS_DIR"
3848
)
3949

4050
configure_lit_site_cfg(
4151
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
4252
${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py
4353
MAIN_CONFIG
4454
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py
55+
PATHS
56+
"LLVM_SOURCE_DIR"
57+
"LLVM_BINARY_DIR"
58+
"LLVM_TOOLS_DIR"
59+
"LLVM_LIBS_DIR"
60+
"CLANG_BINARY_DIR"
61+
"SHLIBDIR"
4562
)
4663

4764
option(CLANG_TEST_USE_VG "Run Clang tests under Valgrind" OFF)

clang/test/Unit/lit.site.cfg.py.in

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import sys
44

5-
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
6-
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
7-
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
8-
config.llvm_libs_dir = "@LLVM_LIBS_DIR@"
5+
config.llvm_src_root = path(r"@LLVM_SOURCE_DIR@")
6+
config.llvm_obj_root = path(r"@LLVM_BINARY_DIR@")
7+
config.llvm_tools_dir = path(r"@LLVM_TOOLS_DIR@")
8+
config.llvm_libs_dir = path(r"@LLVM_LIBS_DIR@")
99
config.llvm_build_mode = "@LLVM_BUILD_MODE@"
10-
config.clang_obj_root = "@CLANG_BINARY_DIR@"
10+
config.clang_obj_root = path(r"@CLANG_BINARY_DIR@")
1111
config.enable_shared = @ENABLE_SHARED@
12-
config.shlibdir = "@SHLIBDIR@"
12+
config.shlibdir = path(r"@SHLIBDIR@")
1313
config.target_triple = "@TARGET_TRIPLE@"
1414

1515
# Support substitution of the tools_dir, libs_dirs, and build_mode with user
@@ -25,4 +25,5 @@ except KeyError:
2525
lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key))
2626

2727
# Let the main config do the real work.
28-
lit_config.load_config(config, "@CLANG_SOURCE_DIR@/test/Unit/lit.cfg.py")
28+
lit_config.load_config(
29+
config, os.path.join(path(r"@CLANG_SOURCE_DIR@"), "test/Unit/lit.cfg.py"))

clang/test/lit.site.cfg.py.in

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import sys
44

5-
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
6-
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
7-
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
8-
config.llvm_libs_dir = "@LLVM_LIBS_DIR@"
9-
config.llvm_shlib_dir = "@SHLIBDIR@"
5+
config.llvm_src_root = path(r"@LLVM_SOURCE_DIR@")
6+
config.llvm_obj_root = path(r"@LLVM_BINARY_DIR@")
7+
config.llvm_tools_dir = path(r"@LLVM_TOOLS_DIR@")
8+
config.llvm_libs_dir = path(r"@LLVM_LIBS_DIR@")
9+
config.llvm_shlib_dir = path(r"@SHLIBDIR@")
1010
config.llvm_plugin_ext = "@LLVM_PLUGIN_EXT@"
11-
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
12-
config.clang_obj_root = "@CLANG_BINARY_DIR@"
13-
config.clang_src_dir = "@CLANG_SOURCE_DIR@"
14-
config.clang_tools_dir = "@CLANG_TOOLS_DIR@"
11+
config.lit_tools_dir = path(r"@LLVM_LIT_TOOLS_DIR@")
12+
config.clang_obj_root = path(r"@CLANG_BINARY_DIR@")
13+
config.clang_src_dir = path(r"@CLANG_SOURCE_DIR@")
14+
config.clang_tools_dir = path(r"@CLANG_TOOLS_DIR@")
1515
config.host_triple = "@LLVM_HOST_TRIPLE@"
1616
config.target_triple = "@TARGET_TRIPLE@"
1717
config.host_cxx = "@CMAKE_CXX_COMPILER@"
@@ -50,4 +50,5 @@ if not "@CLANG_DEFAULT_LINKER@":
5050
config.available_features.add('platform-linker')
5151

5252
# Let the main config do the real work.
53-
lit_config.load_config(config, "@CLANG_SOURCE_DIR@/test/lit.cfg.py")
53+
lit_config.load_config(
54+
config, os.path.join(config.clang_src_dir, "test/lit.cfg.py"))

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,8 +1396,14 @@ endmacro()
13961396
# variables needed for the 'lit.site.cfg' files. This function bundles the
13971397
# common variables that any Lit instance is likely to need, and custom
13981398
# variables can be passed in.
1399+
# The keyword PATHS is followed by a list of cmake variable names that are
1400+
# mentioned as `path("@varname@")` in the lit.cfg.py.in file. Variables in that
1401+
# list are treated as paths that are relative to the directory the generated
1402+
# lit.cfg.py file is in, and the `path()` function converts the relative
1403+
# path back to absolute form. This makes it possible to move a build directory
1404+
# containing lit.cfg.py files from one machine to another.
13991405
function(configure_lit_site_cfg site_in site_out)
1400-
cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN})
1406+
cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING;PATHS" ${ARGN})
14011407

14021408
if ("${ARG_MAIN_CONFIG}" STREQUAL "")
14031409
get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
@@ -1447,12 +1453,17 @@ function(configure_lit_site_cfg site_in site_out)
14471453
set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
14481454
set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
14491455

1450-
set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!")
1456+
set(LIT_SITE_CFG_IN_HEADER "# Autogenerated from ${site_in}\n# Do not edit!")
1457+
1458+
string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1459+
"# Allow generated lit.site.cfg.py to be relocatable.\n"
1460+
"def path(p): return os.path.join(os.path.dirname(__file__), p) if p else ''\n"
1461+
)
14511462

14521463
# Override config_target_triple (and the env)
14531464
if(LLVM_TARGET_TRIPLE_ENV)
14541465
# This is expanded into the heading.
1455-
string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n"
1466+
string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}"
14561467
"import os\n"
14571468
"target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n"
14581469
"config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n"
@@ -1462,7 +1473,45 @@ function(configure_lit_site_cfg site_in site_out)
14621473
set(TARGET_TRIPLE "\"+config.target_triple+\"")
14631474
endif()
14641475

1476+
if (ARG_PATHS)
1477+
# Walk ARG_PATHS and collect the current value of the variables in there.
1478+
foreach(path ${ARG_PATHS})
1479+
list(APPEND ARG_PATH_VALUES "${${path}}")
1480+
endforeach()
1481+
1482+
# Compute paths relative to the directory containing output lit.site.cfg.py.
1483+
# Passing ARG_PATH_VALUES as-is to execute_process() makes cmake strip
1484+
# empty list entries. So escape the ;s in the list and do the splitting
1485+
# outselves. cmake has no relpath function, so use Python for that.
1486+
string(REPLACE ";" "\\;" ARG_PATH_VALUES_ESCAPED "${ARG_PATH_VALUES}")
1487+
get_filename_component(OUTPUT_DIR ${site_out} DIRECTORY)
1488+
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
1489+
"import os, sys; sys.stdout.write(';'.join(os.path.relpath(p, sys.argv[1]) if p else '' for p in sys.argv[2].split(';')))"
1490+
${OUTPUT_DIR}
1491+
${ARG_PATH_VALUES_ESCAPED}
1492+
OUTPUT_VARIABLE ARG_PATH_VALUES_RELATIVE)
1493+
1494+
list(LENGTH ARG_PATHS len_paths)
1495+
list(LENGTH ARG_PATH_VALUES len_path_values)
1496+
list(LENGTH ARG_PATH_VALUES_RELATIVE len_path_value_rels)
1497+
if ((NOT ${len_paths} EQUAL ${len_path_values}) OR
1498+
(NOT ${len_paths} EQUAL ${len_path_value_rels}))
1499+
message(SEND_ERROR "PATHS lengths got confused")
1500+
endif()
1501+
1502+
# Transform variables mentioned in ARG_PATHS to relative paths for
1503+
# the configure_file() call. Variables are copied to subscopeds by cmake,
1504+
# so this only modifies the local copy of the variables.
1505+
math(EXPR arg_path_limit "${len_paths} - 1")
1506+
foreach(i RANGE ${arg_path_limit})
1507+
list(GET ARG_PATHS ${i} val1)
1508+
list(GET ARG_PATH_VALUES_RELATIVE ${i} val2)
1509+
set(${val1} ${val2})
1510+
endforeach()
1511+
endif()
1512+
14651513
configure_file(${site_in} ${site_out} @ONLY)
1514+
14661515
if (EXISTS "${ARG_MAIN_CONFIG}")
14671516
set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')")
14681517
get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP)

llvm/test/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,23 @@ configure_lit_site_cfg(
2222
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
2323
MAIN_CONFIG
2424
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
25+
PATHS
26+
"LLVM_SOURCE_DIR"
27+
"LLVM_BINARY_DIR"
28+
"LLVM_TOOLS_DIR"
29+
"LLVM_LIBRARY_DIR"
30+
"SHLIBDIR"
2531
)
2632
configure_lit_site_cfg(
2733
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
2834
${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py
2935
MAIN_CONFIG
3036
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py
37+
PATHS
38+
"LLVM_SOURCE_DIR"
39+
"LLVM_BINARY_DIR"
40+
"LLVM_TOOLS_DIR"
41+
"SHLIBDIR"
3142
)
3243

3344
# Set the depends list as a variable so that it can grow conditionally.

llvm/test/Unit/lit.site.cfg.py.in

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import sys
44

5-
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
6-
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
7-
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
5+
config.llvm_src_root = path(r"@LLVM_SOURCE_DIR@")
6+
config.llvm_obj_root = path(r"@LLVM_BINARY_DIR@")
7+
config.llvm_tools_dir = path(r"@LLVM_TOOLS_DIR@")
88
config.llvm_build_mode = "@LLVM_BUILD_MODE@"
99
config.enable_shared = @ENABLE_SHARED@
10-
config.shlibdir = "@SHLIBDIR@"
10+
config.shlibdir = path(r"@SHLIBDIR@")
1111

1212
# Support substitution of the tools_dir and build_mode with user parameters.
1313
# This is used when we can't determine the tool dir at configuration time.
@@ -20,4 +20,5 @@ except KeyError:
2020
lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key))
2121

2222
# Let the main config do the real work.
23-
lit_config.load_config(config, "@LLVM_SOURCE_DIR@/test/Unit/lit.cfg.py")
23+
lit_config.load_config(
24+
config, os.path.join(config.llvm_src_root, "test/Unit/lit.cfg.py"))

llvm/test/lit.site.cfg.py.in

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import sys
44

55
config.host_triple = "@LLVM_HOST_TRIPLE@"
66
config.target_triple = "@TARGET_TRIPLE@"
7-
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
8-
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
9-
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
10-
config.llvm_lib_dir = "@LLVM_LIBRARY_DIR@"
11-
config.llvm_shlib_dir = "@SHLIBDIR@"
7+
config.llvm_src_root = path(r"@LLVM_SOURCE_DIR@")
8+
config.llvm_obj_root = path(r"@LLVM_BINARY_DIR@")
9+
config.llvm_tools_dir = path(r"@LLVM_TOOLS_DIR@")
10+
config.llvm_lib_dir = path(r"@LLVM_LIBRARY_DIR@")
11+
config.llvm_shlib_dir = path(r"@SHLIBDIR@")
1212
config.llvm_shlib_ext = "@SHLIBEXT@"
1313
config.llvm_exe_ext = "@EXEEXT@"
14-
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
14+
config.lit_tools_dir = path(r"@LLVM_LIT_TOOLS_DIR@")
1515
config.python_executable = "@PYTHON_EXECUTABLE@"
1616
config.gold_executable = "@GOLD_EXECUTABLE@"
1717
config.ld64_executable = "@LD64_EXECUTABLE@"
@@ -63,4 +63,5 @@ import lit.llvm
6363
lit.llvm.initialize(lit_config, config)
6464

6565
# Let the main config do the real work.
66-
lit_config.load_config(config, "@LLVM_SOURCE_DIR@/test/lit.cfg.py")
66+
lit_config.load_config(
67+
config, os.path.join(config.llvm_src_root, "test/lit.cfg.py"))

llvm/utils/gn/secondary/clang/test/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ template("write_lit_config") {
1212
input = invoker.input
1313
output = invoker.output
1414
values = [
15-
"LIT_SITE_CFG_IN_HEADER=## Autogenerated from $input, do not edit",
15+
# FIXME: Write relative paths for path()s.
16+
"LIT_SITE_CFG_IN_HEADER=" +
17+
"## Autogenerated from $input, do not edit\n\n" +
18+
"def path(p): return p if p else \"\"",
1619
"CLANG_BINARY_DIR=" +
1720
rebase_path(get_label_info("//clang", "target_out_dir")),
1821
"CLANG_SOURCE_DIR=" + rebase_path("//clang"),

llvm/utils/gn/secondary/llvm/test/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ template("write_lit_config") {
1414
input = invoker.input
1515
output = invoker.output
1616
values = [
17-
"LIT_SITE_CFG_IN_HEADER=## Autogenerated from $input, do not edit",
17+
# FIXME: Write relative paths for path()s.
18+
"LIT_SITE_CFG_IN_HEADER=" +
19+
"## Autogenerated from $input, do not edit\n\n" +
20+
"def path(p): return p if p else \"\"",
1821
"ENABLE_SHARED=0",
1922
"LLVM_BINARY_DIR=" +
2023
rebase_path(get_label_info("//llvm", "target_out_dir")),

0 commit comments

Comments
 (0)