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 <vector>
23 
24 #include <android-base/result.h>
25 
26 #include "linkerconfig/basecontext.h"
27 #include "linkerconfig/configwriter.h"
28 #include "linkerconfig/namespace.h"
29 
30 namespace android {
31 namespace linkerconfig {
32 namespace modules {
33 
34 // LibProvider is a provider for alias of library requirements.
35 // When "foo" namespace requires "alias" (formatted ":name"),
36 // you would expect
37 //   foo.GetLink(<ns>).AddSharedLib(<shared_libs);
38 // which is equivalent to
39 //   namespace.foo.link.<ns>.shared_libs = <shared_libs>
40 // The referenced namespace (<ns>) is created via <ns_builder> and added
41 // in the current section.
42 struct LibProvider {
43   std::string ns;
44   std::function<Namespace()> ns_builder;
45   std::vector<std::string> shared_libs;
46 };
47 
48 using LibProviders = std::unordered_map<std::string, LibProvider>;
49 
50 class Section {
51  public:
Section(std::string name,std::vector<Namespace> namespaces)52   Section(std::string name, std::vector<Namespace> namespaces)
53       : name_(std::move(name)), namespaces_(std::move(namespaces)) {
54   }
55 
56   Section(const Section&) = delete;
57   Section(Section&&) = default;
58 
59   void WriteConfig(ConfigWriter& writer);
60   std::vector<std::string> GetBinaryPaths();
61   std::string GetName();
62 
63   android::base::Result<void> Resolve(const BaseContext& ctx,
64                                       const LibProviders& lib_providers = {});
65   Namespace* GetNamespace(const std::string& namespace_name);
66 
67   template <class _Function>
ForEachNamespaces(_Function f)68   void ForEachNamespaces(_Function f) {
69     for (auto& ns : namespaces_) {
70       f(ns);
71     }
72   }
73 
74  private:
75   const std::string name_;
76   std::vector<Namespace> namespaces_;
77 };
78 }  // namespace modules
79 }  // namespace linkerconfig
80 }  // namespace android
81