| 1113 | } |
| 1114 | |
| 1115 | bool generate(const std::string& inputPath, const std::string& cppOut) { |
| 1116 | std::ifstream input(inputPath, std::ios::binary); |
| 1117 | if (!input) { |
| 1118 | std::fprintf(stderr, "ipc_codegen: cannot open %s\n", inputPath.c_str()); |
| 1119 | return false; |
| 1120 | } |
| 1121 | std::ostringstream buffer; |
| 1122 | buffer << input.rdbuf(); |
| 1123 | const std::string source = buffer.str(); |
| 1124 | |
| 1125 | Lexer lexer(source, inputPath); |
| 1126 | Parser parser(lexer); |
| 1127 | File file = parser.parse(); |
| 1128 | |
| 1129 | const std::string header = stem(inputPath) + ".gen.h"; |
| 1130 | const std::string outputPath = cppOut.empty() ? header : cppOut + "/" + header; |
| 1131 | |
| 1132 | std::ostringstream generated; |
| 1133 | Emitter emitter(generated, file); |
| 1134 | emitter.emit(inputPath, header); |
| 1135 | |
| 1136 | std::ofstream output(outputPath, std::ios::binary | std::ios::trunc); |
| 1137 | if (!output) { |
| 1138 | std::fprintf(stderr, "ipc_codegen: cannot write %s\n", outputPath.c_str()); |
| 1139 | return false; |
| 1140 | } |
| 1141 | output << generated.str(); |
| 1142 | if (!output) { |
| 1143 | std::fprintf(stderr, "ipc_codegen: failed to write %s\n", outputPath.c_str()); |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | std::fprintf(stderr, "ipc_codegen: wrote %s (%zu interfaces)\n", |
| 1148 | outputPath.c_str(), file.interfaces.size()); |
| 1149 | return true; |
| 1150 | } |
| 1151 | |
| 1152 | } // namespace |
| 1153 | |