1"""Build macro for libzip.""" 2 3# forked from kythe/kythe/tools/build_rules/expand_template.bzl 4def _expand_template_impl(ctx): 5 ctx.actions.expand_template( 6 template = ctx.file.template, 7 output = ctx.outputs.out, 8 substitutions = ctx.attr.substitutions, 9 ) 10 11expand_template = rule( 12 attrs = { 13 "out": attr.output(mandatory = True), 14 "substitutions": attr.string_dict(mandatory = True), 15 "template": attr.label( 16 mandatory = True, 17 allow_single_file = True, 18 ), 19 }, 20 output_to_genfiles = True, 21 implementation = _expand_template_impl, 22) 23 24def cmake_substitutions(vars, defines = {}): 25 """Returns a dict of template substitutions combining `vars` and `defines`. 26 27 Args: 28 vars: will be turned into a dict replacing `${key}` and `@key@` with `value`. 29 defines: will be turned into a dict replacing `#cmakedefine` with `#define {value}` 30 if present is true, otherwise `/* #undef %s /*`. 31 Returns: 32 substitutions 33 """ 34 subs = {} 35 for key, value in vars.items(): 36 subs["${%s}" % (key,)] = str(value) if value != None else "" 37 subs["@%s@" % (key,)] = str(value) if value != None else "" 38 39 # TODO(shahms): Better handling of #cmakedefine delimiters and line endings to 40 # avoid the prefix-substitution problem. 41 # Potentially allow value to be: True, False, None or string. 42 # True/False => Same as current 43 # None => assume no suffix value, include \n in sub and replacement 44 # string => use string to lookup in vars and assume ${} or @@ tail? 45 for macro, present in defines.items(): 46 if present: 47 subs["#cmakedefine %s" % macro] = "#define %s" % macro 48 else: 49 subs["#cmakedefine %s" % macro] = "/* #undef %s */" % macro 50 return subs 51