Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <iostream> |
| 22 | #include <unordered_map> |
| 23 | #include <sstream> |
| 24 | #include <string> |
| 25 | |
| 26 | #include <vintf/parse_string.h> |
| 27 | #include <vintf/parse_xml.h> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace vintf { |
| 31 | |
| 32 | /** |
| 33 | * Slurps the device manifest file and add build time flag to it. |
| 34 | */ |
| 35 | class AssembleVintf { |
| 36 | public: |
| 37 | template<typename T> |
| 38 | static bool getFlag(const std::string& key, T* value) { |
| 39 | const char *envValue = getenv(key.c_str()); |
| 40 | if (envValue == NULL) { |
| 41 | std::cerr << "Required " << key << " flag." << std::endl; |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | if (!parse(envValue, value)) { |
| 46 | std::cerr << "Cannot parse " << envValue << "." << std::endl; |
| 47 | return false; |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | static bool assemble(std::basic_istream<char>& inFile, std::basic_ostream<char>& outFile) { |
| 53 | std::stringstream ss; |
| 54 | ss << inFile.rdbuf(); |
| 55 | std::string fileContent = ss.str(); |
| 56 | |
| 57 | HalManifest halManifest; |
| 58 | if (!gHalManifestConverter(&halManifest, fileContent)) { |
| 59 | std::cerr << "Illformed HAL manifest: " << gHalManifestConverter.lastError() |
| 60 | << std::endl; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | if (halManifest.mType == SchemaType::DEVICE) { |
| 65 | if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest.device.mSepolicyVersion)) { |
| 66 | return false; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | std::string outFileContent = gHalManifestConverter(halManifest); |
| 71 | outFile << outFileContent; |
| 72 | outFile.flush(); |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | } // namespace vintf |
| 79 | } // namespace android |
| 80 | |
| 81 | void help() { |
| 82 | std::cerr << |
| 83 | "assemble_vintf -h\n" |
| 84 | " Display this help text.\n" |
| 85 | "assemble_vintf -i <input file> [-o <output file>]\n" |
| 86 | " Fill in build-time flags into the given manifest.\n" |
| 87 | " If no designated output file, write to stdout.\n"; |
| 88 | } |
| 89 | |
| 90 | int main(int argc, char **argv) { |
| 91 | std::ifstream inFile; |
| 92 | std::ofstream outFile; |
| 93 | std::ostream* outFileRef = &std::cout; |
| 94 | int res; |
| 95 | while((res = getopt(argc, argv, "hi:o:v:")) >= 0) { |
| 96 | switch (res) { |
| 97 | case 'i': { |
| 98 | inFile.open(optarg); |
| 99 | if (!inFile.is_open()) { |
| 100 | std::cerr << "Failed to open " << optarg << std::endl; |
| 101 | return 1; |
| 102 | } |
| 103 | } break; |
| 104 | |
| 105 | case 'o': { |
| 106 | outFile.open(optarg); |
| 107 | if (!outFile.is_open()) { |
| 108 | std::cerr << "Failed to open " << optarg << std::endl; |
| 109 | return 1; |
| 110 | } |
| 111 | outFileRef = &outFile; |
| 112 | } break; |
| 113 | |
| 114 | case 'h': |
| 115 | default: { |
| 116 | help(); |
| 117 | return 1; |
| 118 | } break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (!inFile.is_open()) { |
| 123 | std::cerr << "Missing input file" << std::endl; |
| 124 | help(); |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | return ::android::vintf::AssembleVintf::assemble(inFile, *outFileRef) |
| 129 | ? 0 : 1; |
| 130 | } |
| 131 | |