Skip to content

Commit cd84bfb

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 29c6f5c commit cd84bfb

File tree

12 files changed

+77
-34
lines changed

12 files changed

+77
-34
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.

llvm/CMakeLists.txt

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -672,16 +672,38 @@ option(LLVM_ENABLE_PLUGINS "Enable plugin support" ${LLVM_ENABLE_PLUGINS_default
672672

673673
include(HandleLLVMOptions)
674674

675-
include(FindPythonInterp)
676-
if( NOT PYTHONINTERP_FOUND )
677-
message(FATAL_ERROR
678-
"Unable to find Python interpreter, required for builds and testing.
675+
if(CMAKE_VERSION VERSION_LESS 3.12)
676+
include(FindPythonInterp)
677+
if( NOT PYTHONINTERP_FOUND )
678+
message(FATAL_ERROR
679+
"Unable to find Python interpreter, required for builds and testing.
679680
680-
Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
681-
endif()
681+
Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
682+
endif()
683+
684+
if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
685+
message(FATAL_ERROR "Python 2.7 or newer is required")
686+
endif()
682687

683-
if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
684-
message(FATAL_ERROR "Python 2.7 or newer is required")
688+
add_executable(Python3::Interpreter IMPORTED)
689+
set_target_properties(Python3::Interpreter PROPERTIES
690+
IMPORTED_LOCATION ${PYTHON_EXECUTABLE})
691+
set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
692+
else()
693+
find_package(Python3 COMPONENTS Interpreter)
694+
if(NOT Python3_Interpreter_FOUND)
695+
message(WARNING "Python3 not found, using python2 as a fallback")
696+
find_package(Python2 COMPONENTS Interpreter REQUIRED)
697+
if(Python2_VERSION VERSION_LESS 2.7)
698+
message(SEND_ERROR "Python 2.7 or newer is required")
699+
endif()
700+
701+
# Treat python2 as python3
702+
add_executable(Python3::Interpreter IMPORTED)
703+
set_target_properties(Python3::Interpreter PROPERTIES
704+
IMPORTED_LOCATION ${Python2_EXECUTABLE})
705+
set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
706+
endif()
685707
endif()
686708

687709
######
@@ -717,7 +739,7 @@ endif (LLVM_USE_PERF)
717739

718740
message(STATUS "Constructing LLVMBuild project information")
719741
execute_process(
720-
COMMAND ${PYTHON_EXECUTABLE} -B ${LLVMBUILDTOOL}
742+
COMMAND "${Python3_EXECUTABLE}" -B ${LLVMBUILDTOOL}
721743
--native-target "${LLVM_NATIVE_ARCH}"
722744
--enable-targets "${LLVM_TARGETS_TO_BUILD}"
723745
--enable-optional-components "${LLVMOPTIONALCOMPONENTS}"

llvm/cmake/config-ix.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ function(find_python_module module)
639639
return()
640640
endif()
641641

642-
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" "import ${module}"
642+
execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import ${module}"
643643
RESULT_VARIABLE status
644644
ERROR_QUIET)
645645

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function(add_llvm_symbol_exports target_name export_file)
117117
set(native_export_file "${target_name}.def")
118118

119119
add_custom_command(OUTPUT ${native_export_file}
120-
COMMAND ${PYTHON_EXECUTABLE} -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
120+
COMMAND "${Python3_EXECUTABLE}" -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))"
121121
< ${export_file} > ${native_export_file}
122122
DEPENDS ${export_file}
123123
VERBATIM
@@ -1046,7 +1046,7 @@ function(export_executable_symbols target)
10461046
set(mangling itanium)
10471047
endif()
10481048
add_custom_command(OUTPUT ${exported_symbol_file}
1049-
COMMAND ${PYTHON_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
1049+
COMMAND "${Python3_EXECUTABLE}" ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file}
10501050
WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
10511051
DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs}
10521052
VERBATIM
@@ -1447,7 +1447,7 @@ function(make_paths_relative out_pathlist basedir pathlist)
14471447
# empty list entries. So escape the ;s in the list and do the splitting
14481448
# ourselves. cmake has no relpath function, so use Python for that.
14491449
string(REPLACE ";" "\\;" pathlist_escaped "${pathlist}")
1450-
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" "\n
1450+
execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "\n
14511451
import os, sys\n
14521452
base = sys.argv[1]
14531453
def haslink(p):\n
@@ -1522,7 +1522,6 @@ function(configure_lit_site_cfg site_in site_out)
15221522
# SHLIBDIR points the build tree.
15231523
string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}")
15241524

1525-
set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
15261525
# FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for
15271526
# plugins. We may rename it.
15281527
if(LLVM_ENABLE_PLUGINS)
@@ -1680,7 +1679,7 @@ function(add_lit_target target comment)
16801679
ALLOW_EXTERNAL
16811680
)
16821681

1683-
set(LIT_COMMAND "${PYTHON_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
1682+
set(LIT_COMMAND "${Python3_EXECUTABLE};${lit_base_dir}/${lit_file_name}")
16841683
list(APPEND LIT_COMMAND ${LIT_ARGS})
16851684
foreach(param ${ARG_PARAMS})
16861685
list(APPEND LIT_COMMAND --param ${param})

0 commit comments

Comments
 (0)