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 | |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 52 | static bool assemble(std::basic_istream<char>& inFile, |
| 53 | std::basic_ostream<char>& outFile, |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 54 | bool outputMatrix) { |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 55 | std::stringstream ss; |
| 56 | ss << inFile.rdbuf(); |
| 57 | std::string fileContent = ss.str(); |
| 58 | |
| 59 | HalManifest halManifest; |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 60 | if (gHalManifestConverter(&halManifest, fileContent)) { |
| 61 | if (halManifest.mType == SchemaType::DEVICE) { |
| 62 | if (!getFlag("BOARD_SEPOLICY_VERS", &halManifest.device.mSepolicyVersion)) { |
| 63 | return false; |
| 64 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 65 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 66 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 67 | if (outputMatrix) { |
| 68 | CompatibilityMatrix mat = halManifest.generateCompatibleMatrix(); |
| 69 | std::string error; |
| 70 | if (!halManifest.checkCompatibility(mat, &error)) { |
| 71 | std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " |
| 72 | << error << std::endl; |
| 73 | } |
| 74 | outFile << "<!-- \n" |
| 75 | " Autogenerated skeleton compatibility matrix. \n" |
| 76 | " Use with caution. Modify it to suit your needs.\n" |
| 77 | " All HALs are set to optional.\n" |
| 78 | " Many entries other than HALs are zero-filled and\n" |
| 79 | " require human attention. \n" |
| 80 | "-->\n" |
| 81 | << gCompatibilityMatrixConverter(mat); |
| 82 | } else { |
| 83 | outFile << gHalManifestConverter(halManifest); |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 84 | } |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 85 | outFile.flush(); |
| 86 | return true; |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 89 | CompatibilityMatrix matrix; |
| 90 | if (gCompatibilityMatrixConverter(&matrix, fileContent)) { |
| 91 | // TODO (b/37342627): get BOARD_VNDK_VERSION and put it here. |
| 92 | outFile << gCompatibilityMatrixConverter(matrix); |
| 93 | outFile.flush(); |
| 94 | return true; |
| 95 | } |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 96 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 97 | std::cerr << "Input file has unknown format." << std::endl |
| 98 | << "Error when attempting to convert to manifest: " |
| 99 | << gHalManifestConverter.lastError() << std::endl |
| 100 | << "Error when attempting to convert to compatibility matrix: " |
| 101 | << gCompatibilityMatrixConverter.lastError() << std::endl; |
| 102 | return false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 103 | } |
| 104 | }; |
| 105 | |
| 106 | } // namespace vintf |
| 107 | } // namespace android |
| 108 | |
| 109 | void help() { |
| 110 | std::cerr << |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 111 | "assemble_vintf: Checks if a given manifest / matrix file is valid.\n" |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 112 | "assemble_vintf -h\n" |
| 113 | " Display this help text.\n" |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 114 | "assemble_vintf -i <input file> [-o <output file>] [-m]\n" |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 115 | " Fill in build-time flags into the given file.\n" |
| 116 | " Input file format is automatically detected.\n" |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 117 | " If no designated output file, write to stdout.\n" |
| 118 | " If -m is set, a compatible compatibility matrix is\n" |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 119 | " generated instead; for example, given a device manifest,\n" |
| 120 | " a framework compatibility matrix is generated. This flag\n" |
| 121 | " is ignored when input is a compatibility matrix itself.\n"; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int main(int argc, char **argv) { |
| 125 | std::ifstream inFile; |
| 126 | std::ofstream outFile; |
| 127 | std::ostream* outFileRef = &std::cout; |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 128 | bool outputMatrix = false; |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 129 | int res; |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 130 | while((res = getopt(argc, argv, "hi:o:m")) >= 0) { |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 131 | switch (res) { |
| 132 | case 'i': { |
| 133 | inFile.open(optarg); |
| 134 | if (!inFile.is_open()) { |
| 135 | std::cerr << "Failed to open " << optarg << std::endl; |
| 136 | return 1; |
| 137 | } |
| 138 | } break; |
| 139 | |
| 140 | case 'o': { |
| 141 | outFile.open(optarg); |
| 142 | if (!outFile.is_open()) { |
| 143 | std::cerr << "Failed to open " << optarg << std::endl; |
| 144 | return 1; |
| 145 | } |
| 146 | outFileRef = &outFile; |
| 147 | } break; |
| 148 | |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 149 | case 'm': { |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 150 | outputMatrix = true; |
Yifan Hong | a59d256 | 2017-04-18 18:01:16 -0700 | [diff] [blame] | 151 | } break; |
| 152 | |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 153 | case 'h': |
| 154 | default: { |
| 155 | help(); |
| 156 | return 1; |
| 157 | } break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (!inFile.is_open()) { |
| 162 | std::cerr << "Missing input file" << std::endl; |
| 163 | help(); |
| 164 | return 1; |
| 165 | } |
| 166 | |
Yifan Hong | 959ee1b | 2017-04-28 14:37:56 -0700 | [diff] [blame^] | 167 | return ::android::vintf::AssembleVintf::assemble(inFile, *outFileRef, outputMatrix) |
Yifan Hong | 4d18bcc | 2017-04-07 21:47:16 +0000 | [diff] [blame] | 168 | ? 0 : 1; |
| 169 | } |
| 170 | |