From 7ac434ec9125ad884195b082b8de41dd90b625c2 Mon Sep 17 00:00:00 2001 From: gdonovan Date: Sun, 4 Nov 2018 10:31:14 -0500 Subject: [PATCH] #39 Remove usage of deprecated build-in http_archive rule. Also: - Move tools/bazel.rc to support new locations: https://docs.bazel.build/versions/master/guide.html#where-are-the-bazelrc-files. - Build with as many incompatible changes as possible. - Update bazel-deps and rules_scala. - Removed no longer needed scala maven workarounds. --- .bazelrc | 33 ++++++++++ .gitignore | 3 + WORKSPACE | 16 +++-- dev-scripts/dependencies/setup.sh | 2 +- tools/bazel.rc | 6 -- tools/bazel_defs/declare_maven.bzl | 63 ------------------ tools/bazel_defs/scala.bzl | 96 ---------------------------- tools/bazel_defs/scala_maven_jar.bzl | 16 ----- 8 files changed, 46 insertions(+), 189 deletions(-) create mode 100644 .bazelrc delete mode 100644 tools/bazel.rc delete mode 100644 tools/bazel_defs/declare_maven.bzl delete mode 100644 tools/bazel_defs/scala.bzl delete mode 100644 tools/bazel_defs/scala_maven_jar.bzl diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000..f7af1e5 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,33 @@ +# Avoid downloading protobuf twice: +build --proto_toolchain_for_java="@com_google_protobuf//:java_toolchain" +# Run with a persistent scalac worker. See: https://github.com/bazelbuild/rules_scala#getting-started +build --strategy=Scalac=worker +test --strategy=Scalac=worker + +# Build with as much future compatibility as possible +# See https://docs.bazel.build/versions/master/skylark/backward-compatibility.html +# TODO We have protobuf 3.6.1 in the WORKSPACE but an older protobuf is causing the errors below. Why? +build --incompatible_bzl_disallow_load_after_statement +build --incompatible_depset_is_not_iterable +build --incompatible_depset_union +build --incompatible_disable_deprecated_attr_params=false # Fixed in protobuf 3.6.1 +build --incompatible_disable_objc_provider_resources +build --incompatible_disallow_conflicting_providers +build --incompatible_disallow_data_transition +build --incompatible_disallow_dict_plus +build --incompatible_disallow_filetype +build --incompatible_disallow_legacy_javainfo=false # See: https://github.com/bazelbuild/rules_scala/issues/513 +build --incompatible_disallow_old_style_args_add +build --incompatible_disallow_slash_operator +build --incompatible_expand_directories +build --incompatible_generate_javacommon_source_jar +build --incompatible_new_actions_api +build --incompatible_no_support_tools_in_action_inputs=false # See: https://github.com/bazelbuild/rules_scala/issues/596 +build --incompatible_no_transitive_loads +build --incompatible_package_name_is_a_function=false # Fixed in protobuf 3.6.1 +build --incompatible_range_type +build --incompatible_remove_native_git_repository +build --incompatible_remove_native_http_archive +build --incompatible_static_name_resolution +build --incompatible_string_is_not_iterable + diff --git a/.gitignore b/.gitignore index f28c2ab..1d10da1 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,6 @@ hs_err_pid* *.DS_STORE # Ignore bazel-deps resolverCache. See: https://github.com/johnynek/bazel-deps#options target/local-repo + +# Ignore IntelliJ library files +.ijwb \ No newline at end of file diff --git a/WORKSPACE b/WORKSPACE index 94d65b3..030c26c 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,16 +1,18 @@ load("//thirdparty:workspace.bzl", "maven_dependencies") -load("//tools/bazel_defs:declare_maven.bzl", "declare_maven") -maven_dependencies(declare_maven) +maven_dependencies() -# Provide dependencies for proto_library and java_proto_library rules. +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# LICENSE: The Apache Software License, Version 2.0 +# proto_library rules implicitly depend on @com_google_protobuf//:protoc http_archive( name = "com_google_protobuf", - sha256 = "091e1aa2b64ea6d512ff9294ecc9da95132c3b961a8fb39a3bab3929e5122f50", - strip_prefix = "protobuf-3.4.1", - urls = ["https://github.com/google/protobuf/releases/download/v3.4.1/protobuf-java-3.4.1.zip"], + sha256 = "d7a221b3d4fb4f05b7473795ccea9e05dab3b8721f6286a95fffbffc2d926f8b", + strip_prefix = "protobuf-3.6.1", + urls = ["https://github.com/google/protobuf/archive/v3.6.1.zip"], ) -rules_scala_version = "5874a2441596fe9a0bf80e167a4d7edd945c221e" # update this as needed +rules_scala_version = "4be50865a332aef46c46c94b345c320c3353e9e1" # HEAD on 11-1-2018. update this as needed. http_archive( name = "io_bazel_rules_scala", diff --git a/dev-scripts/dependencies/setup.sh b/dev-scripts/dependencies/setup.sh index 04bb706..ff32a7b 100644 --- a/dev-scripts/dependencies/setup.sh +++ b/dev-scripts/dependencies/setup.sh @@ -6,7 +6,7 @@ SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) ROOT_DIR=$(cd "${SCRIPT_DIR}/../.." && pwd) BAZEL_DEPS_DIR="$ROOT_DIR/../bazel-deps" # Pin a specific version of bazel-deps; change this to upgrade: -BAZEL_DEPS_VERSION="aad33e40cd2ce2400347e5db28b767e5d512a4fd" +BAZEL_DEPS_VERSION="1af8921d52f053fad575f26762533a3823b4a847" if [ -d "$BAZEL_DEPS_DIR" ] then diff --git a/tools/bazel.rc b/tools/bazel.rc deleted file mode 100644 index add211e..0000000 --- a/tools/bazel.rc +++ /dev/null @@ -1,6 +0,0 @@ -# Avoid downloading protobuf twice: -build --proto_toolchain_for_java="@com_google_protobuf//:java_toolchain" -# Run with a persistent scalac worker. See: https://github.com/bazelbuild/rules_scala#getting-started -build --strategy=Scalac=worker -test --strategy=Scalac=worker - diff --git a/tools/bazel_defs/declare_maven.bzl b/tools/bazel_defs/declare_maven.bzl deleted file mode 100644 index 0670eea..0000000 --- a/tools/bazel_defs/declare_maven.bzl +++ /dev/null @@ -1,63 +0,0 @@ -load('//tools/bazel_defs:scala_maven_jar.bzl', 'scala_maven_jar') - -def declare_maven(hash): - '''Used in WORKSPACE to reify the dependencies calculated by bazel-deps into importable targets.''' - - # Here be dragons. - # - # The maven_jar repository rule from Bazel downloads the given jar and converts it to a bazel repository by - # generating a BUILD file. That BUILD file defines two targets, which are bound under the "@//" repository: - - # @//jar -- this is a java_import target that loads the jar for Bazel's use - @//jar:file -- this is a - # filegroup referencing the jar file - # - # For java libraries, we bind the //jar target to the label "//external:" (with from the hash); the - # java_library targets in the third_party/jvm/ build files then reference those bound labels. Easy peasy. - # - # Scala libraries are a bit different, because the generated java_import target doesn't work for scala jars -- - # specifically, it doesn't expose scala macros from the external JAR, so compilation fails if we bind the //jar - # target to the //external: label. bazel-deps handles this by binding the //jar:file target from all scala - # libraries to the //external: label, automatically -- hash['actual'] is //jar:file for scala libraries and //jar - # for java ones. scala_library correctly handles the dependency being either a java_import or a filegroup target, - # making macros work. bazel-deps + rules_scala make this all work out of the box. - # - # Unfortunately, scala_library's handling of jars-as-filegroups is not mirrored in intellij, so intellij is not able - # to locate any classes provided by scala libraries. That's a big problem. The intellij plugin needs all - # dependencies to be a java_import-like target, not a filegroup, so it can correctly locate classes inside the jars. - # That's where this method's fanciness comes in: for scala libraries, instead of binding the //jar:file target to - # the //external label, we instead take the //jar:file target and pass it to scala_import, and bind the imported - # result to the //external:label. In other words, we create the equivalent of maven_jar's //jar target, but with - # the scala_import rule, not the java_import one. Intellij correctly processes dependencies from the - # scala_import'ed jar, and scalac handles them correctly to at build time. - # - # We create this scala_import target by way of the scala_maven_jar repository rule above, generating a BUILD file - # that declares a target "@__imported//:scala_jar"; that target uses scala_import. Like maven_jar, it creates - # a new repository "@__imported", referencing the "@//jar:file" target from maven_jar. By then binding - # that :scala_jar target to the //external label, no changes to the generated BUILD files in third_party/ are - # required -- binding to the //external label abstracts away the different kinds of targets that are used for - # different kinds of libraries. - # - # See https://github.com/bazelbuild/rules_scala/pull/278 for lots of discussion and the source of scala_import. A - # future release of rules_scala or bazel-deps will hopefully include scala_import, and may remove the need for - # scala_maven_jar entirely. - - native.maven_jar( - name = hash["name"], - artifact = hash["artifact"], - sha1 = hash["sha1"], - repository = hash["repository"] - ) - if hash['lang'] == 'scala': - imported_name = hash['name'] + '__imported' - scala_maven_jar( - name = imported_name, - jar = hash['actual'], - ) - native.bind( - name = hash["bind"], - actual = '@%s//:scala_jar' % imported_name, - ) - else: - native.bind( - name = hash["bind"], - actual = hash["actual"] - ) diff --git a/tools/bazel_defs/scala.bzl b/tools/bazel_defs/scala.bzl deleted file mode 100644 index 43ba1b1..0000000 --- a/tools/bazel_defs/scala.bzl +++ /dev/null @@ -1,96 +0,0 @@ -# scala_import by github.com/ittaiz -# https://github.com/bazelbuild/rules_scala/pull/326 -# TODO: remove this when scala_import PR gets merged - -def _scala_import_impl(ctx): - target_data = _code_jars_and_intellij_metadata_from(ctx.attr.jar) - (code_jars, intellij_metadata) = (target_data.code_jars, target_data.intellij_metadata) - jars2labels = _add_labels_of_current_code_jars(code_jars, ctx.label) - code_jars_depset = depset(code_jars) - transitive_runtime_jars = _collect_runtime(ctx.attr.runtime_deps) - jars = _collect(ctx.attr.deps, jars2labels) - return struct( - scala = intellij_metadata, - jars_to_labels = jars2labels, - providers = [ - _create_provider(code_jars_depset, transitive_runtime_jars, jars) - ], - ) -def _create_provider(code_jars_depset, transitive_runtime_jars, jars): - test_provider = java_common.create_provider() - if hasattr(test_provider, "full_compile_jars"): - return java_common.create_provider( - use_ijar = False, - compile_time_jars = code_jars_depset, - runtime_jars = transitive_runtime_jars + jars.transitive_runtime_jars, - transitive_compile_time_jars = jars.transitive_compile_jars + code_jars_depset, - transitive_runtime_jars = transitive_runtime_jars + jars.transitive_runtime_jars + code_jars_depset, - ) - else: - return java_common.create_provider( - compile_time_jars = code_jars_depset, - runtime_jars = transitive_runtime_jars + jars.transitive_runtime_jars, - transitive_compile_time_jars = jars.transitive_compile_jars + code_jars_depset, - transitive_runtime_jars = transitive_runtime_jars + jars.transitive_runtime_jars + code_jars_depset, - ) - -def _add_labels_of_current_code_jars(code_jars, label): - jars2labels = {} - for jar in code_jars: - jars2labels[jar.path] = label - return jars2labels - -def _code_jars_and_intellij_metadata_from(jar): - if (jar): - code_jars = _filter_out_non_code_jars(jar.files) - intellij_metadata = struct( - outputs = struct( - ijar = None, - class_jar = code_jars[0], - ), - ) - else: - code_jars = [] - intellij_metadata = None - return struct(code_jars = code_jars, intellij_metadata = intellij_metadata) - -def _filter_out_non_code_jars(files): - return [file for file in files if not _is_source_jar(file)] - -def _is_source_jar(file): - return file.basename.endswith("-sources.jar") - -def _collect(deps, jars2labels): - transitive_compile_jars = depset() - runtime_jars = depset() - - for dep_target in deps: - java_provider = dep_target[java_common.provider] - transitive_compile_jars += java_provider.transitive_compile_time_jars - runtime_jars += java_provider.transitive_runtime_jars - if hasattr(dep_target, "jars_to_labels"): #transitively accumulate labels - jars2labels.update(dep_target.jars_to_labels) - else: - for jar in java_provider.compile_jars: - jars2labels[jar.path] = dep_target.label - - return struct(transitive_runtime_jars = runtime_jars, transitive_compile_jars = transitive_compile_jars) - -def _collect_runtime(runtime_deps): - runtime_jars = depset() - - for dep_target in runtime_deps: - java_provider = dep_target[java_common.provider] - runtime_jars += java_provider.transitive_runtime_jars - - return runtime_jars - - -scala_import = rule( - implementation=_scala_import_impl, - attrs={ - "jar": attr.label(), - "deps": attr.label_list(), - "runtime_deps": attr.label_list() - }, -) \ No newline at end of file diff --git a/tools/bazel_defs/scala_maven_jar.bzl b/tools/bazel_defs/scala_maven_jar.bzl deleted file mode 100644 index 35bd4db..0000000 --- a/tools/bazel_defs/scala_maven_jar.bzl +++ /dev/null @@ -1,16 +0,0 @@ -load('//tools/bazel_defs:scala.bzl', 'scala_import') - -def _scala_maven_jar_impl(repository_ctx): - repository_ctx.file( - 'BUILD.bazel', - content = "load('@//tools/bazel_defs:scala.bzl', 'scala_import')\nscala_import(name='scala_jar', jar='%s', visibility=['//visibility:public'])" % repository_ctx.attr.jar, - executable = False, - ) - -scala_maven_jar = repository_rule( - implementation = _scala_maven_jar_impl, - attrs = { - "jar": attr.label(), - }, - local = True, -) \ No newline at end of file