Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame] | 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/librarylistloader.h" |
| 18 | |
| 19 | #include <android-base/result.h> |
| 20 | #include <android-base/strings.h> |
| 21 | #include <algorithm> |
| 22 | #include <fstream> |
| 23 | #include <iterator> |
| 24 | #include <set> |
| 25 | #include <sstream> |
| 26 | #include <unordered_map> |
| 27 | |
| 28 | #include "linkerconfig/log.h" |
| 29 | |
| 30 | using LibraryList = std::set<std::string>; |
| 31 | |
| 32 | using android::base::ErrnoErrorf; |
| 33 | using android::base::Result; |
| 34 | |
| 35 | namespace { |
| 36 | std::unordered_map<std::string, LibraryList> library_file_cache; |
| 37 | Result<LibraryList> GetLibrariesFromFile(std::string file_path) { |
| 38 | auto cached_data = library_file_cache.find(file_path); |
| 39 | if (cached_data != library_file_cache.end()) { |
| 40 | return cached_data->second; |
| 41 | } |
| 42 | |
| 43 | std::string library_name; |
| 44 | LibraryList library_list; |
| 45 | std::ifstream library_file(file_path.c_str(), std::ifstream::in); |
| 46 | |
| 47 | if (!library_file) { |
| 48 | return ErrnoErrorf("Failed to open file {}", file_path); |
| 49 | } |
| 50 | |
| 51 | while (std::getline(library_file, library_name)) { |
Kiyoung Kim | f74c2b7 | 2019-09-27 17:45:10 +0900 | [diff] [blame] | 52 | library_name = android::base::Trim(library_name); |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame] | 53 | if (!library_name.empty()) { |
| 54 | library_list.insert(library_name); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | library_file_cache.insert({file_path, library_list}); |
| 59 | |
| 60 | return library_list; |
| 61 | } |
| 62 | } // namespace |
| 63 | |
| 64 | namespace android { |
| 65 | namespace linkerconfig { |
| 66 | namespace generator { |
| 67 | std::string GetLibrariesString(std::string library_file_path) { |
| 68 | auto library_list_result = GetLibrariesFromFile(library_file_path); |
| 69 | if (library_list_result) { |
| 70 | return android::base::Join(*library_list_result, ':'); |
| 71 | } else { |
| 72 | // Consider unavailable library file as empty |
| 73 | LOG(WARNING) << library_list_result.error(); |
| 74 | return ""; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | std::string GetPublicLibrariesString(std::string library_file_path, |
| 79 | std::string private_library_file_path) { |
| 80 | auto library_list = GetLibrariesFromFile(library_file_path); |
| 81 | auto private_library_list = GetLibrariesFromFile(private_library_file_path); |
| 82 | |
| 83 | if (!library_list) { |
| 84 | // Consider unavailable library file as empty |
| 85 | LOG(WARNING) << library_list.error(); |
| 86 | return ""; |
| 87 | } |
| 88 | |
| 89 | if (!private_library_list) { |
| 90 | // No private library found. All libraries are public |
| 91 | LOG(WARNING) << private_library_list.error(); |
| 92 | return android::base::Join(*library_list, ':'); |
| 93 | } |
| 94 | |
| 95 | LibraryList public_library_list; |
| 96 | |
| 97 | std::set_difference( |
Kiyoung Kim | f74c2b7 | 2019-09-27 17:45:10 +0900 | [diff] [blame] | 98 | library_list->begin(), |
| 99 | library_list->end(), |
| 100 | private_library_list->begin(), |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame] | 101 | private_library_list->end(), |
| 102 | std::inserter(public_library_list, public_library_list.begin())); |
| 103 | |
| 104 | return android::base::Join(public_library_list, ':'); |
| 105 | } |
| 106 | |
| 107 | std::string GetPrivateLibrariesString(std::string library_file_path, |
| 108 | std::string private_library_file_path) { |
| 109 | auto library_list = GetLibrariesFromFile(library_file_path); |
| 110 | auto private_library_list = GetLibrariesFromFile(private_library_file_path); |
| 111 | |
| 112 | if (!library_list) { |
| 113 | // Consider unavailable library file as empty |
| 114 | LOG(WARNING) << library_list.error(); |
| 115 | return ""; |
| 116 | } |
| 117 | |
| 118 | if (!private_library_list) { |
| 119 | // No private library found. All libraries are public |
| 120 | LOG(WARNING) << private_library_list.error(); |
| 121 | return ""; |
| 122 | } |
| 123 | |
| 124 | LibraryList private_only_library_list; |
| 125 | |
Kiyoung Kim | f74c2b7 | 2019-09-27 17:45:10 +0900 | [diff] [blame] | 126 | std::set_intersection(library_list->begin(), |
| 127 | library_list->end(), |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame] | 128 | private_library_list->begin(), |
| 129 | private_library_list->end(), |
| 130 | std::inserter(private_only_library_list, |
| 131 | private_only_library_list.begin())); |
| 132 | |
| 133 | return android::base::Join(private_only_library_list, ':'); |
| 134 | } |
| 135 | } // namespace generator |
| 136 | } // namespace linkerconfig |
| 137 | } // namespace android |