Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Add support for scala.js #14

Draft
wants to merge 3 commits into
base: upstream/scalapb-deps-toolchain
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.5.0
2.2.0
38 changes: 37 additions & 1 deletion scala/BUILD
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
load("@rules_java//java:defs.bzl", "java_import", "java_library")
load("//scala:scala_toolchain.bzl", "scala_toolchain")
load("//scala:scala_toolchain.bzl", "scala_toolchain", "scala_deps_toolchain", "export_scala_toolchain_deps")
load("//scala:providers.bzl", "declare_deps_provider")

toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

toolchain_type(
name = "deps_toolchain_type",
visibility = ["//visibility:public"],
)

scala_deps_toolchain(
name = "default_deps_toolchain_impl",
dep_providers = [
":scalajs_linker_provider",
],
visibility = ["//visibility:public"],
)

toolchain(
name = "default_deps_toolchain",
toolchain = ":default_deps_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala:deps_toolchain_type",
visibility = ["//visibility:public"],
)

scala_toolchain(
name = "default_toolchain_impl",
scalacopts = [],
Expand Down Expand Up @@ -118,3 +138,19 @@ declare_deps_provider(
visibility = ["//visibility:public"],
deps = ["//external:io_bazel_rules_scala/dependency/scala/parser_combinators"],
)

declare_deps_provider(
name = "scalajs_linker_provider",
deps_id = "scalajs_linker_deps",
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/scala_js/scalajs_ir",
"//external:io_bazel_rules_scala/dependency/scala_js/scalajs_linker"
],
)

export_scala_toolchain_deps(
name = "scalajs_linker_deps",
deps_id = "scalajs_linker_deps",
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ resolve_deps = {
allow_files = False,
),
}

12 changes: 12 additions & 0 deletions scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def scala_repositories(
"io_bazel_rules_scala_scalactic",
"io_bazel_rules_scala_scala_xml",
"io_bazel_rules_scala_scala_parser_combinators",
"io_bazel_rules_scala_scalajs_ir",
"io_bazel_rules_scala_scalajs_linker",
],
maven_servers = _default_maven_server_urls(),
fetch_sources = fetch_sources,
Expand Down Expand Up @@ -131,3 +133,13 @@ def scala_repositories(
name = "io_bazel_rules_scala/dependency/scala/scalactic/scalactic",
actual = "@io_bazel_rules_scala_scalactic",
)

native.bind(
name = "io_bazel_rules_scala/dependency/scala_js/scalajs_ir",
actual = "@io_bazel_rules_scala_scalajs_ir",
)

native.bind(
name = "io_bazel_rules_scala/dependency/scala_js/scalajs_linker",
actual = "@io_bazel_rules_scala_scalajs_linker",
)
30 changes: 27 additions & 3 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ load(
_compile_scala = "compile_scala",
_expand_location = "expand_location",
)
load("@io_bazel_rules_scala//scala/private/toolchain_deps:toolchain_deps.bzl", "find_deps_info_on")
load(":resources.bzl", _resource_paths = "paths")

def phase_compile_binary(ctx, p):
Expand All @@ -41,6 +42,25 @@ def phase_compile_library(ctx, p):
)
return _phase_compile_default(ctx, p, args)

def phase_compile_scalajs_library(ctx, p):
scalajs_compiler_plugin = find_deps_info_on(
ctx,
"@io_bazel_rules_scala//scala:toolchain_type",
"scalajs_compiler_plugin", # TODO: make global?
).deps

args = struct(
plugins = scalajs_compiler_plugin,
srcjars = p.collect_srcjars,
unused_dependency_checker_ignored_targets = [
target.label
for target in p.scalac_provider.default_classpath + ctx.attr.exports +
ctx.attr.unused_dependency_checker_ignored_targets
],
)

return _phase_compile_default(ctx, p, args)

def phase_compile_library_for_plugin_bootstrapping(ctx, p):
args = struct(
buildijar = ctx.attr.build_ijar,
Expand Down Expand Up @@ -110,6 +130,7 @@ def _phase_compile_default(ctx, p, _args = struct()):
_args.buildijar if hasattr(_args, "buildijar") else True,
_args.implicit_junit_deps_needed_for_java_compilation if hasattr(_args, "implicit_junit_deps_needed_for_java_compilation") else [],
unused_dependency_checker_ignored_targets = _args.unused_dependency_checker_ignored_targets if hasattr(_args, "unused_dependency_checker_ignored_targets") else [],
plugins = _args.plugins if hasattr(_args, "plugins") else ctx.attr.plugins,
)

def _phase_compile(
Expand All @@ -119,7 +140,8 @@ def _phase_compile(
buildijar,
# TODO: generalize this hack
implicit_junit_deps_needed_for_java_compilation,
unused_dependency_checker_ignored_targets):
unused_dependency_checker_ignored_targets,
plugins):
manifest = ctx.outputs.manifest
jars = p.collect_jars.compile_jars
rjars = p.collect_jars.transitive_runtime_jars
Expand All @@ -141,6 +163,7 @@ def _phase_compile(
deps_providers,
default_classpath,
unused_dependency_checker_ignored_targets,
plugins
)

# TODO: simplify the return values and use provider
Expand All @@ -165,7 +188,8 @@ def _compile_or_empty(
dependency_info,
deps_providers,
default_classpath,
unused_dependency_checker_ignored_targets):
unused_dependency_checker_ignored_targets,
plugins):
# We assume that if a srcjar is present, it is not empty
if len(ctx.files.srcs) + len(srcjars.to_list()) == 0:
_build_nosrc_jar(ctx)
Expand Down Expand Up @@ -195,7 +219,7 @@ def _compile_or_empty(
jars,
all_srcjars,
transitive_compile_jars,
ctx.attr.plugins,
plugins,
ctx.attr.resource_strip_prefix,
ctx.files.resources,
ctx.files.resource_jars,
Expand Down
39 changes: 39 additions & 0 deletions scala/private/phases/phase_link_scalajs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# PHASE: link scalajs jars
#
#
#
def _colon_paths(data):
return ":".join([
f.path
for f in sorted(data)
])

def phase_link_scalajs(ctx, p):
"""
Links scala.js IR into vanilla javascript.
"""

output_js = ctx.outputs.js
runtime_jars = sorted(p.collect_jars.transitive_runtime_jars.to_list())

worker_content = "{output}\n{main_class}\n{method}\n{include_jars}".format(
output = output_js.path,
main_class = getattr(ctx.attr, "main_class", ""),
method = getattr(ctx.attr, "method", ""),
include_jars = _colon_paths(runtime_jars),
)
argfile = ctx.actions.declare_file(
"%s_worker_input" % ctx.label.name,
sibling = output_js,
)
ctx.actions.write(output = argfile, content = worker_content)
ctx.actions.run(
inputs = runtime_jars + [argfile],
outputs = [output_js],
executable = ctx.attr._scalajs_linker.files_to_run,
mnemonic = "ScalaJSLink",
progress_message = "Linking scala.js file: %s" % ctx.label,
execution_requirements = {"supports-workers": "1"},
arguments = ["@" + argfile.path],
)
6 changes: 6 additions & 0 deletions scala/private/phases/phases.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ load(
_phase_compile_common = "phase_compile_common",
_phase_compile_junit_test = "phase_compile_junit_test",
_phase_compile_library = "phase_compile_library",
_phase_compile_scalajs_library = "phase_compile_scalajs_library",
_phase_compile_library_for_plugin_bootstrapping = "phase_compile_library_for_plugin_bootstrapping",
_phase_compile_macro_library = "phase_compile_macro_library",
_phase_compile_repl = "phase_compile_repl",
Expand Down Expand Up @@ -61,6 +62,7 @@ load(
)
load("@io_bazel_rules_scala//scala/private:phases/phase_declare_executable.bzl", _phase_declare_executable = "phase_declare_executable")
load("@io_bazel_rules_scala//scala/private:phases/phase_merge_jars.bzl", _phase_merge_jars = "phase_merge_jars")
load("@io_bazel_rules_scala//scala/private:phases/phase_link_scalajs.bzl", _phase_link_scalajs = "phase_link_scalajs")
load("@io_bazel_rules_scala//scala/private:phases/phase_jvm_flags.bzl", _phase_jvm_flags = "phase_jvm_flags")
load("@io_bazel_rules_scala//scala/private:phases/phase_coverage_runfiles.bzl", _phase_coverage_runfiles = "phase_coverage_runfiles")
load("@io_bazel_rules_scala//scala/private:phases/phase_scalafmt.bzl", _phase_scalafmt = "phase_scalafmt")
Expand Down Expand Up @@ -91,6 +93,9 @@ phase_declare_executable = _phase_declare_executable
# merge_jars
phase_merge_jars = _phase_merge_jars

# link scala.js
phase_link_scalajs = _phase_link_scalajs

# jvm_flags
phase_jvm_flags = _phase_jvm_flags

Expand Down Expand Up @@ -121,6 +126,7 @@ phase_collect_jars_common = _phase_collect_jars_common
# compile
phase_compile_binary = _phase_compile_binary
phase_compile_library = _phase_compile_library
phase_compile_scalajs_library = _phase_compile_scalajs_library
phase_compile_library_for_plugin_bootstrapping = _phase_compile_library_for_plugin_bootstrapping
phase_compile_macro_library = _phase_compile_macro_library
phase_compile_junit_test = _phase_compile_junit_test
Expand Down
59 changes: 59 additions & 0 deletions scala/private/rules/scala_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load(
"phase_dependency_common",
"phase_java_wrapper_common",
"phase_merge_jars",
"phase_link_scalajs",
"phase_runfiles_common",
"phase_scalac_provider",
"phase_write_executable_common",
Expand Down Expand Up @@ -80,3 +81,61 @@ def make_scala_binary(*extras):
)

scala_binary = make_scala_binary()

def _scalajs_binary_impl(ctx):
return run_phases(
ctx,
# customizable phases
[
("scalac_provider", phase_scalac_provider),
#("write_manifest", phase_write_manifest),
("dependency", phase_dependency_common),
("collect_jars", phase_collect_jars_common),
#("java_wrapper", phase_java_wrapper_common),
#("declare_executable", phase_declare_executable),
# no need to build an ijar for an executable
#("compile", phase_compile_binary),
#("coverage", phase_coverage_common),
("link_scalajs", phase_link_scalajs),
#("runfiles", phase_runfiles_common),
#("write_executable", phase_write_executable_common),
("default_info", phase_default_info),
],
)

_scalajs_binary_attrs = {
"main_class": attr.string(mandatory = True),
"method": attr.string(mandatory = True),
"classpath_resources": attr.label_list(allow_files = True),
"_scalajs_linker": attr.label(
default = Label("@io_bazel_rules_scala//src/scala/scripts:scalajs_linker")
),
}

_scalajs_binary_attrs.update(implicit_deps)

_scalajs_binary_attrs.update(common_attrs)

_scalajs_binary_attrs.update(resolve_deps)

_scalajs_binary_attrs.update()

def make_scalajs_binary(*extras):
return rule(
attrs = _dicts.add(
_scalajs_binary_attrs,
extras_phases(extras),
*[extra["attrs"] for extra in extras if "attrs" in extra]
),
executable = False,
# TODO: what does fragments do?
fragments = ["java"],
outputs = _dicts.add(
{"js": "%{name}.js"},
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scalajs_binary_impl,
)

scalajs_binary = make_scalajs_binary()
53 changes: 53 additions & 0 deletions scala/private/rules/scala_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ load(
"phase_compile_library",
"phase_compile_library_for_plugin_bootstrapping",
"phase_compile_macro_library",
"phase_compile_scalajs_library",
"phase_coverage_common",
"phase_coverage_library",
"phase_default_info",
Expand Down Expand Up @@ -251,3 +252,55 @@ def make_scala_macro_library(*extras):
)

scala_macro_library = make_scala_macro_library()


##
# scalajs_library
##

def _scalajs_library_impl(ctx):
return run_phases(
ctx,
# customizable phases
[
("scalac_provider", phase_scalac_provider),
("collect_srcjars", phase_collect_srcjars),
("write_manifest", phase_write_manifest),
("dependency", phase_dependency_common),
("collect_jars", phase_collect_jars_common),
("compile", phase_compile_scalajs_library),
Copy link
Author

@gvacaliuc gvacaliuc Mar 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that there is a way to change the compile phase here w/o creating a whole new rule

("coverage", phase_coverage_library),
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_library),
("collect_exports_jars", phase_collect_exports_jars),
("default_info", phase_default_info),
],
)

_scalajs_library_attrs = {}

_scalajs_library_attrs.update(implicit_deps)

_scalajs_library_attrs.update(common_attrs)

_scalajs_library_attrs.update(_library_attrs)

_scalajs_library_attrs.update(resolve_deps)

def make_scalajs_library(*extras):
return rule(
attrs = _dicts.add(
_scalajs_library_attrs,
extras_phases(extras),
*[extra["attrs"] for extra in extras if "attrs" in extra]
),
fragments = ["java"],
outputs = _dicts.add(
common_outputs,
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scalajs_library_impl,
)

scalajs_library = make_scalajs_library()
4 changes: 4 additions & 0 deletions scala/scala.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load(
load(
"@io_bazel_rules_scala//scala/private:rules/scala_binary.bzl",
_scala_binary = "scala_binary",
_scalajs_binary = "scalajs_binary",
)
load(
"@io_bazel_rules_scala//scala/private:rules/scala_doc.bzl",
Expand All @@ -21,6 +22,7 @@ load(
load(
"@io_bazel_rules_scala//scala/private:rules/scala_library.bzl",
_scala_library = "scala_library",
_scalajs_library = "scalajs_library",
_scala_library_for_plugin_bootstrapping = "scala_library_for_plugin_bootstrapping",
_scala_library_suite = "scala_library_suite",
_scala_macro_library = "scala_macro_library",
Expand Down Expand Up @@ -50,9 +52,11 @@ def scala_specs2_junit_test(name, **kwargs):

# Re-export private rules for public consumption
scala_binary = _scala_binary
scalajs_binary = _scalajs_binary
scala_doc = _scala_doc
scala_junit_test = _scala_junit_test
scala_library = _scala_library
scalajs_library = _scalajs_library
scala_library_for_plugin_bootstrapping = _scala_library_for_plugin_bootstrapping
scala_library_suite = _scala_library_suite
scala_macro_library = _scala_macro_library
Expand Down
Loading