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