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 <getopt.h>
18 
19 #include <iostream>
20 
21 #include <android-base/strings.h>
22 #include <vintf/AssembleVintf.h>
23 #include "utils.h"
24 
25 void help() {
26     std::cerr << "assemble_vintf: Checks if a given manifest / matrix file is valid and \n"
27                  "    fill in build-time flags into the given file.\n"
28                  "assemble_vintf -h\n"
29                  "               Display this help text.\n"
30                  "assemble_vintf -i <input file>[:<input file>[...]] [-o <output file>] [-m]\n"
31                  "               [-c [<check file>]]\n"
32                  "               Fill in build-time flags into the given file.\n"
33                  "    -i <input file>[:<input file>[...]]\n"
34                  "               A list of input files. Format is automatically detected for the\n"
35                  "               first file, and the remaining files must have the same format.\n"
36                  "               Files other than the first file should only have <hal> defined;\n"
37                  "               other entries are ignored. Argument may also be specified\n"
38                  "               multiple times.\n"
39                  "    -o <output file>\n"
40                  "               Optional output file. If not specified, write to stdout.\n"
41                  "    -m\n"
42                  "               a compatible compatibility matrix is\n"
43                  "               generated instead; for example, given a device manifest,\n"
44                  "               a framework compatibility matrix is generated. This flag\n"
45                  "               is ignored when input is a compatibility matrix.\n"
46                  "    -c [<check file>]\n"
47                  "               The path of the \"check file\"; for example, this is the path\n"
48                  "               of the device manifest for framework compatibility matrix.\n"
49                  "               After writing the output file, the program checks against\n"
50                  "               the \"check file\", depending on environment variables.\n"
51                  "               - PRODUCT_ENFORCE_VINTF_MANIFEST=true: check compatibility\n"
52                  "               If any check fails, an error message is written to stderr.\n"
53                  "               Return 1.\n"
54                  "    --kernel=<version>:<android-base.config>[:<android-base-arch.config>[...]]\n"
55                  "               Add a kernel entry to framework compatibility matrix or device\n"
56                  "               manifest. Ignored for other input format.\n"
57                  "               There can be any number of --kernel for framework compatibility\n"
58                  "               matrix, but at most one --kernel and at most one config file for\n"
59                  "               device manifest.\n"
60                  "               <version> has format: 3.18.0\n"
61                  "               <android-base.config> is the location of android-base.config\n"
62                  "               <android-base-arch.config> is the location of an optional\n"
63                  "               arch-specific config fragment, more than one may be specified\n"
64                  "    -l, --hals-only\n"
65                  "               Output has only <hal> entries. Cannot be used with -n.\n"
66                  "    -n, --no-hals\n"
67                  "               Output has no <hal> entries (but all other entries).\n"
68                  "               Cannot be used with -l.\n"
69                  "    --no-kernel-requirements\n"
70                  "               Output has no <config> entries in <kernel>, and kernel minor\n"
71                  "               version is set to zero. (For example, 3.18.0).\n";
72 }
73 
74 int main(int argc, char** argv) {
75     using namespace ::android::vintf;
76     const struct option longopts[] = {{"kernel", required_argument, NULL, 'k'},
77                                       {"hals-only", no_argument, NULL, 'l'},
78                                       {"no-hals", no_argument, NULL, 'n'},
79                                       {"no-kernel-requirements", no_argument, NULL, 'K'},
80                                       {0, 0, 0, 0}};
81 
82     std::string outFilePath;
83     auto assembleVintf = AssembleVintf::newInstance();
84     int res;
85     int optind;
86     while ((res = getopt_long(argc, argv, "hi:o:mc:nl", longopts, &optind)) >= 0) {
87         switch (res) {
88             case 'i': {
89                 for (const auto& inFilePath : ::android::base::Split(optarg, ":")) {
90                     if (!assembleVintf->openInFile(inFilePath.c_str())) {
91                         std::cerr << "Failed to open " << inFilePath << std::endl;
92                         return 1;
93                     }
94                 }
95             } break;
96 
97             case 'o': {
98                 outFilePath = optarg;
99                 if (!assembleVintf->openOutFile(optarg)) {
100                     std::cerr << "Failed to open " << optarg << std::endl;
101                     return 1;
102                 }
103             } break;
104 
105             case 'm': {
106                 assembleVintf->setOutputMatrix();
107             } break;
108 
109             case 'c': {
110                 if (!assembleVintf->openCheckFile(optarg)) {
111                     std::cerr << "Failed to open " << optarg << std::endl;
112                     return 1;
113                 }
114             } break;
115 
116             case 'k': {
117                 if (!assembleVintf->addKernel(optarg)) {
118                     std::cerr << "ERROR: Unrecognized --kernel argument." << std::endl;
119                     return 1;
120                 }
121             } break;
122 
123             case 'l': {
124                 if (!assembleVintf->setHalsOnly()) {
125                     return 1;
126                 }
127             } break;
128 
129             case 'n': {
130                 if (!assembleVintf->setNoHals()) {
131                     return 1;
132                 }
133             } break;
134 
135             case 'K': {
136                 if (!assembleVintf->setNoKernelRequirements()) {
137                     return 1;
138                 }
139             } break;
140 
141             case 'h':
142             default: {
143                 help();
144                 return 1;
145             } break;
146         }
147     }
148 
149     bool success = assembleVintf->assemble();
150 
151     return success ? 0 : 1;
152 }
153