xref: /aosp_15_r20/external/skia/bazel/gen_compile_flags_txt_linux_amd64.bzl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1"""This module provides the gen_compile_flags_txt_linux_amd64 macro."""
2
3load("@skia_user_config//:copts.bzl", "DEFAULT_COPTS")
4load("//toolchain:linux_amd64_toolchain_config.bzl", "EXTERNAL_TOOLCHAIN")
5
6def _gen_compile_flags_txt_linux_amd64_rule_impl(ctx):
7    # We need to set the working directory to the workspace root before invoking "bazel info
8    # output_base", or Bazel will fail with "ERROR: bazel should not be called from a
9    # bazel output directory.". This error is due to the fact that the script generated by this
10    # rule will have its working directory set to a path under its runfiles directory.
11    bazel_output_base = "$(cd $BUILD_WORKSPACE_DIRECTORY && bazel info output_base)"
12
13    flags = [
14        "-xc++",  # Treat all files as C++. Without this, clangd treats .h files as C files.
15        "-I.",  # Required for includes relative to the workspace root to work.
16
17        # Based on
18        # https://skia.googlesource.com/skia/+/064f144adedd8ad8ac9c613c54e6354268041912/toolchain/linux_amd64_toolchain_config.bzl#214.
19        #
20        # It would be nice to have these in a constant exported by
21        # //toolchain:linux_amd64_toolchain_config.bzl.
22        "-isystem",
23        "%s/%s/include/c++/v1" % (bazel_output_base, EXTERNAL_TOOLCHAIN),
24        "-isystem",
25        "%s/%s/include/x86_64-unknown-linux-gnu/c++/v1/" % (bazel_output_base, EXTERNAL_TOOLCHAIN),
26        "-isystem",
27        "%s/%s/usr/include" % (bazel_output_base, EXTERNAL_TOOLCHAIN),
28        "-isystem",
29        "%s/%s/lib/clang/15.0.1/include" % (bazel_output_base, EXTERNAL_TOOLCHAIN),
30        "-isystem",
31        "%s/%s/usr/include/x86_64-linux-gnu" % (bazel_output_base, EXTERNAL_TOOLCHAIN),
32
33        # Based on
34        # https://skia.googlesource.com/skia/+/064f144adedd8ad8ac9c613c54e6354268041912/toolchain/linux_amd64_toolchain_config.bzl#238.
35        #
36        # It would be nice to have these in a constant exported by
37        # //toolchain:linux_amd64_toolchain_config.bzl.
38        "-std=c++17",
39        "-stdlib=libc++",
40    ] + ctx.attr.flags
41
42    script = "#!/bin/sh\n"
43    for flag in flags:
44        script += "echo %s\n" % flag
45
46    output_file = ctx.actions.declare_file(ctx.attr.name)
47    ctx.actions.write(output_file, script, is_executable = True)
48
49    return [DefaultInfo(executable = output_file)]
50
51_gen_compile_flags_txt_linux_amd64_rule = rule(
52    doc = """Implements the gen_compile_flags_txt_linux_amd64 macro.
53
54    This has to be a rule, rather than a macro, because macros do not evaluate select()
55    expressions, as is the case with lists such as DEFAULT_COPTS.
56    """,
57    implementation = _gen_compile_flags_txt_linux_amd64_rule_impl,
58    attrs = {
59        "flags": attr.string_list(
60            mandatory = True,
61        ),
62    },
63    executable = True,
64)
65
66def gen_compile_flags_txt_linux_amd64(name):
67    """Generates a compile_flags.txt file for use with clangd.
68
69    See entry in //BUILD.bazel for usage instructions.
70    """
71    _gen_compile_flags_txt_linux_amd64_rule(
72        name = name,
73        flags = DEFAULT_COPTS,
74    )
75