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