xref: /llvm-project/llvm/utils/gn/gn.py (revision c49770c60f26e449379447109f7d915bd8de0384)
10ff3cc20SNico Weber#!/usr/bin/env python3
2ceabe8b0SNico Weber"""Calls `gn` with the right --dotfile= and --root= arguments for LLVM."""
3ceabe8b0SNico Weber
4ceabe8b0SNico Weber# GN normally expects a file called '.gn' at the root of the repository.
5ceabe8b0SNico Weber# Since LLVM's GN build isn't supported, putting that file at the root
6ceabe8b0SNico Weber# is deemed inappropriate, which requires passing --dotfile= and -root= to GN.
7ceabe8b0SNico Weber# Since that gets old fast, this script automatically passes these arguments.
8ceabe8b0SNico Weber
9ceabe8b0SNico Weberimport os
10ceabe8b0SNico Weberimport subprocess
11ceabe8b0SNico Weberimport sys
12ceabe8b0SNico Weber
13ceabe8b0SNico Weber
14ceabe8b0SNico WeberTHIS_DIR = os.path.dirname(__file__)
15b71edfaaSTobias HietaROOT_DIR = os.path.join(THIS_DIR, "..", "..", "..")
16ceabe8b0SNico Weber
17ceabe8b0SNico Weber
18285becfaSNico Weberdef get_platform():
1938e6bcc1SNico Weber    import platform
20b71edfaaSTobias Hieta
21b71edfaaSTobias Hieta    if sys.platform == "darwin":
22b71edfaaSTobias Hieta        return "mac-amd64" if platform.machine() != "arm64" else "mac-arm64"
23b71edfaaSTobias Hieta    if platform.machine() not in ("AMD64", "x86_64"):
24285becfaSNico Weber        return None
25b71edfaaSTobias Hieta    if sys.platform.startswith("linux"):
26b71edfaaSTobias Hieta        return "linux-amd64"
27b71edfaaSTobias Hieta    if sys.platform == "win32":
28b71edfaaSTobias Hieta        return "windows-amd64"
29285becfaSNico Weber
30285becfaSNico Weber
31285becfaSNico Weberdef print_no_gn(mention_get):
32b71edfaaSTobias Hieta    print("gn binary not found in PATH")
33285becfaSNico Weber    if mention_get:
34b71edfaaSTobias Hieta        print("run llvm/utils/gn/get.py to download a binary and try again, or")
35b71edfaaSTobias Hieta    print("follow https://gn.googlesource.com/gn/#getting-started")
36285becfaSNico Weber    return 1
37285becfaSNico Weber
38285becfaSNico Weber
39ceabe8b0SNico Weberdef main():
40285becfaSNico Weber    # Find real gn executable.
41b71edfaaSTobias Hieta    gn = "gn"
42b71edfaaSTobias Hieta    if (
43b71edfaaSTobias Hieta        subprocess.call(
44b71edfaaSTobias Hieta            "gn --version",
45*c49770c6SNicolas van Kempen            stdout=subprocess.DEVNULL,
46c90886b9SMitch Phillips            stderr=subprocess.STDOUT,
47b71edfaaSTobias Hieta            shell=True,
48b71edfaaSTobias Hieta        )
49b71edfaaSTobias Hieta        != 0
50b71edfaaSTobias Hieta    ):
51285becfaSNico Weber        # Not on path. See if get.py downloaded a prebuilt binary and run that
52285becfaSNico Weber        # if it's there, or suggest to run get.py if it isn't.
53285becfaSNico Weber        platform = get_platform()
54285becfaSNico Weber        if not platform:
55285becfaSNico Weber            return print_no_gn(mention_get=False)
56b71edfaaSTobias Hieta        gn = os.path.join(os.path.dirname(__file__), "bin", platform, "gn")
57b71edfaaSTobias Hieta        if not os.path.exists(gn + (".exe" if sys.platform == "win32" else "")):
58285becfaSNico Weber            return print_no_gn(mention_get=True)
59ceabe8b0SNico Weber
60ceabe8b0SNico Weber    # Compute --dotfile= and --root= args to add.
61ceabe8b0SNico Weber    extra_args = []
62b71edfaaSTobias Hieta    gn_main_arg = next((x for x in sys.argv[1:] if not x.startswith("-")), None)
63b71edfaaSTobias Hieta    if gn_main_arg != "help":  # `gn help` gets confused by the switches.
64ceabe8b0SNico Weber        cwd = os.getcwd()
65b71edfaaSTobias Hieta        dotfile = os.path.relpath(os.path.join(THIS_DIR, ".gn"), cwd)
66ceabe8b0SNico Weber        root = os.path.relpath(ROOT_DIR, cwd)
67b71edfaaSTobias Hieta        extra_args = ["--dotfile=" + dotfile, "--root=" + root]
68ceabe8b0SNico Weber
69ceabe8b0SNico Weber    # Run GN command with --dotfile= and --root= added.
70ceabe8b0SNico Weber    cmd = [gn] + extra_args + sys.argv[1:]
71ceabe8b0SNico Weber    sys.exit(subprocess.call(cmd))
72ceabe8b0SNico Weber
73ceabe8b0SNico Weber
74b71edfaaSTobias Hietaif __name__ == "__main__":
75ceabe8b0SNico Weber    main()
76