Commit e5852b6d authored by Gijs Kruitbosch's avatar Gijs Kruitbosch
Browse files

Bug 1773520 - add vscode build backend by default if vscode is installed,...

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
parent 926ae49c
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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>`__
+7 −0
Original line number Diff line number Diff line
@@ -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


+69 −0
Original line number Diff line number Diff line
@@ -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`
+3 −69
Original line number Diff line number Diff line
@@ -49,8 +49,10 @@ def run(command_context, ide, args):
        return 1

    if ide == "vscode":
        from .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"