1 /*
2  * Copyright (C) 2019 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 #pragma once
17 
18 #include <functional>
19 #include <map>
20 #include <optional>
21 #include <string>
22 
23 #include "linkerconfig/basecontext.h"
24 
25 namespace android {
26 namespace linkerconfig {
27 namespace contents {
28 
29 class Context;
30 using ApexNamespaceBuilder =
31     std::function<modules::Namespace(const Context&, const modules::ApexInfo&)>;
32 
33 enum class SectionType {
34   System,
35   Vendor,
36   Product,
37   Unrestricted,
38   Other,
39 };
40 
41 enum class LinkerConfigType {
42   Default,
43   Legacy,
44   Recovery,
45   ApexBinary,
46 };
47 
48 class Context : public modules::BaseContext {
49  public:
Context()50   Context()
51       : current_section_(SectionType::System),
52         current_linkerconfig_type_(LinkerConfigType::Default) {
53   }
54   bool IsSystemSection() const;
55   bool IsVendorSection() const;
56   bool IsProductSection() const;
57   bool IsUnrestrictedSection() const;
58 
59   bool IsDefaultConfig() const;
60   bool IsLegacyConfig() const;
61   bool IsRecoveryConfig() const;
62   bool IsApexBinaryConfig() const;
63 
64   void SetCurrentSection(SectionType value);
65   void SetCurrentLinkerConfigType(LinkerConfigType value);
66 
67   // Returns true if vndk apex is available
68   bool IsVndkAvailable() const;
69 
70   // Returns the namespace that covers /system/${LIB}.
71   std::string GetSystemNamespaceName() const;
72 
73   modules::Namespace BuildApexNamespace(const modules::ApexInfo& apex_info,
74                                         bool visible) const override;
75   void RegisterApexNamespaceBuilder(const std::string& name,
76                                     ApexNamespaceBuilder builder);
77 
78   bool IsSectionVndkEnabled() const;
79 
80  private:
81   std::map<std::string, ApexNamespaceBuilder> builders_;
82 
83   SectionType current_section_;
84   LinkerConfigType current_linkerconfig_type_;
85 };
86 
87 std::string Var(const std::string& name);
88 
89 std::string Var(const std::string& name, const std::string& default_value);
90 
91 }  // namespace contents
92 }  // namespace linkerconfig
93 }  // namespace android
94