blob: 766a80601cd92166f1108b995dc0f6dd030a696e [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 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
Andreas Huber5345ec22016-07-29 13:33:27 -070017#ifndef COORDINATOR_H_
18
19#define COORDINATOR_H_
20
21#include <android-base/macros.h>
Steven Moreland47792c42017-09-20 10:03:20 -070022#include <hidl-util/FQName.h>
Steven Morelandf78c44d2017-09-25 18:41:45 -070023#include <hidl-util/Formatter.h>
24#include <utils/Errors.h>
Steven Morelandd537ab02016-09-12 10:32:01 -070025#include <map>
Andreas Huber39fa7182016-08-19 14:27:33 -070026#include <set>
Andreas Huber5345ec22016-07-29 13:33:27 -070027#include <string>
Andreas Huberdca261f2016-08-04 13:47:51 -070028#include <vector>
Andreas Huber5345ec22016-07-29 13:33:27 -070029
30namespace android {
31
32struct AST;
Andreas Huberfd4afab2016-08-03 13:02:57 -070033struct Type;
Andreas Huber5345ec22016-07-29 13:33:27 -070034
35struct Coordinator {
Steven Moreland47792c42017-09-20 10:03:20 -070036 Coordinator() {};
Andreas Huberdca261f2016-08-04 13:47:51 -070037
Steven Moreland89a9ebb2017-12-04 10:18:00 -080038 const std::string& getRootPath() const;
Steven Moreland47792c42017-09-20 10:03:20 -070039 void setRootPath(const std::string &rootPath);
Steven Moreland6d3d3c82018-02-08 11:49:34 -080040 void setOutputPath(const std::string& outputPath);
Andreas Huber5345ec22016-07-29 13:33:27 -070041
Steven Moreland37c57ee2017-09-25 19:08:56 -070042 void setVerbose(bool value);
Andreas Huber308d8a22017-11-06 14:46:52 -080043 bool isVerbose() const;
Steven Moreland37c57ee2017-09-25 19:08:56 -070044
Steven Morelanda492aae2020-05-12 17:14:48 -070045 void setRequireFrozen(bool requireFrozen);
46
Steven Moreland394af5c2018-02-09 14:41:46 -080047 void setDepFile(const std::string& depFile);
48
Steven Moreland89a9ebb2017-12-04 10:18:00 -080049 const std::string& getOwner() const;
50 void setOwner(const std::string& owner);
51
Steven Moreland25c81662017-05-12 14:57:36 -070052 // adds path only if it doesn't exist
Steven Moreland47792c42017-09-20 10:03:20 -070053 status_t addPackagePath(const std::string& root, const std::string& path, std::string* error);
54 // adds path if it hasn't already been added
Steven Moreland25c81662017-05-12 14:57:36 -070055 void addDefaultPackagePath(const std::string& root, const std::string& path);
56
Steven Morelandf78c44d2017-09-25 18:41:45 -070057 enum class Location {
Steven Moreland5abcf012018-02-08 18:50:18 -080058 STANDARD_OUT,
Steven Moreland6d3d3c82018-02-08 11:49:34 -080059 DIRECT, // mOutputPath + file name
60 PACKAGE_ROOT, // e.x. mRootPath + /nfc/1.0/Android.bp
61 GEN_OUTPUT, // e.x. mOutputPath + /android/hardware/foo/1.0/*.cpp
62 GEN_SANITIZED, // e.x. mOutputPath + /android/hardware/foo/V1_0/*.cpp
Steven Morelandf78c44d2017-09-25 18:41:45 -070063 };
64
Steven Moreland255c9a12018-02-26 13:10:27 -080065 status_t getFilepath(const FQName& fqName, Location location, const std::string& fileName,
66 std::string* path) const;
Steven Morelandf78c44d2017-09-25 18:41:45 -070067
Steven Moreland6d3d3c82018-02-08 11:49:34 -080068 Formatter getFormatter(const FQName& fqName, Location location,
Steven Morelandf78c44d2017-09-25 18:41:45 -070069 const std::string& fileName) const;
70
Steven Moreland37c57ee2017-09-25 19:08:56 -070071 // must be called before file access
72 void onFileAccess(const std::string& path, const std::string& mode) const;
73
Steven Moreland394af5c2018-02-09 14:41:46 -080074 status_t writeDepFile(const std::string& forFile) const;
75
Steven Morelandc59326e2017-06-20 15:19:30 -070076 enum class Enforce {
77 FULL, // default
78 NO_HASH, // only for use with -Lhash
79 NONE, // only for use during enforcement
80 };
81
Andreas Huber39fa7182016-08-19 14:27:33 -070082 // Attempts to parse the interface/types referred to by fqName.
83 // Parsing an interface also parses the associated package's types.hal
84 // file if it exists.
85 // If "parsedASTs" is non-NULL, successfully parsed ASTs are inserted
86 // into the set.
Yifan Hongf619fc72017-04-07 15:40:06 -070087 // If !enforce, enforceRestrictionsOnPackage won't be run.
Steven Morelandc59326e2017-06-20 15:19:30 -070088 AST* parse(const FQName& fqName, std::set<AST*>* parsedASTs = nullptr,
89 Enforce enforcement = Enforce::FULL) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070090
Steven Moreland71f09132018-02-20 12:24:30 -080091 // Same as parse, but it distinguishes between "missing file" and "could not parse AST"
92 // return OK, out *ast:
93 // 0xdeadbeef -> successfully parsed
94 // nullptr -> file not present
95 // return !OK
96 // could not parse AST and file exists
97 status_t parseOptional(const FQName& fqName, AST** ast, std::set<AST*>* parsedASTs = nullptr,
98 Enforce enforcement = Enforce::FULL) const;
99
Andreas Huberdca261f2016-08-04 13:47:51 -0700100 // Given package-root paths of ["hardware/interfaces",
101 // "vendor/<something>/interfaces"], package roots of
102 // ["android.hardware", "vendor.<something>.hardware"], and a
103 // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath()
Yifan Hong97288ac2016-12-12 16:03:51 -0800104 // will return "hardware/interfaces/nfc/1.0" (if sanitized = false)
105 // or "hardware/interfaces/nfc/V1_0" (if sanitized = true).
Steven Moreland255c9a12018-02-26 13:10:27 -0800106 status_t getPackagePath(const FQName& fqName, bool relative, bool sanitized,
107 std::string* path) const;
Andreas Huber5345ec22016-07-29 13:33:27 -0700108
Andreas Huberdca261f2016-08-04 13:47:51 -0700109 // Given package roots of ["android.hardware",
110 // "vendor.<something>.hardware"] and a FQName of
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700111 // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will
Andreas Huberdca261f2016-08-04 13:47:51 -0700112 // return "android.hardware".
Steven Moreland255c9a12018-02-26 13:10:27 -0800113 status_t getPackageRoot(const FQName& fqName, std::string* root) const;
Andreas Huberdca261f2016-08-04 13:47:51 -0700114
Andreas Huberd2943e12016-08-05 11:59:31 -0700115 status_t getPackageInterfaceFiles(
116 const FQName &package,
117 std::vector<std::string> *fileNames) const;
118
Neel Mehta23bdeaf2019-08-01 18:32:56 -0700119 // Returns true if the package points to a directory that exists
120 status_t packageExists(const FQName& package, bool* result) const;
121
Steven Morelandaa186832016-09-26 13:51:43 -0700122 status_t appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -0700123 const FQName &package,
124 std::vector<FQName> *packageInterfaces) const;
125
Steven Moreland87b26d72017-10-11 09:35:42 -0700126 status_t isTypesOnlyPackage(const FQName& package, bool* result) const;
127
Steven Moreland40b86352018-02-01 16:03:30 -0800128 // Returns types which are imported/defined but not referenced in code
129 status_t addUnreferencedTypes(const std::vector<FQName>& packageInterfaces,
130 std::set<FQName>* unreferencedDefinitions,
Steven Morelandb65e5d92018-02-08 12:44:51 -0800131 std::set<FQName>* unreferencedImports) const;
Steven Moreland40b86352018-02-01 16:03:30 -0800132
Yifan Hong78b38d12017-02-13 18:14:46 +0000133 // Enforce a set of restrictions on a set of packages. These include:
134 // - minor version upgrades
135 // "packages" contains names like "android.hardware.nfc@1.1".
Steven Moreland218625a2017-04-18 22:31:50 -0700136 // - hashing restrictions
Steven Morelandc59326e2017-06-20 15:19:30 -0700137 status_t enforceRestrictionsOnPackage(const FQName& fqName,
138 Enforce enforcement = Enforce::FULL) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000139
Neel Mehta698840c2019-05-17 11:43:41 -0700140 // opt is the option that was parsed
141 // optarg contains the argument provided to opt
142 // - optarg == NULL if opt is not expecting an argument
143 using HandleArg = std::function<void(int opt, char* optarg)>;
144
145 // options is the same format as optstring for getopt
146 void parseOptions(int argc, char** argv, const std::string& options,
147 const HandleArg& handleArg);
148
Neel Mehta80a8b3c2019-05-23 16:42:30 -0700149 static void emitOptionsUsageString(Formatter& out);
150 static void emitOptionsDetailString(Formatter& out);
151
Neel Mehtaf6293d32019-06-12 17:16:38 -0700152 // Returns path relative to mRootPath
153 std::string makeRelative(const std::string& filename) const;
154
Neel Mehta698840c2019-05-17 11:43:41 -0700155 private:
Andreas Huberd2943e12016-08-05 11:59:31 -0700156 static bool MakeParentHierarchy(const std::string &path);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700157
Steven Moreland2d3ff5c2018-01-18 17:22:45 -0800158 enum class HashStatus {
159 ERROR,
160 UNFROZEN,
161 FROZEN,
162 CHANGED, // frozen but changed
163 };
164 HashStatus checkHash(const FQName& fqName) const;
165 status_t getUnfrozenDependencies(const FQName& fqName, std::set<FQName>* result) const;
166
Steven Moreland47792c42017-09-20 10:03:20 -0700167 // indicates that packages in "android.hardware" will be looked up in hardware/interfaces
168 struct PackageRoot {
169 std::string path; // e.x. hardware/interfaces
170 FQName root; // e.x. android.hardware@0.0
171 };
Steven Moreland18d9af12017-10-03 08:52:05 -0700172
Steven Moreland255c9a12018-02-26 13:10:27 -0800173 // nullptr if it doesn't exist
174 const PackageRoot* findPackageRoot(const FQName& fqName) const;
Steven Moreland18d9af12017-10-03 08:52:05 -0700175
176 // Given package-root paths of ["hardware/interfaces",
177 // "vendor/<something>/interfaces"], package roots of
178 // ["android.hardware", "vendor.<something>.hardware"], and a
179 // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath()
180 // will return "hardware/interfaces".
Steven Moreland255c9a12018-02-26 13:10:27 -0800181 status_t getPackageRootPath(const FQName& fqName, std::string* path) const;
Steven Moreland18d9af12017-10-03 08:52:05 -0700182
183 // Given an FQName of "android.hardware.nfc@1.0::INfc", return
184 // "android/hardware/".
Steven Moreland255c9a12018-02-26 13:10:27 -0800185 status_t convertPackageRootToPath(const FQName& fqName, std::string* path) const;
Steven Moreland18d9af12017-10-03 08:52:05 -0700186
Steven Moreland47792c42017-09-20 10:03:20 -0700187 std::vector<PackageRoot> mPackageRoots;
Steven Moreland6d3d3c82018-02-08 11:49:34 -0800188 std::string mRootPath; // root of android source tree (to locate package roots)
189 std::string mOutputPath; // root of output directory
Steven Moreland394af5c2018-02-09 14:41:46 -0800190 std::string mDepFile; // location to write depfile
Steven Moreland89a9ebb2017-12-04 10:18:00 -0800191
192 // hidl-gen options
Steven Moreland75fb19b2018-02-09 11:22:43 -0800193 bool mVerbose = false;
Steven Morelanda492aae2020-05-12 17:14:48 -0700194 bool mRequireFrozen = false;
Steven Moreland89a9ebb2017-12-04 10:18:00 -0800195 std::string mOwner;
Steven Moreland37c57ee2017-09-25 19:08:56 -0700196
Yifan Hong78b38d12017-02-13 18:14:46 +0000197 // cache to parse().
Steven Moreland28b9b532017-05-12 17:02:58 -0700198 mutable std::map<FQName, AST *> mCache;
Andreas Huber5345ec22016-07-29 13:33:27 -0700199
Yifan Hong78b38d12017-02-13 18:14:46 +0000200 // cache to enforceRestrictionsOnPackage().
Steven Moreland28b9b532017-05-12 17:02:58 -0700201 mutable std::set<FQName> mPackagesEnforced;
Yifan Hong78b38d12017-02-13 18:14:46 +0000202
Steven Moreland394af5c2018-02-09 14:41:46 -0800203 mutable std::set<std::string> mReadFiles;
204
Steven Moreland7a368042017-08-28 12:47:24 -0700205 // Returns the given path if it is absolute, otherwise it returns
206 // the path relative to mRootPath
207 std::string makeAbsolute(const std::string& string) const;
Steven Morelandb90d3272017-05-26 10:02:39 -0700208
Yifan Hong78b38d12017-02-13 18:14:46 +0000209 // Rules of enforceRestrictionsOnPackage are listed below.
Steven Moreland51b02f42018-03-14 15:43:29 -0700210 status_t enforceMinorVersionUprevs(const FQName& fqName, Enforce enforcement) const;
Steven Moreland28b9b532017-05-12 17:02:58 -0700211 status_t enforceHashes(const FQName &fqName) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000212
Andreas Huber5345ec22016-07-29 13:33:27 -0700213 DISALLOW_COPY_AND_ASSIGN(Coordinator);
214};
215
216} // namespace android
217
218#endif // COORDINATOR_H_