blob: 44ad308f30060a1b9b5044469c992369cc24975e [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 Moreland89a9ebb2017-12-04 10:18:00 -080045 const std::string& getOwner() const;
46 void setOwner(const std::string& owner);
47
Steven Moreland25c81662017-05-12 14:57:36 -070048 // adds path only if it doesn't exist
Steven Moreland47792c42017-09-20 10:03:20 -070049 status_t addPackagePath(const std::string& root, const std::string& path, std::string* error);
50 // adds path if it hasn't already been added
Steven Moreland25c81662017-05-12 14:57:36 -070051 void addDefaultPackagePath(const std::string& root, const std::string& path);
52
Steven Morelandf78c44d2017-09-25 18:41:45 -070053 enum class Location {
Steven Moreland6d3d3c82018-02-08 11:49:34 -080054 DIRECT, // mOutputPath + file name
55 PACKAGE_ROOT, // e.x. mRootPath + /nfc/1.0/Android.bp
56 GEN_OUTPUT, // e.x. mOutputPath + /android/hardware/foo/1.0/*.cpp
57 GEN_SANITIZED, // e.x. mOutputPath + /android/hardware/foo/V1_0/*.cpp
Steven Morelandf78c44d2017-09-25 18:41:45 -070058 };
59
Steven Moreland6d3d3c82018-02-08 11:49:34 -080060 std::string getFilepath(const FQName& fqName, Location location,
Steven Moreland5c70d612017-10-03 08:36:14 -070061 const std::string& fileName = "") const;
Steven Morelandf78c44d2017-09-25 18:41:45 -070062
Steven Moreland6d3d3c82018-02-08 11:49:34 -080063 Formatter getFormatter(const FQName& fqName, Location location,
Steven Morelandf78c44d2017-09-25 18:41:45 -070064 const std::string& fileName) const;
65
Steven Moreland37c57ee2017-09-25 19:08:56 -070066 // must be called before file access
67 void onFileAccess(const std::string& path, const std::string& mode) const;
68
Steven Morelandc59326e2017-06-20 15:19:30 -070069 enum class Enforce {
70 FULL, // default
71 NO_HASH, // only for use with -Lhash
72 NONE, // only for use during enforcement
73 };
74
Andreas Huber39fa7182016-08-19 14:27:33 -070075 // Attempts to parse the interface/types referred to by fqName.
76 // Parsing an interface also parses the associated package's types.hal
77 // file if it exists.
78 // If "parsedASTs" is non-NULL, successfully parsed ASTs are inserted
79 // into the set.
Yifan Hongf619fc72017-04-07 15:40:06 -070080 // If !enforce, enforceRestrictionsOnPackage won't be run.
Steven Morelandc59326e2017-06-20 15:19:30 -070081 AST* parse(const FQName& fqName, std::set<AST*>* parsedASTs = nullptr,
82 Enforce enforcement = Enforce::FULL) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070083
Andreas Huberdca261f2016-08-04 13:47:51 -070084 // Given package-root paths of ["hardware/interfaces",
85 // "vendor/<something>/interfaces"], package roots of
86 // ["android.hardware", "vendor.<something>.hardware"], and a
87 // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath()
Yifan Hong97288ac2016-12-12 16:03:51 -080088 // will return "hardware/interfaces/nfc/1.0" (if sanitized = false)
89 // or "hardware/interfaces/nfc/V1_0" (if sanitized = true).
Andreas Huber881227d2016-08-02 14:20:21 -070090 std::string getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -080091 const FQName &fqName, bool relative = false,
92 bool sanitized = false) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070093
Andreas Huberdca261f2016-08-04 13:47:51 -070094 // Given package roots of ["android.hardware",
95 // "vendor.<something>.hardware"] and a FQName of
Iliyan Malchev5bb14022016-08-09 15:04:39 -070096 // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will
Andreas Huberdca261f2016-08-04 13:47:51 -070097 // return "android.hardware".
Andreas Huberdca261f2016-08-04 13:47:51 -070098 std::string getPackageRoot(const FQName &fqName) const;
99
Yifan Hongc8934042016-11-17 17:10:52 -0800100 // return getPackageRoot + ":" + getPackageRootPath
101 std::string getPackageRootOption(const FQName &fqName) const;
102
Andreas Huberd2943e12016-08-05 11:59:31 -0700103 status_t getPackageInterfaceFiles(
104 const FQName &package,
105 std::vector<std::string> *fileNames) const;
106
Steven Morelandaa186832016-09-26 13:51:43 -0700107 status_t appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -0700108 const FQName &package,
109 std::vector<FQName> *packageInterfaces) const;
110
Steven Moreland87b26d72017-10-11 09:35:42 -0700111 status_t isTypesOnlyPackage(const FQName& package, bool* result) const;
112
Steven Moreland40b86352018-02-01 16:03:30 -0800113 // Returns types which are imported/defined but not referenced in code
114 status_t addUnreferencedTypes(const std::vector<FQName>& packageInterfaces,
115 std::set<FQName>* unreferencedDefinitions,
Steven Morelandb65e5d92018-02-08 12:44:51 -0800116 std::set<FQName>* unreferencedImports) const;
Steven Moreland40b86352018-02-01 16:03:30 -0800117
Yifan Hong78b38d12017-02-13 18:14:46 +0000118 // Enforce a set of restrictions on a set of packages. These include:
119 // - minor version upgrades
120 // "packages" contains names like "android.hardware.nfc@1.1".
Steven Moreland218625a2017-04-18 22:31:50 -0700121 // - hashing restrictions
Steven Morelandc59326e2017-06-20 15:19:30 -0700122 status_t enforceRestrictionsOnPackage(const FQName& fqName,
123 Enforce enforcement = Enforce::FULL) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000124
Steven Moreland0057ad52017-09-25 19:11:19 -0700125private:
Andreas Huberd2943e12016-08-05 11:59:31 -0700126 static bool MakeParentHierarchy(const std::string &path);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700127
Steven Moreland2d3ff5c2018-01-18 17:22:45 -0800128 enum class HashStatus {
129 ERROR,
130 UNFROZEN,
131 FROZEN,
132 CHANGED, // frozen but changed
133 };
134 HashStatus checkHash(const FQName& fqName) const;
135 status_t getUnfrozenDependencies(const FQName& fqName, std::set<FQName>* result) const;
136
Steven Moreland47792c42017-09-20 10:03:20 -0700137 // indicates that packages in "android.hardware" will be looked up in hardware/interfaces
138 struct PackageRoot {
139 std::string path; // e.x. hardware/interfaces
140 FQName root; // e.x. android.hardware@0.0
141 };
Steven Moreland18d9af12017-10-03 08:52:05 -0700142
143 const PackageRoot& findPackageRoot(const FQName& fqName) const;
144
145 // Given package-root paths of ["hardware/interfaces",
146 // "vendor/<something>/interfaces"], package roots of
147 // ["android.hardware", "vendor.<something>.hardware"], and a
148 // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath()
149 // will return "hardware/interfaces".
150 std::string getPackageRootPath(const FQName& fqName) const;
151
152 // Given an FQName of "android.hardware.nfc@1.0::INfc", return
153 // "android/hardware/".
154 std::string convertPackageRootToPath(const FQName& fqName) const;
155
Steven Moreland47792c42017-09-20 10:03:20 -0700156 std::vector<PackageRoot> mPackageRoots;
Steven Moreland6d3d3c82018-02-08 11:49:34 -0800157 std::string mRootPath; // root of android source tree (to locate package roots)
158 std::string mOutputPath; // root of output directory
Steven Moreland89a9ebb2017-12-04 10:18:00 -0800159
160 // hidl-gen options
Steven Moreland75fb19b2018-02-09 11:22:43 -0800161 bool mVerbose = false;
Steven Moreland89a9ebb2017-12-04 10:18:00 -0800162 std::string mOwner;
Steven Moreland37c57ee2017-09-25 19:08:56 -0700163
Yifan Hong78b38d12017-02-13 18:14:46 +0000164 // cache to parse().
Steven Moreland28b9b532017-05-12 17:02:58 -0700165 mutable std::map<FQName, AST *> mCache;
Andreas Huber5345ec22016-07-29 13:33:27 -0700166
Yifan Hong78b38d12017-02-13 18:14:46 +0000167 // cache to enforceRestrictionsOnPackage().
Steven Moreland28b9b532017-05-12 17:02:58 -0700168 mutable std::set<FQName> mPackagesEnforced;
Yifan Hong78b38d12017-02-13 18:14:46 +0000169
Steven Moreland7a368042017-08-28 12:47:24 -0700170 // Returns the given path if it is absolute, otherwise it returns
171 // the path relative to mRootPath
172 std::string makeAbsolute(const std::string& string) const;
Steven Morelandb90d3272017-05-26 10:02:39 -0700173
Yifan Hong78b38d12017-02-13 18:14:46 +0000174 // Rules of enforceRestrictionsOnPackage are listed below.
Steven Moreland28b9b532017-05-12 17:02:58 -0700175 status_t enforceMinorVersionUprevs(const FQName &fqName) const;
176 status_t enforceHashes(const FQName &fqName) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000177
Andreas Huber5345ec22016-07-29 13:33:27 -0700178 DISALLOW_COPY_AND_ASSIGN(Coordinator);
179};
180
181} // namespace android
182
183#endif // COORDINATOR_H_