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
IsRecoveryConfig() const58 bool Context::IsRecoveryConfig() const {
59 return current_linkerconfig_type_ == LinkerConfigType::Recovery;
60 }
61
IsApexBinaryConfig() const62 bool Context::IsApexBinaryConfig() const {
63 return current_linkerconfig_type_ == LinkerConfigType::ApexBinary;
64 }
65
SetCurrentSection(SectionType section_type)66 void Context::SetCurrentSection(SectionType section_type) {
67 current_section_ = section_type;
68 }
69
GetSystemNamespaceName() const70 std::string Context::GetSystemNamespaceName() const {
71 return IsSystemSection() || IsUnrestrictedSection() ? "default" : "system";
72 }
73
SetCurrentLinkerConfigType(LinkerConfigType config_type)74 void Context::SetCurrentLinkerConfigType(LinkerConfigType config_type) {
75 current_linkerconfig_type_ = config_type;
76 }
77
IsVndkAvailable() const78 bool Context::IsVndkAvailable() const {
79 for (auto& apex : GetApexModules()) {
80 if (StartsWith(apex.name, "com.android.vndk.")) {
81 return true;
82 }
83 }
84 return false;
85 }
86
RegisterApexNamespaceBuilder(const std::string & name,ApexNamespaceBuilder builder)87 void Context::RegisterApexNamespaceBuilder(const std::string& name,
88 ApexNamespaceBuilder builder) {
89 builders_[name] = builder;
90 }
91
BuildApexNamespace(const ApexInfo & apex_info,bool visible) const92 Namespace Context::BuildApexNamespace(const ApexInfo& apex_info,
93 bool visible) const {
94 auto builder = builders_.find(apex_info.name);
95 if (builder != builders_.end()) {
96 return builder->second(*this, apex_info);
97 }
98
99 return BaseContext::BuildApexNamespace(apex_info, visible);
100 }
101
Var(const std::string & name)102 std::string Var(const std::string& name) {
103 auto val = modules::Variables::GetValue(name);
104 if (val.has_value()) {
105 return *val;
106 }
107 CHECK(!"undefined var") << name << " is not defined";
108 return "";
109 }
110
Var(const std::string & name,const std::string & default_value)111 std::string Var(const std::string& name, const std::string& default_value) {
112 auto val = modules::Variables::GetValue(name);
113 if (val.has_value()) {
114 return *val;
115 }
116 return default_value;
117 }
118
IsSectionVndkEnabled() const119 bool Context::IsSectionVndkEnabled() const {
120 if (!IsVndkAvailable()) {
121 return false;
122 }
123 if (IsVendorSection()) {
124 return true;
125 }
126 if (IsProductSection() &&
127 android::linkerconfig::modules::IsProductVndkVersionDefined()) {
128 return true;
129 }
130
131 return false;
132 }
133
134 } // namespace contents
135 } // namespace linkerconfig
136 } // namespace android
137