Skip to content

Commit 61f471a

Browse files
committedOct 7, 2019
[lldb] Unifying lldb python path
Based on mgorny@'s D67890 There are 3 places where python site-package path is calculated independently: 1. finishSwigPythonLLDB.py where files are written to site-packages. 2. lldb/scripts/CMakeLists.txt where site-packages are installed. 3. ScriptInterpreterPython.cpp where site-packages are added to PYTHONPATH. This change creates the path once and use it everywhere. So that they will not go out of sync. Also it provides a chance for cross compiling users to specify the right path for site-packages. Subscribers: lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D68442 llvm-svn: 373991
1 parent f4c7345 commit 61f471a

File tree

8 files changed

+49
-214
lines changed

8 files changed

+49
-214
lines changed
 

‎lldb/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ if (WIN32)
3737
endif()
3838

3939
if (NOT LLDB_DISABLE_PYTHON)
40+
execute_process(
41+
COMMAND ${PYTHON_EXECUTABLE}
42+
-c "import distutils.sysconfig; print(distutils.sysconfig.get_python_lib(False, False, ''))"
43+
OUTPUT_VARIABLE LLDB_PYTHON_DEFAULT_RELATIVE_PATH
44+
OUTPUT_STRIP_TRAILING_WHITESPACE)
45+
46+
file(TO_CMAKE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH} LLDB_PYTHON_DEFAULT_RELATIVE_PATH)
47+
set(LLDB_PYTHON_RELATIVE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH}
48+
CACHE STRING "Path where Python modules are installed, relative to install prefix")
49+
4050
add_subdirectory(scripts)
4151
endif ()
4252

@@ -195,6 +205,12 @@ if (NOT LLDB_DISABLE_PYTHON)
195205
get_target_property(lldb_scripts_dir swig_wrapper BINARY_DIR)
196206
get_target_property(liblldb_build_dir liblldb LIBRARY_OUTPUT_DIRECTORY)
197207

208+
if(LLDB_BUILD_FRAMEWORK)
209+
set(lldb_python_build_path "${liblldb_build_dir}/LLDB.framework/Resources/Python/lldb")
210+
else()
211+
set(lldb_python_build_path "${CMAKE_BINARY_DIR}/${LLDB_PYTHON_RELATIVE_PATH}/lldb")
212+
endif()
213+
198214
# Add a Post-Build Event to copy over Python files and create the symlink
199215
# to liblldb.so for the Python API(hardlink on Windows).
200216
add_custom_target(finish_swig ALL
@@ -206,6 +222,7 @@ if (NOT LLDB_DISABLE_PYTHON)
206222
--prefix=${CMAKE_BINARY_DIR}
207223
--cmakeBuildConfiguration=${CMAKE_CFG_INTDIR}
208224
--lldbLibDir=lib${LLVM_LIBDIR_SUFFIX}
225+
--lldbPythonPath=${lldb_python_build_path}
209226
${use_python_wrapper_from_src_dir}
210227
${use_six_py_from_system}
211228
VERBATIM
@@ -219,6 +236,20 @@ if (NOT LLDB_DISABLE_PYTHON)
219236
# Ensure we do the python post-build step when building lldb.
220237
add_dependencies(lldb finish_swig)
221238

239+
if(NOT LLDB_BUILD_FRAMEWORK)
240+
# Install the LLDB python module
241+
add_custom_target(lldb-python-scripts)
242+
add_dependencies(lldb-python-scripts finish_swig)
243+
install(DIRECTORY ${CMAKE_BINARY_DIR}/${LLDB_PYTHON_RELATIVE_PATH}/
244+
DESTINATION ${LLDB_PYTHON_RELATIVE_PATH}
245+
COMPONENT lldb-scripts)
246+
if (NOT LLVM_ENABLE_IDE)
247+
add_llvm_install_targets(install-lldb-python-scripts
248+
COMPONENT lldb-python-scripts
249+
DEPENDS lldb-python-scripts)
250+
endif()
251+
endif()
252+
222253
# Add a Post-Build Event to copy the custom Python DLL to the lldb binaries dir so that Windows can find it when launching
223254
# lldb.exe or any other executables that were linked with liblldb.
224255
if (WIN32 AND NOT "${PYTHON_DLL}" STREQUAL "")

‎lldb/scripts/CMakeLists.txt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,3 @@ add_custom_target(swig_wrapper ALL DEPENDS
5555
${CMAKE_CURRENT_BINARY_DIR}/lldb.py
5656
)
5757

58-
if(NOT LLDB_BUILD_FRAMEWORK)
59-
execute_process(
60-
COMMAND ${PYTHON_EXECUTABLE}
61-
-c "import distutils.sysconfig, sys; print(distutils.sysconfig.get_python_lib(True, False, sys.argv[1]))"
62-
${CMAKE_BINARY_DIR}
63-
OUTPUT_VARIABLE SWIG_PYTHON_DIR
64-
OUTPUT_STRIP_TRAILING_WHITESPACE)
65-
execute_process(
66-
COMMAND ${PYTHON_EXECUTABLE}
67-
-c "import distutils.sysconfig; print(distutils.sysconfig.get_python_lib(True, False, ''))"
68-
OUTPUT_VARIABLE SWIG_INSTALL_DIR
69-
OUTPUT_STRIP_TRAILING_WHITESPACE)
70-
71-
# Install the LLDB python module
72-
add_custom_target(lldb-python-scripts)
73-
add_dependencies(lldb-python-scripts finish_swig)
74-
install(DIRECTORY ${SWIG_PYTHON_DIR}/
75-
DESTINATION ${SWIG_INSTALL_DIR}
76-
COMPONENT lldb-scripts)
77-
if (NOT LLVM_ENABLE_IDE)
78-
add_llvm_install_targets(install-lldb-python-scripts
79-
COMPONENT lldb-python-scripts
80-
DEPENDS lldb-python-scripts)
81-
endif()
82-
endif()

‎lldb/scripts/Python/finishSwigPythonLLDB.py

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -597,86 +597,6 @@ def get_config_build_dir(vDictArgs, vstrFrameworkPythonDir):
597597

598598
return (bOk, strConfigBldDir, strErrMsg)
599599

600-
#++---------------------------------------------------------------------------
601-
# Details: Determine where to put the files. Retrieve the directory path for
602-
# Python's dist_packages/ site_package folder on a Windows platform.
603-
# Args: vDictArgs - (R) Program input parameters.
604-
# Returns: Bool - True = function success, False = failure.
605-
# Str - Python Framework directory path.
606-
# strErrMsg - Error description on task failure.
607-
# Throws: None.
608-
#--
609-
610-
611-
def get_framework_python_dir_windows(vDictArgs):
612-
dbg = utilsDebug.CDebugFnVerbose(
613-
"Python script get_framework_python_dir_windows()")
614-
bOk = True
615-
strWkDir = ""
616-
strErrMsg = ""
617-
618-
# We are being built by LLVM, so use the PYTHON_INSTALL_DIR argument,
619-
# and append the python version directory to the end of it. Depending
620-
# on the system other stuff may need to be put here as well.
621-
from distutils.sysconfig import get_python_lib
622-
strPythonInstallDir = ""
623-
bHaveArgPrefix = "--prefix" in vDictArgs
624-
if bHaveArgPrefix:
625-
strPythonInstallDir = os.path.normpath(vDictArgs["--prefix"])
626-
627-
bHaveArgCmakeBuildConfiguration = "--cmakeBuildConfiguration" in vDictArgs
628-
if bHaveArgCmakeBuildConfiguration:
629-
strPythonInstallDir = os.path.join(
630-
strPythonInstallDir,
631-
vDictArgs["--cmakeBuildConfiguration"])
632-
633-
if strPythonInstallDir.__len__() != 0:
634-
strWkDir = get_python_lib(True, False, strPythonInstallDir)
635-
else:
636-
strWkDir = get_python_lib(True, False)
637-
strWkDir = os.path.normcase(os.path.join(strWkDir, "lldb"))
638-
639-
return (bOk, strWkDir, strErrMsg)
640-
641-
#++---------------------------------------------------------------------------
642-
# Details: Retrieve the directory path for Python's dist_packages/
643-
# site_package folder on a UNIX style platform.
644-
# Args: vDictArgs - (R) Program input parameters.
645-
# Returns: Bool - True = function success, False = failure.
646-
# Str - Python Framework directory path.
647-
# strErrMsg - Error description on task failure.
648-
# Throws: None.
649-
#--
650-
651-
652-
def get_framework_python_dir_other_platforms(vDictArgs):
653-
dbg = utilsDebug.CDebugFnVerbose(
654-
"Python script get_framework_python_dir_other_platform()")
655-
bOk = True
656-
strWkDir = ""
657-
strErrMsg = ""
658-
bDbg = "-d" in vDictArgs
659-
660-
bMakeFileCalled = "-m" in vDictArgs
661-
if bMakeFileCalled:
662-
dbg.dump_text("Built by LLVM")
663-
return get_framework_python_dir_windows(vDictArgs)
664-
else:
665-
dbg.dump_text("Built by XCode")
666-
# We are being built by XCode, so all the lldb Python files can go
667-
# into the LLDB.framework/Resources/Python subdirectory.
668-
strWkDir = vDictArgs["--targetDir"]
669-
strWkDir = os.path.join(strWkDir, "LLDB.framework")
670-
if os.path.exists(strWkDir):
671-
if bDbg:
672-
print((strMsgFoundLldbFrameWkDir % strWkDir))
673-
strWkDir = os.path.join(strWkDir, "Resources", "Python", "lldb")
674-
strWkDir = os.path.normcase(strWkDir)
675-
else:
676-
bOk = False
677-
strErrMsg = strErrMsgFrameWkPyDirNotExist % strWkDir
678-
679-
return (bOk, strWkDir, strErrMsg)
680600

681601
#++---------------------------------------------------------------------------
682602
# Details: Retrieve the directory path for Python's dist_packages/
@@ -694,19 +614,8 @@ def get_framework_python_dir(vDictArgs):
694614
dbg = utilsDebug.CDebugFnVerbose(
695615
"Python script get_framework_python_dir()")
696616
bOk = True
697-
strWkDir = ""
698617
strErrMsg = ""
699-
700-
eOSType = utilsOsType.determine_os_type()
701-
if eOSType == utilsOsType.EnumOsType.Unknown:
702-
bOk = False
703-
strErrMsg = strErrMsgOsTypeUnknown
704-
elif eOSType == utilsOsType.EnumOsType.Windows:
705-
bOk, strWkDir, strErrMsg = get_framework_python_dir_windows(vDictArgs)
706-
else:
707-
bOk, strWkDir, strErrMsg = get_framework_python_dir_other_platforms(
708-
vDictArgs)
709-
618+
strWkDir = os.path.normpath(vDictArgs["--lldbPythonPath"])
710619
return (bOk, strWkDir, strErrMsg)
711620

712621
#++---------------------------------------------------------------------------

‎lldb/scripts/finishSwigWrapperClasses.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def validate_arguments(vArgv):
179179
"prefix=",
180180
"cmakeBuildConfiguration=",
181181
"lldbLibDir=",
182+
"lldbPythonPath=",
182183
"argsFile",
183184
"useSystemSix"]
184185
dictArgReq = {"-h": "o", # o = optional, m = mandatory
@@ -191,7 +192,8 @@ def validate_arguments(vArgv):
191192
"--cmakeBuildConfiguration": "o",
192193
"--lldbLibDir": "o",
193194
"--argsFile": "o",
194-
"--useSystemSix": "o"}
195+
"--useSystemSix": "o",
196+
"--lldbPythonPath": "m"}
195197

196198
# Check for mandatory parameters
197199
nResult, dictArgs, strMsg = utilsArgsParse.parse(vArgv, strListArgs,
@@ -293,11 +295,9 @@ def run_post_process_for_each_script_supported(vDictArgs):
293295

294296
# Iterate script directory find any script language directories
295297
for scriptLang in listDirs:
296-
# __pycache__ is a magic directory in Python 3 that holds .pyc files
297-
if scriptLang != "__pycache__" and scriptLang != "swig_bot_lib":
298-
dbg.dump_text("Executing language script for \'%s\'" % scriptLang)
299-
nResult, strStatusMsg = run_post_process(
300-
scriptLang, strFinishFileName, vDictArgs)
298+
dbg.dump_text("Executing language script for \'%s\'" % scriptLang)
299+
nResult, strStatusMsg = run_post_process(
300+
scriptLang, strFinishFileName, vDictArgs)
301301
if nResult < 0:
302302
break
303303

@@ -337,13 +337,6 @@ def main(vArgv):
337337
if gbDbgFlag:
338338
print_out_input_parameters(dictArgs)
339339

340-
# Check to see if we were called from the Makefile system. If we were, check
341-
# if the caller wants SWIG to generate a dependency file.
342-
# Not used in this program, but passed through to the language script file
343-
# called by this program
344-
global gbMakeFileFlag
345-
gbMakeFileFlag = "-m" in dictArgs
346-
347340
nResult, strMsg = run_post_process_for_each_script_supported(dictArgs)
348341

349342
program_exit(nResult, strMsg)

‎lldb/scripts/get_relative_lib_dir.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
2-
# Call a python script to gather the arch-specific libdir for
3-
# modules like the lldb module.
4-
execute_process(
5-
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../../../../scripts/get_relative_lib_dir.py
6-
RESULT_VARIABLE get_libdir_status
7-
OUTPUT_VARIABLE relative_libdir
8-
)
9-
if (get_libdir_status EQUAL 0)
10-
add_definitions(-DLLDB_PYTHON_RELATIVE_LIBDIR="${relative_libdir}")
11-
endif()
1+
if(NOT LLDB_PYTHON_RELATIVE_PATH)
2+
message(FATAL_ERROR "LLDB_PYTHON_RELATIVE_PATH is not set.")
123
endif()
4+
add_definitions(-DLLDB_PYTHON_RELATIVE_LIBDIR="${LLDB_PYTHON_RELATIVE_PATH}")
135

146
add_lldb_library(lldbPluginScriptInterpreterPython PLUGIN
157
PythonDataObjects.cpp

‎lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -305,39 +305,20 @@ void ScriptInterpreterPython::ComputePythonDirForApple(
305305
auto rend = llvm::sys::path::rend(path_ref);
306306
auto framework = std::find(rbegin, rend, "LLDB.framework");
307307
if (framework == rend) {
308-
ComputePythonDirForPosix(path);
308+
ComputePythonDir(path);
309309
return;
310310
}
311311
path.resize(framework - rend);
312312
llvm::sys::path::append(path, style, "LLDB.framework", "Resources", "Python");
313313
}
314314

315-
void ScriptInterpreterPython::ComputePythonDirForPosix(
315+
void ScriptInterpreterPython::ComputePythonDir(
316316
llvm::SmallVectorImpl<char> &path) {
317-
auto style = llvm::sys::path::Style::posix;
318-
#if defined(LLDB_PYTHON_RELATIVE_LIBDIR)
319317
// Build the path by backing out of the lib dir, then building with whatever
320318
// the real python interpreter uses. (e.g. lib for most, lib64 on RHEL
321-
// x86_64).
322-
llvm::sys::path::remove_filename(path, style);
323-
llvm::sys::path::append(path, style, LLDB_PYTHON_RELATIVE_LIBDIR);
324-
#else
325-
llvm::sys::path::append(path, style,
326-
"python" + llvm::Twine(PY_MAJOR_VERSION) + "." +
327-
llvm::Twine(PY_MINOR_VERSION),
328-
"site-packages");
329-
#endif
330-
}
331-
332-
void ScriptInterpreterPython::ComputePythonDirForWindows(
333-
llvm::SmallVectorImpl<char> &path) {
334-
auto style = llvm::sys::path::Style::windows;
335-
llvm::sys::path::remove_filename(path, style);
336-
llvm::sys::path::append(path, style, "lib", "site-packages");
337-
338-
// This will be injected directly through FileSpec.GetDirectory().SetString(),
339-
// so we need to normalize manually.
340-
std::replace(path.begin(), path.end(), '\\', '/');
319+
// x86_64, or bin on Windows).
320+
llvm::sys::path::remove_filename(path);
321+
llvm::sys::path::append(path, LLDB_PYTHON_RELATIVE_LIBDIR);
341322
}
342323

343324
FileSpec ScriptInterpreterPython::GetPythonDir() {
@@ -350,11 +331,10 @@ FileSpec ScriptInterpreterPython::GetPythonDir() {
350331

351332
#if defined(__APPLE__)
352333
ComputePythonDirForApple(path);
353-
#elif defined(_WIN32)
354-
ComputePythonDirForWindows(path);
355334
#else
356-
ComputePythonDirForPosix(path);
335+
ComputePythonDir(path);
357336
#endif
337+
llvm::sys::path::native(path);
358338
spec.GetDirectory().SetString(path);
359339
return spec;
360340
}();

‎lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class ScriptInterpreterPython : public ScriptInterpreter,
4848

4949
protected:
5050
static void ComputePythonDirForApple(llvm::SmallVectorImpl<char> &path);
51-
static void ComputePythonDirForPosix(llvm::SmallVectorImpl<char> &path);
52-
static void ComputePythonDirForWindows(llvm::SmallVectorImpl<char> &path);
51+
static void ComputePythonDir(llvm::SmallVectorImpl<char> &path);
5352
};
5453
} // namespace lldb_private
5554

0 commit comments

Comments
 (0)