Move intellij_info.bzl to a tool packaged with bazel.

This will allow use of the Skylark IDE aspect in bazel,
unblocking the migration away from the native aspect.

--
MOS_MIGRATED_REVID=139796095
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD
index 05e709a..8fcb520 100644
--- a/src/test/java/com/google/devtools/build/lib/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/BUILD
@@ -647,12 +647,20 @@
     ],
 )
 
+# Copy intellij_info.bzl so it's in a predictable resource location
+genrule(
+    name = "intellij_info_bzl_copy",
+    srcs = ["//tools/ide:intellij_info"],
+    outs = ["ideinfo/intellij_info.bzl"],
+    cmd = "cp $(SRCS) $@",
+)
+
 java_test(
     name = "ideinfo_test",
     srcs = glob([
         "ideinfo/*.java",
     ]),
-    resources = ["ideinfo/intellij_info.bzl"],
+    resources = [":intellij_info_bzl_copy"],
     tags = ["ideinfo"],
     test_class = "com.google.devtools.build.lib.AllTests",
     deps = [
diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/intellij_info_tests.bzl b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/intellij_info_tests.bzl
deleted file mode 100644
index 14bea49..0000000
--- a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/intellij_info_tests.bzl
+++ /dev/null
@@ -1,144 +0,0 @@
-# Copyright 2016 The Bazel Authors. All rights reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#    http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# Tests for intellij_info.bzl
-
-load(":skylarktests/testing.bzl",
-     "start_test",
-     "end_test",
-     "fail_test",
-     "assert_equals",
-     "assert_contains_all",
-     "assert_true")
-
-
-load(":intellij_info.bzl", "intellij_info_test_aspect")
-
-def test(impl):
-    return rule(impl,
-        attrs = {
-            'targets' : attr.label_list(aspects = [intellij_info_test_aspect]),
-        },
-        test = True,
-    )
-
-def _source_paths(env, artifact_locations):
-    for f in artifact_locations:
-        assert_true(env, f.is_source, '%s is not source' % f)
-    return [f.relative_path for f in artifact_locations]
-
-def _jar_string(library_artifact, name):
-    if hasattr(library_artifact, name):
-      return "<%s:%s>" % (name, getattr(library_artifact, name).relative_path)
-    else:
-      return ""
-
-def _library_artifact_string(env, library_artifact):
-    return _jar_string(library_artifact, 'jar') + \
-           _jar_string(library_artifact, 'interface_jar') + \
-           _jar_string(library_artifact, 'source_jar')
-
-def _jar_expected_string(base, jar, interface_jar, source_jar):
-    s = ""
-    if jar:
-        s += "<jar:%s>" % (base + "/" + jar)
-    if interface_jar:
-        s += "<interface_jar:%s>" % (base + "/" + interface_jar)
-    if source_jar:
-        s += "<source_jar:%s>" % (base + "/" + source_jar)
-    return s
-
-################################################
-
-def _test_simple_java_library(ctx):
-    env = start_test(ctx)
-    infos = ctx.attr.targets[0].intellij_infos
-    info = infos[str(ctx.label.relative(":simple1"))]
-    if not info:
-        fail_test(env, "info not found")
-        end_test(ctx, env)
-        return
-    assert_equals(env,
-            ctx.label.package + "/BUILD",
-            info.build_file_artifact_location.relative_path)
-
-    assert_equals(env,
-                True,
-                info.build_file_artifact_location.is_source)
-
-    assert_equals(env, "java_library", info.kind_string)
-
-    assert_equals(env,
-            [ctx.label.package + "/skylarktests/testfiles/Simple1.java"],
-            _source_paths(env, info.java_rule_ide_info.sources))
-
-    # When Java header compilation is active, the interface jar is an -hjar.jar instead of an
-    # -ijar.jar. Try to detect that and test accordingly.
-    interface_jar_name = "libsimple1-ijar.jar"
-    for jar in info.java_rule_ide_info.jars:
-      if "-hjar.jar" in getattr(jar, "interface_jar").relative_path:
-        interface_jar_name = "libsimple1-hjar.jar"
-
-    assert_equals(env,
-            [_jar_expected_string(ctx.label.package,
-                                 "libsimple1.jar", interface_jar_name, "libsimple1-src.jar")],
-            [_library_artifact_string(env, a) for a in info.java_rule_ide_info.jars])
-
-    assert_equals(env,
-            ctx.label.package + "/libsimple1.jdeps",
-            info.java_rule_ide_info.jdeps.relative_path)
-
-    end_test(env)
-
-test_simple_java_library_rule_test = test(_test_simple_java_library)
-
-def test_simple_java_library():
-    native.java_library(name = "simple1", srcs = ["skylarktests/testfiles/Simple1.java"])
-    test_simple_java_library_rule_test(name = "test_simple_java_library",
-        targets = [":simple1"]
-    )
-
-################################################
-def _test_java_library_with_dependencies(ctx):
-    env = start_test(ctx)
-    infos = ctx.attr.targets[0].intellij_infos
-    info_simple = infos[str(ctx.label.relative(":simple2"))]
-    info_complex = infos[str(ctx.label.relative(":complex2"))]
-    assert_equals(env,
-            [ctx.label.package + "/skylarktests/testfiles/Complex2.java"],
-            _source_paths(env, info_complex.java_rule_ide_info.sources))
-    assert_contains_all(env,
-                        [str(ctx.label.relative(":simple2"))],
-                        info_complex.dependencies)
-    end_test(env)
-
-test_java_library_with_dependencies_rule_test = test(_test_java_library_with_dependencies)
-
-def test_java_library_with_dependencies():
-    native.java_library(name = "simple2", srcs = ["skylarktests/testfiles/Simple2.java"])
-    native.java_library(name = "complex2",
-                        srcs = ["skylarktests/testfiles/Complex2.java"],
-                        deps = [":simple2"])
-    test_java_library_with_dependencies_rule_test(name = "test_java_library_with_dependencies",
-        targets = [":complex2"]
-    )
-
-def skylark_tests():
-  test_simple_java_library()
-  test_java_library_with_dependencies()
-
-  native.test_suite(name = "skylark_tests",
-                    tests = [":test_simple_java_library",
-                             ":test_java_library_with_dependencies"])
-
diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Complex2.java b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Complex2.java
deleted file mode 100644
index 5a0f6e2..0000000
--- a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Complex2.java
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2016 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-package com.google.devtools.build.lib.ideinfo.skylarktests.testfiles;
-
-/**
- * Test class.
- */
-public class Complex2 {
-
-}
diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple1.java b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple1.java
deleted file mode 100644
index 84d7729..0000000
--- a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple1.java
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2016 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-package com.google.devtools.build.lib.ideinfo.skylarktests.testfiles;
-
-/**
- * Test class.
- */
-public class Simple1 {
-
-}
diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple2.java b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple2.java
deleted file mode 100644
index 3b28249..0000000
--- a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple2.java
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2016 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-package com.google.devtools.build.lib.ideinfo.skylarktests.testfiles;
-
-/**
- * Test class.
- */
-public class Simple2 {
-
-}
diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testing.bzl b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testing.bzl
deleted file mode 100644
index f1b30f7..0000000
--- a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testing.bzl
+++ /dev/null
@@ -1,49 +0,0 @@
-# Copyright 2016 The Bazel Authors. All rights reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#    http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# Small testing framework for Skylark rules and aspects.
-
-def start_test(ctx):
-  return struct(errors = [], ctx = ctx)
-
-def end_test(env):
-  cmd = "\n".join([
-      "cat << EOF",
-      "\n".join(reversed(env.errors)),
-      "EOF",
-      "exit %d" % (1 if env.errors else 0)])
-  env.ctx.file_action(
-          output = env.ctx.outputs.executable,
-          content = cmd,
-          executable = True,
-  )
-
-def fail_test(env, msg):
-  print(msg)
-  env.errors.append(msg)
-
-
-def assert_equals(env, expected, actual):
-  if not expected == actual:
-    fail_test(env, "'%s' != '%s'" % (expected, actual))
-
-def assert_contains_all(env, expected, actual):
-  for e in expected:
-    if e not in actual:
-      fail_test(env, "'%s' is not in '%s'" % (e, actual))
-
-
-def assert_true(env, condition, message):
-  if not condition:
-    fail_test(env, message)
diff --git a/src/test/shell/integration/BUILD b/src/test/shell/integration/BUILD
index c6f0cff..078e962 100644
--- a/src/test/shell/integration/BUILD
+++ b/src/test/shell/integration/BUILD
@@ -135,9 +135,33 @@
 )
 
 sh_test(
-    name = "ide_info_generation",
+    name = "ide_info_generation_native",
     size = "large",
     srcs = ["ide_info_generation.sh"],
+    args = [
+        "AndroidStudioInfoAspect",
+        "ide-info",
+        "aswb-build",
+        "ide-info-text",
+        "aswb-build.txt",
+    ],
+    data = [
+        ":test-deps",
+        "//src/tools/android/java/com/google/devtools/build/android/ideinfo:PackageParser",
+    ],
+)
+
+sh_test(
+    name = "ide_info_generation_skylark",
+    size = "large",
+    srcs = ["ide_info_generation.sh"],
+    args = [
+        "@bazel_tools//tools/ide:intellij_info.bzl%intellij_info_aspect",
+        "ide-info-text",
+        "intellij-build.txt",
+        "ide-info-text",
+        "intellij-build.txt",
+    ],
     data = [
         ":test-deps",
         "//src/tools/android/java/com/google/devtools/build/android/ideinfo:PackageParser",
diff --git a/src/test/shell/integration/ide_info_generation.sh b/src/test/shell/integration/ide_info_generation.sh
index 9cd215a..b3e2873 100755
--- a/src/test/shell/integration/ide_info_generation.sh
+++ b/src/test/shell/integration/ide_info_generation.sh
@@ -16,6 +16,12 @@
 #
 # Integration tests for IDE build info generation.
 
+ASPECT=$1
+BINARY_OUTPUT_GROUP=$2
+BINARY_OUTPUT=$3
+TEXT_OUTPUT_GROUP=$4
+TEXT_OUTPUT=$5
+
 # Load the test setup defined in the parent directory
 CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 source "${CURRENT_DIR}/../integration_test_setup.sh" \
@@ -61,12 +67,12 @@
 EOF
 
   bazel build //com/google/example:complex \
-        --aspects AndroidStudioInfoAspect --output_groups "ide-info" \
+        --aspects $ASPECT --output_groups "$BINARY_OUTPUT_GROUP" \
     || fail "Expected success"
-  SIMPLE_ASWB_BUILD="${PRODUCT_NAME}-bin/com/google/example/simple.aswb-build"
-  [ -e  $SIMPLE_ASWB_BUILD ] || fail "$SIMPLE_ASWB_BUILD not found"
-  COMPLEX_ASWB_BUILD="${PRODUCT_NAME}-bin/com/google/example/complex.aswb-build"
-  [ -e  $COMPLEX_ASWB_BUILD ] || fail "$COMPLEX_ASWB_BUILD not found"
+  SIMPLE_BUILD="${PRODUCT_NAME}-bin/com/google/example/simple.$BINARY_OUTPUT"
+  [ -e  $SIMPLE_BUILD ] || fail "$SIMPLE_BUILD not found"
+  COMPLEX_BUILD="${PRODUCT_NAME}-bin/com/google/example/complex.$BINARY_OUTPUT"
+  [ -e  $COMPLEX_BUILD ] || fail "$COMPLEX_BUILD not found"
 }
 
 function test_detailed_result() {
@@ -111,26 +117,26 @@
 EOF
 
   bazel build //com/google/example:complex \
-       --aspects AndroidStudioInfoAspect --output_groups "ide-info" \
+       --aspects $ASPECT --output_groups "$BINARY_OUTPUT_GROUP" \
        --experimental_show_artifacts 2> $TEST_log \
     || fail "Expected success"
-  SIMPLE_ASWB_BUILD="${PRODUCT_NAME}-bin/com/google/example/simple.aswb-build"
-  [ -e  $SIMPLE_ASWB_BUILD ] || fail "$SIMPLE_ASWB_BUILD not found"
-  COMPLEX_ASWB_BUILD="${PRODUCT_NAME}-bin/com/google/example/complex.aswb-build"
-  [ -e  $COMPLEX_ASWB_BUILD ] || fail "$COMPLEX_ASWB_BUILD not found"
+  SIMPLE_BUILD="${PRODUCT_NAME}-bin/com/google/example/simple.$BINARY_OUTPUT"
+  [ -e  $SIMPLE_BUILD ] || fail "$SIMPLE_BUILD not found"
+  COMPLEX_BUILD="${PRODUCT_NAME}-bin/com/google/example/complex.$BINARY_OUTPUT"
+  [ -e  $COMPLEX_BUILD ] || fail "$COMPLEX_BUILD not found"
 
   expect_log '^Build artifacts:'
-  expect_log "^>>>.*/com/google/example/complex.aswb-build"
-  expect_log "^>>>.*/com/google/example/simple.aswb-build"
+  expect_log "^>>>.*/com/google/example/complex.$BINARY_OUTPUT"
+  expect_log "^>>>.*/com/google/example/simple.$BINARY_OUTPUT"
 
   # second build; test that up-to-date artifacts are output.
   bazel build //com/google/example:complex \
-       --aspects AndroidStudioInfoAspect --output_groups "ide-info" \
+       --aspects $ASPECT --output_groups "$BINARY_OUTPUT_GROUP" \
        --experimental_show_artifacts 2> $TEST_log \
     || fail "Expected success"
   expect_log '^Build artifacts:'
-  expect_log "^>>>.*/com/google/example/complex.aswb-build"
-  expect_log "^>>>.*/com/google/example/simple.aswb-build"
+  expect_log "^>>>.*/com/google/example/complex.$BINARY_OUTPUT"
+  expect_log "^>>>.*/com/google/example/simple.$BINARY_OUTPUT"
 }
 
 function test_ide_resolve_output_group() {
@@ -171,7 +177,7 @@
 EOF
 
   bazel build //com/google/example:complex \
-        --aspects AndroidStudioInfoAspect --output_groups "ide-resolve" \
+        --aspects $ASPECT --output_groups "ide-resolve" \
     || fail "Expected success"
   [ -e ${PRODUCT_NAME}-bin/com/google/example/libsimple.jar ] \
     || fail "${PRODUCT_NAME}-bin/com/google/example/libsimple.jar not found"
@@ -199,7 +205,7 @@
 EOF
 
   bazel build //com/google/example:test \
-        --aspects AndroidStudioInfoAspect --output_groups "ide-resolve" \
+        --aspects $ASPECT --output_groups "ide-resolve" \
         --experimental_show_artifacts \
     || fail "Expected success"
   EXAMPLE_DIR="${PRODUCT_NAME}-bin/com/google/example"
@@ -253,12 +259,12 @@
 EOF
 
   bazel build //com/google/example:complex \
-        --aspects AndroidStudioInfoAspect --output_groups "ide-info-text" \
+        --aspects $ASPECT --output_groups "$TEXT_OUTPUT_GROUP" \
     || fail "Expected success"
-  SIMPLE_ASWB_BUILD="${PRODUCT_NAME}-bin/com/google/example/simple.aswb-build.txt"
-  [ -e  $SIMPLE_ASWB_BUILD ] || fail "$SIMPLE_ASWB_BUILD not found"
-  COMPLEX_ASWB_BUILD="${PRODUCT_NAME}-bin/com/google/example/complex.aswb-build.txt"
-  [ -e  $COMPLEX_ASWB_BUILD ] || fail "$COMPLEX_ASWB_BUILD not found"
+  SIMPLE_BUILD="${PRODUCT_NAME}-bin/com/google/example/simple.$TEXT_OUTPUT"
+  [ -e  $SIMPLE_BUILD ] || fail "$SIMPLE_BUILD not found"
+  COMPLEX_BUILD="${PRODUCT_NAME}-bin/com/google/example/complex.$TEXT_OUTPUT"
+  [ -e  $COMPLEX_BUILD ] || fail "$COMPLEX_BUILD not found"
 }
 
 run_suite "Test IDE info files generation"
diff --git a/tools/BUILD b/tools/BUILD
index 19d04c5..e2ba653 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -17,6 +17,7 @@
         "//tools/build_defs/repo:srcs",
         "//tools/build_rules:srcs",
         "//tools/proto/toolchains:srcs",
+        "//tools/ide:srcs",
         "//tools/jdk:srcs",
         "//tools/genrule:srcs",
         "//tools/cpp:srcs",
@@ -44,6 +45,7 @@
         "//tools/proto/toolchains:srcs",
         "//tools/cpp:srcs",
         "//tools/genrule:srcs",
+        "//tools/ide:embedded_tools_srcs",
         "//tools/j2objc:srcs",
         "//tools/jdk:package-srcs",
         "//tools/jdk:srcs",
diff --git a/tools/ide/BUILD b/tools/ide/BUILD
new file mode 100644
index 0000000..d771e5c
--- /dev/null
+++ b/tools/ide/BUILD
@@ -0,0 +1,24 @@
+licenses(["notice"])  # Apache 2.0
+
+filegroup(
+    name = "intellij_info",
+    srcs = ["intellij_info.bzl"],
+    visibility = [
+        "//src/test/java/com/google/devtools/build/lib:__pkg__",
+    ],
+)
+
+filegroup(
+    name = "srcs",
+    srcs = glob(["**"]),
+    visibility = ["//tools:__pkg__"],
+)
+
+filegroup(
+    name = "embedded_tools_srcs",
+    srcs = [
+        "BUILD.tools",
+        "intellij_info.bzl",
+    ],
+    visibility = ["//visibility:public"],
+)
diff --git a/tools/ide/BUILD.tools b/tools/ide/BUILD.tools
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/ide/BUILD.tools
diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/intellij_info.bzl b/tools/ide/intellij_info.bzl
similarity index 100%
rename from src/test/java/com/google/devtools/build/lib/ideinfo/intellij_info.bzl
rename to tools/ide/intellij_info.bzl