xref: /aosp_15_r20/system/extras/torq/validate_simpleperf.py (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker#
2*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2024 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker#
4*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker#
8*288bf522SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker#
10*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker# limitations under the License.
15*288bf522SAndroid Build Coastguard Worker#
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Workerimport os
18*288bf522SAndroid Build Coastguard Workerimport subprocess
19*288bf522SAndroid Build Coastguard Workerfrom utils import path_exists, dir_exists
20*288bf522SAndroid Build Coastguard Workerfrom validation_error import ValidationError
21*288bf522SAndroid Build Coastguard Worker
22*288bf522SAndroid Build Coastguard WorkerTORQ_TEMP_DIR = "/tmp/.torq"
23*288bf522SAndroid Build Coastguard WorkerTEMP_CACHE_BUILDER_SCRIPT = TORQ_TEMP_DIR + "/binary_cache_builder.py"
24*288bf522SAndroid Build Coastguard WorkerSIMPLEPERF_SCRIPTS_DIR = "/system/extras/simpleperf/scripts"
25*288bf522SAndroid Build Coastguard WorkerBUILDER_SCRIPT = SIMPLEPERF_SCRIPTS_DIR + "/binary_cache_builder.py"
26*288bf522SAndroid Build Coastguard Worker
27*288bf522SAndroid Build Coastguard Workerdef verify_simpleperf_args(args):
28*288bf522SAndroid Build Coastguard Worker  args.scripts_path = TORQ_TEMP_DIR
29*288bf522SAndroid Build Coastguard Worker  if ("ANDROID_BUILD_TOP" in os.environ
30*288bf522SAndroid Build Coastguard Worker      and path_exists(os.environ["ANDROID_BUILD_TOP"] + BUILDER_SCRIPT)):
31*288bf522SAndroid Build Coastguard Worker    args.scripts_path = (os.environ["ANDROID_BUILD_TOP"]
32*288bf522SAndroid Build Coastguard Worker                         + SIMPLEPERF_SCRIPTS_DIR)
33*288bf522SAndroid Build Coastguard Worker
34*288bf522SAndroid Build Coastguard Worker  if args.symbols is None or not dir_exists(args.symbols):
35*288bf522SAndroid Build Coastguard Worker    if args.symbols is not None:
36*288bf522SAndroid Build Coastguard Worker      return None, ValidationError(
37*288bf522SAndroid Build Coastguard Worker          ("%s is not a valid path." % args.symbols),
38*288bf522SAndroid Build Coastguard Worker          "Set --symbols to a valid symbols lib path or set "
39*288bf522SAndroid Build Coastguard Worker          "$ANDROID_PRODUCT_OUT to your android product out directory "
40*288bf522SAndroid Build Coastguard Worker          "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).")
41*288bf522SAndroid Build Coastguard Worker    if "ANDROID_PRODUCT_OUT" not in os.environ:
42*288bf522SAndroid Build Coastguard Worker      return None, ValidationError(
43*288bf522SAndroid Build Coastguard Worker          "ANDROID_PRODUCT_OUT is not set.",
44*288bf522SAndroid Build Coastguard Worker          "Set --symbols to a valid symbols lib path or set "
45*288bf522SAndroid Build Coastguard Worker          "$ANDROID_PRODUCT_OUT to your android product out directory "
46*288bf522SAndroid Build Coastguard Worker          "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).")
47*288bf522SAndroid Build Coastguard Worker    if not dir_exists(os.environ["ANDROID_PRODUCT_OUT"]):
48*288bf522SAndroid Build Coastguard Worker      return None, ValidationError(
49*288bf522SAndroid Build Coastguard Worker          ("%s is not a valid $ANDROID_PRODUCT_OUT."
50*288bf522SAndroid Build Coastguard Worker           % (os.environ["ANDROID_PRODUCT_OUT"])),
51*288bf522SAndroid Build Coastguard Worker          "Set --symbols to a valid symbols lib path or set "
52*288bf522SAndroid Build Coastguard Worker          "$ANDROID_PRODUCT_OUT to your android product out directory "
53*288bf522SAndroid Build Coastguard Worker          "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).")
54*288bf522SAndroid Build Coastguard Worker    args.symbols = os.environ["ANDROID_PRODUCT_OUT"]
55*288bf522SAndroid Build Coastguard Worker
56*288bf522SAndroid Build Coastguard Worker  if (args.scripts_path != TORQ_TEMP_DIR or
57*288bf522SAndroid Build Coastguard Worker      path_exists(TEMP_CACHE_BUILDER_SCRIPT)):
58*288bf522SAndroid Build Coastguard Worker    return args, None
59*288bf522SAndroid Build Coastguard Worker
60*288bf522SAndroid Build Coastguard Worker  error = download_simpleperf_scripts()
61*288bf522SAndroid Build Coastguard Worker
62*288bf522SAndroid Build Coastguard Worker  if error is not None:
63*288bf522SAndroid Build Coastguard Worker    return None, error
64*288bf522SAndroid Build Coastguard Worker
65*288bf522SAndroid Build Coastguard Worker  return args, None
66*288bf522SAndroid Build Coastguard Worker
67*288bf522SAndroid Build Coastguard Workerdef download_simpleperf_scripts():
68*288bf522SAndroid Build Coastguard Worker  i = 0
69*288bf522SAndroid Build Coastguard Worker  while i <= 3:
70*288bf522SAndroid Build Coastguard Worker    i += 1
71*288bf522SAndroid Build Coastguard Worker    confirmation = input("You do not have an Android Root configured with "
72*288bf522SAndroid Build Coastguard Worker                         "the simpleperf directory. To use simpleperf, torq "
73*288bf522SAndroid Build Coastguard Worker                         "will download simpleperf scripts to '%s'. "
74*288bf522SAndroid Build Coastguard Worker                         "Are you ok with this download? [Y/N]: "
75*288bf522SAndroid Build Coastguard Worker                         % TORQ_TEMP_DIR)
76*288bf522SAndroid Build Coastguard Worker
77*288bf522SAndroid Build Coastguard Worker    if confirmation.lower() == "y":
78*288bf522SAndroid Build Coastguard Worker      break
79*288bf522SAndroid Build Coastguard Worker    elif confirmation.lower() == "n":
80*288bf522SAndroid Build Coastguard Worker      return ValidationError("Did not download simpleperf scripts.",
81*288bf522SAndroid Build Coastguard Worker                             "Set $ANDROID_BUILD_TOP to your android root "
82*288bf522SAndroid Build Coastguard Worker                             "path and make sure you have $ANDROID_BUILD_TOP"
83*288bf522SAndroid Build Coastguard Worker                             "/system/extras/simpleperf/scripts "
84*288bf522SAndroid Build Coastguard Worker                             "downloaded.")
85*288bf522SAndroid Build Coastguard Worker    if i == 3:
86*288bf522SAndroid Build Coastguard Worker      return ValidationError("Invalid inputs.",
87*288bf522SAndroid Build Coastguard Worker                             "Set $ANDROID_BUILD_TOP to your android root "
88*288bf522SAndroid Build Coastguard Worker                             "path and make sure you have $ANDROID_BUILD_TOP"
89*288bf522SAndroid Build Coastguard Worker                             "/system/extras/simpleperf/scripts "
90*288bf522SAndroid Build Coastguard Worker                             "downloaded.")
91*288bf522SAndroid Build Coastguard Worker
92*288bf522SAndroid Build Coastguard Worker  subprocess.run(("mkdir -p %s && wget -P %s "
93*288bf522SAndroid Build Coastguard Worker                 "https://android.googlesource.com/platform/system/extras"
94*288bf522SAndroid Build Coastguard Worker                 "/+archive/refs/heads/main/simpleperf/scripts.tar.gz "
95*288bf522SAndroid Build Coastguard Worker                 "&& tar -xvzf %s/scripts.tar.gz -C %s"
96*288bf522SAndroid Build Coastguard Worker                  % (TORQ_TEMP_DIR, TORQ_TEMP_DIR, TORQ_TEMP_DIR,
97*288bf522SAndroid Build Coastguard Worker                     TORQ_TEMP_DIR)),
98*288bf522SAndroid Build Coastguard Worker                 shell=True)
99*288bf522SAndroid Build Coastguard Worker
100*288bf522SAndroid Build Coastguard Worker  if not path_exists(TEMP_CACHE_BUILDER_SCRIPT):
101*288bf522SAndroid Build Coastguard Worker    raise Exception("Error while downloading simpleperf scripts. Try again "
102*288bf522SAndroid Build Coastguard Worker                    "or set $ANDROID_BUILD_TOP to your android root path and "
103*288bf522SAndroid Build Coastguard Worker                    "make sure you have $ANDROID_BUILD_TOP/system/extras/"
104*288bf522SAndroid Build Coastguard Worker                    "simpleperf/scripts downloaded.")
105*288bf522SAndroid Build Coastguard Worker
106*288bf522SAndroid Build Coastguard Worker  return None
107