1#!/usr/bin/env python
2# Copyright (c) 2017 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"""
11This scripts tests creating an Android Studio project using the
12generate_gradle.py script and making a debug build using it.
13
14It expect to be given the webrtc output build directory as the first argument
15all other arguments are optional.
16"""
17
18import argparse
19import logging
20import os
21import shutil
22import subprocess
23import sys
24import tempfile
25
26
27SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
28SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
29GENERATE_GRADLE_SCRIPT = os.path.join(SRC_DIR,
30                                      'build/android/gradle/generate_gradle.py')
31GRADLEW_BIN = os.path.join(SCRIPT_DIR, 'third_party/gradle/gradlew')
32
33
34def _RunCommand(argv, cwd=SRC_DIR, **kwargs):
35  logging.info('Running %r', argv)
36  subprocess.check_call(argv, cwd=cwd, **kwargs)
37
38
39def _ParseArgs():
40  parser = argparse.ArgumentParser(
41      description='Test generating Android gradle project.')
42  parser.add_argument('build_dir_android',
43      help='The path to the build directory for Android.')
44  parser.add_argument('--project_dir',
45      help='A temporary directory to put the output.')
46
47  args = parser.parse_args()
48  return args
49
50
51def main():
52  logging.basicConfig(level=logging.INFO)
53  args = _ParseArgs()
54
55  project_dir = args.project_dir
56  if not project_dir:
57    project_dir = tempfile.mkdtemp()
58
59  output_dir = os.path.abspath(args.build_dir_android)
60  project_dir = os.path.abspath(project_dir)
61
62  try:
63    env = os.environ.copy()
64    env['PATH'] = os.pathsep.join([
65        os.path.join(SRC_DIR, 'third_party', 'depot_tools'), env.get('PATH', '')
66    ])
67    _RunCommand([GENERATE_GRADLE_SCRIPT, '--output-directory', output_dir,
68        '--target', '//examples:AppRTCMobile',
69        '--project-dir', project_dir,
70        '--use-gradle-process-resources', '--split-projects'],
71        env=env)
72    _RunCommand([GRADLEW_BIN, 'assembleDebug'], project_dir)
73  finally:
74    # Do not delete temporary directory if user specified it manually.
75    if not args.project_dir:
76      shutil.rmtree(project_dir, True)
77
78
79if __name__ == '__main__':
80  sys.exit(main())
81