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