xref: /aosp_15_r20/external/tensorflow/tensorflow/c/experimental/ops/gen/cpp/cpp_generator.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow 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 #include "tensorflow/c/experimental/ops/gen/cpp/cpp_generator.h"
16 
17 #include "tensorflow/c/experimental/ops/gen/cpp/renderers/cpp_file_renderer.h"
18 #include "tensorflow/core/lib/io/path.h"
19 
20 namespace tensorflow {
21 namespace generator {
22 
CppGenerator(cpp::CppConfig cpp_config,PathConfig path_config)23 CppGenerator::CppGenerator(cpp::CppConfig cpp_config, PathConfig path_config)
24     : controller_(path_config),
25       cpp_config_(cpp_config),
26       path_config_(path_config) {}
27 
GenerateOneFile(cpp::RendererContext::Mode mode) const28 SourceCode CppGenerator::GenerateOneFile(
29     cpp::RendererContext::Mode mode) const {
30   SourceCode generated_code;
31   const std::vector<OpSpec> ops(controller_.GetModelOps());
32   std::vector<cpp::OpView> views(ops.begin(), ops.end());
33   cpp::RendererContext context{mode, generated_code, cpp_config_, path_config_};
34   cpp::CppFileRenderer(context, views).Render();
35   return generated_code;
36 }
37 
HeaderFileContents() const38 SourceCode CppGenerator::HeaderFileContents() const {
39   return GenerateOneFile(cpp::RendererContext::kHeader);
40 }
41 
SourceFileContents() const42 SourceCode CppGenerator::SourceFileContents() const {
43   return GenerateOneFile(cpp::RendererContext::kSource);
44 }
45 
HeaderFileName() const46 string CppGenerator::HeaderFileName() const {
47   return io::JoinPath(path_config_.output_path, cpp_config_.unit + "_ops.h");
48 }
49 
SourceFileName() const50 string CppGenerator::SourceFileName() const {
51   return io::JoinPath(path_config_.output_path, cpp_config_.unit + "_ops.cc");
52 }
53 
WriteHeaderFile() const54 void CppGenerator::WriteHeaderFile() const {
55   controller_.WriteFile(HeaderFileName(), HeaderFileContents());
56 }
57 
WriteSourceFile() const58 void CppGenerator::WriteSourceFile() const {
59   controller_.WriteFile(SourceFileName(), SourceFileContents());
60 }
61 
62 }  // namespace generator
63 }  // namespace tensorflow
64