Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Phase format #874

Closed
wants to merge 8 commits into from
Closed
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
15 changes: 15 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
align.openParenCallSite = false
align.openParenDefnSite = false
continuationIndent.defnSite = 2
danglingParentheses = true
docstrings = JavaDoc
importSelectors = singleLine
maxColumn = 120
newlines.afterImplicitKWInVerticalMultiline = true
rewrite.redundantBraces.stringInterpolation = true
rewrite.rules = [
RedundantParens,
PreferCurlyFors,
SortImports
]
unindentTopLevelOperators = false
Empty file added BUILD
Empty file.
55 changes: 44 additions & 11 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ workspace(name = "io_bazel_rules_scala")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "cdaac537b56375f658179ee2f27813cac19542443f4722b6730d84e4125355e6",
strip_prefix = "buildtools-f27d1753c8b3210d9e87cdc9c45bc2739ae2c2db",
url = "https://github.com/bazelbuild/buildtools/archive/f27d1753c8b3210d9e87cdc9c45bc2739ae2c2db.zip",
)

load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")

buildifier_dependencies()

load("//scala:scala.bzl", "scala_repositories")

scala_repositories()
Expand All @@ -27,6 +39,38 @@ load("//specs2:specs2_junit.bzl", "specs2_junit_repositories")

specs2_junit_repositories()

load("//scala/scalafmt:scalafmt.bzl", "scalafmt_default_config", "scalafmt_repositories")

scalafmt_repositories()

scalafmt_default_config()

RULES_JVM_EXTERNAL_TAG = "2.9"

http_archive(
name = "rules_jvm_external",
sha256 = "e5b97a31a3e8feed91636f42e19b11c49487b85e5de2f387c999ea14d77c7f45",
strip_prefix = "rules_jvm_external-{}".format(RULES_JVM_EXTERNAL_TAG),
type = "zip",
url = "https://github.com/bazelbuild/rules_jvm_external/archive/{}.zip".format(RULES_JVM_EXTERNAL_TAG),
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
name = "scalafmt",
artifacts = [
"net.sourceforge.argparse4j:argparse4j:0.8.1",
"org.scalameta:parsers_2.11:4.2.0",
"com.geirsson:metaconfig-core_2.11:0.8.3",
"org.scalameta:scalafmt-core_2.11:jar:2.0.0",
],
fetch_sources = True,
repositories = [
"http://central.maven.org/maven2",
],
)

load("//scala:scala_cross_version.bzl", "default_scala_major_version", "scala_mvn_artifact")

# test adding a scala jar:
Expand Down Expand Up @@ -151,13 +195,6 @@ http_archive(
url = "https://github.com/bazelbuild/rules_go/releases/download/0.18.7/rules_go-0.18.7.tar.gz",
)

http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "cdaac537b56375f658179ee2f27813cac19542443f4722b6730d84e4125355e6",
strip_prefix = "buildtools-f27d1753c8b3210d9e87cdc9c45bc2739ae2c2db",
url = "https://github.com/bazelbuild/buildtools/archive/f27d1753c8b3210d9e87cdc9c45bc2739ae2c2db.zip",
)

load(
"@io_bazel_rules_go//go:deps.bzl",
"go_register_toolchains",
Expand All @@ -168,10 +205,6 @@ go_rules_dependencies()

go_register_toolchains()

load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")

buildifier_dependencies()

http_archive(
name = "bazel_toolchains",
sha256 = "5962fe677a43226c409316fcb321d668fc4b7fa97cb1f9ef45e7dc2676097b26",
Expand Down
51 changes: 51 additions & 0 deletions scala/private/phases/api.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
load(
"@io_bazel_rules_scala//scala:providers.bzl",
_ScalaRulePhase = "ScalaRulePhase",
)

def _adjust_phases(phases, adjustments):
if len(adjustments) == 0:
return phases
phases = phases[:]
for (relation, peer_name, name, function) in adjustments:
for idx, (needle, _) in enumerate(phases):
if relation in ["^", "first"]:
phases.insert(0, (name, function))
elif relation in ["$", "last"]:
phases.append((name, function))
elif needle == peer_name:
if relation in ["-", "before"]:
phases.insert(idx, (name, function))
elif relation in ["+", "after"]:
phases.insert(idx + 1, (name, function))
elif relation in ["=", "replace"]:
phases[idx] = (name, function)
return phases

def run_phases(ctx, customizable_phases, fixed_phase):
phase_providers = [
p[_ScalaRulePhase]
for p in ctx.attr._phase_providers
if _ScalaRulePhase in p
]

if phase_providers != []:
customizable_phases = _adjust_phases(customizable_phases, [p for pp in phase_providers for p in pp.phases])

global_provider = {}
current_provider = struct(**global_provider)
for (name, function) in customizable_phases + [fixed_phase]:
new_provider = function(ctx, current_provider)
if new_provider != None:
global_provider[name] = new_provider
current_provider = struct(**global_provider)

return current_provider

def extras_phases(extras):
return {
"_phase_providers": attr.label_list(
default = [pp for extra in extras for pp in extra["phase_providers"]],
providers = [_ScalaRulePhase],
),
}
15 changes: 15 additions & 0 deletions scala/private/phases/phase_collect_exports_jars.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# PHASE: collect exports jars
#
# DOCUMENT THIS
#
load(
"@io_bazel_rules_scala//scala/private:common.bzl",
"collect_jars",
)

def phase_collect_exports_jars(ctx, p):
# Add information from exports (is key that AFTER all build actions/runfiles analysis)
# Since after, will not show up in deploy_jar or old jars runfiles
# Notice that compile_jars is intentionally transitive for exports
return collect_jars(ctx.attr.exports)
71 changes: 71 additions & 0 deletions scala/private/phases/phase_collect_jars.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# PHASE: collect jars
#
# DOCUMENT THIS
#
load(
"@io_bazel_rules_scala//scala/private:rule_impls.bzl",
"collect_jars_from_common_ctx",
)

def phase_test_collect_jars(ctx, p):
args = struct(
base_classpath = p.scalac_provider.default_classpath + [ctx.attr._scalatest],
extra_runtime_deps = [
ctx.attr._scalatest_reporter,
ctx.attr._scalatest_runner,
],
)
return phase_common_collect_jars(ctx, p, args)

def phase_repl_collect_jars(ctx, p):
args = struct(
base_classpath = p.scalac_provider.default_repl_classpath,
)
return phase_common_collect_jars(ctx, p, args)

def phase_macro_library_collect_jars(ctx, p):
args = struct(
base_classpath = p.scalac_provider.default_macro_classpath,
)
return phase_common_collect_jars(ctx, p, args)

def phase_junit_test_collect_jars(ctx, p):
args = struct(
extra_deps = [
ctx.attr._junit,
ctx.attr._hamcrest,
ctx.attr.suite_label,
ctx.attr._bazel_test_runner,
],
)
return phase_common_collect_jars(ctx, p, args)

def phase_library_for_plugin_bootstrapping_collect_jars(ctx, p):
args = struct(
unused_dependency_checker_mode = "off",
)
return phase_common_collect_jars(ctx, p, args)

def phase_common_collect_jars(ctx, p, _args = struct()):
return _phase_collect_jars(
ctx,
_args.base_classpath if hasattr(_args, "base_classpath") else p.scalac_provider.default_classpath,
_args.extra_deps if hasattr(_args, "extra_deps") else [],
_args.extra_runtime_deps if hasattr(_args, "extra_runtime_deps") else [],
_args.unused_dependency_checker_mode if hasattr(_args, "unused_dependency_checker_mode") else p.unused_deps_checker,
)

def _phase_collect_jars(
ctx,
base_classpath,
extra_deps,
extra_runtime_deps,
unused_dependency_checker_mode):
return collect_jars_from_common_ctx(
ctx,
base_classpath,
extra_deps,
extra_runtime_deps,
unused_dependency_checker_mode == "off",
)
14 changes: 14 additions & 0 deletions scala/private/phases/phase_collect_srcjars.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# PHASE: collect srcjars
#
# DOCUMENT THIS
#
load(
"@io_bazel_rules_scala//scala/private:common.bzl",
"collect_srcjars",
)

def phase_collect_srcjars(ctx, p):
# This will be used to pick up srcjars from non-scala library
# targets (like thrift code generation)
return collect_srcjars(ctx.attr.deps)
144 changes: 144 additions & 0 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#
# PHASE: compile
#
# DOCUMENT THIS
#
load(
"@io_bazel_rules_scala//scala/private:rule_impls.bzl",
"compile_or_empty",
"pack_source_jars",
)

def phase_binary_compile(ctx, p):
args = struct(
unused_dependency_checker_ignored_targets = [
target.label
for target in p.scalac_provider.default_classpath +
ctx.attr.unused_dependency_checker_ignored_targets
],
)
return phase_common_compile(ctx, p, args)

def phase_library_compile(ctx, p):
args = struct(
srcjars = p.collect_srcjars,
buildijar = True,
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_common_compile(ctx, p, args)

def phase_library_for_plugin_bootstrapping_compile(ctx, p):
args = struct(
buildijar = True,
unused_dependency_checker_ignored_targets = [
target.label
for target in p.scalac_provider.default_classpath + ctx.attr.exports
],
unused_dependency_checker_mode = "off",
)
return phase_common_compile(ctx, p, args)

def phase_macro_library_compile(ctx, p):
args = struct(
unused_dependency_checker_ignored_targets = [
target.label
for target in p.scalac_provider.default_macro_classpath + ctx.attr.exports +
ctx.attr.unused_dependency_checker_ignored_targets
],
)
return phase_common_compile(ctx, p, args)

def phase_junit_test_compile(ctx, p):
args = struct(
implicit_junit_deps_needed_for_java_compilation = [
ctx.attr._junit,
ctx.attr._hamcrest,
],
unused_dependency_checker_ignored_targets = [
target.label
for target in p.scalac_provider.default_classpath +
ctx.attr.unused_dependency_checker_ignored_targets
] + [
ctx.attr._junit.label,
ctx.attr._hamcrest.label,
ctx.attr.suite_label.label,
ctx.attr._bazel_test_runner.label,
],
)
return phase_common_compile(ctx, p, args)

def phase_repl_compile(ctx, p):
args = struct(
unused_dependency_checker_ignored_targets = [
target.label
for target in p.scalac_provider.default_repl_classpath +
ctx.attr.unused_dependency_checker_ignored_targets
],
)
return phase_common_compile(ctx, p, args)

def phase_test_compile(ctx, p):
args = struct(
unused_dependency_checker_ignored_targets = [
target.label
for target in p.scalac_provider.default_classpath +
ctx.attr.unused_dependency_checker_ignored_targets
],
)
return phase_common_compile(ctx, p, args)

def phase_common_compile(ctx, p, _args = struct()):
return _phase_compile(
ctx,
p,
_args.srcjars if hasattr(_args, "srcjars") else depset(),
_args.buildijar if hasattr(_args, "buildijar") else False,
_args.implicit_junit_deps_needed_for_java_compilation if hasattr(_args, "implicit_junit_deps_needed_for_java_compilation") else [],
_args.unused_dependency_checker_ignored_targets if hasattr(_args, "unused_dependency_checker_ignored_targets") else [],
_args.unused_dependency_checker_mode if hasattr(_args, "unused_dependency_checker_mode") else p.unused_deps_checker,
)

def _phase_compile(
ctx,
p,
srcjars,
buildijar,
implicit_junit_deps_needed_for_java_compilation,
unused_dependency_checker_ignored_targets,
unused_dependency_checker_mode):
manifest = ctx.outputs.manifest
jars = p.collect_jars.compile_jars
rjars = p.collect_jars.transitive_runtime_jars
transitive_compile_jars = p.collect_jars.transitive_compile_jars
jars2labels = p.collect_jars.jars2labels.jars_to_labels
deps_providers = p.collect_jars.deps_providers

out = compile_or_empty(
ctx,
manifest,
jars,
srcjars,
buildijar,
transitive_compile_jars,
jars2labels,
implicit_junit_deps_needed_for_java_compilation,
unused_dependency_checker_mode,
unused_dependency_checker_ignored_targets,
deps_providers,
)

return struct(
class_jar = out.class_jar,
coverage = out.coverage,
full_jars = out.full_jars,
ijar = out.ijar,
ijars = out.ijars,
rjars = depset(out.full_jars, transitive = [rjars]),
java_jar = out.java_jar,
source_jars = pack_source_jars(ctx) + out.source_jars,
merged_provider = out.merged_provider,
)
Loading