Bug 1773520 - add vscode build backend by default if vscode is installed, r=firefox-build-system-reviewers,nalexander,glandium

Differential Revision: https://phabricator.services.mozilla.com/D150147
This commit is contained in:
Gijs Kruitbosch 2022-07-18 13:14:23 +00:00
Родитель 5c3ca70147
Коммит e00e722239
4 изменённых файлов: 82 добавлений и 69 удалений

Просмотреть файл

@ -73,6 +73,9 @@ the terminal:
./mach ide vscode
After that, subsequent `./mach build` invocations will automatically run
the `Clangd` integration.
If `VS Code` is already open with a previous configuration generated, please make sure to
restart `VS Code` otherwise the new configuration will not be used, and the `compile_commands.json`
needed by `clangd` server will not be refreshed. This is a known `bug <https://github.com/clangd/vscode-clangd/issues/42>`__

Просмотреть файл

@ -365,6 +365,7 @@ imply_option("--build-backends", build_backend)
"--help",
)
@imports("sys")
@imports(_from="mozbuild.backend.clangd", _import="find_vscode_cmd")
def build_backend_defaults(
host,
target,
@ -395,6 +396,12 @@ def build_backend_defaults(
and project not in ("mobile/android", "memory", "tools/update-programs")
):
all_backends.append("VisualStudio")
if (
compile_environment
and find_vscode_cmd()
and project not in ("mobile/android", "memory", "tools/update-programs")
):
all_backends.append("Clangd")
return tuple(all_backends) or None

Просмотреть файл

@ -17,6 +17,75 @@ from mozbuild.compilation.database import CompileDBBackend
import mozpack.path as mozpath
def find_vscode_cmd():
import shutil
import sys
# Try to look up the `code` binary on $PATH, and use it if present. This
# should catch cases like being run from within a vscode-remote shell,
# even if vscode itself is also installed on the remote host.
path = shutil.which("code")
if path is not None:
return [path]
# If the binary wasn't on $PATH, try to find it in a variety of other
# well-known install locations based on the current platform.
if sys.platform.startswith("darwin"):
cmd_and_path = [
{"path": "/usr/local/bin/code", "cmd": ["/usr/local/bin/code"]},
{
"path": "/Applications/Visual Studio Code.app",
"cmd": ["open", "/Applications/Visual Studio Code.app", "--args"],
},
{
"path": "/Applications/Visual Studio Code - Insiders.app",
"cmd": [
"open",
"/Applications/Visual Studio Code - Insiders.app",
"--args",
],
},
]
elif sys.platform.startswith("win"):
from pathlib import Path
vscode_path = mozpath.join(
str(Path.home()),
"AppData",
"Local",
"Programs",
"Microsoft VS Code",
"Code.exe",
)
vscode_insiders_path = mozpath.join(
str(Path.home()),
"AppData",
"Local",
"Programs",
"Microsoft VS Code Insiders",
"Code - Insiders.exe",
)
cmd_and_path = [
{"path": vscode_path, "cmd": [vscode_path]},
{"path": vscode_insiders_path, "cmd": [vscode_insiders_path]},
]
elif sys.platform.startswith("linux"):
cmd_and_path = [
{"path": "/usr/local/bin/code", "cmd": ["/usr/local/bin/code"]},
{"path": "/snap/bin/code", "cmd": ["/snap/bin/code"]},
{"path": "/usr/bin/code", "cmd": ["/usr/bin/code"]},
{"path": "/usr/bin/code-insiders", "cmd": ["/usr/bin/code-insiders"]},
]
# Did we guess the path?
for element in cmd_and_path:
if os.path.exists(element["path"]):
return element["cmd"]
# Path cannot be found
return None
class ClangdBackend(CompileDBBackend):
"""
Configuration that generates the backend for clangd, it is used with `clangd`

Просмотреть файл

@ -49,8 +49,10 @@ def run(command_context, ide, args):
return 1
if ide == "vscode":
from mozbuild.backend.clangd import find_vscode_cmd
# Check if platform has VSCode installed
vscode_cmd = find_vscode_cmd(command_context)
vscode_cmd = find_vscode_cmd()
if vscode_cmd is None:
choice = prompt_bool(
"VSCode cannot be found, and may not be installed. Proceed?"
@ -126,74 +128,6 @@ def get_visualstudio_workspace_path(command_context):
)
def find_vscode_cmd(command_context):
import shutil
# Try to look up the `code` binary on $PATH, and use it if present. This
# should catch cases like being run from within a vscode-remote shell,
# even if vscode itself is also installed on the remote host.
path = shutil.which("code")
if path is not None:
return [path]
# If the binary wasn't on $PATH, try to find it in a variety of other
# well-known install locations based on the current platform.
if "linux" in command_context.platform[0]:
cmd_and_path = [
{"path": "/usr/local/bin/code", "cmd": ["/usr/local/bin/code"]},
{"path": "/snap/bin/code", "cmd": ["/snap/bin/code"]},
{"path": "/usr/bin/code", "cmd": ["/usr/bin/code"]},
{"path": "/usr/bin/code-insiders", "cmd": ["/usr/bin/code-insiders"]},
]
elif "macos" in command_context.platform[0]:
cmd_and_path = [
{"path": "/usr/local/bin/code", "cmd": ["/usr/local/bin/code"]},
{
"path": "/Applications/Visual Studio Code.app",
"cmd": ["open", "/Applications/Visual Studio Code.app", "--args"],
},
{
"path": "/Applications/Visual Studio Code - Insiders.app",
"cmd": [
"open",
"/Applications/Visual Studio Code - Insiders.app",
"--args",
],
},
]
elif "win64" in command_context.platform[0]:
from pathlib import Path
vscode_path = mozpath.join(
str(Path.home()),
"AppData",
"Local",
"Programs",
"Microsoft VS Code",
"Code.exe",
)
vscode_insiders_path = mozpath.join(
str(Path.home()),
"AppData",
"Local",
"Programs",
"Microsoft VS Code Insiders",
"Code - Insiders.exe",
)
cmd_and_path = [
{"path": vscode_path, "cmd": [vscode_path]},
{"path": vscode_insiders_path, "cmd": [vscode_insiders_path]},
]
# Did we guess the path?
for element in cmd_and_path:
if os.path.exists(element["path"]):
return element["cmd"]
# Path cannot be found
return None
def setup_vscode(command_context, vscode_cmd):
vscode_settings = mozpath.join(
command_context.topsrcdir, ".vscode", "settings.json"