You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.0 KiB

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