blob: 7fed128195d96b0d30057bb8582f8cbf3cca7813 [file] [log] [blame]
Jae Shin5233fe12017-12-18 22:29:48 +09001#!/usr/bin/env python
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Utility functions for VNDK snapshot."""
18
Jae Shin4db81a52017-12-29 13:24:54 +090019import glob
Jae Shin94075212018-06-14 14:54:34 +090020import logging
Jae Shin5233fe12017-12-18 22:29:48 +090021import os
Jae Shin4db81a52017-12-29 13:24:54 +090022import re
Jae Shin25d3abf2018-04-18 18:00:26 +090023import subprocess
Jae Shin5233fe12017-12-18 22:29:48 +090024import sys
25
Jae Shin4db81a52017-12-29 13:24:54 +090026# Global Keys
27# All paths are relative to install_dir: prebuilts/vndk/v{version}
Jaewoong Jung6a5aaca2019-01-17 15:41:06 -080028ROOT_BP_PATH = 'Android.bp'
Jae Shin4db81a52017-12-29 13:24:54 +090029COMMON_DIR_NAME = 'common'
30COMMON_DIR_PATH = COMMON_DIR_NAME
Jaewoong Jung6fbb9d22018-11-28 09:25:22 -080031COMMON_BP_PATH = os.path.join(COMMON_DIR_PATH, 'Android.bp')
Jae Shin4db81a52017-12-29 13:24:54 +090032CONFIG_DIR_PATH_PATTERN = '*/configs'
33MANIFEST_FILE_NAME = 'manifest.xml'
Jae Shin4db81a52017-12-29 13:24:54 +090034MODULE_PATHS_FILE_NAME = 'module_paths.txt'
35NOTICE_FILES_DIR_NAME = 'NOTICE_FILES'
36NOTICE_FILES_DIR_PATH = os.path.join(COMMON_DIR_PATH, NOTICE_FILES_DIR_NAME)
Jae Shinaf0c0032018-06-21 11:54:05 +090037BINDER32 = 'binder32'
Jae Shin4db81a52017-12-29 13:24:54 +090038
Jae Shin5233fe12017-12-18 22:29:48 +090039
Jae Shin94075212018-06-14 14:54:34 +090040def set_logging_config(verbose_level):
41 verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG)
42 verbosity = min(verbose_level, 2)
43 logging.basicConfig(
44 format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
45 level=verbose_map[verbosity])
46
47
Jae Shinbd82ebb2018-06-21 14:13:46 +090048def check_call(cmd):
49 logging.debug('Running `{}`'.format(' '.join(cmd)))
Jae Shin94075212018-06-14 14:54:34 +090050 subprocess.check_call(cmd)
51
52
Jae Shinbd82ebb2018-06-21 14:13:46 +090053def check_output(cmd):
54 logging.debug('Running `{}`'.format(' '.join(cmd)))
Jae Shince3bb752018-06-18 12:40:53 +090055 output = subprocess.check_output(cmd)
Jae Shinbd82ebb2018-06-21 14:13:46 +090056 logging.debug('Output: `{}`'.format(output))
Jae Shince3bb752018-06-18 12:40:53 +090057 return output
58
59
Jae Shin5233fe12017-12-18 22:29:48 +090060def get_android_build_top():
61 ANDROID_BUILD_TOP = os.getenv('ANDROID_BUILD_TOP')
62 if not ANDROID_BUILD_TOP:
63 print('Error: Missing ANDROID_BUILD_TOP env variable. Please run '
64 '\'. build/envsetup.sh; lunch <build target>\'. Exiting script.')
65 sys.exit(1)
66 return ANDROID_BUILD_TOP
67
68
69def join_realpath(root, *args):
70 return os.path.realpath(os.path.join(root, *args))
71
72
73def _get_dir_from_env(env_var, default):
74 return os.path.realpath(os.getenv(env_var, default))
75
76
77def get_out_dir(android_build_top):
78 return _get_dir_from_env('OUT_DIR', join_realpath(android_build_top,
79 'out'))
80
81
82def get_dist_dir(out_dir):
83 return _get_dir_from_env('DIST_DIR', join_realpath(out_dir, 'dist'))
84
85
Jae Shinaf0c0032018-06-21 11:54:05 +090086def get_snapshot_archs(install_dir):
87 """Returns a list of VNDK snapshot arch flavors under install_dir.
Jae Shin4db81a52017-12-29 13:24:54 +090088
89 Args:
90 install_dir: string, absolute path of prebuilts/vndk/v{version}
91 """
Jae Shinaf0c0032018-06-21 11:54:05 +090092 archs = []
Jae Shin4db81a52017-12-29 13:24:54 +090093 for file in glob.glob('{}/*'.format(install_dir)):
94 basename = os.path.basename(file)
95 if os.path.isdir(file) and basename != COMMON_DIR_NAME:
Jae Shinaf0c0032018-06-21 11:54:05 +090096 archs.append(basename)
97 return archs
Jae Shin4db81a52017-12-29 13:24:54 +090098
99
Jae Shinaf0c0032018-06-21 11:54:05 +0900100def prebuilt_arch_from_path(path):
Jae Shin4db81a52017-12-29 13:24:54 +0900101 """Extracts arch of prebuilts from path relative to install_dir.
Jae Shin5233fe12017-12-18 22:29:48 +0900102
103 Args:
104 path: string, path relative to prebuilts/vndk/v{version}
105
106 Returns:
Jae Shin4db81a52017-12-29 13:24:54 +0900107 string, arch of prebuilt (e.g., 'arm' or 'arm64' or 'x86' or 'x86_64')
Jae Shin5233fe12017-12-18 22:29:48 +0900108 """
Jae Shin4db81a52017-12-29 13:24:54 +0900109 return path.split('/')[1].split('-')[1]
110
111
Jae Shinaf0c0032018-06-21 11:54:05 +0900112def snapshot_arch_from_path(path):
113 """Extracts VNDK snapshot arch from path relative to install_dir.
Jae Shin4db81a52017-12-29 13:24:54 +0900114
115 Args:
116 path: string, path relative to prebuilts/vndk/v{version}
117
118 Returns:
Jae Shinaf0c0032018-06-21 11:54:05 +0900119 string, VNDK snapshot arch (e.g. 'arm64')
Jae Shin4db81a52017-12-29 13:24:54 +0900120 """
121 return path.split('/')[0]
Jae Shin5233fe12017-12-18 22:29:48 +0900122
123
124def find(path, names):
125 """Returns a list of files in a directory that match the given names.
126
127 Args:
128 path: string, absolute path of directory from which to find files
129 names: list of strings, names of the files to find
130 """
131 found = []
132 for root, _, files in os.walk(path):
133 for file_name in sorted(files):
134 if file_name in names:
135 abspath = os.path.abspath(os.path.join(root, file_name))
136 rel_to_root = abspath.replace(os.path.abspath(path), '')
137 found.append(rel_to_root[1:]) # strip leading /
138 return found
Jae Shin25d3abf2018-04-18 18:00:26 +0900139
140
141def fetch_artifact(branch, build, pattern, destination='.'):
142 """Fetches build artifacts from Android Build server.
143
144 Args:
145 branch: string, branch to pull build artifacts from
146 build: string, build number to pull build artifacts from
147 pattern: string, pattern of build artifact file name
148 destination: string, destination to pull build artifact to
149 """
150 fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact'
151 cmd = [
152 fetch_artifact_path, '--branch', branch, '--target=vndk', '--bid',
153 build, pattern, destination
154 ]
Jae Shinbd82ebb2018-06-21 14:13:46 +0900155 check_call(cmd)