xref: /aosp_15_r20/external/webrtc/tools_webrtc/executable_host_build.py (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker#!/usr/bin/env vpython3
2*d9f75844SAndroid Build Coastguard Worker
3*d9f75844SAndroid Build Coastguard Worker# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
4*d9f75844SAndroid Build Coastguard Worker#
5*d9f75844SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license
6*d9f75844SAndroid Build Coastguard Worker# that can be found in the LICENSE file in the root of the source
7*d9f75844SAndroid Build Coastguard Worker# tree. An additional intellectual property rights grant can be found
8*d9f75844SAndroid Build Coastguard Worker# in the file PATENTS.  All contributing project authors may
9*d9f75844SAndroid Build Coastguard Worker# be found in the AUTHORS file in the root of the source tree.
10*d9f75844SAndroid Build Coastguard Worker"""
11*d9f75844SAndroid Build Coastguard WorkerThis script builds a GN executable targeting the host machine.
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard WorkerIt is useful, for example, for mobile devices performance testing where
14*d9f75844SAndroid Build Coastguard Workerit makes sense to build WebRTC for a mobile platform (e.g. Android) but
15*d9f75844SAndroid Build Coastguard Workerpart of the test is performed on the host machine (e.g. running an
16*d9f75844SAndroid Build Coastguard Workerexecutable to analyze a video downloaded from a device).
17*d9f75844SAndroid Build Coastguard Worker
18*d9f75844SAndroid Build Coastguard WorkerThe script has only one (mandatory) option: --executable_name, which is
19*d9f75844SAndroid Build Coastguard Workerthe output name of the GN executable. For example, if you have the
20*d9f75844SAndroid Build Coastguard Workerfollowing executable in your out folder:
21*d9f75844SAndroid Build Coastguard Worker
22*d9f75844SAndroid Build Coastguard Worker  out/Debug/random_exec
23*d9f75844SAndroid Build Coastguard Worker
24*d9f75844SAndroid Build Coastguard WorkerYou will be able to compile the same executable targeting your host machine
25*d9f75844SAndroid Build Coastguard Workerby running:
26*d9f75844SAndroid Build Coastguard Worker
27*d9f75844SAndroid Build Coastguard Worker  $ vpython3 tools_webrtc/executable_host_build.py --executable_name random_exec
28*d9f75844SAndroid Build Coastguard Worker
29*d9f75844SAndroid Build Coastguard WorkerThe generated executable will have the same name as the input executable with
30*d9f75844SAndroid Build Coastguard Workersuffix '_host'.
31*d9f75844SAndroid Build Coastguard Worker
32*d9f75844SAndroid Build Coastguard WorkerThis script should not be used standalone but from GN, through an action:
33*d9f75844SAndroid Build Coastguard Worker
34*d9f75844SAndroid Build Coastguard Worker  action("random_exec_host") {
35*d9f75844SAndroid Build Coastguard Worker    script = "//tools_webrtc/executable_host_build.py"
36*d9f75844SAndroid Build Coastguard Worker    outputs = [
37*d9f75844SAndroid Build Coastguard Worker      "${root_out_dir}/random_exec_host",
38*d9f75844SAndroid Build Coastguard Worker    ]
39*d9f75844SAndroid Build Coastguard Worker    args = [
40*d9f75844SAndroid Build Coastguard Worker      "--executable_name",
41*d9f75844SAndroid Build Coastguard Worker      "random_exec",
42*d9f75844SAndroid Build Coastguard Worker    ]
43*d9f75844SAndroid Build Coastguard Worker  }
44*d9f75844SAndroid Build Coastguard Worker
45*d9f75844SAndroid Build Coastguard WorkerThe executable for the host machine will be generated in the GN out directory
46*d9f75844SAndroid Build Coastguard Worker(e.g. out/Debug in the previous example).
47*d9f75844SAndroid Build Coastguard Worker"""
48*d9f75844SAndroid Build Coastguard Worker
49*d9f75844SAndroid Build Coastguard Workerfrom contextlib import contextmanager
50*d9f75844SAndroid Build Coastguard Worker
51*d9f75844SAndroid Build Coastguard Workerimport argparse
52*d9f75844SAndroid Build Coastguard Workerimport os
53*d9f75844SAndroid Build Coastguard Workerimport shutil
54*d9f75844SAndroid Build Coastguard Workerimport subprocess
55*d9f75844SAndroid Build Coastguard Workerimport sys
56*d9f75844SAndroid Build Coastguard Workerimport tempfile
57*d9f75844SAndroid Build Coastguard Worker
58*d9f75844SAndroid Build Coastguard WorkerSCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
59*d9f75844SAndroid Build Coastguard WorkerSRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir))
60*d9f75844SAndroid Build Coastguard Workersys.path.append(os.path.join(SRC_DIR, 'build'))
61*d9f75844SAndroid Build Coastguard Workerimport find_depot_tools
62*d9f75844SAndroid Build Coastguard Worker
63*d9f75844SAndroid Build Coastguard Worker
64*d9f75844SAndroid Build Coastguard Workerdef _ParseArgs():
65*d9f75844SAndroid Build Coastguard Worker  desc = 'Generates a GN executable targeting the host machine.'
66*d9f75844SAndroid Build Coastguard Worker  parser = argparse.ArgumentParser(description=desc)
67*d9f75844SAndroid Build Coastguard Worker  parser.add_argument('--executable_name',
68*d9f75844SAndroid Build Coastguard Worker                      required=True,
69*d9f75844SAndroid Build Coastguard Worker                      help='Name of the executable to build')
70*d9f75844SAndroid Build Coastguard Worker  args = parser.parse_args()
71*d9f75844SAndroid Build Coastguard Worker  return args
72*d9f75844SAndroid Build Coastguard Worker
73*d9f75844SAndroid Build Coastguard Worker
74*d9f75844SAndroid Build Coastguard Worker@contextmanager
75*d9f75844SAndroid Build Coastguard Workerdef HostBuildDir():
76*d9f75844SAndroid Build Coastguard Worker  temp_dir = tempfile.mkdtemp()
77*d9f75844SAndroid Build Coastguard Worker  try:
78*d9f75844SAndroid Build Coastguard Worker    yield temp_dir
79*d9f75844SAndroid Build Coastguard Worker  finally:
80*d9f75844SAndroid Build Coastguard Worker    shutil.rmtree(temp_dir)
81*d9f75844SAndroid Build Coastguard Worker
82*d9f75844SAndroid Build Coastguard Worker
83*d9f75844SAndroid Build Coastguard Workerdef _RunCommand(argv, cwd=SRC_DIR, **kwargs):
84*d9f75844SAndroid Build Coastguard Worker  with open(os.devnull, 'w') as devnull:
85*d9f75844SAndroid Build Coastguard Worker    subprocess.check_call(argv, cwd=cwd, stdout=devnull, **kwargs)
86*d9f75844SAndroid Build Coastguard Worker
87*d9f75844SAndroid Build Coastguard Worker
88*d9f75844SAndroid Build Coastguard Workerdef DepotToolPath(*args):
89*d9f75844SAndroid Build Coastguard Worker  return os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, *args)
90*d9f75844SAndroid Build Coastguard Worker
91*d9f75844SAndroid Build Coastguard Worker
92*d9f75844SAndroid Build Coastguard Workerif __name__ == '__main__':
93*d9f75844SAndroid Build Coastguard Worker  ARGS = _ParseArgs()
94*d9f75844SAndroid Build Coastguard Worker  EXECUTABLE_TO_BUILD = ARGS.executable_name
95*d9f75844SAndroid Build Coastguard Worker  EXECUTABLE_FINAL_NAME = ARGS.executable_name + '_host'
96*d9f75844SAndroid Build Coastguard Worker  with HostBuildDir() as build_dir:
97*d9f75844SAndroid Build Coastguard Worker    _RunCommand([sys.executable, DepotToolPath('gn.py'), 'gen', build_dir])
98*d9f75844SAndroid Build Coastguard Worker    _RunCommand([DepotToolPath('ninja'), '-C', build_dir, EXECUTABLE_TO_BUILD])
99*d9f75844SAndroid Build Coastguard Worker    shutil.copy(os.path.join(build_dir, EXECUTABLE_TO_BUILD),
100*d9f75844SAndroid Build Coastguard Worker                EXECUTABLE_FINAL_NAME)
101