main.cpp revision cd16ae4797a0ee318ac08026330732e03016956d
1f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland/* 2f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * Copyright (C) 2016 The Android Open Source Project 3f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * 4f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * Licensed under the Apache License, Version 2.0 (the "License"); 5f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * you may not use this file except in compliance with the License. 6f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * You may obtain a copy of the License at 7f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * 8f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * http://www.apache.org/licenses/LICENSE-2.0 9f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * 10f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * Unless required by applicable law or agreed to in writing, software 11f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * distributed under the License is distributed on an "AS IS" BASIS, 12f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * See the License for the specific language governing permissions and 14f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * limitations under the License. 15f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland */ 16f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 17f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include "AST.h" 18f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 19f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <android-base/logging.h> 20f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <android-base/macros.h> 21f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <set> 22f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <map> 23f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <stdio.h> 24f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <string> 25f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <unistd.h> 26f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <vector> 27f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <utils/Errors.h> 28f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 29f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandusing namespace android; 30f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 31f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandextern status_t parseFile(android::AST *ast); 32f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 33f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstatic void usage(const char *me) { 34f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland fprintf(stderr, 352b02118f0cf27fabdb76e01bf60ca75dd1ad905bSteven Moreland "usage: %s [-g] [-o dir] -p package (-r interface-root)+ (header-filepath)+\n", 36f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland me); 37f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 38cd16ae4797a0ee318ac08026330732e03016956dSteven Moreland fprintf(stderr, " -h print this message\n"); 39f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland fprintf(stderr, " -o output path\n"); 40f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland fprintf(stderr, " (example: ~/android/master)\n"); 41f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland fprintf(stderr, " -p package\n"); 42f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland fprintf(stderr, " (example: android.hardware.baz@1.0)\n"); 43f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland fprintf(stderr, " -g (enable open-gl mode) \n"); 44f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland fprintf(stderr, " -r package:path root " 45f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland "(e.g., android.hardware:hardware/interfaces)\n"); 46f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland} 47f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 48f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstatic void addPackageRootToMap(const std::string &val, 49f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland std::map<std::string, std::string> &packageRootPaths) { 50f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland auto index = val.find_first_of(':'); 51f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland CHECK(index != std::string::npos); 52f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 53f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland auto package = val.substr(0, index); 54f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland auto path = val.substr(index + 1); 55f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 56f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland packageRootPaths[package] = path; 57f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland} 58f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 59f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstatic bool isPathPrefix(const std::string &prefix, const std::string &base) { 60f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland if (prefix.size() >= base.size()) { 61f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland LOG(INFO) << "Not long enough"; 62f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland return false; 63f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 64f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 65f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland if (base[prefix.size()] != '.') { 66f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland LOG(INFO) << "not full"; 67f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland return false; 68f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 69f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 70f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland return prefix == base.substr(0, prefix.size()); 71f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland} 72f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 73f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstatic void applyPackageRootPath( 74f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland const std::map<std::string, std::string> &packageRootPaths, 75f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland const std::string &package, 76f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland std::string &outputPath) { 77f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 78f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland auto index = package.find_first_of('@'); 79f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland CHECK(index != std::string::npos); 80f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 81f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland auto packagePath = package.substr(0, index); 82f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland auto packageVersion = package.substr(index + 1); 83f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 84f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland for (auto const& pair : packageRootPaths) { 85f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland const std::string& rootPackage = pair.first; 86f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland const std::string& rootPath = pair.second; 87f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 88f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland if (isPathPrefix(rootPackage, packagePath)) { 89f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 90f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland packagePath = packagePath.substr(rootPackage.size() + 1); 91f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland std::replace(packagePath.begin(), packagePath.end(), '.', '/'); 92f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland packagePath += '/' + packageVersion; 93f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 94f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland if (outputPath.empty()) { 95f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland outputPath = rootPath; 96f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 97f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 98f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland outputPath += '/' + packagePath + '/'; 99f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland return; 100f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 101f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 102f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 103f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland CHECK(false) << "No package root path provided for package: " << package; 104f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland} 105f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 106f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandint main(int argc, char **argv) { 107f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland const char *me = argv[0]; 108f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 109f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland std::string outputDir; 110f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland std::string package; 111f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland std::map<std::string, std::string> packageRootPaths; 112f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland bool isOpenGl = false; 113f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 114f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland int res; 115f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland while ((res = getopt(argc, argv, "gho:p:r:")) >= 0) { 116f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland switch (res) { 117f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland case 'o': { 118f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland outputDir = optarg; 119f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland break; 120f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 121f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland case 'p': { 122f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland package = optarg; 123f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland break; 124f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 125f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland case 'g': { 126f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland isOpenGl = true; 127f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland break; 128f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 129f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland case 'r': 130f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland { 131f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland addPackageRootToMap(optarg, packageRootPaths); 132f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland break; 133f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 134f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland case 'h': 135f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland default: 136f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland { 137f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland usage(me); 138f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland exit(1); 139f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland break; 140f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 141f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 142f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 143f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 144f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland // if no arguments are provided, show usage instead of specific errors 145f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland if (optind == 1) { 146f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland usage(me); 147f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland exit(0); 148f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 149f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 150f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland applyPackageRootPath(packageRootPaths, package, outputDir); 151f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 152f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland if (package.empty()) { 153f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland LOG(WARNING) << "You must provide a package."; 154f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland usage(me); 155f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland exit(0); 156f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 157f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 158f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland if (optind == argc) { 159f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland LOG(WARNING) << "You must provide a header-filepath."; 160f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland usage(me); 161f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland exit(0); 162f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 163f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 164f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland for(int i = optind; i < argc; i++) { 165f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland std::string path = argv[i]; 166f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 167f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland LOG(INFO) << "Processing " << path; 168f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 169f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland AST ast(path, outputDir, package, isOpenGl); 170f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 171f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland int res = parseFile(&ast); 172f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 173f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland if (res != 0) { 174f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland LOG(ERROR) << "Could not parse: " << res; 175f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland exit(1); 176f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 177f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 178f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland ast.processContents(); 179f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 180f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland ast.generateCode(); 181f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland } 182f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland 183f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland return 0; 184f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland} 185