Debian Codegraph @

bazel/external/gn/BUILD.bazel

raw
1 # This is a convenience wrapper to run a platform-specific version of GN with any provided flags
2 # from the directory where the user ran Bazel. Without this, users would need to specify the
3 # exact flavor of GN needed and use --run_under to change directories before running GN.
4 #
5 # For example:
6 # bazelisk //bazel/external/gn -- format gn/sksl.gni
7 # is the same as
8 # gn format gn/sksl.gni
9 # except it uses a hermetic version of gn instead of whatever is on the user's machine. Note the --
10 # to pass arguments to gn and not have Bazel try to understand them.
11 py_binary(
12 name = "gn",
13 srcs = [":gn.py"],
14 data = [":gn_exe"],
15 # We can compile remotely, but because we are running the executable to modify files in
16 # the source tree, running it in a remote, sandboxed would have no effect locally.
17 tags = ["no-remote"],
18 )
19
20 _GN = """
21 import os
22 import subprocess
23 import sys
24
25 # rlocationpath returns the path to the given label from the runfiles path (which is the
26 # parent directory, according to local experimentation).
27 # https://bazel.build/reference/be/make-variables#predefined_label_variables
28 # We need to get the absolute path here because we want to run gn from the
29 # location where the user invoked Bazel.
30 os.chdir("..")
31 gn_exe = os.path.abspath("$(rlocationpath :gn_exe)")
32
33 # https://bazel.build/docs/user-manual#running-executables
34 # Note: Bazel eats single quotes, so we must use double quotes.
35 os.chdir(os.environ["BUILD_WORKING_DIRECTORY"]) # path where user ran Bazel
36
37 # Pass any other args along to GN, and stream any output to the user.
38 result = subprocess.run([gn_exe] + sys.argv[1:], encoding="utf-8")
39 sys.exit(result.returncode)
40 """
41
42 genrule(
43 name = "create_gn_script",
44 outs = ["gn.py"],
45 cmd = "echo '%s' > $@" % _GN,
46 tools = [
47 ":gn_exe",
48 ],
49 )
50
51 filegroup(
52 name = "gn_exe",
53 srcs = select({
54 "//bazel/common_config_settings:linux_x64": ["@gn_linux_amd64//:gn"],
55 "//bazel/common_config_settings:mac_arm64": ["@gn_mac_arm64//:gn"],
56 "//bazel/common_config_settings:mac_x64": ["@gn_mac_amd64//:gn"],
57 }),
58 )