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 <map> 19 #include <string> 20 #include <unordered_map> 21 #include <utility> 22 #include <variant> 23 #include <vector> 24 25 #include <android-base/result.h> 26 27 #include "linkerconfig/basecontext.h" 28 #include "linkerconfig/configwriter.h" 29 #include "linkerconfig/namespace.h" 30 31 namespace android { 32 namespace linkerconfig { 33 namespace modules { 34 35 struct AllowAllSharedLibs { AllowAllSharedLibsAllowAllSharedLibs36 AllowAllSharedLibs() { 37 } ApplyAllowAllSharedLibs38 void Apply(Link& link) const { 39 link.AllowAllSharedLibs(); 40 } 41 }; 42 struct SharedLibs { 43 std::vector<std::string> list; SharedLibsSharedLibs44 SharedLibs(std::vector<std::string> list) : list(std::move(list)) { 45 } ApplySharedLibs46 void Apply(Link& link) const { 47 link.AddSharedLib(list); 48 } 49 }; 50 // LibProvider is a provider for alias of library requirements. 51 // When "foo" namespace requires "alias" (formatted ":name"), 52 // you would expect 53 // foo.GetLink(<ns>).AddSharedLib(<shared_libs>); 54 // or 55 // foo.GetLink(<ns>).AllowAllSharedLibs(); 56 // which is equivalent to 57 // namespace.foo.link.<ns>.shared_libs = <shared_libs> 58 // namespace.foo.link.<ns>.allow_all_shared_libs = true 59 // The referenced namespace (<ns>) is created via <ns_builder> and added 60 // in the current section. 61 struct LibProvider { 62 std::string ns; 63 std::function<Namespace()> ns_builder; 64 std::variant<SharedLibs, AllowAllSharedLibs> link_modifier; 65 }; 66 67 // LibProviders maps "alias" to one or more LibProviders. 68 using LibProviders = std::unordered_map<std::string, std::vector<LibProvider>>; 69 70 class Section { 71 public: Section(std::string name,std::vector<Namespace> namespaces)72 Section(std::string name, std::vector<Namespace> namespaces) 73 : name_(std::move(name)), namespaces_(std::move(namespaces)) { 74 } 75 76 Section(const Section&) = delete; 77 Section(Section&&) = default; 78 79 void WriteConfig(ConfigWriter& writer) const; 80 std::vector<std::string> GetBinaryPaths(); 81 std::string GetName(); 82 83 void Resolve(const BaseContext& ctx, const LibProviders& lib_providers = {}); 84 Namespace* GetNamespace(const std::string& namespace_name); 85 86 template <class _Function> ForEachNamespaces(_Function f)87 void ForEachNamespaces(_Function f) { 88 for (auto& ns : namespaces_) { 89 f(ns); 90 } 91 } 92 93 private: 94 const std::string name_; 95 std::vector<Namespace> namespaces_; 96 }; 97 } // namespace modules 98 } // namespace linkerconfig 99 } // namespace android 100