• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.tradefed.util;
18 
19 import com.android.tradefed.log.LogUtil.CLog;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 
27 /** A collection of helper methods to prepare environment variables. */
28 public class EnvironmentVariableUtil {
29 
30     /**
31      * Builds the value of PATH.
32      *
33      * @param tools A list of tools that will be added to PATH.
34      * @param addition The String that will be appended to the end of the return.
35      * @return The value of PATH.
36      */
buildPath(Set<String> tools, String addition)37     public static String buildPath(Set<String> tools, String addition) {
38         List<String> paths = new ArrayList<>();
39         for (String t : tools) {
40             try {
41                 File tool = new File(t);
42                 paths.add(
43                         tool.exists()
44                                 ? tool.getParent()
45                                 : DeviceActionUtil.findExecutableOnPath(t).getParent());
46             } catch (DeviceActionUtil.DeviceActionConfigError e) {
47                 CLog.e("Failed to find %s!", t);
48                 CLog.e(e);
49             }
50         }
51 
52         paths.add(addition);
53         return paths.stream().distinct().collect(Collectors.joining(getPathSeparator()));
54     }
55 
56     /**
57      * Builds the value of LD_LIBRARY_PATH that uses the shared libs inside module folder.
58      *
59      * @param moduleDir The root of module folder.
60      * @param subDirs The sub-directories that are relative to the root of module folder.
61      * @return The value of LD_LIBRARY_PATH.
62      */
buildMinimalLdLibraryPath(File moduleDir, List<String> subDirs)63     public static String buildMinimalLdLibraryPath(File moduleDir, List<String> subDirs) {
64         List<String> paths = new ArrayList<>();
65         paths.add(moduleDir.getAbsolutePath());
66         paths.addAll(
67                 subDirs.stream()
68                         .map(d -> new File(moduleDir, d))
69                         .filter(f -> f.exists())
70                         .map(f -> f.getAbsolutePath())
71                         .collect(Collectors.toList()));
72         return paths.stream().distinct().collect(Collectors.joining(getPathSeparator()));
73     }
74 
getPathSeparator()75     private static String getPathSeparator() {
76         return System.getProperty("path.separator");
77     }
78 }
79