Skip to content

Commit 804063f

Browse files
authored
Merge pull request #307 from gkk-stripe/gkk-scala-2.12-master-merge
Merge master into scala 2.12 branch
2 parents 4edcdcb + 4e1d42a commit 804063f

File tree

20 files changed

+194
-96
lines changed

20 files changed

+194
-96
lines changed

.bazelrc.travis

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ build --verbose_failures
1717
# runs stuff in a container, and since Travis already runs its script
1818
# in a container (unless you require sudo in your .travis.yml) this
1919
# fails to run tests.
20-
build --spawn_strategy=standalone --genrule_strategy=standalone --strategy=Scalac=worker --strategy=ScroogeRule=worker --worker_max_instances=3
21-
test --test_strategy=standalone
20+
build --strategy=Scalac=worker --strategy=ScroogeRule=worker --worker_max_instances=3
21+
#test --test_strategy=standalone
2222

2323
# Below this line, .travis.yml will cat the default bazelrc.
2424
# This is needed so Bazel starts with the base workspace in its

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<li><a href="#scala_library">scala_library/scala_macro_library</a></li>
88
<li><a href="#scala_binary">scala_binary</a></li>
99
<li><a href="#scala_test">scala_test</a></li>
10+
<li><a href="#scalapb_proto_library">scalapb_proto_library</a></li>
1011
</ul>
1112
</div>
1213

@@ -467,10 +468,20 @@ thrift_library(name, srcs, deps, absolute_prefix, absolute_prefixes)
467468
</tbody>
468469
</table>
469470

471+
<a name="scalapb_proto_library"></a>
470472
## scalapb_proto_library
471473

474+
You first need to add the following to your WORKSPACE file:
475+
476+
```python
477+
load("@io_bazel_rules_scala//scala_proto:scala_proto.bzl", "scala_proto_repositories")
478+
scala_proto_repositories()
479+
```
480+
481+
Then you can import `scalapb_proto_library` in any BUILD file like this:
482+
472483
```python
473-
load("//scala_proto:scala_proto.bzl", "scalapb_proto_library")
484+
load("@io_bazel_rules_scala//scala_proto:scala_proto.bzl", "scalapb_proto_library")
474485
scalapb_proto_library(name, deps, with_grpc, with_java, with_flat_package, with_single_line_to_string)
475486
```
476487

@@ -498,7 +509,7 @@ generated by the [ScalaPB compiler](https://github.com/scalapb/ScalaPB).
498509
<td><code>deps</code></td>
499510
<td>
500511
<p><code>List of labels, required</code></p>
501-
<p>List of proto dependencies that this target depends on. Must be of type <code>proto_library</code></p>
512+
<p>List of dependencies for this target. Must either be of type <code>proto_library</code> or <code>java_proto_library</code> (allowed only if <code>with_java</code> is enabled) </p>
502513
</td>
503514
</tr>
504515
<tr>
@@ -512,7 +523,7 @@ generated by the [ScalaPB compiler](https://github.com/scalapb/ScalaPB).
512523
<td><code>with_java</code></td>
513524
<td>
514525
<p><code>boolean; optional (default False)</code></p>
515-
<p>Enables generation of converters to and from java protobuf bindings</p>
526+
<p>Enables generation of converters to and from java protobuf bindings. If you set this to <code>True</code> make sure that you pass the corresponding <code>java_proto_library</code> target in <code>deps</code></p>
516527
</td>
517528
</tr>
518529
<tr>

WORKSPACE

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ maven_jar(
5656

5757
http_archive(
5858
name = "com_google_protobuf",
59-
urls = ["https://github.com/google/protobuf/archive/b4b0e304be5a68de3d0ee1af9b286f958750f5e4.zip"],
60-
strip_prefix = "protobuf-b4b0e304be5a68de3d0ee1af9b286f958750f5e4",
61-
sha256 = "ff771a662fb6bd4d3cc209bcccedef3e93980a49f71df1e987f6afa3bcdcba3a",
59+
urls = ["https://github.com/google/protobuf/archive/b04e5cba356212e4e8c66c61bbe0c3a20537c5b9.zip"],
60+
strip_prefix = "protobuf-b04e5cba356212e4e8c66c61bbe0c3a20537c5b9",
61+
sha256 = "cf4a434ac3a83040e9f65be92e153d00d075d8cd25e3f6c6d8670879f5796aa0",
6262
)
6363

6464
http_archive(
6565
name = "com_google_protobuf_java",
66-
urls = ["https://github.com/google/protobuf/archive/b4b0e304be5a68de3d0ee1af9b286f958750f5e4.zip"],
67-
strip_prefix = "protobuf-b4b0e304be5a68de3d0ee1af9b286f958750f5e4",
68-
sha256 = "ff771a662fb6bd4d3cc209bcccedef3e93980a49f71df1e987f6afa3bcdcba3a",
66+
urls = ["https://github.com/google/protobuf/archive/b04e5cba356212e4e8c66c61bbe0c3a20537c5b9.zip"],
67+
strip_prefix = "protobuf-b04e5cba356212e4e8c66c61bbe0c3a20537c5b9",
68+
sha256 = "cf4a434ac3a83040e9f65be92e153d00d075d8cd25e3f6c6d8670879f5796aa0",
6969
)

scala/scala.bzl

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Rules for supporting the Scala language."""
1616

1717
load("//specs2:specs2_junit.bzl", "specs2_junit_dependencies")
18+
load(":scala_cross_version.bzl", "scala_version", "scala_mvn_artifact")
1819
_jar_filetype = FileType([".jar"])
1920
_java_filetype = FileType([".java"])
2021
_scala_filetype = FileType([".scala"])
@@ -66,7 +67,7 @@ def _adjust_resources_path(path, resource_strip_prefix):
6667
def _add_resources_cmd(ctx):
6768
res_cmd = []
6869
for f in ctx.files.resources:
69-
c_dir, res_path = _adjust_resources_path(f.path, ctx.attr.resource_strip_prefix)
70+
c_dir, res_path = _adjust_resources_path(f.short_path, ctx.attr.resource_strip_prefix)
7071
target_path = res_path
7172
if target_path[0] == "/":
7273
target_path = target_path[1:]
@@ -197,6 +198,7 @@ PrintCompileTime: {print_compile_time}
197198
ResourceDests: {resource_dest}
198199
ResourceJars: {resource_jars}
199200
ResourceSrcs: {resource_src}
201+
ResourceShortPaths: {resource_short_paths}
200202
ResourceStripPrefix: {resource_strip_prefix}
201203
ScalacOpts: {scala_opts}
202204
SourceJars: {srcjars}
@@ -218,8 +220,9 @@ DependencyAnalyzerMode: {dependency_analyzer_mode}
218220
srcjars=",".join([f.path for f in all_srcjars]),
219221
java_files=",".join([f.path for f in java_srcs]),
220222
resource_src=",".join([f.path for f in ctx.files.resources]),
223+
resource_short_paths=",".join([f.short_path for f in ctx.files.resources]),
221224
resource_dest=",".join(
222-
[_adjust_resources_path_by_default_prefixes(f.path)[1] for f in ctx.files.resources]
225+
[_adjust_resources_path_by_default_prefixes(f.short_path)[1] for f in ctx.files.resources]
223226
),
224227
resource_strip_prefix=ctx.attr.resource_strip_prefix,
225228
resource_jars=",".join([f.path for f in ctx.files.resource_jars]),
@@ -390,15 +393,20 @@ def _write_launcher(ctx, rjars, main_class, jvm_flags, args="", wrapper_preamble
390393
javabin = "%s/%s" % (runfiles_root, ctx.executable._java.short_path)
391394
template = ctx.attr._java_stub_template.files.to_list()[0]
392395

396+
exec_str = ""
397+
if wrapper_preamble == "":
398+
exec_str = "exec "
399+
393400
wrapper = ctx.new_file(ctx.label.name + "_wrapper.sh")
394401
ctx.file_action(
395402
output = wrapper,
396403
content = """#!/bin/bash
397404
{preamble}
398405
399-
{javabin} "$@" {args}
406+
{exec_str}{javabin} "$@" {args}
400407
""".format(
401408
preamble=wrapper_preamble,
409+
exec_str=exec_str,
402410
javabin=javabin,
403411
args=args,
404412
),
@@ -514,7 +522,7 @@ def _collect_jars_when_dependency_analyzer_is_on(dep_targets):
514522
jars2labels = jars2labels,
515523
transitive_compile_jars = transitive_compile_jars)
516524

517-
def _collect_jars(dep_targets, dependency_analyzer_is_off = True):
525+
def collect_jars(dep_targets, dependency_analyzer_is_off = True):
518526
"""Compute the runtime and compile-time dependencies from the given targets""" # noqa
519527

520528
if dependency_analyzer_is_off:
@@ -542,10 +550,10 @@ def _collect_jars_from_common_ctx(ctx, extra_deps = [], extra_runtime_deps = [])
542550

543551
# Get jars from deps
544552
auto_deps = [ctx.attr._scalalib, ctx.attr._scalareflect]
545-
deps_jars = _collect_jars(ctx.attr.deps + auto_deps + extra_deps, dependency_analyzer_is_off)
553+
deps_jars = collect_jars(ctx.attr.deps + auto_deps + extra_deps, dependency_analyzer_is_off)
546554
(cjars, transitive_rjars, jars2labels, transitive_compile_jars) = (deps_jars.compile_jars, deps_jars.transitive_runtime_jars, deps_jars.jars2labels, deps_jars.transitive_compile_jars)
547555

548-
runtime_dep_jars = _collect_jars(ctx.attr.runtime_deps + extra_runtime_deps, dependency_analyzer_is_off)
556+
runtime_dep_jars = collect_jars(ctx.attr.runtime_deps + extra_runtime_deps, dependency_analyzer_is_off)
549557
transitive_rjars += runtime_dep_jars.transitive_runtime_jars
550558

551559
if not dependency_analyzer_is_off:
@@ -556,7 +564,7 @@ def _collect_jars_from_common_ctx(ctx, extra_deps = [], extra_runtime_deps = [])
556564
def _format_full_jars_for_intellij_plugin(full_jars):
557565
return [struct (class_jar = jar, ijar = None) for jar in full_jars]
558566

559-
def _create_java_provider(ctx, scalaattr, transitive_compile_time_jars):
567+
def create_java_provider(ctx, scalaattr, transitive_compile_time_jars):
560568
# This is needed because Bazel >=0.6.0 requires ctx.actions and a Java
561569
# toolchain. Fortunately, the same change that added this requirement also
562570
# added this field to the Java provider so we can use it to test which
@@ -610,7 +618,7 @@ def _lib(ctx, non_macro_lib):
610618
# Add information from exports (is key that AFTER all build actions/runfiles analysis)
611619
# Since after, will not show up in deploy_jar or old jars runfiles
612620
# Notice that compile_jars is intentionally transitive for exports
613-
exports_jars = _collect_jars(ctx.attr.exports)
621+
exports_jars = collect_jars(ctx.attr.exports)
614622
next_cjars += exports_jars.compile_jars
615623
transitive_rjars += exports_jars.transitive_runtime_jars
616624

@@ -631,7 +639,7 @@ def _lib(ctx, non_macro_lib):
631639
transitive_exports = [] #needed by intellij plugin
632640
)
633641

634-
java_provider = _create_java_provider(ctx, scalaattr, jars.transitive_compile_jars)
642+
java_provider = create_java_provider(ctx, scalaattr, jars.transitive_compile_jars)
635643

636644
return struct(
637645
files = depset([ctx.outputs.jar]), # Here is the default output
@@ -695,7 +703,7 @@ def _scala_binary_common(ctx, cjars, rjars, transitive_compile_time_jars, jars2l
695703
transitive_exports = [] #needed by intellij plugin
696704
)
697705

698-
java_provider = _create_java_provider(ctx, scalaattr, transitive_compile_time_jars)
706+
java_provider = create_java_provider(ctx, scalaattr, transitive_compile_time_jars)
699707

700708
return struct(
701709
files=depset([ctx.outputs.executable]),
@@ -772,7 +780,7 @@ def _scala_test_impl(ctx):
772780
jars.transitive_compile_jars, jars.jars2labels)
773781
# _scalatest is an http_jar, so its compile jar is run through ijar
774782
# however, contains macros, so need to handle separately
775-
scalatest_jars = _collect_jars([ctx.attr._scalatest]).transitive_runtime_jars
783+
scalatest_jars = collect_jars([ctx.attr._scalatest]).transitive_runtime_jars
776784
cjars += scalatest_jars
777785
transitive_rjars += scalatest_jars
778786

@@ -973,17 +981,6 @@ scala_repl = rule(
973981
fragments = ["java"]
974982
)
975983

976-
def scala_version():
977-
"""return the scala version for use in maven coordinates"""
978-
return "2.12"
979-
980-
def scala_mvn_artifact(artifact):
981-
gav = artifact.split(":")
982-
groupid = gav[0]
983-
artifactid = gav[1]
984-
version = gav[2]
985-
return "%s:%s_%s:%s" % (groupid, artifactid, scala_version(), version)
986-
987984
SCALA_BUILD_FILE = """
988985
# scala.BUILD
989986
java_import(

scala/scala_cross_version.bzl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2015 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helper functions for Scala cross-version support. Encapsulates the logic
16+
of abstracting over Scala major version (2.11, 2.12, etc) for dependency
17+
resolution."""
18+
19+
def scala_version():
20+
"""return the scala version for use in maven coordinates"""
21+
return "2.12"
22+
23+
def scala_mvn_artifact(artifact):
24+
gav = artifact.split(":")
25+
groupid = gav[0]
26+
artifactid = gav[1]
27+
version = gav[2]
28+
return "%s:%s_%s:%s" % (groupid, artifactid, scala_version(), version)

0 commit comments

Comments
 (0)