Mirko Bonadei | b56706f | 2018-09-18 11:03:39 +0200 | [diff] [blame] | 1 | #!/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 | """ |
| 11 | This script builds a GN executable targeting the host machine. |
| 12 | |
| 13 | It is useful, for example, for mobile devices performance testing where |
| 14 | it makes sense to build WebRTC for a mobile platform (e.g. Android) but |
| 15 | part of the test is performed on the host machine (e.g. running an |
| 16 | executable to analyze a video downloaded from a device). |
| 17 | |
| 18 | The script has only one (mandatory) option: --executable_name, which is |
| 19 | the output name of the GN executable. For example, if you have the |
| 20 | following executable in your out folder: |
| 21 | |
| 22 | out/Debug/random_exec |
| 23 | |
| 24 | You will be able to compile the same executable targeting your host machine |
| 25 | by running: |
| 26 | |
| 27 | $ python tools_webrtc/executable_host_build.py --executable_name random_exec |
| 28 | |
| 29 | The generated executable will have the same name as the input executable with |
| 30 | suffix '_host'. |
| 31 | |
| 32 | This 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 | |
| 45 | The executable for the host machine will be generated in the GN out directory |
| 46 | (e.g. out/Debug in the previous example). |
| 47 | """ |
| 48 | |
| 49 | from contextlib import contextmanager |
| 50 | |
| 51 | import argparse |
| 52 | import os |
| 53 | import shutil |
| 54 | import subprocess |
| 55 | import sys |
| 56 | import tempfile |
| 57 | |
| 58 | |
| 59 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 60 | SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir)) |
| 61 | sys.path.append(os.path.join(SRC_DIR, 'build')) |
| 62 | import find_depot_tools |
| 63 | |
| 64 | |
| 65 | def _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 |
| 76 | def HostBuildDir(): |
| 77 | temp_dir = tempfile.mkdtemp() |
| 78 | try: |
| 79 | yield temp_dir |
| 80 | finally: |
| 81 | shutil.rmtree(temp_dir) |
| 82 | |
| 83 | |
| 84 | def _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 | |
| 89 | def DepotToolPath(*args): |
| 90 | return os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, *args) |
| 91 | |
| 92 | |
| 93 | if __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) |