blob: cc1e7ee59ec107567b2d36a893e671b31ecaf793 [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
58
59SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
60SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir))
61sys.path.append(os.path.join(SRC_DIR, 'build'))
62import find_depot_tools
63
64
65def _ParseArgs():
66 desc = 'Generates a GN executable targeting the host machine.'
67 parser = argparse.ArgumentParser(description=desc)
68 parser.add_argument('--executable_name',
69 required=True,
70 help='Name of the executable to build')
71 args = parser.parse_args()
72 return args
73
74
75@contextmanager
76def HostBuildDir():
77 temp_dir = tempfile.mkdtemp()
78 try:
79 yield temp_dir
80 finally:
81 shutil.rmtree(temp_dir)
82
83
84def _RunCommand(argv, cwd=SRC_DIR, **kwargs):
85 with open(os.devnull, 'w') as devnull:
86 subprocess.check_call(argv, cwd=cwd, stdout=devnull, **kwargs)
87
88
89def DepotToolPath(*args):
90 return os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, *args)
91
92
93if __name__ == '__main__':
94 ARGS = _ParseArgs()
95 EXECUTABLE_TO_BUILD = ARGS.executable_name
96 EXECUTABLE_FINAL_NAME = ARGS.executable_name + '_host'
97 with HostBuildDir() as build_dir:
98 _RunCommand([sys.executable, DepotToolPath('gn.py'), 'gen', build_dir])
99 _RunCommand([DepotToolPath('ninja'), '-C', build_dir, EXECUTABLE_TO_BUILD])
100 shutil.copy(os.path.join(build_dir, EXECUTABLE_TO_BUILD),
101 EXECUTABLE_FINAL_NAME)