1 """Internal rules for building upb."""
3 load(":upb_proto_library.bzl", "GeneratedSrcsInfo")
8 def _get_real_short_path(file):
9 # For some reason, files from other archives have short paths that look like:
10 # ../com_google_protobuf/google/protobuf/descriptor.proto
11 short_path = file.short_path
12 if short_path.startswith("../"):
13 second_slash = short_path.index("/", 3)
14 short_path = short_path[second_slash + 1:]
17 def _get_real_root(file):
18 real_short_path = _get_real_short_path(file)
19 return file.path[:-len(real_short_path) - 1]
21 def _get_real_roots(files):
24 real_root = _get_real_root(file)
26 roots[real_root] = True
29 def lua_cclibrary(name, srcs, hdrs = [], deps = [], luadeps = []):
30 lib_rule = name + "_lib"
31 so_rule = "lib" + name + ".so"
32 so_file = _remove_prefix(name, "lua/") + ".so"
35 name = _librule(name),
38 deps = deps + [_librule(dep) for dep in luadeps] + ["@lua//:liblua_headers"],
44 deps = [_librule(name)],
47 "-undefined dynamic_lookup",
49 "//conditions:default": [],
54 name = name + "_copy",
55 srcs = [":" + so_rule],
65 def _remove_prefix(str, prefix):
66 if not str.startswith(prefix):
67 fail("%s doesn't start with %s" % (str, prefix))
68 return str[len(prefix):]
70 def _remove_suffix(str, suffix):
71 if not str.endswith(suffix):
72 fail("%s doesn't end with %s" % (str, suffix))
73 return str[:-len(suffix)]
75 def lua_library(name, srcs, strip_prefix, luadeps = []):
76 outs = [_remove_prefix(src, strip_prefix + "/") for src in srcs]
78 name = name + "_copy",
81 cmd = "cp $(SRCS) $(@D)",
86 data = outs + luadeps,
89 def make_shell_script(name, contents, out):
90 contents = contents.replace("$", "$$")
94 cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents,
97 def _lua_binary_or_test(name, luamain, luadeps, rule):
101 name = "gen_" + name,
104 BASE=$(dirname $(rlocation upb/upb_c.so))
105 export LUA_CPATH="$BASE/?.so"
106 export LUA_PATH="$BASE/?.lua"
107 $(rlocation lua/lua) $(rlocation upb/tools/upbc.lua) "$@"
114 data = ["@lua//:lua", luamain] + luadeps,
117 def lua_binary(name, luamain, luadeps = []):
118 _lua_binary_or_test(name, luamain, luadeps, native.sh_binary)
120 def lua_test(name, luamain, luadeps = []):
121 _lua_binary_or_test(name, luamain, luadeps, native.sh_test)
123 def generated_file_staleness_test(name, outs, generated_pattern):
124 """Tests that checked-in file(s) match the contents of generated file(s).
126 The resulting test will verify that all output files exist and have the
127 correct contents. If the test fails, it can be invoked with --fix to
128 bring the checked-in files up to date.
131 name: Name of the rule.
132 outs: the checked-in files that are copied from generated files.
133 generated_pattern: the pattern for transforming each "out" file into a
134 generated file. For example, if generated_pattern="generated/%s" then
135 a file foo.txt will look for generated file generated/foo.txt.
138 script_name = name + ".py"
139 script_src = "//:tools/staleness_test.py"
141 # Filter out non-existing rules so Blaze doesn't error out before we even
143 existing_outs = native.glob(include = outs)
145 # The file list contains a few extra bits of information at the end.
146 # These get unpacked by the Config class in staleness_test_lib.py.
147 file_list = outs + [generated_pattern, native.package_name() or ".", name]
150 name = name + "_makescript",
151 outs = [script_name],
154 cmd = "cat $(location " + script_src + ") > $@; " +
155 "sed -i.bak -e 's|INSERT_FILE_LIST_HERE|" + "\\\n ".join(file_list) + "|' $@",
160 srcs = [script_name],
161 data = existing_outs + [generated_pattern % file for file in outs],
163 "//:staleness_test_lib",
167 # upb_amalgamation() rule, with file_list aspect.
171 "srcs": "list of srcs",
175 def _file_list_aspect_impl(target, ctx):
176 if GeneratedSrcsInfo in target:
177 srcs = target[GeneratedSrcsInfo]
178 return [SrcList(srcs = srcs.srcs + srcs.hdrs)]
181 for src in ctx.rule.attr.srcs:
182 srcs += src.files.to_list()
183 for hdr in ctx.rule.attr.hdrs:
184 srcs += hdr.files.to_list()
185 for hdr in ctx.rule.attr.textual_hdrs:
186 srcs += hdr.files.to_list()
187 return [SrcList(srcs = srcs)]
189 _file_list_aspect = aspect(
190 implementation = _file_list_aspect_impl,
193 def _upb_amalgamation(ctx):
195 for lib in ctx.attr.libs:
196 inputs += lib[SrcList].srcs
197 srcs = [src for src in inputs if src.path.endswith("c")]
200 outputs = ctx.outputs.outs,
201 arguments = [ctx.bin_dir.path + "/"] + [f.path for f in srcs] + ["-I" + root for root in _get_real_roots(inputs)],
202 progress_message = "Making amalgamation",
203 executable = ctx.executable.amalgamator,
207 upb_amalgamation = rule(
209 "amalgamator": attr.label(
213 "libs": attr.label_list(aspects = [_file_list_aspect]),
214 "outs": attr.output_list(),
216 implementation = _upb_amalgamation,
220 # No-op (for Google-internal usage).