• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetCurrentApex() const66 const ApexInfo& Context::GetCurrentApex() const {
67   CHECK(current_apex_ != nullptr) << "only valid when IsApexBinaryConfig()";
68   return *current_apex_;
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 IsSystemSection() || IsUnrestrictedSection() ? "default" : "system";
77 }
78 
SetCurrentLinkerConfigType(LinkerConfigType config_type)79 void Context::SetCurrentLinkerConfigType(LinkerConfigType config_type) {
80   current_linkerconfig_type_ = config_type;
81 }
82 
SetCurrentApex(const ApexInfo * apex)83 void Context::SetCurrentApex(const ApexInfo* apex) {
84   current_apex_ = apex;
85 }
86 
IsVndkAvailable() const87 bool Context::IsVndkAvailable() const {
88   for (auto& apex : GetApexModules()) {
89     if (StartsWith(apex.name, "com.android.vndk.")) {
90       return true;
91     }
92   }
93   return false;
94 }
95 
Var(const std::string & name)96 std::string Var(const std::string& name) {
97   auto val = modules::Variables::GetValue(name);
98   if (val.has_value()) {
99     return *val;
100   }
101   CHECK(!"undefined var") << name << " is not defined";
102   return "";
103 }
104 
Var(const std::string & name,const std::string & default_value)105 std::string Var(const std::string& name, const std::string& default_value) {
106   auto val = modules::Variables::GetValue(name);
107   if (val.has_value()) {
108     return *val;
109   }
110   return default_value;
111 }
112 
IsSectionVndkEnabled() const113 bool Context::IsSectionVndkEnabled() const {
114   if (!IsVndkAvailable()) {
115     return false;
116   }
117   if (IsVendorSection()) {
118     return true;
119   }
120   if (IsProductSection() &&
121       android::linkerconfig::modules::IsProductVndkVersionDefined()) {
122     return true;
123   }
124   if (IsApexBinaryConfig()) {
125     // section for non-system APEX (aka Vendor APEX)
126     // can be seen as vndk-enabled because the apex either bundles
127     // with vndk libs in it or relies on VNDK from "vndk" namespace
128     return !GetCurrentApex().InSystem();
129   }
130   return false;
131 }
132 
133 }  // namespace contents
134 }  // namespace linkerconfig
135 }  // namespace android
136