From 00bb397b0dc79fcad27bfe63456a2100039706f2 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Tue, 27 Oct 2020 09:14:40 -0700 Subject: [lldb] Support Python imports relative the to the current file being sourced Make it possible to use a relative path in command script import to the location of the file being sourced. This allows the user to put Python scripts next to LLDB command files and importing them without having to specify an absolute path. To enable this behavior pass `-c` to `command script import`. The argument can only be used when sourcing the command from a file. rdar://68310384 Differential revision: https://reviews.llvm.org/D89334 --- lldb/source/Commands/CommandObjectCommands.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'lldb/source/Commands/CommandObjectCommands.cpp') diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp index 96ce82d..0c441dd 100644 --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -1272,6 +1272,9 @@ protected: case 'r': // NO-OP break; + case 'c': + relative_to_command_file = true; + break; default: llvm_unreachable("Unimplemented option"); } @@ -1280,11 +1283,13 @@ protected: } void OptionParsingStarting(ExecutionContext *execution_context) override { + relative_to_command_file = false; } llvm::ArrayRef GetDefinitions() override { return llvm::makeArrayRef(g_script_import_options); } + bool relative_to_command_file = false; }; bool DoExecute(Args &command, CommandReturnObject &result) override { @@ -1294,6 +1299,17 @@ protected: return false; } + FileSpec source_dir = {}; + if (m_options.relative_to_command_file) { + source_dir = GetDebugger().GetCommandInterpreter().GetCurrentSourceDir(); + if (!source_dir) { + result.AppendError("command script import -c can only be specified " + "from a command file"); + result.SetStatus(eReturnStatusFailed); + return false; + } + } + for (auto &entry : command.entries()) { Status error; @@ -1308,7 +1324,7 @@ protected: // more) m_exe_ctx.Clear(); if (GetDebugger().GetScriptInterpreter()->LoadScriptingModule( - entry.c_str(), init_session, error)) { + entry.c_str(), init_session, error, nullptr, source_dir)) { result.SetStatus(eReturnStatusSuccessFinishNoResult); } else { result.AppendErrorWithFormat("module importing failed: %s", -- cgit v1.1