|
| 1 | +# re2c generator rules - assuming that re2c is available as '@re2c//:re2c'. |
| 2 | + |
| 3 | +load("@bazel_skylib//lib:paths.bzl", "paths") |
| 4 | + |
| 5 | +visibility("public") |
| 6 | + |
| 7 | +# Define supported extensions as defined by: |
| 8 | +# . https://bazel.build/reference/be/c-cpp#cc_library_args |
| 9 | +# . https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java |
| 10 | +_C_SRC_EXT = [".c", ".cc", ".cpp", ".cxx", ".c++", ".C", ".cu", ".cl"] |
| 11 | +_C_HDR_EXT = [".h", ".hh", ".hpp", ".ipp", ".hxx", ".h++", ".inc", ".inl", ".tlh", ".tli", ".H", ".tcc"] |
| 12 | +_RE2C_EXT = [".re", ".re2c"] |
| 13 | + |
| 14 | +def _re2c_impl(ctx): |
| 15 | + srcs = ctx.files.srcs |
| 16 | + if len(srcs) != 1: |
| 17 | + fail("Attribute `srcs` must contain exactly 1 re2c source file ({exts}).".format(exts=", ".join(_RE2C_EXT))) |
| 18 | + outs = ctx.outputs.outs |
| 19 | + if not outs or [o for o in outs if paths.split_extension(o.path)[1] not in _C_SRC_EXT + _C_HDR_EXT]: |
| 20 | + fail("Attribute `outs` must contain at least 1 C/C++ shource or header file ({exts}).".format(exts=", ".join(_C_SRC_EXT + _C_HDR_EXT))) |
| 21 | + ctx.actions.run_shell( |
| 22 | + outputs = outs + [err_file], |
| 23 | + tools = [ctx.executable._re2c_tool], |
| 24 | + inputs = ctx.files.srcs + ctx.files.data, |
| 25 | + command = """ |
| 26 | + "{re2c}" "{src}" -o "{out}" {re2c_args} {includes} |
| 27 | + """.format( |
| 28 | + re2c = ctx.executable._re2c_tool.path, |
| 29 | + src = srcs[0].path, |
| 30 | + out = outs[0].path, |
| 31 | + includes = " ".join(list(dict([("-I" + f.dirname, 1) for f in ctx.files.data]))), |
| 32 | + re2c_args = " ".join(ctx.attr.re2c_args), |
| 33 | + ), |
| 34 | + mnemonic = "RE2C", |
| 35 | + progress_message = "Generating re2c %{label}", |
| 36 | + ) |
| 37 | + |
| 38 | +_re2c_attr = { |
| 39 | + "srcs": attr.label_list( |
| 40 | + doc = "The re2c source.", |
| 41 | + allow_files = _RE2C_EXT, |
| 42 | + mandatory = True, |
| 43 | + ), |
| 44 | + "data": attr.label_list( |
| 45 | + doc = "Other input data files (e.g. include files).", |
| 46 | + allow_files = _C_SRC_EXT + _C_HDR_EXT, |
| 47 | + mandatory = False, |
| 48 | + ), |
| 49 | + "outs": attr.output_list( |
| 50 | + doc = "The generated output source.", |
| 51 | + mandatory = True, |
| 52 | + ), |
| 53 | + "re2c_args": attr.string_list( |
| 54 | + doc = "Arguments to re2c invocation.", |
| 55 | + ), |
| 56 | + "_re2c_tool": attr.label( |
| 57 | + doc = "The target of the 're2c' executable.", |
| 58 | + default = Label("//:re2c"), |
| 59 | + allow_single_file = True, |
| 60 | + executable = True, |
| 61 | + cfg = "exec", |
| 62 | + ), |
| 63 | +} |
| 64 | + |
| 65 | +re2c_gen = rule( |
| 66 | + attrs = _re2c_attr, |
| 67 | + doc = """Use re2c tool to create an output file (e.g. '.cc') from a '.re' input.""", |
| 68 | + output_to_genfiles = True, |
| 69 | + implementation = _re2c_impl, |
| 70 | +) |
0 commit comments