-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
174 lines (148 loc) · 6.2 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 3.28)
project(test_project_template)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
find_package(Clang REQUIRED)
function(make_paths_relative out_pathlist basedir pathlist)
# Passing 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
# ourselves. cmake has no relpath function, so use Python for that.
string(REPLACE ";" "\\;" pathlist_escaped "${pathlist}")
execute_process(COMMAND "${Python_EXECUTABLE}" "-c" "#!/usr/bin/python3\n
import os, sys\n
base = sys.argv[1]\n
def haslink(p):\n
if not p or p == os.path.dirname(p): return False\n
return os.path.islink(p) or haslink(os.path.dirname(p))\n
def relpath(p):\n
if not p: return ''\n
if os.path.splitdrive(p)[0] != os.path.splitdrive(base)[0]: return p\n
if haslink(p) or haslink(base): return p\n
return os.path.relpath(p, base)\n
if len(sys.argv) < 3: sys.exit(0)\n
sys.stdout.write(';'.join(relpath(p) for p in sys.argv[2].split(';')))\n
"
${basedir}
${pathlist_escaped}
OUTPUT_VARIABLE pathlist_relative
ERROR_VARIABLE error
RESULT_VARIABLE result)
if (NOT result EQUAL 0)
message(FATAL_ERROR "make_paths_relative() failed due to error '${result}', with stderr\n${error}")
endif()
set(${out_pathlist} "${pathlist_relative}" PARENT_SCOPE)
endfunction()
macro(set_llvm_build_mode)
# Configuration-time: See Unit/lit.site.cfg.in
if (CMAKE_CFG_INTDIR STREQUAL ".")
set(LLVM_BUILD_MODE ".")
else ()
set(LLVM_BUILD_MODE "%(build_mode)s")
endif ()
endmacro()
string(CONCAT LLVM_LIT_PATH_FUNCTION
"# Allow generated file to be relocatable.\n"
"from pathlib import Path\n"
"def path(p):\n"
" if not p: return ''\n"
" return str((Path(__file__).parent / p).resolve())\n"
)
function(configure_lit_site_cfg site_in site_out)
cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING;PATHS" ${ARGN})
if ("${ARG_MAIN_CONFIG}" STREQUAL "")
get_filename_component(INPUT_DIR ${site_in} DIRECTORY)
set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg")
endif()
if ("${ARG_OUTPUT_MAPPING}" STREQUAL "")
set(ARG_OUTPUT_MAPPING "${site_out}")
endif()
foreach(c ${LLVM_TARGETS_TO_BUILD})
set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
endforeach(c)
set(TARGETS_TO_BUILD ${TARGETS_BUILT})
set(SHLIBEXT "${LTDL_SHLIB_EXT}")
set_llvm_build_mode()
# The below might not be the build tree but provided binary tree.
set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}")
string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}")
# SHLIBDIR points the build tree.
string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
# FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
# plugins. We may rename it.
if(LLVM_ENABLE_PLUGINS)
set(ENABLE_SHARED "1")
else()
set(ENABLE_SHARED "0")
endif()
if(LLVM_ENABLE_ASSERTIONS)
set(ENABLE_ASSERTIONS "1")
else()
set(ENABLE_ASSERTIONS "0")
endif()
set(HOST_OS ${CMAKE_SYSTEM_NAME})
set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}")
string(CONCAT LIT_SITE_CFG_IN_HEADER
"# Autogenerated from ${site_in}\n# Do not edit!\n\n"
"${LLVM_LIT_PATH_FUNCTION}"
)
# 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}"
"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"
)
# This is expanded to; config.target_triple = ""+config.target_triple+""
set(TARGET_TRIPLE "\"+config.target_triple+\"")
endif()
if (ARG_PATHS)
# Walk ARG_PATHS and collect the current value of the variables in there.
# list(APPEND) ignores empty elements exactly if the list is empty,
# so start the list with a dummy element and drop it, to make sure that
# even empty values make it into the values list.
set(ARG_PATH_VALUES "dummy")
foreach(path ${ARG_PATHS})
list(APPEND ARG_PATH_VALUES "${${path}}")
endforeach()
list(REMOVE_AT ARG_PATH_VALUES 0)
get_filename_component(OUTPUT_DIR ${site_out} DIRECTORY)
make_paths_relative(
ARG_PATH_VALUES_RELATIVE "${OUTPUT_DIR}" "${ARG_PATH_VALUES}")
list(LENGTH ARG_PATHS len_paths)
list(LENGTH ARG_PATH_VALUES len_path_values)
list(LENGTH ARG_PATH_VALUES_RELATIVE len_path_value_rels)
if ((NOT ${len_paths} EQUAL ${len_path_values}) OR
(NOT ${len_paths} EQUAL ${len_path_value_rels}))
message(SEND_ERROR "PATHS lengths got confused")
endif()
# Transform variables mentioned in ARG_PATHS to relative paths for
# the configure_file() call. Variables are copied to subscopeds by cmake,
# so this only modifies the local copy of the variables.
math(EXPR arg_path_limit "${len_paths} - 1")
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)
if (EXISTS "${ARG_MAIN_CONFIG}")
# Remember main config / generated site config for llvm-lit.in.
get_property(LLVM_LIT_CONFIG_FILES GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES)
list(APPEND LLVM_LIT_CONFIG_FILES "${ARG_MAIN_CONFIG}" "${site_out}")
set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES ${LLVM_LIT_CONFIG_FILES})
endif()
endfunction()
configure_lit_site_cfg(
"${CMAKE_CURRENT_SOURCE_DIR}/test/lit.site.cfg.py.in"
"${CMAKE_CURRENT_BINARY_DIR}/test/lit.site.cfg.py"
MAIN_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/lit.cfg.py"
)