From 3ef253eec60a3ac58468b0f9c9d0c1eb4ba20a8a Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 28 Dec 2023 18:04:42 +0100 Subject: [PATCH 1/3] update GH workflows --- .github/workflows/build.yaml | 35 +++++++++++++++++++++++++++++++ .github/workflows/build.yml | 28 ------------------------- .github/workflows/buildifier.yaml | 18 ++++++++++++++++ 3 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/build.yaml delete mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/buildifier.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..d69c989 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,35 @@ +name: Build + +on: + pull_request: + push: + branches: [master] + workflow_dispatch: + +jobs: + run-tests: + timeout-minutes: 15 + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + + runs-on: ${{ matrix.os }} + + steps: + - name: Clone repository + uses: actions/checkout@v4 + + - name: Install Bazel + run: tools/install_deps.sh + + - name: Run tests + run: tools/run_tests.sh + + - name: Set up Go + uses: actions/setup-go@v5 + + - name: Install buildifier + run: go install github.com/bazelbuild/buildtools/buildifier@latest + + - name: Verify Starlark code formatting + run: buildifier -r path. diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 5eb0b2f..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Build - -on: - # Run workflow on any pull request. - pull_request: - - # Run workflow on pushes to the master branch. - push: - branches: [master] - -jobs: - run-tests: - timeout-minutes: 15 - strategy: - matrix: - os: [ubuntu-latest, macos-latest] - - runs-on: ${{ matrix.os }} - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Install bazel - run: tools/install_deps.sh - - - name: Run tests - run: tools/run_tests.sh diff --git a/.github/workflows/buildifier.yaml b/.github/workflows/buildifier.yaml new file mode 100644 index 0000000..663bc1a --- /dev/null +++ b/.github/workflows/buildifier.yaml @@ -0,0 +1,18 @@ +name: Buildifier + +on: + push: + branches: [master] + pull_request: + branches: [master] + workflow_dispatch: + +jobs: + check: + runs-on: ubuntu-latest + steps: + - name: Clone repository + uses: actions/checkout@v4 + + - name: Run buildifier + run: bazelisk run //.github/workflows:buildifier.check From eab77654aa5770a0c723128136dbe96663663273 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 28 Dec 2023 18:04:50 +0100 Subject: [PATCH 2/3] create .bazelversion --- .bazelversion | 1 + 1 file changed, 1 insertion(+) create mode 100644 .bazelversion diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..66ce77b --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +7.0.0 From c6c704a98e24a8f0c05a1d20432a858eb9c40df0 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 28 Dec 2023 18:05:02 +0100 Subject: [PATCH 3/3] run buildifier over all source files $ buildifier -r . --- WORKSPACE | 1 + dart/build_rules/core.bzl | 36 ++- dart/build_rules/internal.bzl | 429 +++++++++++++++--------------- dart/build_rules/repositories.bzl | 53 ++-- dart/build_rules/vm.bzl | 400 ++++++++++++++-------------- dart/build_rules/web.bzl | 294 ++++++++++---------- examples/hello_bin/BUILD | 14 +- examples/web_app/BUILD | 8 +- 8 files changed, 622 insertions(+), 613 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 9554e52..637a218 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,4 +1,5 @@ workspace(name = "io_bazel_rules_dart") load("//dart/build_rules:repositories.bzl", "dart_repositories") + dart_repositories() diff --git a/dart/build_rules/core.bzl b/dart/build_rules/core.bzl index 8f7c145..145305f 100644 --- a/dart/build_rules/core.bzl +++ b/dart/build_rules/core.bzl @@ -14,33 +14,31 @@ """Dart rules shared across deployment platforms.""" - load(":internal.bzl", "assert_third_party_licenses", "make_dart_context") - def _dart_library_impl(ctx): - """Implements the dart_library() rule.""" - assert_third_party_licenses(ctx) + """Implements the dart_library() rule.""" + assert_third_party_licenses(ctx) - dart_ctx = make_dart_context(ctx.label, - srcs=ctx.files.srcs, - data=ctx.files.data, - deps=ctx.attr.deps) - - return struct( - dart=dart_ctx, - ) + dart_ctx = make_dart_context( + ctx.label, + srcs = ctx.files.srcs, + data = ctx.files.data, + deps = ctx.attr.deps, + ) + return struct( + dart = dart_ctx, + ) dart_library_attrs = { - "srcs": attr.label_list(allow_files=True, mandatory=True), - "data": attr.label_list(allow_files=True), - "deps": attr.label_list(providers=["dart"]), - "license_files": attr.label_list(allow_files=True) + "srcs": attr.label_list(allow_files = True, mandatory = True), + "data": attr.label_list(allow_files = True), + "deps": attr.label_list(providers = ["dart"]), + "license_files": attr.label_list(allow_files = True), } - dart_library = rule( - implementation=_dart_library_impl, - attrs=dart_library_attrs, + implementation = _dart_library_impl, + attrs = dart_library_attrs, ) diff --git a/dart/build_rules/internal.bzl b/dart/build_rules/internal.bzl index 0f581c3..28d3cb1 100644 --- a/dart/build_rules/internal.bzl +++ b/dart/build_rules/internal.bzl @@ -21,238 +21,235 @@ a mechanism for enforcing limitied visibility of Skylark rules. This code makes no gurantees of API stability and is intended solely for use by the Dart rules. """ - _third_party_prefix = "third_party/dart/" - def assert_third_party_licenses(ctx): - """Asserts license attr on non-testonly third-party packages.""" - if (not ctx.attr.testonly - and not ctx.attr.license_files - and ctx.label.package.startswith(_third_party_prefix)): - fail("%s lacks license_files attribute, " % ctx.label + - "required for all non-testonly third-party Dart library rules") - + """Asserts license attr on non-testonly third-party packages.""" + if (not ctx.attr.testonly and + not ctx.attr.license_files and + ctx.label.package.startswith(_third_party_prefix)): + fail("%s lacks license_files attribute, " % ctx.label + + "required for all non-testonly third-party Dart library rules") def collect_files(dart_ctx): - srcs = dart_ctx.srcs - data = dart_ctx.data - for d in dart_ctx.transitive_deps.values(): - srcs = srcs + d.dart.srcs - data = srcs + d.dart.data - return (srcs, data) - + srcs = dart_ctx.srcs + data = dart_ctx.data + for d in dart_ctx.transitive_deps.values(): + srcs = srcs + d.dart.srcs + data = srcs + d.dart.data + return (srcs, data) def _collect_transitive_deps(deps): - """Collects transitive closure of deps. - - Args: - deps: input deps Target collection. All targets must have a 'dart' provider. + """Collects transitive closure of deps. - Returns: - Transitive closure of deps. - """ - transitive_deps = {} - for dep in deps: - transitive_deps.update(dep.dart.transitive_deps) - transitive_deps["%s" % dep.dart.label] = dep - return transitive_deps + Args: + deps: input deps Target collection. All targets must have a 'dart' provider. + Returns: + Transitive closure of deps. + """ + transitive_deps = {} + for dep in deps: + transitive_deps.update(dep.dart.transitive_deps) + transitive_deps["%s" % dep.dart.label] = dep + return transitive_deps def _label_to_dart_package_name(label): - """Returns the Dart package name for the specified label. - - Packages under //third_party/dart resolve to their external Pub package names. - All other packages resolve to a unique identifier based on their repo path. - - Examples: - //foo/bar/baz: foo.bar.baz - //third_party/dart/args: args - //third_party/guice: third_party.guice - - Restrictions: - Since packages outside of //third_party/dart are identified by their path - components joined by periods, it is an error for the label package to - contain periods. - - Args: - label: the label whose package name is to be returned. - - Returns: - The Dart package name associated with the label. - """ - package_name = label.package - if label.package.startswith(_third_party_prefix): - third_party_path = label.package[len(_third_party_prefix):] - if "/" not in third_party_path: - package_name = third_party_path - if "." in package_name: - fail("Dart package paths may not contain '.': " + label.package) - return package_name.replace("/", ".") - - -def _new_dart_context(label, - package, - lib_root, - srcs=None, - data=None, - deps=None, - transitive_deps=None): - return struct( - label=label, - package=package, - lib_root=lib_root, - srcs=srcs or [], - data=data or [], - deps=deps or [], - transitive_deps=dict(transitive_deps or {}), - ) - - -def make_dart_context(label, - package=None, - lib_root=None, - srcs=None, - data=None, - deps=None): - if not package: - package = _label_to_dart_package_name(label) - if not lib_root: - lib_root = "%s/lib/" % label.package - srcs = srcs or [] - data = data or [] - deps = deps or [] - transitive_deps = _collect_transitive_deps(deps) - return struct( - label=label, - package=package, - lib_root=lib_root, - srcs=srcs, - data=data, - deps=deps, - transitive_deps=transitive_deps, - ) - + """Returns the Dart package name for the specified label. + + Packages under //third_party/dart resolve to their external Pub package names. + All other packages resolve to a unique identifier based on their repo path. + + Examples: + //foo/bar/baz: foo.bar.baz + //third_party/dart/args: args + //third_party/guice: third_party.guice + + Restrictions: + Since packages outside of //third_party/dart are identified by their path + components joined by periods, it is an error for the label package to + contain periods. + + Args: + label: the label whose package name is to be returned. + + Returns: + The Dart package name associated with the label. + """ + package_name = label.package + if label.package.startswith(_third_party_prefix): + third_party_path = label.package[len(_third_party_prefix):] + if "/" not in third_party_path: + package_name = third_party_path + if "." in package_name: + fail("Dart package paths may not contain '.': " + label.package) + return package_name.replace("/", ".") + +def _new_dart_context( + label, + package, + lib_root, + srcs = None, + data = None, + deps = None, + transitive_deps = None): + return struct( + label = label, + package = package, + lib_root = lib_root, + srcs = srcs or [], + data = data or [], + deps = deps or [], + transitive_deps = dict(transitive_deps or {}), + ) + +def make_dart_context( + label, + package = None, + lib_root = None, + srcs = None, + data = None, + deps = None): + if not package: + package = _label_to_dart_package_name(label) + if not lib_root: + lib_root = "%s/lib/" % label.package + srcs = srcs or [] + data = data or [] + deps = deps or [] + transitive_deps = _collect_transitive_deps(deps) + return struct( + label = label, + package = package, + lib_root = lib_root, + srcs = srcs, + data = data, + deps = deps, + transitive_deps = transitive_deps, + ) def _merge_dart_context(dart_ctx1, dart_ctx2): - """Merges two dart contexts whose package and lib_root must be identical.""" - if dart_ctx1.package != dart_ctx2.package: - fail("Incompatible packages: %s and %s" % (dart_ctx1.package, - dart_ctx2.package)) - if dart_ctx1.lib_root != dart_ctx2.lib_root: - fail("Incompatible lib_roots for package %s:\n" % dart_ctx1.package + - " %s declares: %s\n" % (dart_ctx1.label, dart_ctx1.lib_root) + - " %s declares: %s\n" % (dart_ctx2.label, dart_ctx2.lib_root) + - "Targets in the same package must declare the same lib_root") - - transitive_deps = {} - transitive_deps.update(dart_ctx1.transitive_deps) - transitive_deps.update(dart_ctx2.transitive_deps) - return _new_dart_context( - label=dart_ctx1.label, - package=dart_ctx1.package, - lib_root=dart_ctx1.lib_root, - srcs=dart_ctx1.srcs + dart_ctx2.srcs, - data=dart_ctx1.data + dart_ctx2.data, - deps=dart_ctx1.deps + dart_ctx2.deps, - transitive_deps=transitive_deps, - ) - - -def _collect_dart_context(dart_ctx, transitive=True, include_self=True): - """Collects and returns dart contexts.""" - # Collect direct or transitive deps. - dart_ctxs = [dart_ctx] - if transitive: - dart_ctxs += [d.dart for d in dart_ctx.transitive_deps.values()] - else: - dart_ctxs += [d.dart for d in dart_ctx.deps] - - # Optionally, exclude all self-packages. - if not include_self: - dart_ctxs = [c for c in dart_ctxs if c.package != dart_ctx.package] - - # Merge Dart context by package. - ctx_map = {} - for dc in dart_ctxs: - if dc.package in ctx_map: - dc = _merge_dart_context(ctx_map[dc.package], dc) - ctx_map[dc.package] = dc - return ctx_map - + """Merges two dart contexts whose package and lib_root must be identical.""" + if dart_ctx1.package != dart_ctx2.package: + fail("Incompatible packages: %s and %s" % ( + dart_ctx1.package, + dart_ctx2.package, + )) + if dart_ctx1.lib_root != dart_ctx2.lib_root: + fail("Incompatible lib_roots for package %s:\n" % dart_ctx1.package + + " %s declares: %s\n" % (dart_ctx1.label, dart_ctx1.lib_root) + + " %s declares: %s\n" % (dart_ctx2.label, dart_ctx2.lib_root) + + "Targets in the same package must declare the same lib_root") + + transitive_deps = {} + transitive_deps.update(dart_ctx1.transitive_deps) + transitive_deps.update(dart_ctx2.transitive_deps) + return _new_dart_context( + label = dart_ctx1.label, + package = dart_ctx1.package, + lib_root = dart_ctx1.lib_root, + srcs = dart_ctx1.srcs + dart_ctx2.srcs, + data = dart_ctx1.data + dart_ctx2.data, + deps = dart_ctx1.deps + dart_ctx2.deps, + transitive_deps = transitive_deps, + ) + +def _collect_dart_context(dart_ctx, transitive = True, include_self = True): + """Collects and returns dart contexts.""" + + # Collect direct or transitive deps. + dart_ctxs = [dart_ctx] + if transitive: + dart_ctxs += [d.dart for d in dart_ctx.transitive_deps.values()] + else: + dart_ctxs += [d.dart for d in dart_ctx.deps] + + # Optionally, exclude all self-packages. + if not include_self: + dart_ctxs = [c for c in dart_ctxs if c.package != dart_ctx.package] + + # Merge Dart context by package. + ctx_map = {} + for dc in dart_ctxs: + if dc.package in ctx_map: + dc = _merge_dart_context(ctx_map[dc.package], dc) + ctx_map[dc.package] = dc + return ctx_map def package_spec_action(ctx, dart_ctx, output): - """Creates an action that generates a Dart package spec. - - Arguments: - ctx: The rule context. - dart_ctx: The Dart context. - output: The output package_spec file. - """ - # There's a 1-to-many relationship between packages and targets, but - # collect_transitive_packages() asserts that their lib_roots are the same. - dart_ctxs = _collect_dart_context(dart_ctx, - transitive=True, - include_self=True).values() - - # Generate the content. - content = "# Generated by Bazel\n" - for dc in dart_ctxs: - relative_lib_root = _relative_path(dart_ctx.label.package, dc.lib_root) - content += "%s:%s\n" % (dc.package, relative_lib_root) - - # Emit the package spec. - ctx.actions.write( - output=output, - content=content, - ) - + """Creates an action that generates a Dart package spec. + + Arguments: + ctx: The rule context. + dart_ctx: The Dart context. + output: The output package_spec file. + """ + + # There's a 1-to-many relationship between packages and targets, but + # collect_transitive_packages() asserts that their lib_roots are the same. + dart_ctxs = _collect_dart_context( + dart_ctx, + transitive = True, + include_self = True, + ).values() + + # Generate the content. + content = "# Generated by Bazel\n" + for dc in dart_ctxs: + relative_lib_root = _relative_path(dart_ctx.label.package, dc.lib_root) + content += "%s:%s\n" % (dc.package, relative_lib_root) + + # Emit the package spec. + ctx.actions.write( + output = output, + content = content, + ) def _relative_path(from_dir, to_path): - """Returns the relative path from a directory to a path via the repo root.""" - return "../" * (from_dir.count("/") + 1) + to_path - + """Returns the relative path from a directory to a path via the repo root.""" + return "../" * (from_dir.count("/") + 1) + to_path def layout_action(ctx, srcs, output_dir): - """Generates a flattened directory of sources. - - For each file f in srcs, a file is emitted at output_dir/f.short_path. - Returns a dict mapping short_path to the emitted file. - - Args: - ctx: the build context. - srcs: the set of input srcs to be flattened. - output_dir: the full output directory path into which the files are emitted. - - Returns: - A map from input file short_path to File in output_dir. - """ - commands = [] - output_files = {} - # TODO(cbracken) extract next two lines to func - if not output_dir.endswith("/"): - output_dir += "/" - for src_file in srcs: - dest_file = ctx.actions.declare_file(output_dir + src_file.short_path) - dest_dir = dest_file.path[:dest_file.path.rfind("/")] - link_target = _relative_path(dest_dir, src_file.path) - commands += ["ln -s '%s' '%s'" % (link_target, dest_file.path)] - output_files[src_file.short_path] = dest_file - - # Emit layout script. - layout_cmd = ctx.actions.declare_file(ctx.label.name + "_layout.sh") - ctx.actions.write( - output=layout_cmd, - content="#!/bin/bash\n" + "\n".join(commands), - is_executable=True, - ) - - # Invoke the layout action. - ctx.actions.run( - inputs=list(srcs), - outputs=output_files.values(), - executable=layout_cmd, - progress_message="Building flattened source layout for %s" % ctx, - mnemonic="DartLayout", - ) - return output_files + """Generates a flattened directory of sources. + + For each file f in srcs, a file is emitted at output_dir/f.short_path. + Returns a dict mapping short_path to the emitted file. + + Args: + ctx: the build context. + srcs: the set of input srcs to be flattened. + output_dir: the full output directory path into which the files are emitted. + + Returns: + A map from input file short_path to File in output_dir. + """ + commands = [] + output_files = {} + + # TODO(cbracken) extract next two lines to func + if not output_dir.endswith("/"): + output_dir += "/" + for src_file in srcs: + dest_file = ctx.actions.declare_file(output_dir + src_file.short_path) + dest_dir = dest_file.path[:dest_file.path.rfind("/")] + link_target = _relative_path(dest_dir, src_file.path) + commands += ["ln -s '%s' '%s'" % (link_target, dest_file.path)] + output_files[src_file.short_path] = dest_file + + # Emit layout script. + layout_cmd = ctx.actions.declare_file(ctx.label.name + "_layout.sh") + ctx.actions.write( + output = layout_cmd, + content = "#!/bin/bash\n" + "\n".join(commands), + is_executable = True, + ) + + # Invoke the layout action. + ctx.actions.run( + inputs = list(srcs), + outputs = output_files.values(), + executable = layout_cmd, + progress_message = "Building flattened source layout for %s" % ctx, + mnemonic = "DartLayout", + ) + return output_files diff --git a/dart/build_rules/repositories.bzl b/dart/build_rules/repositories.bzl index 3c7c338..2400a24 100644 --- a/dart/build_rules/repositories.bzl +++ b/dart/build_rules/repositories.bzl @@ -16,7 +16,6 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - _DART_SDK_BUILD_FILE = """ package(default_visibility = [ "//visibility:public" ]) @@ -55,33 +54,33 @@ filegroup( """ def dart_repositories(): - sdk_channel = "stable" - sdk_version = "2.17.7" - linux_x64_sha = "ba8bc85883e38709351f78c527cbf72e22cd234b3678a1ec6a2e781f7984e624" - macos_arm64_sha = "a4be379202cf731c7e33de20b4abc4ca1e2e726bc5973222b3a7ae5a0cabfce1" - macos_x64_sha = "ba258fff40822cb410c4f1f7916b63f0837903a6bae8f4bd83341053b10ecbe3" + sdk_channel = "stable" + sdk_version = "2.17.7" + linux_x64_sha = "ba8bc85883e38709351f78c527cbf72e22cd234b3678a1ec6a2e781f7984e624" + macos_arm64_sha = "a4be379202cf731c7e33de20b4abc4ca1e2e726bc5973222b3a7ae5a0cabfce1" + macos_x64_sha = "ba258fff40822cb410c4f1f7916b63f0837903a6bae8f4bd83341053b10ecbe3" - sdk_base_url = ("https://storage.googleapis.com/dart-archive/channels/" + - sdk_channel + "/release/" + - sdk_version + "/sdk/") + sdk_base_url = ("https://storage.googleapis.com/dart-archive/channels/" + + sdk_channel + "/release/" + + sdk_version + "/sdk/") - http_archive( - name = "dart_linux_x86_64", - url = sdk_base_url + "dartsdk-linux-x64-release.zip", - sha256 = linux_x64_sha, - build_file_content = _DART_SDK_BUILD_FILE, - ) + http_archive( + name = "dart_linux_x86_64", + url = sdk_base_url + "dartsdk-linux-x64-release.zip", + sha256 = linux_x64_sha, + build_file_content = _DART_SDK_BUILD_FILE, + ) - http_archive( - name = "dart_darwin_arm64", - url = sdk_base_url + "dartsdk-macos-arm64-release.zip", - sha256 = macos_arm64_sha, - build_file_content = _DART_SDK_BUILD_FILE, - ) + http_archive( + name = "dart_darwin_arm64", + url = sdk_base_url + "dartsdk-macos-arm64-release.zip", + sha256 = macos_arm64_sha, + build_file_content = _DART_SDK_BUILD_FILE, + ) - http_archive( - name = "dart_darwin_x86_64", - url = sdk_base_url + "dartsdk-macos-x64-release.zip", - sha256 = macos_x64_sha, - build_file_content = _DART_SDK_BUILD_FILE, - ) + http_archive( + name = "dart_darwin_x86_64", + url = sdk_base_url + "dartsdk-macos-x64-release.zip", + sha256 = macos_x64_sha, + build_file_content = _DART_SDK_BUILD_FILE, + ) diff --git a/dart/build_rules/vm.bzl b/dart/build_rules/vm.bzl index 3a24a8f..0156f7b 100644 --- a/dart/build_rules/vm.bzl +++ b/dart/build_rules/vm.bzl @@ -14,240 +14,240 @@ """Dart rules targeting the Dart VM.""" - load(":internal.bzl", "collect_files", "layout_action", "make_dart_context", "package_spec_action") - def _dart_vm_binary_impl(ctx): - """Implements the dart_vm_binary() rule.""" - dart_ctx = make_dart_context(ctx.label, - srcs=ctx.files.srcs, - data=ctx.files.data, - deps=ctx.attr.deps) - - if ctx.attr.snapshot: - # Build snapshot - out_snapshot = ctx.actions.declare_file(ctx.label.name + ".snapshot") - vm_snapshot_action( - ctx=ctx, - dart_ctx=dart_ctx, - output=out_snapshot, - vm_flags=ctx.attr.vm_flags, - script_file=ctx.file.script_file, - script_args=ctx.attr.script_args, + """Implements the dart_vm_binary() rule.""" + dart_ctx = make_dart_context( + ctx.label, + srcs = ctx.files.srcs, + data = ctx.files.data, + deps = ctx.attr.deps, ) - script_file = out_snapshot - else: - script_file = ctx.file.script_file - - # Emit package spec. - package_spec = ctx.actions.declare_file(ctx.label.name + ".packages") - package_spec_action( - ctx=ctx, - dart_ctx=dart_ctx, - output=package_spec, - ) - - # Emit entrypoint script. - ctx.actions.expand_template( - output=ctx.outputs.executable, - template=ctx.file._entrypoint_template, - is_executable=True, - substitutions={ - "%workspace%": ctx.workspace_name, - "%dart_vm%": ctx.executable._dart_vm.short_path, - "%package_spec%": package_spec.short_path, - "%vm_flags%": " ".join(ctx.attr.vm_flags), - "%script_file%": script_file.short_path, - "%script_args%": " ".join(ctx.attr.script_args), - }, - ) - - # Compute runfiles. - all_srcs, all_data = collect_files(dart_ctx) - runfiles_files = all_data + [ - ctx.executable._dart_vm, - ctx.outputs.executable, - package_spec, - ] - if ctx.attr.snapshot: - runfiles_files += [out_snapshot] - else: - runfiles_files += all_srcs - runfiles = ctx.runfiles( - files=list(runfiles_files), - collect_data=True, - ) - return struct( - runfiles=runfiles, - ) + if ctx.attr.snapshot: + # Build snapshot + out_snapshot = ctx.actions.declare_file(ctx.label.name + ".snapshot") + vm_snapshot_action( + ctx = ctx, + dart_ctx = dart_ctx, + output = out_snapshot, + vm_flags = ctx.attr.vm_flags, + script_file = ctx.file.script_file, + script_args = ctx.attr.script_args, + ) + script_file = out_snapshot + else: + script_file = ctx.file.script_file + + # Emit package spec. + package_spec = ctx.actions.declare_file(ctx.label.name + ".packages") + package_spec_action( + ctx = ctx, + dart_ctx = dart_ctx, + output = package_spec, + ) + # Emit entrypoint script. + ctx.actions.expand_template( + output = ctx.outputs.executable, + template = ctx.file._entrypoint_template, + is_executable = True, + substitutions = { + "%workspace%": ctx.workspace_name, + "%dart_vm%": ctx.executable._dart_vm.short_path, + "%package_spec%": package_spec.short_path, + "%vm_flags%": " ".join(ctx.attr.vm_flags), + "%script_file%": script_file.short_path, + "%script_args%": " ".join(ctx.attr.script_args), + }, + ) + + # Compute runfiles. + all_srcs, all_data = collect_files(dart_ctx) + runfiles_files = all_data + [ + ctx.executable._dart_vm, + ctx.outputs.executable, + package_spec, + ] + if ctx.attr.snapshot: + runfiles_files += [out_snapshot] + else: + runfiles_files += all_srcs + runfiles = ctx.runfiles( + files = list(runfiles_files), + collect_data = True, + ) + + return struct( + runfiles = runfiles, + ) _dart_vm_binary_attrs = { - "script_file": attr.label(allow_single_file=True, mandatory=True), + "script_file": attr.label(allow_single_file = True, mandatory = True), "script_args": attr.string_list(), "vm_flags": attr.string_list(), - "srcs": attr.label_list(allow_files=True, mandatory=True), - "data": attr.label_list(allow_files=True), - "deps": attr.label_list(providers=["dart"]), - "snapshot": attr.bool(default=True), + "srcs": attr.label_list(allow_files = True, mandatory = True), + "data": attr.label_list(allow_files = True), + "deps": attr.label_list(providers = ["dart"]), + "snapshot": attr.bool(default = True), "_dart_vm": attr.label( - allow_single_file=True, - executable=True, - cfg="host", - default=Label("//dart/build_rules/ext:dart_vm"), + allow_single_file = True, + executable = True, + cfg = "host", + default = Label("//dart/build_rules/ext:dart_vm"), ), "_entrypoint_template": attr.label( - allow_single_file=True, - default=Label("//dart/build_rules/templates:dart_vm_binary")), + allow_single_file = True, + default = Label("//dart/build_rules/templates:dart_vm_binary"), + ), } - dart_vm_binary = rule( - implementation=_dart_vm_binary_impl, - attrs=_dart_vm_binary_attrs, - executable=True, + implementation = _dart_vm_binary_impl, + attrs = _dart_vm_binary_attrs, + executable = True, ) - def vm_snapshot_action(ctx, dart_ctx, output, vm_flags, script_file, script_args): - """Emits a Dart VM snapshot.""" - build_dir = ctx.label.name + ".build/" - - # Emit package spec. - package_spec_path = ctx.label.package + "/" + ctx.label.name + ".packages" - package_spec = ctx.actions.declare_file(build_dir + package_spec_path) - package_spec_action( - ctx=ctx, - output=package_spec, - dart_ctx=dart_ctx, - ) - - # Build a flattened directory of dart2js inputs, including inputs from the - # src tree, genfiles, and bin. - all_srcs, _ = collect_files(dart_ctx) - build_dir_files = layout_action( - ctx=ctx, - srcs=all_srcs, - output_dir=build_dir, - ) - out_script = build_dir_files[script_file.short_path] - - # TODO(cbracken) assert --snapshot not in flags - # TODO(cbracken) assert --packages not in flags - arguments = [ - "--packages=%s" % package_spec.path, - "--snapshot=%s" % output.path, - ] - arguments += vm_flags - arguments += [out_script.path] - arguments += script_args - ctx.actions.run( - inputs=build_dir_files.values() + [package_spec], - outputs=[output], - executable=ctx.executable._dart_vm, - arguments=arguments, - progress_message="Building Dart VM snapshot %s" % ctx, - mnemonic="DartVMSnapshot", - ) + """Emits a Dart VM snapshot.""" + build_dir = ctx.label.name + ".build/" + + # Emit package spec. + package_spec_path = ctx.label.package + "/" + ctx.label.name + ".packages" + package_spec = ctx.actions.declare_file(build_dir + package_spec_path) + package_spec_action( + ctx = ctx, + output = package_spec, + dart_ctx = dart_ctx, + ) + # Build a flattened directory of dart2js inputs, including inputs from the + # src tree, genfiles, and bin. + all_srcs, _ = collect_files(dart_ctx) + build_dir_files = layout_action( + ctx = ctx, + srcs = all_srcs, + output_dir = build_dir, + ) + out_script = build_dir_files[script_file.short_path] + + # TODO(cbracken) assert --snapshot not in flags + # TODO(cbracken) assert --packages not in flags + arguments = [ + "--packages=%s" % package_spec.path, + "--snapshot=%s" % output.path, + ] + arguments += vm_flags + arguments += [out_script.path] + arguments += script_args + ctx.actions.run( + inputs = build_dir_files.values() + [package_spec], + outputs = [output], + executable = ctx.executable._dart_vm, + arguments = arguments, + progress_message = "Building Dart VM snapshot %s" % ctx, + mnemonic = "DartVMSnapshot", + ) def _dart_vm_snapshot_impl(ctx): - """Implements the dart_vm_snapshot build rule.""" - dart_ctx = make_dart_context(ctx.label, - srcs=ctx.files.srcs, - data=ctx.files.data, - deps=ctx.attr.deps) - vm_snapshot_action( - ctx=ctx, - dart_ctx=dart_ctx, - output=ctx.outputs.snapshot, - vm_flags=ctx.attr.vm_flags, - script_file=ctx.file.script_file, - script_args=ctx.attr.script_args, - ) - return struct() - + """Implements the dart_vm_snapshot build rule.""" + dart_ctx = make_dart_context( + ctx.label, + srcs = ctx.files.srcs, + data = ctx.files.data, + deps = ctx.attr.deps, + ) + vm_snapshot_action( + ctx = ctx, + dart_ctx = dart_ctx, + output = ctx.outputs.snapshot, + vm_flags = ctx.attr.vm_flags, + script_file = ctx.file.script_file, + script_args = ctx.attr.script_args, + ) + return struct() dart_vm_snapshot = rule( - implementation=_dart_vm_snapshot_impl, - attrs=_dart_vm_binary_attrs, - outputs={"snapshot": "%{name}.snapshot"}, + implementation = _dart_vm_snapshot_impl, + attrs = _dart_vm_binary_attrs, + outputs = {"snapshot": "%{name}.snapshot"}, ) - def _dart_vm_test_impl(ctx): - """Implements the dart_vm_test() rule.""" - dart_ctx = make_dart_context(ctx.label, - srcs=ctx.files.srcs, - data=ctx.files.data, - deps=ctx.attr.deps) - - # Emit package spec. - package_spec = ctx.actions.declare_file(ctx.label.name + ".packages") - package_spec_action( - ctx=ctx, - dart_ctx=dart_ctx, - output=package_spec, - ) - - # Emit entrypoint script. - ctx.actions.expand_template( - output=ctx.outputs.executable, - template=ctx.file._entrypoint_template, - is_executable=True, - substitutions={ - "%workspace%": ctx.workspace_name, - "%dart_vm%": ctx.executable._dart_vm.short_path, - "%package_spec%": package_spec.short_path, - "%vm_flags%": " ".join(ctx.attr.vm_flags), - "%script_file%": ctx.file.script_file.short_path, - "%script_args%": " ".join(ctx.attr.script_args), - }, - ) - - # Compute runfiles. - all_srcs, all_data = collect_files(dart_ctx) - runfiles_files = all_data + [ - ctx.executable._dart_vm, - ctx.outputs.executable, - ] - runfiles_files += all_srcs - runfiles_files += [package_spec] - runfiles = ctx.runfiles( - files=list(runfiles_files), - ) - - return struct( - runfiles=runfiles, - instrumented_files=struct( - source_attributes=["srcs"], - dependency_attributes=["deps"], - ), - ) + """Implements the dart_vm_test() rule.""" + dart_ctx = make_dart_context( + ctx.label, + srcs = ctx.files.srcs, + data = ctx.files.data, + deps = ctx.attr.deps, + ) + + # Emit package spec. + package_spec = ctx.actions.declare_file(ctx.label.name + ".packages") + package_spec_action( + ctx = ctx, + dart_ctx = dart_ctx, + output = package_spec, + ) + + # Emit entrypoint script. + ctx.actions.expand_template( + output = ctx.outputs.executable, + template = ctx.file._entrypoint_template, + is_executable = True, + substitutions = { + "%workspace%": ctx.workspace_name, + "%dart_vm%": ctx.executable._dart_vm.short_path, + "%package_spec%": package_spec.short_path, + "%vm_flags%": " ".join(ctx.attr.vm_flags), + "%script_file%": ctx.file.script_file.short_path, + "%script_args%": " ".join(ctx.attr.script_args), + }, + ) + + # Compute runfiles. + all_srcs, all_data = collect_files(dart_ctx) + runfiles_files = all_data + [ + ctx.executable._dart_vm, + ctx.outputs.executable, + ] + runfiles_files += all_srcs + runfiles_files += [package_spec] + runfiles = ctx.runfiles( + files = list(runfiles_files), + ) + + return struct( + runfiles = runfiles, + instrumented_files = struct( + source_attributes = ["srcs"], + dependency_attributes = ["deps"], + ), + ) _dart_vm_test_attrs = { - "script_file": attr.label(allow_single_file=True, mandatory=True), + "script_file": attr.label(allow_single_file = True, mandatory = True), "script_args": attr.string_list(), "vm_flags": attr.string_list(), - "srcs": attr.label_list(allow_files=True, mandatory=True), - "data": attr.label_list(allow_files=True), - "deps": attr.label_list(providers=["dart"]), + "srcs": attr.label_list(allow_files = True, mandatory = True), + "data": attr.label_list(allow_files = True), + "deps": attr.label_list(providers = ["dart"]), "_dart_vm": attr.label( - allow_single_file=True, - executable=True, - cfg="host", - default=Label("//dart/build_rules/ext:dart_vm")), + allow_single_file = True, + executable = True, + cfg = "host", + default = Label("//dart/build_rules/ext:dart_vm"), + ), "_entrypoint_template": attr.label( - allow_single_file=True, - default=Label("//dart/build_rules/templates:dart_vm_test_template")), + allow_single_file = True, + default = Label("//dart/build_rules/templates:dart_vm_test_template"), + ), } - dart_vm_test = rule( - implementation=_dart_vm_test_impl, - attrs=_dart_vm_test_attrs, - executable=True, - test=True, + implementation = _dart_vm_test_impl, + attrs = _dart_vm_test_attrs, + executable = True, + test = True, ) diff --git a/dart/build_rules/web.bzl b/dart/build_rules/web.bzl index 9c3e46d..c0af330 100644 --- a/dart/build_rules/web.bzl +++ b/dart/build_rules/web.bzl @@ -14,161 +14,169 @@ """Dart rules targeting web clients.""" - load(":internal.bzl", "collect_files", "layout_action", "make_dart_context", "package_spec_action") - -def dart2js_action(ctx, dart_ctx, script_file, - enable_asserts, csp, dump_info, minify, preserve_uris, - js_output, part_outputs, other_outputs): - """dart2js compile action.""" - # Create a build directory. - build_dir = ctx.label.name + ".build/" - - # Emit package spec. - package_spec_path = ctx.label.package + "/" + ctx.label.name + ".packages" - package_spec = ctx.actions.declare_file(build_dir + package_spec_path) - package_spec_action( - ctx=ctx, - dart_ctx=dart_ctx, - output=package_spec, - ) - - # Build a flattened directory of dart2js inputs, including inputs from the - # src tree, genfiles, and bin. - all_srcs, _ = collect_files(dart_ctx) - build_dir_files = layout_action( - ctx=ctx, - srcs=all_srcs, - output_dir=build_dir, - ) - out_script = build_dir_files[script_file.short_path] - - # Compute action inputs. - inputs = ( - build_dir_files.values() + [package_spec] - ) - tools = ( - ctx.files._dart2js + - ctx.files._dart2js_support - ) - - # Compute dart2js args. - dart2js_args = [ - "--packages=%s" % package_spec.path, - "--out=%s" % js_output.path, - ] - if enable_asserts: - dart2js_args += ["--enable-asserts"] - if csp: - dart2js_args += ["--csp"] - if dump_info: - dart2js_args += ["--dump-info"] - if minify: - dart2js_args += ["--minify"] - if preserve_uris: - dart2js_args += ["--preserve-uris"] - dart2js_args += [out_script.path] - ctx.actions.run( - inputs=inputs, - tools=tools, - executable=ctx.executable._dart2js_helper, - arguments=[ - str(ctx.label), - str(ctx.attr.deferred_lib_count), - ctx.outputs.js.path, - ctx.executable._dart2js.path, - ] + dart2js_args, - outputs=[js_output] + part_outputs + other_outputs, - progress_message="Compiling with dart2js %s" % ctx, - mnemonic="Dart2jsCompile", - ) - +def dart2js_action( + ctx, + dart_ctx, + script_file, + enable_asserts, + csp, + dump_info, + minify, + preserve_uris, + js_output, + part_outputs, + other_outputs): + """dart2js compile action.""" + + # Create a build directory. + build_dir = ctx.label.name + ".build/" + + # Emit package spec. + package_spec_path = ctx.label.package + "/" + ctx.label.name + ".packages" + package_spec = ctx.actions.declare_file(build_dir + package_spec_path) + package_spec_action( + ctx = ctx, + dart_ctx = dart_ctx, + output = package_spec, + ) + + # Build a flattened directory of dart2js inputs, including inputs from the + # src tree, genfiles, and bin. + all_srcs, _ = collect_files(dart_ctx) + build_dir_files = layout_action( + ctx = ctx, + srcs = all_srcs, + output_dir = build_dir, + ) + out_script = build_dir_files[script_file.short_path] + + # Compute action inputs. + inputs = ( + build_dir_files.values() + [package_spec] + ) + tools = ( + ctx.files._dart2js + + ctx.files._dart2js_support + ) + + # Compute dart2js args. + dart2js_args = [ + "--packages=%s" % package_spec.path, + "--out=%s" % js_output.path, + ] + if enable_asserts: + dart2js_args += ["--enable-asserts"] + if csp: + dart2js_args += ["--csp"] + if dump_info: + dart2js_args += ["--dump-info"] + if minify: + dart2js_args += ["--minify"] + if preserve_uris: + dart2js_args += ["--preserve-uris"] + dart2js_args += [out_script.path] + ctx.actions.run( + inputs = inputs, + tools = tools, + executable = ctx.executable._dart2js_helper, + arguments = [ + str(ctx.label), + str(ctx.attr.deferred_lib_count), + ctx.outputs.js.path, + ctx.executable._dart2js.path, + ] + dart2js_args, + outputs = [js_output] + part_outputs + other_outputs, + progress_message = "Compiling with dart2js %s" % ctx, + mnemonic = "Dart2jsCompile", + ) def _dart_web_application_impl(ctx): - """Implements the dart_web_application build rule.""" - dart_ctx = make_dart_context(ctx.label, - srcs=ctx.files.srcs, - data=ctx.files.data, - deps=ctx.attr.deps) - - # Compute outputs. - js_output = ctx.outputs.js - other_outputs = [ - ctx.outputs.deps_file, - ctx.outputs.sourcemap, - ] - if ctx.attr.dump_info: - other_outputs += [ctx.outputs.info_json] - part_outputs = [] - for i in range(1, ctx.attr.deferred_lib_count + 1): - part_outputs += [getattr(ctx.outputs, "part_js%s" % i)] - other_outputs += [getattr(ctx.outputs, "part_sourcemap%s" % i)] - - # Invoke dart2js. - dart2js_action( - ctx=ctx, - dart_ctx=dart_ctx, - script_file=ctx.file.script_file, - enable_asserts=ctx.attr.enable_asserts, - csp=ctx.attr.csp, - dump_info=ctx.attr.dump_info, - minify=ctx.attr.minify, - preserve_uris=ctx.attr.preserve_uris, - js_output=js_output, - part_outputs=part_outputs, - other_outputs=other_outputs, - ) - - # TODO(cbracken) aggregate, inject licenses - return struct() - - - + """Implements the dart_web_application build rule.""" + dart_ctx = make_dart_context( + ctx.label, + srcs = ctx.files.srcs, + data = ctx.files.data, + deps = ctx.attr.deps, + ) + + # Compute outputs. + js_output = ctx.outputs.js + other_outputs = [ + ctx.outputs.deps_file, + ctx.outputs.sourcemap, + ] + if ctx.attr.dump_info: + other_outputs += [ctx.outputs.info_json] + part_outputs = [] + for i in range(1, ctx.attr.deferred_lib_count + 1): + part_outputs += [getattr(ctx.outputs, "part_js%s" % i)] + other_outputs += [getattr(ctx.outputs, "part_sourcemap%s" % i)] + + # Invoke dart2js. + dart2js_action( + ctx = ctx, + dart_ctx = dart_ctx, + script_file = ctx.file.script_file, + enable_asserts = ctx.attr.enable_asserts, + csp = ctx.attr.csp, + dump_info = ctx.attr.dump_info, + minify = ctx.attr.minify, + preserve_uris = ctx.attr.preserve_uris, + js_output = js_output, + part_outputs = part_outputs, + other_outputs = other_outputs, + ) + + # TODO(cbracken) aggregate, inject licenses + return struct() def _dart_web_application_outputs(dump_info, deferred_lib_count): - """Returns the expected output map for dart_web_application.""" - outputs = { - "js": "%{name}.js", - "deps_file": "%{name}.js.deps", - "sourcemap": "%{name}.js.map", - } - if dump_info: - outputs["info_json"] = "%{name}.js.info.json" - for i in range(1, deferred_lib_count + 1): - outputs["part_js%s" % i] = "%%{name}.js_%s.part.js" % i - outputs["part_sourcemap%s" % i] = "%%{name}.js_%s.part.js.map" % i - return outputs - + """Returns the expected output map for dart_web_application.""" + outputs = { + "js": "%{name}.js", + "deps_file": "%{name}.js.deps", + "sourcemap": "%{name}.js.map", + } + if dump_info: + outputs["info_json"] = "%{name}.js.info.json" + for i in range(1, deferred_lib_count + 1): + outputs["part_js%s" % i] = "%%{name}.js_%s.part.js" % i + outputs["part_sourcemap%s" % i] = "%%{name}.js_%s.part.js.map" % i + return outputs dart_web_application = rule( - implementation=_dart_web_application_impl, - attrs={ - "script_file": attr.label(allow_single_file=True, mandatory=True), - "srcs": attr.label_list(allow_files=True, mandatory=True), - "data": attr.label_list(allow_files=True), - "deps": attr.label_list(providers=["dart"]), - "deferred_lib_count": attr.int(default=0), + implementation = _dart_web_application_impl, + attrs = { + "script_file": attr.label(allow_single_file = True, mandatory = True), + "srcs": attr.label_list(allow_files = True, mandatory = True), + "data": attr.label_list(allow_files = True), + "deps": attr.label_list(providers = ["dart"]), + "deferred_lib_count": attr.int(default = 0), # compiler flags - "enable_asserts": attr.bool(default=False), - "csp": attr.bool(default=False), - "dump_info": attr.bool(default=False), - "minify": attr.bool(default=True), - "preserve_uris": attr.bool(default=False), + "enable_asserts": attr.bool(default = False), + "csp": attr.bool(default = False), + "dump_info": attr.bool(default = False), + "minify": attr.bool(default = True), + "preserve_uris": attr.bool(default = False), # tools "_dart2js": attr.label( - allow_single_file=True, - executable=True, - cfg="host", - default=Label("//dart/build_rules/ext:dart2js")), + allow_single_file = True, + executable = True, + cfg = "host", + default = Label("//dart/build_rules/ext:dart2js"), + ), "_dart2js_support": attr.label( - allow_files=True, - default=Label("//dart/build_rules/ext:dart2js_support")), + allow_files = True, + default = Label("//dart/build_rules/ext:dart2js_support"), + ), "_dart2js_helper": attr.label( - allow_single_file=True, - executable=True, - cfg="host", - default=Label("//dart/build_rules/tools:dart2js_helper")), + allow_single_file = True, + executable = True, + cfg = "host", + default = Label("//dart/build_rules/tools:dart2js_helper"), + ), }, - outputs=_dart_web_application_outputs, + outputs = _dart_web_application_outputs, ) diff --git a/examples/hello_bin/BUILD b/examples/hello_bin/BUILD index c86e308..8cb6252 100644 --- a/examples/hello_bin/BUILD +++ b/examples/hello_bin/BUILD @@ -15,8 +15,11 @@ dart_vm_binary( dart_vm_binary( name = "hello_bin_checked", srcs = ["bin/hello.dart"], + script_args = [ + "foo", + "bar", + ], script_file = "bin/hello.dart", - script_args = ["foo", "bar"], vm_flags = ["--enable-asserts"], deps = [ "//examples/goodbye_lib", @@ -27,8 +30,8 @@ dart_vm_binary( dart_vm_binary( name = "nested_bin", srcs = ["bin/nested.dart"], - script_file = "bin/nested.dart", data = [":hello_bin"], + script_file = "bin/nested.dart", deps = [ "//examples/goodbye_lib", ], @@ -37,12 +40,15 @@ dart_vm_binary( dart_vm_binary( name = "hello_bin_snapshot", srcs = ["bin/hello.dart"], + script_args = [ + "foo", + "bar", + ], script_file = "bin/hello.dart", - script_args = ["foo", "bar"], + snapshot = True, vm_flags = ["--enable-asserts"], deps = [ "//examples/goodbye_lib", "//examples/hello_lib", ], - snapshot = True, ) diff --git a/examples/web_app/BUILD b/examples/web_app/BUILD index 3b60674..7203273 100644 --- a/examples/web_app/BUILD +++ b/examples/web_app/BUILD @@ -12,26 +12,26 @@ dart_library( genrule( name = "gen_lib", - cmd = """echo 'String generatedFunc() => "generated";' > $@""", outs = ["web/generated.dart"], + cmd = """echo 'String generatedFunc() => "generated";' > $@""", ) dart_web_application( name = "js_debug", srcs = [":gen_lib"] + glob(["web/**"]), - script_file = "web/main.dart", deferred_lib_count = 1, + dump_info = True, enable_asserts = True, minify = False, - dump_info = True, + script_file = "web/main.dart", deps = [":web_app"], ) dart_web_application( name = "js_release", srcs = glob(["web/**"]), - script_file = "web/main.dart", deferred_lib_count = 1, + script_file = "web/main.dart", deps = [ ":web_app", "//examples/hello_lib",