Skip to content

Commit c4c3883

Browse files
committed
build: use find_package(Python3) if available
This is primarily motivated by the desire to move from Python2 to Python3. `PYTHON_EXECUTABLE` is ambiguous. This explicitly identifies the python interpreter in use. Since the LLVM build seems to be able to completed successfully with python3, use that across the build. The old path aliases `PYTHON_EXECUTABLE` to be treated as Python3.
1 parent a000435 commit c4c3883

File tree

15 files changed

+109
-44
lines changed

15 files changed

+109
-44
lines changed

clang-tools-extra/test/lit.site.cfg.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
88
config.clang_tools_binary_dir = "@CLANG_TOOLS_BINARY_DIR@"
99
config.clang_tools_dir = "@CLANG_TOOLS_DIR@"
1010
config.clang_libs_dir = "@SHLIBDIR@"
11-
config.python_executable = "@PYTHON_EXECUTABLE@"
11+
config.python_executable = "@Python3_EXECUTABLE@"
1212
config.target_triple = "@TARGET_TRIPLE@"
1313
config.clang_staticanalyzer = @CLANG_ENABLE_STATIC_ANALYZER@
1414

clang/CMakeLists.txt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,38 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
130130
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
131131

132132
if(LLVM_INCLUDE_TESTS)
133-
include(FindPythonInterp)
134-
if(NOT PYTHONINTERP_FOUND)
135-
message(FATAL_ERROR
136-
"Unable to find Python interpreter, required for builds and testing.
133+
if(CMAKE_VERSION VERSION_LESS 3.12)
134+
include(FindPythonInterp)
135+
if(NOT PYTHONINTERP_FOUND)
136+
message(FATAL_ERROR
137+
"Unable to find Python interpreter, required for builds and testing.
137138
138-
Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
139-
endif()
139+
Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
140+
endif()
141+
142+
if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
143+
message(FATAL_ERROR "Python 2.7 or newer is required")
144+
endif()
140145

141-
if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
142-
message(FATAL_ERROR "Python 2.7 or newer is required")
146+
add_executable(Python3::Interpreter IMPORTED)
147+
set_target_properties(Python3::Interpreter PROPERTIES
148+
IMPORTED_LOCATION ${PYTHON_EXECUTABLE})
149+
set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
150+
else()
151+
find_package(Python3 COMPONENTS Interpreter)
152+
if(NOT Python3_Interpreter_FOUND)
153+
message(WARNING "Python3 not found, using python2 as a fallback")
154+
find_package(Python2 COMPONENTS Interpreter REQUIRED)
155+
if(Python2_VERSION VERSION_LESS 2.7)
156+
message(SEND_ERROR "Python 2.7 or newer is required")
157+
endif()
158+
159+
# Treat python2 as python3
160+
add_executable(Python3::Interpreter IMPORTED)
161+
set_target_properties(Python3::Interpreter PROPERTIES
162+
IMPORTED_LOCATION ${Python2_EXECUTABLE})
163+
set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
164+
endif()
143165
endif()
144166

145167
# Check prebuilt llvm/utils.

clang/bindings/python/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
add_custom_target(check-clang-python
44
COMMAND ${CMAKE_COMMAND} -E env
55
CLANG_LIBRARY_PATH=$<TARGET_FILE_DIR:libclang>
6-
${PYTHON_EXECUTABLE} -m unittest discover
6+
"${Python3_EXECUTABLE}" -m unittest discover
77
DEPENDS libclang
88
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..)
99

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ config.enable_backtrace = @ENABLE_BACKTRACES@
2727
config.enable_experimental_new_pass_manager = @ENABLE_EXPERIMENTAL_NEW_PASS_MANAGER@
2828
config.enable_threads = @LLVM_ENABLE_THREADS@
2929
config.host_arch = "@HOST_ARCH@"
30-
config.python_executable = "@PYTHON_EXECUTABLE@"
30+
config.python_executable = "@Python3_EXECUTABLE@"
3131
config.use_z3_solver = lit_config.params.get('USE_Z3_SOLVER', "@USE_Z3_SOLVER@")
3232
config.has_plugins = @LLVM_ENABLE_PLUGINS@
3333

clang/utils/perf-training/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if(LLVM_BUILD_INSTRUMENTED)
2323
)
2424

2525
add_custom_target(clear-profraw
26-
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} profraw
26+
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} profraw
2727
COMMENT "Clearing old profraw data")
2828

2929
if(NOT LLVM_PROFDATA)
@@ -34,7 +34,7 @@ if(LLVM_BUILD_INSTRUMENTED)
3434
message(STATUS "To enable merging PGO data LLVM_PROFDATA has to point to llvm-profdata")
3535
else()
3636
add_custom_target(generate-profdata
37-
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py merge ${LLVM_PROFDATA} ${CMAKE_CURRENT_BINARY_DIR}/clang.profdata ${CMAKE_CURRENT_BINARY_DIR}
37+
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py merge ${LLVM_PROFDATA} ${CMAKE_CURRENT_BINARY_DIR}/clang.profdata ${CMAKE_CURRENT_BINARY_DIR}
3838
COMMENT "Merging profdata"
3939
DEPENDS generate-profraw)
4040
endif()
@@ -55,15 +55,15 @@ if(APPLE AND DTRACE)
5555
)
5656

5757
add_custom_target(clear-dtrace-logs
58-
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} dtrace
58+
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} dtrace
5959
COMMENT "Clearing old dtrace data")
6060

6161
if(NOT CLANG_ORDER_FILE)
6262
message(FATAL_ERROR "Output clang order file is not set")
6363
endif()
6464

6565
add_custom_target(generate-order-file
66-
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py gen-order-file --binary $<TARGET_FILE:clang> --output ${CLANG_ORDER_FILE} ${CMAKE_CURRENT_BINARY_DIR}
66+
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py gen-order-file --binary $<TARGET_FILE:clang> --output ${CLANG_ORDER_FILE} ${CMAKE_CURRENT_BINARY_DIR}
6767
COMMENT "Generating order file"
6868
DEPENDS generate-dtrace-logs)
6969
endif()

clang/utils/perf-training/lit.site.cfg.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ config.perf_helper_dir = "@CMAKE_CURRENT_SOURCE_DIR@"
77
config.test_exec_root = "@CMAKE_CURRENT_BINARY_DIR@"
88
config.test_source_root = "@CLANG_PGO_TRAINING_DATA@"
99
config.target_triple = "@TARGET_TRIPLE@"
10-
config.python_exe = "@PYTHON_EXECUTABLE@"
10+
config.python_exe = "@Python3_EXECUTABLE@"
1111

1212
# Support substitution of the tools and libs dirs with user parameters. This is
1313
# used when we can't determine the tool dir at configuration time.

clang/utils/perf-training/order-files.lit.site.cfg.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ config.perf_helper_dir = "@CMAKE_CURRENT_SOURCE_DIR@"
77
config.test_exec_root = "@CMAKE_CURRENT_BINARY_DIR@"
88
config.test_source_root = "@CLANG_PGO_TRAINING_DATA@"
99
config.target_triple = "@TARGET_TRIPLE@"
10-
config.python_exe = "@PYTHON_EXECUTABLE@"
10+
config.python_exe = "@Python3_EXECUTABLE@"
1111

1212
# Support substitution of the tools and libs dirs with user parameters. This is
1313
# used when we can't determine the tool dir at configuration time.

lld/CMakeLists.txt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,38 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
5656
include(HandleLLVMOptions)
5757

5858
if(LLVM_INCLUDE_TESTS)
59-
include(FindPythonInterp)
60-
if(NOT PYTHONINTERP_FOUND)
61-
message(FATAL_ERROR
62-
"Unable to find Python interpreter, required for testing.
59+
if(CMAKE_VERSION VERSION_LESS 3.12)
60+
include(FindPythonInterp)
61+
if(NOT PYTHONINTERP_FOUND)
62+
message(FATAL_ERROR
63+
"Unable to find Python interpreter, required for testing.
6364
64-
Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
65-
endif()
65+
Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
66+
endif()
67+
68+
if(${PYTHON_VERSION_STRING} VERSION_LESS 2.7)
69+
message(FATAL_ERROR "Python 2.7 or newer is required")
70+
endif()
6671

67-
if(${PYTHON_VERSION_STRING} VERSION_LESS 2.7)
68-
message(FATAL_ERROR "Python 2.7 or newer is required")
72+
add_executable(Python3::Interpeter IMPORTED)
73+
set_target_properties(Python3::Interpreter PROPERTIES
74+
IMPORTED_LOCATION ${PYTHON_EXECUTABLE})
75+
set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
76+
else()
77+
find_package(Python3 COMPONENTS Interpreter)
78+
if(NOT Python3_Interpreter_FOUND)
79+
message(WARNING "Python3 not found, using python2 as a fallback")
80+
find_package(Python2 COMPONENTS Interpreter REQUIRED)
81+
if(Python2_VERSION VERSION_LESS 2.7)
82+
message(SEND_ERROR "Python 2.7 or newer is required")
83+
endif()
84+
85+
# Treat python2 as python3
86+
add_executable(Python3::Interpreter IMPORTED)
87+
set_target_properties(Python3::Interpreter PROPERTIES
88+
IMPORTED_LOCATION ${Python2_EXECUTABLE})
89+
set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
90+
endif()
6991
endif()
7092

7193
# Check prebuilt llvm/utils.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ config.lld_src_root = "@LLD_SOURCE_DIR@"
1111
config.lld_libs_dir = "@LLVM_LIBRARY_OUTPUT_INTDIR@"
1212
config.lld_tools_dir = "@LLVM_RUNTIME_OUTPUT_INTDIR@"
1313
config.target_triple = "@TARGET_TRIPLE@"
14-
config.python_executable = "@PYTHON_EXECUTABLE@"
14+
config.python_executable = "@Python3_EXECUTABLE@"
1515

1616
# Support substitution of the tools and libs dirs with user parameters. This is
1717
# used when we can't determine the tool dir at configuration time.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ config.lld_obj_root = "@LLD_BINARY_DIR@"
1313
config.lld_libs_dir = "@LLVM_LIBRARY_OUTPUT_INTDIR@"
1414
config.lld_tools_dir = "@LLVM_RUNTIME_OUTPUT_INTDIR@"
1515
config.target_triple = "@TARGET_TRIPLE@"
16-
config.python_executable = "@PYTHON_EXECUTABLE@"
16+
config.python_executable = "@Python3_EXECUTABLE@"
1717
config.have_zlib = @HAVE_LIBZ@
1818
config.sizeof_void_p = @CMAKE_SIZEOF_VOID_P@
1919

0 commit comments

Comments
 (0)