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/variables.h"
18
19 #include <android-base/properties.h>
20
21 #include "linkerconfig/log.h"
22
23 namespace android {
24 namespace linkerconfig {
25 namespace modules {
26
27 std::map<std::string, std::string> Variables::variables_;
28
GetValue(const std::string & variable)29 std::optional<std::string> Variables::GetValue(const std::string& variable) {
30 // If variable is in predefined key-value pair, use this value
31 if (variables_.find(variable) != variables_.end() &&
32 !variables_[variable].empty()) {
33 return {variables_[variable]};
34 }
35
36 // If variable is defined as property, use this value
37 std::string prop_value = android::base::GetProperty(variable, "");
38 if (!prop_value.empty()) {
39 return {prop_value};
40 }
41
42 // If cannot find variable, return default value
43 return std::nullopt;
44 }
45
AddValue(const std::string & key,const std::string & value)46 void Variables::AddValue(const std::string& key, const std::string& value) {
47 variables_[key] = value;
48 }
49 } // namespace modules
50 } // namespace linkerconfig
51 } // namespace android