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
17 #include "linkerconfig/context.h"
18
19 #include <android-base/strings.h>
20
21 #include "linkerconfig/environment.h"
22 #include "linkerconfig/log.h"
23 #include "linkerconfig/namespacebuilder.h"
24 #include "linkerconfig/variables.h"
25
26 using android::base::StartsWith;
27 using android::linkerconfig::modules::ApexInfo;
28 using android::linkerconfig::modules::Namespace;
29
30 namespace android {
31 namespace linkerconfig {
32 namespace contents {
33
IsSystemSection() const34 bool Context::IsSystemSection() const {
35 return current_section_ == SectionType::System;
36 }
37
IsVendorSection() const38 bool Context::IsVendorSection() const {
39 return current_section_ == SectionType::Vendor;
40 }
41
IsProductSection() const42 bool Context::IsProductSection() const {
43 return current_section_ == SectionType::Product;
44 }
45
IsUnrestrictedSection() const46 bool Context::IsUnrestrictedSection() const {
47 return current_section_ == SectionType::Unrestricted;
48 }
49
IsDefaultConfig() const50 bool Context::IsDefaultConfig() const {
51 return current_linkerconfig_type_ == LinkerConfigType::Default;
52 }
53
IsLegacyConfig() const54 bool Context::IsLegacyConfig() const {
55 return current_linkerconfig_type_ == LinkerConfigType::Legacy;
56 }
57
58 // TODO(b/153944540) : Remove VNDK Lite supports
IsVndkliteConfig() const59 bool Context::IsVndkliteConfig() const {
60 return current_linkerconfig_type_ == LinkerConfigType::Vndklite;
61 }
62
IsRecoveryConfig() const63 bool Context::IsRecoveryConfig() const {
64 return current_linkerconfig_type_ == LinkerConfigType::Recovery;
65 }
66
IsApexBinaryConfig() const67 bool Context::IsApexBinaryConfig() const {
68 return current_linkerconfig_type_ == LinkerConfigType::ApexBinary;
69 }
70
SetCurrentSection(SectionType section_type)71 void Context::SetCurrentSection(SectionType section_type) {
72 current_section_ = section_type;
73 }
74
GetSystemNamespaceName() const75 std::string Context::GetSystemNamespaceName() const {
76 return (IsVendorSection() || IsProductSection() || IsApexBinaryConfig()) &&
77 !IsVndkliteConfig()
78 ? "system"
79 : "default";
80 }
81
SetCurrentLinkerConfigType(LinkerConfigType config_type)82 void Context::SetCurrentLinkerConfigType(LinkerConfigType config_type) {
83 current_linkerconfig_type_ = config_type;
84 }
85
IsVndkAvailable() const86 bool Context::IsVndkAvailable() const {
87 for (auto& apex : GetApexModules()) {
88 if (StartsWith(apex.name, "com.android.vndk.")) {
89 return true;
90 }
91 }
92 return false;
93 }
94
RegisterApexNamespaceBuilder(const std::string & name,ApexNamespaceBuilder builder)95 void Context::RegisterApexNamespaceBuilder(const std::string& name,
96 ApexNamespaceBuilder builder) {
97 builders_[name] = builder;
98 }
99
BuildApexNamespace(const ApexInfo & apex_info,bool visible) const100 Namespace Context::BuildApexNamespace(const ApexInfo& apex_info,
101 bool visible) const {
102 auto builder = builders_.find(apex_info.name);
103 if (builder != builders_.end()) {
104 return builder->second(*this, apex_info);
105 }
106
107 return BaseContext::BuildApexNamespace(apex_info, visible);
108 }
109
Var(const std::string & name)110 std::string Var(const std::string& name) {
111 auto val = modules::Variables::GetValue(name);
112 if (val.has_value()) {
113 return *val;
114 }
115 CHECK(!"undefined var") << name << " is not defined";
116 return "";
117 }
118
Var(const std::string & name,const std::string & default_value)119 std::string Var(const std::string& name, const std::string& default_value) {
120 auto val = modules::Variables::GetValue(name);
121 if (val.has_value()) {
122 return *val;
123 }
124 return default_value;
125 }
126
IsSectionVndkEnabled() const127 bool Context::IsSectionVndkEnabled() const {
128 if (!IsVndkAvailable() || android::linkerconfig::modules::IsVndkLiteDevice()) {
129 return false;
130 }
131 if (IsVendorSection()) {
132 return true;
133 }
134 if (IsProductSection() &&
135 android::linkerconfig::modules::IsProductVndkVersionDefined()) {
136 return true;
137 }
138
139 return false;
140 }
141
142 } // namespace contents
143 } // namespace linkerconfig
144 } // namespace android
145