Skip to content

Commit

Permalink
hmaptool: easier to generate input
Browse files Browse the repository at this point in the history
* hmaptool: implement param files and drop unused options
            now it takes header right on the CLI so that
            it's easier to generate the input from bazel.
* hmap.bzl: remove flatten_headers and use namespace for the
            same purpose
            use ctx.actions.args() for header maps
* other:    fix case in the github actions yml files
  • Loading branch information
ob committed May 16, 2020
1 parent daace38 commit 30ee19a
Show file tree
Hide file tree
Showing 10 changed files with 1,066 additions and 194 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: find . -type f \( -name 'WORKSPACE' -o -name '*.bzl' -o -name '*.bazel' \) | xargs buildifier -lint=fix && git diff --exit-code
check_docs:
name: Check Docs
runs-on: macOS-latest
runs-on: macos-latest
steps:
- uses: actions/checkout@v1
- name: Select Xcode 11.2
Expand All @@ -43,7 +43,7 @@ jobs:
run: bazelisk run docs --nocheck_visibility && git diff --exit-code docs
xcodeproj_tests:
name: .xcodeproj Tests
runs-on: macOS-latest
runs-on: macos-latest
steps:
- uses: actions/checkout@v1
- name: Select Xcode 11.2
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"bazel-*": true
},
"editor.formatOnSave": true,
}
}
5 changes: 2 additions & 3 deletions docs/hmap_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## headermap

<pre>
headermap(<a href="#headermap-name">name</a>, <a href="#headermap-direct_hdr_providers">direct_hdr_providers</a>, <a href="#headermap-flatten_headers">flatten_headers</a>, <a href="#headermap-hdrs">hdrs</a>, <a href="#headermap-namespace">namespace</a>)
headermap(<a href="#headermap-name">name</a>, <a href="#headermap-direct_hdr_providers">direct_hdr_providers</a>, <a href="#headermap-hdrs">hdrs</a>, <a href="#headermap-namespace">namespace</a>)
</pre>

Creates a binary headermap file from the given headers,
Expand All @@ -22,9 +22,8 @@ regardless of the package structure being used.
| :-------------: | :-------------: | :-------------: | :-------------: | :-------------: |
| name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| direct_hdr_providers | Targets whose direct headers should be added to the list of hdrs | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| flatten_headers | Whether headers should be importable with the namespace as a prefix | Boolean | required | |
| hdrs | The list of headers included in the headermap | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | required | |
| namespace | The prefix to be used for header imports when flatten_headers is true | String | required | |
| namespace | The prefix to be used for header imports | String | optional | "" |


<a name="#HeaderMapInfo"></a>
Expand Down
23 changes: 9 additions & 14 deletions rules/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,19 @@ def _apple_framework_packaging_impl(ctx):
else:
ctx.actions.write(framework_manifest, "# Empty framework\n")

# headermap
mappings_file = ctx.actions.declare_file(framework_name + "_framework.hmap.txt")
mappings = []
for header in header_in + private_header_in:
mappings.append(framework_name + "/" + header.basename + "|" + header.path)

# write mapping for hmap tool
ctx.actions.write(
content = "\n".join(mappings) + "\n",
output = mappings_file,
)
hmap_file = ctx.actions.declare_file(framework_name + "_framework_public_hmap.hmap")
args = ctx.actions.args()
args.add("--output", hmap_file.path)
args.add("--namespace", framework_name)
args.add_all(header_in)
args.add_all(private_header_in)
args.set_param_file_format("multiline")
args.use_param_file("@%s", use_always = True)

# write headermap
hmap_file = ctx.actions.declare_file(framework_name + "_framework_public_hmap.hmap")
ctx.actions.run(
inputs = [mappings_file],
mnemonic = "HmapCreate",
arguments = [mappings_file.path, hmap_file.path],
arguments = [args],
executable = ctx.executable._headermap_builder,
outputs = [hmap_file],
)
Expand Down
68 changes: 16 additions & 52 deletions rules/hmap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,6 @@ HeaderMapInfo = provider(
},
)

def _make_headermap_input_file(namespace, hdrs, flatten_headers):
"""Create a header map input file.
This function creates a string representing the mappings from headers to their
namespaced include versions. The format is
virtual_header_path|real_header_path
Note the separator is a pipe character.
:param namespace: 'foo' in #include <foo/bar.h>
:param hdrs: list of header that need to be mapped
:param flatten_headers: boolean value that if set, will "flatten"
the virtual heders. What this means is that the headers
will also be added without the namespace or any paths
(basename).
:return: string with all the headers in the above mentioned
format. This can be saved to a file and read by the hmapbuild tool
included here to create a header map file.
"""

entries = []
for hdr in hdrs:
namespaced_key = namespace + "/" + hdr.basename
entries.append("{}|{}".format(hdr.basename, hdr.path))
if flatten_headers:
entries.append("{}|{}".format(namespaced_key, hdr.path))
return "\n".join(entries) + "\n"

def _make_headermap_impl(ctx):
"""Implementation of the headermap() rule.
Expand All @@ -50,33 +20,31 @@ def _make_headermap_impl(ctx):
:return: provider with the info for this rule
"""

# Write a file for *this* headermap, this is a temporary file
input_f = ctx.actions.declare_file(ctx.label.name + "_input.txt")
all_hdrs = list(ctx.files.hdrs)
# Add a list of headermaps in text or hmap format
args = ctx.actions.args()
if ctx.attr.namespace:
args.add("--namespace", ctx.attr.namespace)

args.add("--output", ctx.outputs.headermap.path)

args.add_all(ctx.files.hdrs)
for provider in ctx.attr.direct_hdr_providers:
if apple_common.Objc in provider:
all_hdrs += provider[apple_common.Objc].direct_headers
args.add_all(provider[apple_common.Objc].direct_headers)
elif CcInfo in provider:
all_hdrs += provider[CcInfo].compilation_context.direct_headers
args.add_all(provider[CcInfo].compilation_context.direct_headers)
else:
fail("direct_hdr_provider %s must contain either 'CcInfo' or 'objc' provider" % provider)

out = _make_headermap_input_file(ctx.attr.namespace, all_hdrs, ctx.attr.flatten_headers)
ctx.actions.write(
content = out,
output = input_f,
)

# Add a list of headermaps in text or hmap format
inputs = [input_f]
args = [input_f.path, ctx.outputs.headermap.path]
args.set_param_file_format(format = "multiline")
args.use_param_file("@%s", use_always = True)
ctx.actions.run(
inputs = inputs,
mnemonic = "HmapCreate",
arguments = args,
arguments = [args],
executable = ctx.executable._headermap_builder,
outputs = [ctx.outputs.headermap],
)

objc_provider = apple_common.new_objc_provider(
header = depset([ctx.outputs.headermap]),
)
Expand All @@ -98,8 +66,8 @@ headermap = rule(
output_to_genfiles = True,
attrs = {
"namespace": attr.string(
mandatory = True,
doc = "The prefix to be used for header imports when flatten_headers is true",
mandatory = False,
doc = "The prefix to be used for header imports",
),
"hdrs": attr.label_list(
mandatory = True,
Expand All @@ -110,10 +78,6 @@ headermap = rule(
mandatory = False,
doc = "Targets whose direct headers should be added to the list of hdrs",
),
"flatten_headers": attr.bool(
mandatory = True,
doc = "Whether headers should be importable with the namespace as a prefix",
),
"_headermap_builder": attr.label(
executable = True,
cfg = "host",
Expand Down
17 changes: 16 additions & 1 deletion rules/hmap/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")

HMAP_COPTS = [
"-DHASH_FUNCTION=HASH_MUR",
Expand All @@ -18,6 +18,21 @@ cc_binary(
],
copts = HMAP_COPTS,
visibility = HMAP_VISIBILITY,
deps = [
":lines",
],
)

cc_library(
name = "lines",
srcs = [
"lines.c",
],
hdrs = [
"lines.h",
],
copts = ["-Wno-parentheses"],
visibility = HMAP_VISIBILITY,
)

cc_binary(
Expand Down
Loading

0 comments on commit 30ee19a

Please sign in to comment.