diff --git a/tensorflow/core/platform/BUILD b/tensorflow/core/platform/BUILD index b5228312901..a9768ad6463 100644 --- a/tensorflow/core/platform/BUILD +++ b/tensorflow/core/platform/BUILD @@ -85,6 +85,7 @@ exports_files( "mutex.h", "net.h", "numa.h", + "resource_loader.h", "snappy.h", "stacktrace_handler.h", "subprocess.h", @@ -513,6 +514,13 @@ cc_library( ], ) +cc_library( + name = "resource_loader", + testonly = 1, + textual_hdrs = ["resource_loader.h"], + deps = tf_platform_deps("resource_loader"), +) + cc_library( name = "rocm_rocdl_path", textual_hdrs = ["rocm_rocdl_path.h"], @@ -907,6 +915,22 @@ tf_cc_test( ], ) +tf_cc_test( + name = "resource_loader_test", + size = "small", + srcs = ["resource_loader_test.cc"], + data = [ + ":resource_loader.h", + ], + deps = [ + ":resource_loader", + ":status", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + ], +) + tf_cc_test( name = "ctstring_test", size = "small", diff --git a/tensorflow/core/platform/default/BUILD b/tensorflow/core/platform/default/BUILD index b2c0d2bb30c..6d8b66a66c0 100644 --- a/tensorflow/core/platform/default/BUILD +++ b/tensorflow/core/platform/default/BUILD @@ -299,6 +299,22 @@ cc_library( }), ) +cc_library( + name = "resource_loader", + testonly = 1, + srcs = ["resource_loader.cc"], + hdrs = ["//tensorflow/core/platform:resource_loader.h"], + tags = [ + "manual", + "no_oss", + "nobuilder", + ], + deps = [ + "//tensorflow/core/platform:logging", + "@bazel_tools//tools/cpp/runfiles", + ], +) + cc_library( name = "rocm_rocdl_path", srcs = ["rocm_rocdl_path.cc"], diff --git a/tensorflow/core/platform/default/resource_loader.cc b/tensorflow/core/platform/default/resource_loader.cc new file mode 100644 index 00000000000..423ac4a3d8d --- /dev/null +++ b/tensorflow/core/platform/default/resource_loader.cc @@ -0,0 +1,37 @@ +/* Copyright 2020 The TensorFlow 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. +==============================================================================*/ + +#include "tensorflow/core/platform/resource_loader.h" + +#include "tensorflow/core/platform/logging.h" +#include "tools/cpp/runfiles/runfiles.h" + +using bazel::tools::cpp::runfiles::Runfiles; + +namespace tensorflow { + +std::string GetDataDependencyFilepath(const std::string& relative_path) { + std::string error; + std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest(&error)); + + if (runfiles == nullptr) { + LOG(FATAL) << "Unable to access the data dependencies of this test.\n" + "Make sure you are running this test using bazel."; + } + string root_dir = "org_tensorflow/"; + return runfiles->Rlocation(root_dir + relative_path); +} + +} // namespace tensorflow diff --git a/tensorflow/core/platform/resource_loader.h b/tensorflow/core/platform/resource_loader.h new file mode 100644 index 00000000000..ca548435e4f --- /dev/null +++ b/tensorflow/core/platform/resource_loader.h @@ -0,0 +1,32 @@ +/* Copyright 2020 The TensorFlow 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 helper library to access "data" dependencies defined in BUILD files. +// Requires the relative paths starting from tensorflow/... +// For example, to get this file, a user would call: +// GetDataDependencyFilepath("tensorflow/core/platform/resource_loadder.h") + +#ifndef TENSORFLOW_CORE_PLATFORM_RESOURCE_LOADER_H_ +#define TENSORFLOW_CORE_PLATFORM_RESOURCE_LOADER_H_ + +#include <string> + +namespace tensorflow { + +std::string GetDataDependencyFilepath(const std::string& relative_path); + +} // namespace tensorflow + +#endif // TENSORFLOW_CORE_PLATFORM_RESOURCE_LOADER_H_ diff --git a/tensorflow/core/platform/resource_loader_test.cc b/tensorflow/core/platform/resource_loader_test.cc new file mode 100644 index 00000000000..590eb889c13 --- /dev/null +++ b/tensorflow/core/platform/resource_loader_test.cc @@ -0,0 +1,33 @@ +/* Copyright 2020 The TensorFlow 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. +==============================================================================*/ + +#include "tensorflow/core/platform/resource_loader.h" + +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { + +const char kDataDependencyPath[] = "tensorflow/core/platform/resource_loader.h"; + +TEST(ResourceLoaderTest, FindsAndOpensFile) { + string filepath = GetDataDependencyFilepath(kDataDependencyPath); + Status s = Env::Default()->FileExists(filepath); + EXPECT_TRUE(s.ok()) << "No file found at this location: " << filepath; +} + +} // namespace tensorflow