C++,runfiles: rename sources to avoid conflict

Avoid include collision inside the Bazel source
tree when a rule depends on
@bazel_tools//tools/cpp/runfiles and includes
"tools/cpp/runfiles/runfiles.h", but they end up
getting "tools/cpp/runfiles/runfiles.h" from the
main repo.

Change-Id: Ic0fa23045b25cf9b1987780f9e752a54fa32be45
Details: https://github.com/bazelbuild/bazel/issues/5640#issuecomment-416957997

Closes #6023.

Change-Id: Ic0fa23045b25cf9b1987780f9e752a54fa32be45
PiperOrigin-RevId: 210878008
diff --git a/src/create_embedded_tools.py b/src/create_embedded_tools.py
index 4ad4a5a..3041f6a 100644
--- a/src/create_embedded_tools.py
+++ b/src/create_embedded_tools.py
@@ -31,6 +31,8 @@
     ('*tools/jdk/BUILD*', lambda x: 'tools/jdk/BUILD'),
     ('*tools/platforms/platforms.BUILD', lambda x: 'platforms/BUILD'),
     ('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)),
+    ('*tools/cpp/runfiles/*',
+     lambda x: 'tools/cpp/runfiles/' + os.path.basename(x)),
     ('*JavaBuilder*_deploy.jar', lambda x: 'tools/jdk/' + os.path.basename(x)),
     ('*JacocoCoverage*_deploy.jar',
      lambda x: 'tools/jdk/JacocoCoverage_deploy.jar'),
@@ -44,7 +46,6 @@
      lambda x: 'tools/jdk/ExperimentalTestRunner_deploy.jar'),
     ('*Runner_deploy.jar', lambda x: 'tools/jdk/TestRunner_deploy.jar'),
     ('*singlejar_local', lambda x: 'tools/jdk/singlejar/singlejar'),
-    ('src/tools/runfiles/runfiles.py', lambda x: 'tools/runfiles/runfiles.py'),
     ('*launcher.exe', lambda x: 'tools/launcher/launcher.exe'),
     ('*def_parser.exe', lambda x: 'tools/def_parser/def_parser.exe'),
     ('*ijar.exe', lambda x: 'tools/jdk/ijar/ijar.exe'),
diff --git a/src/test/py/bazel/test_base.py b/src/test/py/bazel/test_base.py
index 8670f0d..6455ae2 100644
--- a/src/test/py/bazel/test_base.py
+++ b/src/test/py/bazel/test_base.py
@@ -61,7 +61,7 @@
     self._test_cwd = tempfile.mkdtemp(dir=self._tests_root)
     self._test_bazelrc = os.path.join(self._temp, 'test_bazelrc')
     with open(self._test_bazelrc, 'wt') as f:
-      f.write('build --announce --jobs=8\n')
+      f.write('build --jobs=8\n')
     os.chdir(self._test_cwd)
 
   def tearDown(self):
diff --git a/tools/cpp/runfiles/BUILD b/tools/cpp/runfiles/BUILD
index dae87c9..e53618e 100644
--- a/tools/cpp/runfiles/BUILD
+++ b/tools/cpp/runfiles/BUILD
@@ -16,17 +16,32 @@
     name = "embedded_tools",
     srcs = [
         "BUILD.tools",
+        ":srcs_for_embedded_tools",
+    ],
+    visibility = ["//tools:__pkg__"],
+)
+
+genrule(
+    name = "srcs_for_embedded_tools",
+    srcs = [
+        "runfiles_src.cc",
+        "runfiles_src.h",
+    ],
+    outs = [
         "runfiles.cc",
         "runfiles.h",
     ],
-    visibility = ["//tools:__pkg__"],
+    cmd = ("sed " +
+           "  's|^#include.*/runfiles_src.h.*|#include \"tools/cpp/runfiles/runfiles.h\"|' " +
+           "  $(location runfiles_src.cc) > $(location runfiles.cc) && " +
+           "cp $(location runfiles_src.h) $(location runfiles.h)"),
 )
 
 cc_library(
     name = "runfiles",
     testonly = 1,
-    srcs = ["runfiles.cc"],
-    hdrs = ["runfiles.h"],
+    srcs = ["runfiles_src.cc"],
+    hdrs = ["runfiles_src.h"],
 )
 
 cc_test(
diff --git a/tools/cpp/runfiles/runfiles.cc b/tools/cpp/runfiles/runfiles_src.cc
similarity index 98%
rename from tools/cpp/runfiles/runfiles.cc
rename to tools/cpp/runfiles/runfiles_src.cc
index 64e797f..4ee1191 100644
--- a/tools/cpp/runfiles/runfiles.cc
+++ b/tools/cpp/runfiles/runfiles_src.cc
@@ -11,7 +11,7 @@
 // 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.
-#include "tools/cpp/runfiles/runfiles.h"
+#include "tools/cpp/runfiles/runfiles_src.h"  // this line is replaced in @bazel_tools
 
 #ifdef _WIN32
 #include <windows.h>
diff --git a/tools/cpp/runfiles/runfiles.h b/tools/cpp/runfiles/runfiles_src.h
similarity index 96%
rename from tools/cpp/runfiles/runfiles.h
rename to tools/cpp/runfiles/runfiles_src.h
index cfb1f30..e75a569 100644
--- a/tools/cpp/runfiles/runfiles.h
+++ b/tools/cpp/runfiles/runfiles_src.h
@@ -33,12 +33,13 @@
 //
 //       int main(int argc, char** argv) {
 //         std::string error;
-//         std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0],
-//         &error)); if (runfiles == nullptr) {
+//         std::unique_ptr<Runfiles> runfiles(
+//             Runfiles::Create(argv[0], &error));
+//         if (runfiles == nullptr) {
 //           ...  // error handling
 //         }
 //         std::string path =
-//            runfiles->Rlocation("my_workspace/path/to/my/data.txt");
+//             runfiles->Rlocation("my_workspace/path/to/my/data.txt");
 //         ...
 //
 //      The code above creates a Runfiles object and retrieves a runfile path.
diff --git a/tools/cpp/runfiles/runfiles_test.cc b/tools/cpp/runfiles/runfiles_test.cc
index d6ecac9..6f18a9e 100644
--- a/tools/cpp/runfiles/runfiles_test.cc
+++ b/tools/cpp/runfiles/runfiles_test.cc
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "tools/cpp/runfiles/runfiles.h"
+#include "tools/cpp/runfiles/runfiles_src.h"
 
 #ifdef _WIN32
 #include <windows.h>