• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "code_gen/CodeGenBase.h"
18 
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include <cstdint>
26 #include <fstream>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 
31 #include <hidl-util/Formatter.h>
32 
33 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
34 #include "utils/InterfaceSpecUtil.h"
35 
36 #include "VtsCompilerUtils.h"
37 #include "code_gen/driver/HalCodeGen.h"
38 #include "code_gen/driver/HalHidlCodeGen.h"
39 #include "code_gen/driver/LibSharedCodeGen.h"
40 #include "code_gen/fuzzer/FuzzerCodeGenBase.h"
41 #include "code_gen/fuzzer/HalHidlFuzzerCodeGen.h"
42 #include "code_gen/profiler/ProfilerCodeGenBase.h"
43 #include "code_gen/profiler/HalHidlProfilerCodeGen.h"
44 
45 using namespace std;
46 
47 namespace android {
48 namespace vts {
49 
CodeGenBase(const char * input_vts_file_path)50 CodeGenBase::CodeGenBase(const char* input_vts_file_path)
51     : input_vts_file_path_(input_vts_file_path) {}
52 
~CodeGenBase()53 CodeGenBase::~CodeGenBase() {}
54 
Translate(VtsCompileMode mode,const char * input_vts_file_path,const char * output_header_dir_path,const char * output_cpp_file_path)55 void Translate(VtsCompileMode mode,
56                const char* input_vts_file_path,
57                const char* output_header_dir_path,
58                const char* output_cpp_file_path) {
59   string output_header_file_path = string(output_header_dir_path) + "/"
60       + string(input_vts_file_path);
61   output_header_file_path = output_header_file_path + ".h";
62 
63   TranslateToFile(mode, input_vts_file_path, output_header_file_path.c_str(),
64                   android::vts::kHeader);
65 
66   TranslateToFile(mode, input_vts_file_path, output_cpp_file_path,
67                   android::vts::kSource);
68 }
69 
TranslateToFile(VtsCompileMode mode,const char * input_vts_file_path,const char * output_file_path,VtsCompileFileType file_type)70 void TranslateToFile(VtsCompileMode mode,
71                      const char* input_vts_file_path,
72                      const char* output_file_path,
73                      VtsCompileFileType file_type) {
74   string output_cpp_file_path_str = string(output_file_path);
75 
76   size_t found;
77   found = output_cpp_file_path_str.find_last_of("/");
78   string output_dir = output_cpp_file_path_str.substr(0, found + 1);
79 
80   ComponentSpecificationMessage message;
81   if (!ParseInterfaceSpec(input_vts_file_path, &message)) {
82     cerr << __func__ << " can't parse " << input_vts_file_path << endl;
83     exit(-1);
84   }
85 
86   vts_fs_mkdirs(&output_dir[0], 0777);
87 
88   FILE* output_file = fopen(output_file_path, "w+");
89   if (output_file == NULL) {
90     cerr << __func__ << " could not open file " << output_file_path << endl;
91     exit(-1);
92   }
93   Formatter out(output_file);
94 
95   if (mode == kDriver) {
96     unique_ptr<CodeGenBase> code_generator;
97     switch (message.component_class()) {
98       case LIB_SHARED:
99         code_generator.reset(new LibSharedCodeGen(input_vts_file_path));
100         break;
101       case HAL_HIDL:
102         code_generator.reset(new HalHidlCodeGen(input_vts_file_path));
103         break;
104       default:
105         cerr << "not yet supported component_class "
106              << message.component_class();
107         exit(-1);
108     }
109     if (file_type == kHeader) {
110       code_generator->GenerateHeaderFile(out, message);
111     } else if (file_type == kSource){
112       code_generator->GenerateSourceFile(out, message);
113     } else {
114       cerr << __func__ << " doesn't support file_type = kBoth." << endl;
115       exit(-1);
116     }
117   } else if (mode == kFuzzer) {
118     unique_ptr<FuzzerCodeGenBase> fuzzer_generator;
119     switch (message.component_class()) {
120       case HAL_HIDL:
121         {
122           fuzzer_generator = make_unique<HalHidlFuzzerCodeGen>(message);
123           break;
124         }
125       default:
126         cerr << "not yet supported component_class "
127              << message.component_class();
128         exit(-1);
129     }
130     if (file_type == kHeader) {
131       fuzzer_generator->GenerateHeaderFile(out);
132     } else if (file_type == kSource){
133       fuzzer_generator->GenerateSourceFile(out);
134     } else {
135       cerr << __func__ << " doesn't support file_type = kBoth." << endl;
136       exit(-1);
137     }
138   } else if (mode == kProfiler) {
139     unique_ptr<ProfilerCodeGenBase> profiler_generator;
140     switch (message.component_class()) {
141       case HAL_HIDL:
142         profiler_generator.reset(new HalHidlProfilerCodeGen());
143         break;
144       default:
145         cerr << "not yet supported component_class "
146              << message.component_class();
147         exit(-1);
148     }
149     if (file_type == kHeader) {
150       profiler_generator->GenerateHeaderFile(out, message);
151     } else if (file_type == kSource){
152       profiler_generator->GenerateSourceFile(out, message);
153     } else {
154       cerr << __func__ << " doesn't support file_type = kBoth." << endl;
155       exit(-1);
156     }
157   }
158 }
159 
160 }  // namespace vts
161 }  // namespace android
162