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 50 CodeGenBase::CodeGenBase(const char* input_vts_file_path) 51 : input_vts_file_path_(input_vts_file_path) {} 52 53 CodeGenBase::~CodeGenBase() {} 54 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 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 } 84 85 vts_fs_mkdirs(&output_dir[0], 0777); 86 87 FILE* output_file = fopen(output_file_path, "w+"); 88 if (output_file == NULL) { 89 cerr << __func__ << " could not open file " << output_file_path << endl; 90 exit(-1); 91 } 92 Formatter out(output_file); 93 94 if (mode == kDriver) { 95 unique_ptr<CodeGenBase> code_generator; 96 switch (message.component_class()) { 97 case LIB_SHARED: 98 code_generator.reset(new LibSharedCodeGen(input_vts_file_path)); 99 break; 100 case HAL_HIDL: 101 code_generator.reset(new HalHidlCodeGen(input_vts_file_path)); 102 break; 103 default: 104 cerr << "not yet supported component_class " 105 << message.component_class(); 106 exit(-1); 107 } 108 if (file_type == kHeader) { 109 code_generator->GenerateHeaderFile(out, message); 110 } else if (file_type == kSource){ 111 code_generator->GenerateSourceFile(out, message); 112 } else { 113 cerr << __func__ << " doesn't support file_type = kBoth." << endl; 114 exit(-1); 115 } 116 } else if (mode == kFuzzer) { 117 unique_ptr<FuzzerCodeGenBase> fuzzer_generator; 118 switch (message.component_class()) { 119 case HAL_HIDL: 120 { 121 fuzzer_generator = make_unique<HalHidlFuzzerCodeGen>(message); 122 break; 123 } 124 default: 125 cerr << "not yet supported component_class " 126 << message.component_class(); 127 exit(-1); 128 } 129 if (file_type == kHeader) { 130 fuzzer_generator->GenerateHeaderFile(out); 131 } else if (file_type == kSource){ 132 fuzzer_generator->GenerateSourceFile(out); 133 } else { 134 cerr << __func__ << " doesn't support file_type = kBoth." << endl; 135 exit(-1); 136 } 137 } else if (mode == kProfiler) { 138 unique_ptr<ProfilerCodeGenBase> profiler_generator; 139 switch (message.component_class()) { 140 case HAL_HIDL: 141 profiler_generator.reset(new HalHidlProfilerCodeGen()); 142 break; 143 default: 144 cerr << "not yet supported component_class " 145 << message.component_class(); 146 exit(-1); 147 } 148 if (file_type == kHeader) { 149 profiler_generator->GenerateHeaderFile(out, message); 150 } else if (file_type == kSource){ 151 profiler_generator->GenerateSourceFile(out, message); 152 } else { 153 cerr << __func__ << " doesn't support file_type = kBoth." << endl; 154 exit(-1); 155 } 156 } 157 } 158 159 } // namespace vts 160 } // namespace android 161