xref: /aosp_15_r20/build/make/tools/perf/utils.py (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1*9e94795aSAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project
2*9e94795aSAndroid Build Coastguard Worker#
3*9e94795aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*9e94795aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*9e94795aSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*9e94795aSAndroid Build Coastguard Worker#
7*9e94795aSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*9e94795aSAndroid Build Coastguard Worker#
9*9e94795aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*9e94795aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*9e94795aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9e94795aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*9e94795aSAndroid Build Coastguard Worker# limitations under the License.
14*9e94795aSAndroid Build Coastguard Worker
15*9e94795aSAndroid Build Coastguard Workerimport os
16*9e94795aSAndroid Build Coastguard Workerimport pathlib
17*9e94795aSAndroid Build Coastguard Worker
18*9e94795aSAndroid Build Coastguard WorkerDEFAULT_REPORT_DIR = "benchmarks"
19*9e94795aSAndroid Build Coastguard Worker
20*9e94795aSAndroid Build Coastguard Workerdef get_root():
21*9e94795aSAndroid Build Coastguard Worker    top_dir = os.environ.get("ANDROID_BUILD_TOP")
22*9e94795aSAndroid Build Coastguard Worker    d = pathlib.Path.cwd()
23*9e94795aSAndroid Build Coastguard Worker    # with cog, someone may have a new workspace and new source tree top, but
24*9e94795aSAndroid Build Coastguard Worker    # not run lunch yet, resulting in a misleading ANDROID_BUILD_TOP value
25*9e94795aSAndroid Build Coastguard Worker    if top_dir and d.is_relative_to(top_dir):
26*9e94795aSAndroid Build Coastguard Worker        return pathlib.Path(top_dir).resolve()
27*9e94795aSAndroid Build Coastguard Worker    while True:
28*9e94795aSAndroid Build Coastguard Worker        if d.joinpath("build", "soong", "soong_ui.bash").exists():
29*9e94795aSAndroid Build Coastguard Worker            return d.resolve().absolute()
30*9e94795aSAndroid Build Coastguard Worker        d = d.parent
31*9e94795aSAndroid Build Coastguard Worker        if d == pathlib.Path("/"):
32*9e94795aSAndroid Build Coastguard Worker            return None
33*9e94795aSAndroid Build Coastguard Worker
34*9e94795aSAndroid Build Coastguard Workerdef get_dist_dir():
35*9e94795aSAndroid Build Coastguard Worker    dist_dir = os.getenv("DIST_DIR")
36*9e94795aSAndroid Build Coastguard Worker    if dist_dir:
37*9e94795aSAndroid Build Coastguard Worker        return pathlib.Path(dist_dir).resolve()
38*9e94795aSAndroid Build Coastguard Worker    return get_out_dir().joinpath("dist")
39*9e94795aSAndroid Build Coastguard Worker
40*9e94795aSAndroid Build Coastguard Workerdef get_out_dir():
41*9e94795aSAndroid Build Coastguard Worker    out_dir = os.getenv("OUT_DIR")
42*9e94795aSAndroid Build Coastguard Worker    if not out_dir:
43*9e94795aSAndroid Build Coastguard Worker        out_dir = "out"
44*9e94795aSAndroid Build Coastguard Worker    return pathlib.Path(out_dir).resolve()
45