blob: 5bc45847343bc369408eb22964956eadbd1b0b27 [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 Moreland47792c42017-09-20 10:03:20 -070038 const std::string &getRootPath() const;
39 void setRootPath(const std::string &rootPath);
Andreas Huber5345ec22016-07-29 13:33:27 -070040
Steven Moreland37c57ee2017-09-25 19:08:56 -070041 void setVerbose(bool value);
Andreas Huber308d8a22017-11-06 14:46:52 -080042 bool isVerbose() const;
Steven Moreland37c57ee2017-09-25 19:08:56 -070043
Steven Moreland25c81662017-05-12 14:57:36 -070044 // adds path only if it doesn't exist
Steven Moreland47792c42017-09-20 10:03:20 -070045 status_t addPackagePath(const std::string& root, const std::string& path, std::string* error);
46 // adds path if it hasn't already been added
Steven Moreland25c81662017-05-12 14:57:36 -070047 void addDefaultPackagePath(const std::string& root, const std::string& path);
48
Steven Morelandf78c44d2017-09-25 18:41:45 -070049 enum class Location {
50 DIRECT, // outputPath + file name
51 PACKAGE_ROOT, // e.x. hal or other files within package root
52 GEN_OUTPUT, // e.x. android/hardware/foo/1.0/*.cpp
53 GEN_SANITIZED, // e.x. android/hardware/foo/V1_0/*.cpp
54 };
55
56 std::string getFilepath(const std::string& outputPath, const FQName& fqName, Location location,
Steven Moreland5c70d612017-10-03 08:36:14 -070057 const std::string& fileName = "") const;
Steven Morelandf78c44d2017-09-25 18:41:45 -070058
59 Formatter getFormatter(const std::string& outputPath, const FQName& fqName, Location location,
60 const std::string& fileName) const;
61
Steven Moreland37c57ee2017-09-25 19:08:56 -070062 // must be called before file access
63 void onFileAccess(const std::string& path, const std::string& mode) const;
64
Steven Morelandc59326e2017-06-20 15:19:30 -070065 enum class Enforce {
66 FULL, // default
67 NO_HASH, // only for use with -Lhash
68 NONE, // only for use during enforcement
69 };
70
Andreas Huber39fa7182016-08-19 14:27:33 -070071 // Attempts to parse the interface/types referred to by fqName.
72 // Parsing an interface also parses the associated package's types.hal
73 // file if it exists.
74 // If "parsedASTs" is non-NULL, successfully parsed ASTs are inserted
75 // into the set.
Yifan Hongf619fc72017-04-07 15:40:06 -070076 // If !enforce, enforceRestrictionsOnPackage won't be run.
Steven Morelandc59326e2017-06-20 15:19:30 -070077 AST* parse(const FQName& fqName, std::set<AST*>* parsedASTs = nullptr,
78 Enforce enforcement = Enforce::FULL) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070079
Andreas Huberdca261f2016-08-04 13:47:51 -070080 // Given package-root paths of ["hardware/interfaces",
81 // "vendor/<something>/interfaces"], package roots of
82 // ["android.hardware", "vendor.<something>.hardware"], and a
83 // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath()
Yifan Hong97288ac2016-12-12 16:03:51 -080084 // will return "hardware/interfaces/nfc/1.0" (if sanitized = false)
85 // or "hardware/interfaces/nfc/V1_0" (if sanitized = true).
Andreas Huber881227d2016-08-02 14:20:21 -070086 std::string getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -080087 const FQName &fqName, bool relative = false,
88 bool sanitized = false) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070089
Andreas Huberdca261f2016-08-04 13:47:51 -070090 // Given package roots of ["android.hardware",
91 // "vendor.<something>.hardware"] and a FQName of
Iliyan Malchev5bb14022016-08-09 15:04:39 -070092 // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will
Andreas Huberdca261f2016-08-04 13:47:51 -070093 // return "android.hardware".
Andreas Huberdca261f2016-08-04 13:47:51 -070094 std::string getPackageRoot(const FQName &fqName) const;
95
Yifan Hongc8934042016-11-17 17:10:52 -080096 // return getPackageRoot + ":" + getPackageRootPath
97 std::string getPackageRootOption(const FQName &fqName) const;
98
Andreas Huberd2943e12016-08-05 11:59:31 -070099 status_t getPackageInterfaceFiles(
100 const FQName &package,
101 std::vector<std::string> *fileNames) const;
102
Steven Morelandaa186832016-09-26 13:51:43 -0700103 status_t appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -0700104 const FQName &package,
105 std::vector<FQName> *packageInterfaces) const;
106
Steven Moreland87b26d72017-10-11 09:35:42 -0700107 status_t isTypesOnlyPackage(const FQName& package, bool* result) const;
108
Yifan Hong78b38d12017-02-13 18:14:46 +0000109 // Enforce a set of restrictions on a set of packages. These include:
110 // - minor version upgrades
111 // "packages" contains names like "android.hardware.nfc@1.1".
Steven Moreland218625a2017-04-18 22:31:50 -0700112 // - hashing restrictions
Steven Morelandc59326e2017-06-20 15:19:30 -0700113 status_t enforceRestrictionsOnPackage(const FQName& fqName,
114 Enforce enforcement = Enforce::FULL) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000115
Steven Moreland0057ad52017-09-25 19:11:19 -0700116private:
Andreas Huberd2943e12016-08-05 11:59:31 -0700117 static bool MakeParentHierarchy(const std::string &path);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700118
Steven Moreland47792c42017-09-20 10:03:20 -0700119 // indicates that packages in "android.hardware" will be looked up in hardware/interfaces
120 struct PackageRoot {
121 std::string path; // e.x. hardware/interfaces
122 FQName root; // e.x. android.hardware@0.0
123 };
Steven Moreland18d9af12017-10-03 08:52:05 -0700124
125 const PackageRoot& findPackageRoot(const FQName& fqName) const;
126
127 // Given package-root paths of ["hardware/interfaces",
128 // "vendor/<something>/interfaces"], package roots of
129 // ["android.hardware", "vendor.<something>.hardware"], and a
130 // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath()
131 // will return "hardware/interfaces".
132 std::string getPackageRootPath(const FQName& fqName) const;
133
134 // Given an FQName of "android.hardware.nfc@1.0::INfc", return
135 // "android/hardware/".
136 std::string convertPackageRootToPath(const FQName& fqName) const;
137
Steven Moreland47792c42017-09-20 10:03:20 -0700138 std::vector<PackageRoot> mPackageRoots;
Steven Morelandf7fa0682017-05-11 16:14:55 -0700139 std::string mRootPath;
Steven Moreland37c57ee2017-09-25 19:08:56 -0700140 bool mVerbose;
141
Yifan Hong78b38d12017-02-13 18:14:46 +0000142 // cache to parse().
Steven Moreland28b9b532017-05-12 17:02:58 -0700143 mutable std::map<FQName, AST *> mCache;
Andreas Huber5345ec22016-07-29 13:33:27 -0700144
Yifan Hong78b38d12017-02-13 18:14:46 +0000145 // cache to enforceRestrictionsOnPackage().
Steven Moreland28b9b532017-05-12 17:02:58 -0700146 mutable std::set<FQName> mPackagesEnforced;
Yifan Hong78b38d12017-02-13 18:14:46 +0000147
Steven Moreland7a368042017-08-28 12:47:24 -0700148 // Returns the given path if it is absolute, otherwise it returns
149 // the path relative to mRootPath
150 std::string makeAbsolute(const std::string& string) const;
Steven Morelandb90d3272017-05-26 10:02:39 -0700151
Yifan Hong78b38d12017-02-13 18:14:46 +0000152 // Rules of enforceRestrictionsOnPackage are listed below.
Steven Moreland28b9b532017-05-12 17:02:58 -0700153 status_t enforceMinorVersionUprevs(const FQName &fqName) const;
154 status_t enforceHashes(const FQName &fqName) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000155
Andreas Huber5345ec22016-07-29 13:33:27 -0700156 DISALLOW_COPY_AND_ASSIGN(Coordinator);
157};
158
159} // namespace android
160
161#endif // COORDINATOR_H_