Index: clang/test/CMakeLists.txt =================================================================== --- clang/test/CMakeLists.txt +++ clang/test/CMakeLists.txt @@ -35,6 +35,16 @@ ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py MAIN_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py + PATHS + "LLVM_SOURCE_DIR" + "LLVM_BINARY_DIR" + "LLVM_TOOLS_DIR" + "LLVM_LIBS_DIR" + "SHLIBDIR" + "LLVM_LIT_TOOLS_DIR" + "CLANG_BINARY_DIR" + "CLANG_SOURCE_DIR" + "CLANG_TOOLS_DIR" ) configure_lit_site_cfg( @@ -42,6 +52,13 @@ ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py MAIN_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py + PATHS + "LLVM_SOURCE_DIR" + "LLVM_BINARY_DIR" + "LLVM_TOOLS_DIR" + "LLVM_LIBS_DIR" + "CLANG_BINARY_DIR" + "SHLIBDIR" ) option(CLANG_TEST_USE_VG "Run Clang tests under Valgrind" OFF) Index: clang/test/Unit/lit.site.cfg.py.in =================================================================== --- clang/test/Unit/lit.site.cfg.py.in +++ clang/test/Unit/lit.site.cfg.py.in @@ -2,14 +2,14 @@ import sys -config.llvm_src_root = "@LLVM_SOURCE_DIR@" -config.llvm_obj_root = "@LLVM_BINARY_DIR@" -config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" -config.llvm_libs_dir = "@LLVM_LIBS_DIR@" +config.llvm_src_root = path("@LLVM_SOURCE_DIR@") +config.llvm_obj_root = path("@LLVM_BINARY_DIR@") +config.llvm_tools_dir = path("@LLVM_TOOLS_DIR@") +config.llvm_libs_dir = path("@LLVM_LIBS_DIR@") config.llvm_build_mode = "@LLVM_BUILD_MODE@" -config.clang_obj_root = "@CLANG_BINARY_DIR@" +config.clang_obj_root = path("@CLANG_BINARY_DIR@") config.enable_shared = @ENABLE_SHARED@ -config.shlibdir = "@SHLIBDIR@" +config.shlibdir = path("@SHLIBDIR@") config.target_triple = "@TARGET_TRIPLE@" # Support substitution of the tools_dir, libs_dirs, and build_mode with user @@ -25,4 +25,5 @@ lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key)) # Let the main config do the real work. -lit_config.load_config(config, "@CLANG_SOURCE_DIR@/test/Unit/lit.cfg.py") +lit_config.load_config( + config, os.path.join(path("@CLANG_SOURCE_DIR@"), "test/Unit/lit.cfg.py")) Index: clang/test/lit.site.cfg.py.in =================================================================== --- clang/test/lit.site.cfg.py.in +++ clang/test/lit.site.cfg.py.in @@ -2,16 +2,16 @@ import sys -config.llvm_src_root = "@LLVM_SOURCE_DIR@" -config.llvm_obj_root = "@LLVM_BINARY_DIR@" -config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" -config.llvm_libs_dir = "@LLVM_LIBS_DIR@" -config.llvm_shlib_dir = "@SHLIBDIR@" +config.llvm_src_root = path("@LLVM_SOURCE_DIR@") +config.llvm_obj_root = path("@LLVM_BINARY_DIR@") +config.llvm_tools_dir = path("@LLVM_TOOLS_DIR@") +config.llvm_libs_dir = path("@LLVM_LIBS_DIR@") +config.llvm_shlib_dir = path("@SHLIBDIR@") config.llvm_plugin_ext = "@LLVM_PLUGIN_EXT@" -config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" -config.clang_obj_root = "@CLANG_BINARY_DIR@" -config.clang_src_dir = "@CLANG_SOURCE_DIR@" -config.clang_tools_dir = "@CLANG_TOOLS_DIR@" +config.lit_tools_dir = path("@LLVM_LIT_TOOLS_DIR@") +config.clang_obj_root = path("@CLANG_BINARY_DIR@") +config.clang_src_dir = path("@CLANG_SOURCE_DIR@") +config.clang_tools_dir = path("@CLANG_TOOLS_DIR@") config.host_triple = "@LLVM_HOST_TRIPLE@" config.target_triple = "@TARGET_TRIPLE@" config.host_cxx = "@CMAKE_CXX_COMPILER@" @@ -50,4 +50,5 @@ config.available_features.add('platform-linker') # Let the main config do the real work. -lit_config.load_config(config, "@CLANG_SOURCE_DIR@/test/lit.cfg.py") +lit_config.load_config( + config, os.path.join(config.clang_src_dir, "test/lit.cfg.py")) Index: llvm/cmake/modules/AddLLVM.cmake =================================================================== --- llvm/cmake/modules/AddLLVM.cmake +++ llvm/cmake/modules/AddLLVM.cmake @@ -1396,8 +1396,14 @@ # variables needed for the 'lit.site.cfg' files. This function bundles the # common variables that any Lit instance is likely to need, and custom # variables can be passed in. +# The keyword PATHS is followed by a list of cmake variable names that are +# mentioned as `path("@varname@")` in the lit.cfg.py.in file. Variables in that +# list are treated as paths that are relative to the directory the generated +# lit.cfg.py file is in, and the `path()` function converts the relative +# path back to absolute form. This makes it possible to move a build directory +# containing lit.cfg.py files from one machine to another. function(configure_lit_site_cfg site_in site_out) - cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING" ${ARGN}) + cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING;PATHS" ${ARGN}) if ("${ARG_MAIN_CONFIG}" STREQUAL "") get_filename_component(INPUT_DIR ${site_in} DIRECTORY) @@ -1447,12 +1453,17 @@ set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}") set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}") - set(LIT_SITE_CFG_IN_HEADER "## Autogenerated from ${site_in}\n## Do not edit!") + set(LIT_SITE_CFG_IN_HEADER "# Autogenerated from ${site_in}\n# Do not edit!") + + string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n" + "# Allow generated lit.site.cfg.py to be relocatable.\n" + "def path(p): return os.path.join(os.path.dirname(__file__), p) if p else ''\n" + ) # Override config_target_triple (and the env) if(LLVM_TARGET_TRIPLE_ENV) # This is expanded into the heading. - string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}\n\n" + string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}" "import os\n" "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n" "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n" @@ -1462,7 +1473,53 @@ set(TARGET_TRIPLE "\"+config.target_triple+\"") endif() + # Walk ARG_PATHS and collect the current value of the variables in there. + foreach(path ${ARG_PATHS}) + list(APPEND ARG_PATH_VALUES "${${path}}") + endforeach() + + get_filename_component(OUTPUT_DIR ${site_out} DIRECTORY) + + set(arg_path_limit 0) + if (ARG_PATH_VALUES) + # Passign ARG_PATH_VALUES as-is to execute_process() makes cmake strip + # empty list entries. So escape the ;s in the list and do the splitting + # outselves. cmake has no relpath function, so use Python for that. + string(REPLACE ";" "\\;" ARG_PATH_VALUES_ESCAPED "${ARG_PATH_VALUES}") + execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" + "import os, sys; sys.stdout.write(';'.join(os.path.relpath(p, sys.argv[1]) if p else '' for p in sys.argv[2].split(';')))" + ${OUTPUT_DIR} + ${ARG_PATH_VALUES_ESCAPED} + OUTPUT_VARIABLE ARG_PATH_VALUES_RELATIVE) + + list(LENGTH ARG_PATHS len_vars) + list(LENGTH ARG_PATH_VALUES len_values) + list(LENGTH ARG_PATH_VALUES_RELATIVE len_rels) + if ((NOT ${len_vars} EQUAL ${len_values}) OR + (NOT ${len_vars} EQUAL ${len_rels})) + message(SEND_ERROR "PATHS lengths got confused") + endif() + + math(EXPR arg_path_limit "${len_rels} - 1") + + # Transform variables mentioned in ARG_PATHS to relative paths for + # the configure_file() call. + foreach(i RANGE ${arg_path_limit}) + list(GET ARG_PATHS ${i} val1) + list(GET ARG_PATH_VALUES_RELATIVE ${i} val2) + set(${val1} ${val2}) + endforeach() + endif() + configure_file(${site_in} ${site_out} @ONLY) + + # Restore ARG_PATHS variables to their original values. + foreach(i RANGE ${arg_path_limit}) + list(GET ARG_PATHS ${i} val1) + list(GET ARG_PATH_VALUES ${i} val2) + set(${val1} ${val2}) + endforeach() + if (EXISTS "${ARG_MAIN_CONFIG}") set(PYTHON_STATEMENT "map_config('${ARG_MAIN_CONFIG}', '${site_out}')") get_property(LLVM_LIT_CONFIG_MAP GLOBAL PROPERTY LLVM_LIT_CONFIG_MAP) Index: llvm/test/CMakeLists.txt =================================================================== --- llvm/test/CMakeLists.txt +++ llvm/test/CMakeLists.txt @@ -22,12 +22,23 @@ ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py MAIN_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py + PATHS + "LLVM_SOURCE_DIR" + "LLVM_BINARY_DIR" + "LLVM_TOOLS_DIR" + "LLVM_LIBRARY_DIR" + "SHLIBDIR" ) configure_lit_site_cfg( ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py MAIN_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py + PATHS + "LLVM_SOURCE_DIR" + "LLVM_BINARY_DIR" + "LLVM_TOOLS_DIR" + "SHLIBDIR" ) # Set the depends list as a variable so that it can grow conditionally. Index: llvm/test/Unit/lit.site.cfg.py.in =================================================================== --- llvm/test/Unit/lit.site.cfg.py.in +++ llvm/test/Unit/lit.site.cfg.py.in @@ -2,12 +2,12 @@ import sys -config.llvm_src_root = "@LLVM_SOURCE_DIR@" -config.llvm_obj_root = "@LLVM_BINARY_DIR@" -config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" +config.llvm_src_root = path("@LLVM_SOURCE_DIR@") +config.llvm_obj_root = path("@LLVM_BINARY_DIR@") +config.llvm_tools_dir = path("@LLVM_TOOLS_DIR@") config.llvm_build_mode = "@LLVM_BUILD_MODE@" config.enable_shared = @ENABLE_SHARED@ -config.shlibdir = "@SHLIBDIR@" +config.shlibdir = path("@SHLIBDIR@") # Support substitution of the tools_dir and build_mode with user parameters. # This is used when we can't determine the tool dir at configuration time. @@ -20,4 +20,5 @@ lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key)) # Let the main config do the real work. -lit_config.load_config(config, "@LLVM_SOURCE_DIR@/test/Unit/lit.cfg.py") +lit_config.load_config( + config, os.path.join(config.llvm_src_root, "test/Unit/lit.cfg.py")) Index: llvm/test/lit.site.cfg.py.in =================================================================== --- llvm/test/lit.site.cfg.py.in +++ llvm/test/lit.site.cfg.py.in @@ -4,14 +4,14 @@ config.host_triple = "@LLVM_HOST_TRIPLE@" config.target_triple = "@TARGET_TRIPLE@" -config.llvm_src_root = "@LLVM_SOURCE_DIR@" -config.llvm_obj_root = "@LLVM_BINARY_DIR@" -config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" -config.llvm_lib_dir = "@LLVM_LIBRARY_DIR@" -config.llvm_shlib_dir = "@SHLIBDIR@" +config.llvm_src_root = path("@LLVM_SOURCE_DIR@") +config.llvm_obj_root = path("@LLVM_BINARY_DIR@") +config.llvm_tools_dir = path("@LLVM_TOOLS_DIR@") +config.llvm_lib_dir = path("@LLVM_LIBRARY_DIR@") +config.llvm_shlib_dir = path("@SHLIBDIR@") config.llvm_shlib_ext = "@SHLIBEXT@" config.llvm_exe_ext = "@EXEEXT@" -config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" +config.lit_tools_dir = path("@LLVM_LIT_TOOLS_DIR@") config.python_executable = "@PYTHON_EXECUTABLE@" config.gold_executable = "@GOLD_EXECUTABLE@" config.ld64_executable = "@LD64_EXECUTABLE@" @@ -63,4 +63,5 @@ lit.llvm.initialize(lit_config, config) # Let the main config do the real work. -lit_config.load_config(config, "@LLVM_SOURCE_DIR@/test/lit.cfg.py") +lit_config.load_config( + config, os.path.join(config.llvm_src_root, "test/lit.cfg.py"))