def _GenerateGradleFile(build_config, config_json, java_dirs, relativize, use_gradle_process_resources): """Returns the data for a project's build.gradle.""" deps_info = build_config['deps_info'] gradle = build_config['gradle'] if deps_info['type'] == 'android_apk': target_type = 'android_apk' elif deps_info['type'] == 'java_library' and not deps_info['is_prebuilt']: if deps_info['requires_android']: target_type = 'android_library' else: target_type = 'java_library' else: return None variables = {} variables['template_type'] = target_type variables['use_gradle_process_resources'] = use_gradle_process_resources variables['build_tools_version'] = config_json['build_tools_version'] variables['compile_sdk_version'] = config_json['compile_sdk_version'] android_manifest = gradle.get('android_manifest', _DEFAULT_ANDROID_MANIFEST_PATH) variables['android_manifest'] = relativize(android_manifest) variables['java_dirs'] = relativize(java_dirs) variables['prebuilts'] = relativize(gradle['dependent_prebuilt_jars']) deps = [ _ProjectEntry.FromBuildConfigPath(p) for p in gradle['dependent_android_projects'] ] variables['android_project_deps'] = [d.ProjectName() for d in deps] deps = [ _ProjectEntry.FromBuildConfigPath(p) for p in gradle['dependent_java_projects'] ] variables['java_project_deps'] = [d.ProjectName() for d in deps] processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) return processor.Render(_JINJA_TEMPLATE_PATH, variables)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--output-directory', help='Path to the root build directory.') parser.add_argument('-v', '--verbose', dest='verbose_count', default=0, action='count', help='Verbose level') parser.add_argument( '--target', dest='targets', action='append', help='GN target to generate project for. Replaces set of ' 'default targets. May be repeated.') parser.add_argument( '--extra-target', dest='extra_targets', action='append', help='GN target to generate project for, in addition to ' 'the default ones. May be repeated.') parser.add_argument('--project-dir', help='Root of the output project.', default=os.path.join('$CHROMIUM_OUTPUT_DIR', 'gradle')) parser.add_argument('--all', action='store_true', help='Include all .java files reachable from any ' 'apk/test/binary target. On by default unless ' '--split-projects is used (--split-projects can ' 'slow down Studio given too many targets).') parser.add_argument('--use-gradle-process-resources', action='store_true', help='Have gradle generate R.java rather than ninja') parser.add_argument('--split-projects', action='store_true', help='Split projects by their gn deps rather than ' 'combining all the dependencies of each target') version_group = parser.add_mutually_exclusive_group() version_group.add_argument( '--beta', action='store_true', help='Generate a project that is compatible with ' 'Android Studio Beta.') version_group.add_argument( '--canary', action='store_true', help='Generate a project that is compatible with ' 'Android Studio Canary.') sdk_group = parser.add_mutually_exclusive_group() sdk_group.add_argument( '--sdk', choices=[ 'AndroidStudioCurrent', 'AndroidStudioDefault', 'ChromiumSdkRoot' ], default='ChromiumSdkRoot', help="Set the project's SDK root. This can be set to " "Android Studio's current SDK root, the default " "Android Studio SDK root, or Chromium's SDK " "root. The default is Chromium's SDK root, but " "using this means that updates and additions to " "the SDK (e.g. installing emulators), will " "modify this root, hence possibly causing " "conflicts on the next repository sync.") sdk_group.add_argument( '--sdk-path', help='An explict path for the SDK root, setting this ' 'is an alternative to setting the --sdk option') args = parser.parse_args() if args.output_directory: constants.SetOutputDirectory(args.output_directory) constants.CheckOutputDirectory() output_dir = constants.GetOutDirectory() devil_chromium.Initialize(output_directory=output_dir) run_tests_helper.SetLogLevel(args.verbose_count) if args.use_gradle_process_resources: assert args.split_projects, ( 'Gradle resources does not work without --split-projects.') _gradle_output_dir = os.path.abspath( args.project_dir.replace('$CHROMIUM_OUTPUT_DIR', output_dir)) jinja_processor = jinja_template.JinjaProcessor(_FILE_DIR) build_vars = _ReadPropertiesFile(os.path.join(output_dir, 'build_vars.txt')) source_properties = _ReadPropertiesFile( _RebasePath( os.path.join(build_vars['android_sdk_build_tools'], 'source.properties'))) if args.beta: channel = 'beta' elif args.canary: channel = 'canary' else: channel = 'stable' generator = _ProjectContextGenerator(_gradle_output_dir, build_vars, args.use_gradle_process_resources, jinja_processor, args.split_projects, channel) logging.warning('Creating project at: %s', generator.project_dir) # Generate for "all targets" by default when not using --split-projects (too # slow), and when no --target has been explicitly set. "all targets" means all # java targets that are depended on by an apk or java_binary (leaf # java_library targets will not be included). args.all = args.all or (not args.split_projects and not args.targets) targets_from_args = set(args.targets or _DEFAULT_TARGETS) if args.extra_targets: targets_from_args.update(args.extra_targets) if args.all: # Run GN gen if necessary (faster than running "gn gen" in the no-op case). _RunNinja(constants.GetOutDirectory(), ['build.ninja']) # Query ninja for all __build_config targets. targets = _QueryForAllGnTargets(output_dir) else: targets = [ re.sub(r'_test_apk$', '_test_apk__apk', t) for t in targets_from_args ] main_entries = [_ProjectEntry.FromGnTarget(t) for t in targets] logging.warning('Building .build_config files...') _RunNinja(output_dir, [e.NinjaBuildConfigTarget() for e in main_entries]) if args.all: # There are many unused libraries, so restrict to those that are actually # used by apks/binaries/tests or that are explicitly mentioned in --targets. main_entries = [ e for e in main_entries if (e.GetType() in ('android_apk', 'java_binary', 'junit_binary') or e.GnTarget() in targets_from_args or e.GnTarget().endswith('_test_apk__apk')) ] if args.split_projects: main_entries = _FindAllProjectEntries(main_entries) logging.info('Generating for %d targets.', len(main_entries)) entries = [e for e in _CombineTestEntries(main_entries) if e.IsValid()] logging.info('Creating %d projects for targets.', len(entries)) # When only one entry will be generated we want it to have a valid # build.gradle file with its own AndroidManifest. add_all_module = not args.split_projects and len(entries) > 1 logging.warning('Writing .gradle files...') project_entries = [] zip_tuples = [] generated_inputs = [] for entry in entries: data = _GenerateGradleFile(entry, generator, build_vars, source_properties, jinja_processor) if data: # Build all paths references by .gradle that exist within output_dir. generated_inputs.extend(generator.GeneratedInputs(entry)) zip_tuples.extend(( s, os.path.join(generator.EntryOutputDir(entry), _SRCJARS_SUBDIR)) for s in generator.AllSrcjars(entry)) zip_tuples.extend( (s, os.path.join(generator.EntryOutputDir(entry), _RES_SUBDIR)) for s in generator.AllResZips(entry)) if not add_all_module: project_entries.append(entry) _WriteFile( os.path.join(generator.EntryOutputDir(entry), _GRADLE_BUILD_FILE), data) if add_all_module: _GenerateModuleAll(_gradle_output_dir, generator, build_vars, source_properties, jinja_processor) _WriteFile(os.path.join(generator.project_dir, _GRADLE_BUILD_FILE), _GenerateRootGradle(jinja_processor, channel)) _WriteFile(os.path.join(generator.project_dir, 'settings.gradle'), _GenerateSettingsGradle(project_entries, add_all_module)) if args.sdk != "AndroidStudioCurrent": if args.sdk_path: sdk_path = _RebasePath(args.sdk_path) elif args.sdk == "AndroidStudioDefault": sdk_path = os.path.expanduser('~/Android/Sdk') else: sdk_path = _RebasePath(build_vars['android_sdk_root']) _WriteFile(os.path.join(generator.project_dir, 'local.properties'), _GenerateLocalProperties(sdk_path)) if generated_inputs: logging.warning('Building generated source files...') targets = _RebasePath(generated_inputs, output_dir) _RunNinja(output_dir, targets) if zip_tuples: _ExtractZips(generator.project_dir, zip_tuples) logging.warning('Project created!') logging.warning('Generated projects work with Android Studio %s', channel) logging.warning('For more tips: https://chromium.googlesource.com/chromium' '/src.git/+/master/docs/android_studio.md')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--output-directory', help='Path to the root build directory.') parser.add_argument('-v', '--verbose', dest='verbose_count', default=0, action='count', help='Verbose level') parser.add_argument( '--target', dest='targets', action='append', help='GN target to generate project for. Replaces set of ' 'default targets. May be repeated.') parser.add_argument( '--extra-target', dest='extra_targets', action='append', help='GN target to generate project for, in addition to ' 'the default ones. May be repeated.') parser.add_argument('--project-dir', help='Root of the output project.', default=os.path.join('$CHROMIUM_OUTPUT_DIR', 'gradle')) parser.add_argument('--all', action='store_true', help='Include all .java files reachable from any ' 'apk/test/binary target. On by default unless ' '--split-projects is used (--split-projects can ' 'slow down Studio given too many targets).') parser.add_argument('--use-gradle-process-resources', action='store_true', help='Have gradle generate R.java rather than ninja') parser.add_argument('--split-projects', action='store_true', help='Split projects by their gn deps rather than ' 'combining all the dependencies of each target') parser.add_argument('--native-target', dest='native_targets', action='append', help='GN native targets to generate for. May be ' 'repeated.') parser.add_argument( '--compile-sdk-version', type=int, default=0, help='Override compileSdkVersion for android sdk docs. ' 'Useful when sources for android_sdk_version is ' 'not available in Android Studio.') parser.add_argument('--sdk-path', default=os.path.expanduser('~/Android/Sdk'), help='The path to use as the SDK root, overrides the ' 'default at ~/Android/Sdk.') version_group = parser.add_mutually_exclusive_group() version_group.add_argument( '--beta', action='store_true', help='Generate a project that is compatible with ' 'Android Studio Beta.') version_group.add_argument( '--canary', action='store_true', help='Generate a project that is compatible with ' 'Android Studio Canary.') args = parser.parse_args() if args.output_directory: constants.SetOutputDirectory(args.output_directory) constants.CheckOutputDirectory() output_dir = constants.GetOutDirectory() devil_chromium.Initialize(output_directory=output_dir) run_tests_helper.SetLogLevel(args.verbose_count) if args.use_gradle_process_resources: assert args.split_projects, ( 'Gradle resources does not work without --split-projects.') _gradle_output_dir = os.path.abspath( args.project_dir.replace('$CHROMIUM_OUTPUT_DIR', output_dir)) logging.warning('Creating project at: %s', _gradle_output_dir) # Generate for "all targets" by default when not using --split-projects (too # slow), and when no --target has been explicitly set. "all targets" means all # java targets that are depended on by an apk or java_binary (leaf # java_library targets will not be included). args.all = args.all or (not args.split_projects and not args.targets) targets_from_args = set(args.targets or _DEFAULT_TARGETS) if args.extra_targets: targets_from_args.update(args.extra_targets) if args.all: if args.native_targets: _RunGnGen(output_dir, ['--ide=json']) elif not os.path.exists(os.path.join(output_dir, 'build.ninja')): _RunGnGen(output_dir) else: # Faster than running "gn gen" in the no-op case. _RunNinja(output_dir, ['build.ninja']) # Query ninja for all __build_config_crbug_908819 targets. targets = _QueryForAllGnTargets(output_dir) else: assert not args.native_targets, 'Native editing requires --all.' targets = [ re.sub(r'_test_apk$', _INSTRUMENTATION_TARGET_SUFFIX, t) for t in targets_from_args ] # Necessary after "gn clean" if not os.path.exists(os.path.join(output_dir, 'build_vars.txt')): _RunGnGen(output_dir) build_vars = _ReadPropertiesFile(os.path.join(output_dir, 'build_vars.txt')) jinja_processor = jinja_template.JinjaProcessor(_FILE_DIR) if args.beta: channel = 'beta' elif args.canary: channel = 'canary' else: channel = 'stable' if args.compile_sdk_version: build_vars['compile_sdk_version'] = args.compile_sdk_version else: build_vars['compile_sdk_version'] = build_vars['android_sdk_version'] generator = _ProjectContextGenerator(_gradle_output_dir, build_vars, args.use_gradle_process_resources, jinja_processor, args.split_projects, channel) main_entries = [_ProjectEntry.FromGnTarget(t) for t in targets] if args.all: # There are many unused libraries, so restrict to those that are actually # used by apks/bundles/binaries/tests or that are explicitly mentioned in # --targets. BASE_TYPES = ('android_apk', 'android_app_bundle_module', 'java_binary', 'junit_binary') main_entries = [ e for e in main_entries if (e.GetType() in BASE_TYPES or e.GnTarget() in targets_from_args or e.GnTarget().endswith(_INSTRUMENTATION_TARGET_SUFFIX)) ] if args.split_projects: main_entries = _FindAllProjectEntries(main_entries) logging.info('Generating for %d targets.', len(main_entries)) entries = [e for e in _CombineTestEntries(main_entries) if e.IsValid()] logging.info('Creating %d projects for targets.', len(entries)) logging.warning('Writing .gradle files...') project_entries = [] # When only one entry will be generated we want it to have a valid # build.gradle file with its own AndroidManifest. for entry in entries: data = _GenerateGradleFile(entry, generator, build_vars, jinja_processor) if data and not args.all: project_entries.append((entry.ProjectName(), entry.GradleSubdir())) _WriteFile( os.path.join(generator.EntryOutputDir(entry), _GRADLE_BUILD_FILE), data) if args.all: project_entries.append((_MODULE_ALL, _MODULE_ALL)) _GenerateModuleAll(_gradle_output_dir, generator, build_vars, jinja_processor, args.native_targets) _WriteFile(os.path.join(generator.project_dir, _GRADLE_BUILD_FILE), _GenerateRootGradle(jinja_processor, channel)) _WriteFile(os.path.join(generator.project_dir, 'settings.gradle'), _GenerateSettingsGradle(project_entries)) # Ensure the Android Studio sdk is correctly initialized. if not os.path.exists(args.sdk_path): # Help first-time users avoid Android Studio forcibly changing back to # the previous default due to not finding a valid sdk under this dir. shutil.copytree(_RebasePath(build_vars['android_sdk_root']), args.sdk_path) _WriteFile(os.path.join(generator.project_dir, 'local.properties'), _GenerateLocalProperties(args.sdk_path)) _WriteFile(os.path.join(generator.project_dir, 'gradle.properties'), _GenerateGradleProperties()) wrapper_properties = os.path.join(generator.project_dir, 'gradle', 'wrapper', 'gradle-wrapper.properties') if os.path.exists(wrapper_properties): os.unlink(wrapper_properties) if args.canary: _WriteFile(wrapper_properties, _GenerateGradleWrapperPropertiesCanary()) generated_inputs = set() for entry in entries: entries_to_gen = [entry] entries_to_gen.extend(entry.android_test_entries) for entry_to_gen in entries_to_gen: # Build all paths references by .gradle that exist within output_dir. generated_inputs.update(generator.GeneratedInputs(entry_to_gen)) if generated_inputs: targets = _RebasePath(generated_inputs, output_dir) _RunNinja(output_dir, targets) logging.warning( 'Generated files will only appear once you\'ve built them.') logging.warning('Generated projects for Android Studio %s', channel) logging.warning('For more tips: https://chromium.googlesource.com/chromium' '/src.git/+/master/docs/android_studio.md')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--output-directory', help='Path to the root build directory.') parser.add_argument('-v', '--verbose', dest='verbose_count', default=0, action='count', help='Verbose level') parser.add_argument( '--target', dest='targets', action='append', help='GN target to generate project for. Replaces set of ' 'default targets. May be repeated.') parser.add_argument( '--extra-target', dest='extra_targets', action='append', help='GN target to generate project for, in addition to ' 'the default ones. May be repeated.') parser.add_argument('--project-dir', help='Root of the output project.', default=os.path.join('$CHROMIUM_OUTPUT_DIR', 'gradle')) parser.add_argument('--all', action='store_true', help='Generate all java targets (slows down IDE)') parser.add_argument('--use-gradle-process-resources', action='store_true', help='Have gradle generate R.java rather than ninja') parser.add_argument('--split-projects', action='store_true', help='Split projects by their gn deps rather than ' 'combining all the dependencies of each target') args = parser.parse_args() if args.output_directory: constants.SetOutputDirectory(args.output_directory) constants.CheckOutputDirectory() output_dir = constants.GetOutDirectory() devil_chromium.Initialize(output_directory=output_dir) run_tests_helper.SetLogLevel(args.verbose_count) if args.use_gradle_process_resources: assert args.split_projects, ( 'Gradle resources does not work without --split-projects.') _gradle_output_dir = os.path.abspath( args.project_dir.replace('$CHROMIUM_OUTPUT_DIR', output_dir)) jinja_processor = jinja_template.JinjaProcessor(_FILE_DIR) build_vars = _ReadPropertiesFile(os.path.join(output_dir, 'build_vars.txt')) source_properties = _ReadPropertiesFile( _RebasePath( os.path.join(build_vars['android_sdk_build_tools'], 'source.properties'))) generator = _ProjectContextGenerator(_gradle_output_dir, build_vars, args.use_gradle_process_resources, jinja_processor, args.split_projects) logging.warning('Creating project at: %s', generator.project_dir) if args.all: # Run GN gen if necessary (faster than running "gn gen" in the no-op case). _RunNinja(constants.GetOutDirectory(), ['build.ninja']) # Query ninja for all __build_config targets. targets = _QueryForAllGnTargets(output_dir) else: targets = args.targets or _DEFAULT_TARGETS if args.extra_targets: targets.extend(args.extra_targets) targets = [re.sub(r'_test_apk$', '_test_apk__apk', t) for t in targets] # TODO(wnwen): Utilize Gradle's test constructs for our junit tests? targets = [ re.sub(r'_junit_tests$', '_junit_tests__java_binary', t) for t in targets ] main_entries = [_ProjectEntry.FromGnTarget(t) for t in targets] logging.warning('Building .build_config files...') _RunNinja(output_dir, [e.NinjaBuildConfigTarget() for e in main_entries]) # There are many unused libraries, so restrict to those that are actually used # when using --all. if args.all: main_entries = [ e for e in main_entries if (e.GetType() == 'android_apk' or e.GnTarget().endswith('_test_apk__apk') or e.GnTarget().endswith('_junit_tests__java_binary')) ] if args.split_projects: main_entries = _FindAllProjectEntries(main_entries) logging.info('Found %d dependent build_config targets.', len(main_entries)) entries = [e for e in _CombineTestEntries(main_entries) if e.IsValid()] logging.info('Creating %d projects for targets.', len(entries)) # When only one entry will be generated we want it to have a valid # build.gradle file with its own AndroidManifest. add_all_module = not args.split_projects and len(entries) > 1 logging.warning('Writing .gradle files...') project_entries = [] zip_tuples = [] generated_inputs = [] for entry in entries: data = _GenerateGradleFile(entry, generator, build_vars, source_properties, jinja_processor) if data: # Build all paths references by .gradle that exist within output_dir. generated_inputs.extend(generator.GeneratedInputs(entry)) zip_tuples.extend(( s, os.path.join(generator.EntryOutputDir(entry), _SRCJARS_SUBDIR)) for s in generator.AllSrcjars(entry)) zip_tuples.extend( (s, os.path.join(generator.EntryOutputDir(entry), _RES_SUBDIR)) for s in generator.AllResZips(entry)) if not add_all_module: project_entries.append(entry) _WriteFile( os.path.join(generator.EntryOutputDir(entry), _GRADLE_BUILD_FILE), data) if add_all_module: _GenerateModuleAll(_gradle_output_dir, generator, build_vars, source_properties, jinja_processor) _WriteFile(os.path.join(generator.project_dir, _GRADLE_BUILD_FILE), _GenerateRootGradle(jinja_processor)) _WriteFile(os.path.join(generator.project_dir, 'settings.gradle'), _GenerateSettingsGradle(project_entries, add_all_module)) sdk_path = _RebasePath(build_vars['android_sdk_root']) _WriteFile(os.path.join(generator.project_dir, 'local.properties'), _GenerateLocalProperties(sdk_path)) if generated_inputs: logging.warning('Building generated source files...') targets = _RebasePath(generated_inputs, output_dir) _RunNinja(output_dir, targets) if zip_tuples: _ExtractZips(generator.project_dir, zip_tuples) logging.warning('Project created!') logging.warning('Generated projects work with Android Studio 2.3') logging.warning('For more tips: https://chromium.googlesource.com/chromium' '/src.git/+/master/docs/android_studio.md')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--output-directory', help='Path to the root build directory.') parser.add_argument('-v', '--verbose', dest='verbose_count', default=0, action='count', help='Verbose level') parser.add_argument( '--target', dest='targets', action='append', help='GN target to generate project for. Replaces set of ' 'default targets. May be repeated.') parser.add_argument( '--extra-target', dest='extra_targets', action='append', help='GN target to generate project for, in addition to ' 'the default ones. May be repeated.') parser.add_argument('--project-dir', help='Root of the output project.', default=os.path.join('$CHROMIUM_OUTPUT_DIR', 'gradle')) parser.add_argument('--all', action='store_true', help='Include all .java files reachable from any ' 'apk/test/binary target. On by default unless ' '--split-projects is used (--split-projects can ' 'slow down Studio given too many targets).') parser.add_argument('--use-gradle-process-resources', action='store_true', help='Have gradle generate R.java rather than ninja') parser.add_argument('--split-projects', action='store_true', help='Split projects by their gn deps rather than ' 'combining all the dependencies of each target') parser.add_argument( '--fast', action='store_true', help='Skip generating R.java and other generated files.') parser.add_argument('-j', default=1000 if os.path.exists(_SRC_INTERNAL) else 50, help='Value for number of parallel jobs for ninja') parser.add_argument('--native-target', dest='native_targets', action='append', help='GN native targets to generate for. May be ' 'repeated.') version_group = parser.add_mutually_exclusive_group() version_group.add_argument( '--beta', action='store_true', help='Generate a project that is compatible with ' 'Android Studio Beta.') version_group.add_argument( '--canary', action='store_true', help='Generate a project that is compatible with ' 'Android Studio Canary.') sdk_group = parser.add_mutually_exclusive_group() sdk_group.add_argument( '--sdk', choices=[ 'AndroidStudioCurrent', 'AndroidStudioDefault', 'ChromiumSdkRoot' ], default='AndroidStudioDefault', help="Set the project's SDK root. This can be set to " "Android Studio's current SDK root, the default " "Android Studio SDK root, or Chromium's SDK " "root in //third_party. The default is Android " "Studio's SDK root in ~/Android/Sdk.") sdk_group.add_argument( '--sdk-path', help='An explict path for the SDK root, setting this ' 'is an alternative to setting the --sdk option') args = parser.parse_args() if args.output_directory: constants.SetOutputDirectory(args.output_directory) constants.CheckOutputDirectory() output_dir = constants.GetOutDirectory() devil_chromium.Initialize(output_directory=output_dir) run_tests_helper.SetLogLevel(args.verbose_count) if args.use_gradle_process_resources: assert args.split_projects, ( 'Gradle resources does not work without --split-projects.') _gradle_output_dir = os.path.abspath( args.project_dir.replace('$CHROMIUM_OUTPUT_DIR', output_dir)) logging.warning('Creating project at: %s', _gradle_output_dir) # Generate for "all targets" by default when not using --split-projects (too # slow), and when no --target has been explicitly set. "all targets" means all # java targets that are depended on by an apk or java_binary (leaf # java_library targets will not be included). args.all = args.all or (not args.split_projects and not args.targets) targets_from_args = set(args.targets or _DEFAULT_TARGETS) if args.extra_targets: targets_from_args.update(args.extra_targets) if args.all: if args.native_targets: _RunGnGen(output_dir, ['--ide=json']) elif not os.path.exists(os.path.join(output_dir, 'build.ninja')): _RunGnGen(output_dir) else: # Faster than running "gn gen" in the no-op case. _RunNinja(output_dir, ['build.ninja'], args.j) # Query ninja for all __build_config targets. targets = _QueryForAllGnTargets(output_dir) else: assert not args.native_targets, 'Native editing requires --all.' targets = [ re.sub(r'_test_apk$', '_test_apk__apk', t) for t in targets_from_args ] # Necessary after "gn clean" if not os.path.exists(os.path.join(output_dir, 'build_vars.txt')): _RunGnGen(output_dir) build_vars = _ReadPropertiesFile(os.path.join(output_dir, 'build_vars.txt')) jinja_processor = jinja_template.JinjaProcessor(_FILE_DIR) if args.beta: channel = 'beta' elif args.canary: channel = 'canary' else: channel = 'stable' generator = _ProjectContextGenerator(_gradle_output_dir, build_vars, args.use_gradle_process_resources, jinja_processor, args.split_projects, channel) main_entries = [_ProjectEntry.FromGnTarget(t) for t in targets] logging.warning('Building .build_config files...') _RunNinja(output_dir, [e.NinjaBuildConfigTarget() for e in main_entries], args.j) if args.all: # There are many unused libraries, so restrict to those that are actually # used by apks/binaries/tests or that are explicitly mentioned in --targets. main_entries = [ e for e in main_entries if (e.GetType() in ('android_apk', 'java_binary', 'junit_binary') or e.GnTarget() in targets_from_args or e.GnTarget().endswith('_test_apk__apk')) ] if args.split_projects: main_entries = _FindAllProjectEntries(main_entries) logging.info('Generating for %d targets.', len(main_entries)) entries = [e for e in _CombineTestEntries(main_entries) if e.IsValid()] logging.info('Creating %d projects for targets.', len(entries)) logging.warning('Writing .gradle files...') source_properties = _ReadPropertiesFile( _RebasePath( os.path.join(build_vars['android_sdk_build_tools'], 'source.properties'))) project_entries = [] # When only one entry will be generated we want it to have a valid # build.gradle file with its own AndroidManifest. for entry in entries: data = _GenerateGradleFile(entry, generator, build_vars, source_properties, jinja_processor) if data and not args.all: project_entries.append((entry.ProjectName(), entry.GradleSubdir())) _WriteFile( os.path.join(generator.EntryOutputDir(entry), _GRADLE_BUILD_FILE), data) if args.all: project_entries.append((_MODULE_ALL, _MODULE_ALL)) _GenerateModuleAll(_gradle_output_dir, generator, build_vars, source_properties, jinja_processor, args.native_targets) _WriteFile(os.path.join(generator.project_dir, _GRADLE_BUILD_FILE), _GenerateRootGradle(jinja_processor, channel)) _WriteFile(os.path.join(generator.project_dir, 'settings.gradle'), _GenerateSettingsGradle(project_entries)) if args.sdk != "AndroidStudioCurrent": if args.sdk_path: sdk_path = _RebasePath(args.sdk_path) elif args.sdk == "AndroidStudioDefault": sdk_path = os.path.expanduser('~/Android/Sdk') if not os.path.exists(sdk_path): # Help first-time users avoid Android Studio forcibly changing back to # the previous default due to not finding a valid sdk under this dir. shutil.copytree(_RebasePath(build_vars['android_sdk_root']), sdk_path) else: sdk_path = _RebasePath(build_vars['android_sdk_root']) _WriteFile(os.path.join(generator.project_dir, 'local.properties'), _GenerateLocalProperties(sdk_path)) if not args.fast: zip_tuples = [] generated_inputs = set() for entry in entries: # Build all paths references by .gradle that exist within output_dir. generated_inputs.update(generator.GeneratedInputs(entry)) zip_tuples.extend(generator.GeneratedZips(entry)) if generated_inputs: logging.warning('Building generated source files...') targets = _RebasePath(generated_inputs, output_dir) _RunNinja(output_dir, targets, args.j) if zip_tuples: _ExtractZips(generator.project_dir, zip_tuples) logging.warning('Generated projects for Android Studio %s', channel) if not args.fast: logging.warning( 'Run with --fast flag to skip generating files (faster, ' 'but less correct)') logging.warning('For more tips: https://chromium.googlesource.com/chromium' '/src.git/+/master/docs/android_studio.md')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--output-directory', help='Path to the root build directory.') parser.add_argument('-v', '--verbose', dest='verbose_count', default=0, action='count', help='Verbose level') parser.add_argument('--target', dest='targets', action='append', help='GN target to generate project for. ' 'May be repeated.') parser.add_argument('--project-dir', help='Root of the output project.', default=os.path.join('$CHROMIUM_OUTPUT_DIR', 'gradle')) parser.add_argument('--all', action='store_true', help='Generate all java targets (slows down IDE)') parser.add_argument('--use-gradle-process-resources', action='store_true', help='Have gradle generate R.java rather than ninja') args = parser.parse_args() if args.output_directory: constants.SetOutputDirectory(args.output_directory) constants.CheckOutputDirectory() output_dir = constants.GetOutDirectory() devil_chromium.Initialize(output_directory=output_dir) run_tests_helper.SetLogLevel(args.verbose_count) _gradle_output_dir = os.path.abspath( args.project_dir.replace('$CHROMIUM_OUTPUT_DIR', output_dir)) generator = _ProjectContextGenerator(_gradle_output_dir, args.use_gradle_process_resources) logging.warning('Creating project at: %s', generator.project_dir) if args.all: # Run GN gen if necessary (faster than running "gn gen" in the no-op case). _RunNinja(constants.GetOutDirectory(), ['build.ninja']) # Query ninja for all __build_config targets. targets = _QueryForAllGnTargets(output_dir) else: targets = args.targets or _DEFAULT_TARGETS targets = [re.sub(r'_test_apk$', '_test_apk__apk', t) for t in targets] # TODO(wnwen): Utilize Gradle's test constructs for our junit tests? targets = [ re.sub(r'_junit_tests$', '_junit_tests__java_binary', t) for t in targets ] main_entries = [_ProjectEntry(t) for t in targets] logging.warning('Building .build_config files...') _RunNinja(output_dir, [e.NinjaBuildConfigTarget() for e in main_entries]) # There are many unused libraries, so restrict to those that are actually used # when using --all. if args.all: main_entries = [ e for e in main_entries if e.GetType() == 'android_apk' ] all_entries = _FindAllProjectEntries(main_entries) logging.info('Found %d dependent build_config targets.', len(all_entries)) entries = _CombineTestEntries(all_entries) logging.info('Creating %d projects for targets.', len(entries)) logging.warning('Writing .gradle files...') jinja_processor = jinja_template.JinjaProcessor(_FILE_DIR) build_vars = _ReadBuildVars(output_dir) project_entries = [] srcjar_tuples = [] generated_inputs = [] for entry in entries: if entry.GetType() not in ('android_apk', 'java_library', 'java_binary'): continue data = _GenerateGradleFile(entry, generator, build_vars, jinja_processor) if data: project_entries.append(entry) # Build all paths references by .gradle that exist within output_dir. generated_inputs.extend(generator.GeneratedInputs(entry)) srcjar_tuples.extend(( s, os.path.join(generator.EntryOutputDir(entry), _SRCJARS_SUBDIR)) for s in generator.Srcjars(entry)) _WriteFile( os.path.join(generator.EntryOutputDir(entry), 'build.gradle'), data) _WriteFile(os.path.join(generator.project_dir, 'build.gradle'), _GenerateRootGradle(jinja_processor)) _WriteFile(os.path.join(generator.project_dir, 'settings.gradle'), _GenerateSettingsGradle(project_entries)) sdk_path = _RebasePath(build_vars['android_sdk_root']) _WriteFile(os.path.join(generator.project_dir, 'local.properties'), _GenerateLocalProperties(sdk_path)) if generated_inputs: logging.warning('Building generated source files...') targets = _RebasePath(generated_inputs, output_dir) _RunNinja(output_dir, targets) if srcjar_tuples: _ExtractSrcjars(generator.project_dir, srcjar_tuples) logging.warning('Project created! (%d subprojects)', len(project_entries)) logging.warning('Generated projects work best with Android Studio 2.2') logging.warning('For more tips: https://chromium.googlesource.com/chromium' '/src.git/+/master/docs/android_studio.md')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--output-directory', help='Path to the root build directory.') parser.add_argument('-v', '--verbose', dest='verbose_count', default=0, action='count', help='Verbose level') parser.add_argument('--target', dest='targets', action='append', help='GN target to generate project for. ' 'May be repeated.') parser.add_argument('--project-dir', help='Root of the output project.', default=os.path.join('$CHROMIUM_OUTPUT_DIR', 'gradle')) parser.add_argument('--all', action='store_true', help='Generate all java targets (slows down IDE)') parser.add_argument('--use-gradle-process-resources', action='store_true', help='Have gradle generate R.java rather than ninja') args = parser.parse_args() if args.output_directory: constants.SetOutputDirectory(args.output_directory) constants.CheckOutputDirectory() output_dir = constants.GetOutDirectory() devil_chromium.Initialize(output_directory=output_dir) run_tests_helper.SetLogLevel(args.verbose_count) gradle_output_dir = os.path.abspath( args.project_dir.replace('$CHROMIUM_OUTPUT_DIR', output_dir)) logging.warning('Creating project at: %s', gradle_output_dir) if args.all: # Run GN gen if necessary (faster than running "gn gen" in the no-op case). _RunNinja(output_dir, ['build.ninja']) # Query ninja for all __build_config targets. targets = _QueryForAllGnTargets(output_dir) else: targets = args.targets or _DEFAULT_TARGETS # TODO(agrieve): See if it makes sense to utilize Gradle's test constructs # for our instrumentation tests. targets = [re.sub(r'_test_apk$', '_test_apk__apk', t) for t in targets] main_entries = [_ProjectEntry(t) for t in targets] logging.warning('Building .build_config files...') _RunNinja(output_dir, [e.NinjaBuildConfigTarget() for e in main_entries]) # There are many unused libraries, so restrict to those that are actually used # when using --all. if args.all: main_entries = [e for e in main_entries if e.GetType() == 'android_apk'] all_entries = _FindAllProjectEntries(main_entries) logging.info('Found %d dependent build_config targets.', len(all_entries)) logging.warning('Writing .gradle files...') jinja_processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) config_json = build_utils.ReadJson( os.path.join(output_dir, 'gradle', 'config.json')) project_entries = [] srcjar_tuples = [] for entry in all_entries: if entry.GetType() not in ('android_apk', 'java_library'): continue entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) build_config = entry.BuildConfig() srcjars = _RebasePath(build_config['gradle'].get('bundled_srcjars', [])) if not args.use_gradle_process_resources: srcjars += _RebasePath(build_config['javac']['srcjars']) java_sources_file = build_config['gradle'].get('java_sources_file') if java_sources_file: java_sources_file = _RebasePath(java_sources_file) java_dirs = _CreateJavaSourceDir(output_dir, entry_output_dir, java_sources_file) if srcjars: java_dirs.append(os.path.join(entry_output_dir, _SRCJARS_SUBDIR)) data = _GenerateGradleFile(build_config, config_json, java_dirs, relativize, args.use_gradle_process_resources, jinja_processor) if data: project_entries.append(entry) srcjar_tuples.extend( (s, os.path.join(entry_output_dir, _SRCJARS_SUBDIR)) for s in srcjars) _WriteFile(os.path.join(entry_output_dir, 'build.gradle'), data) _WriteFile(os.path.join(gradle_output_dir, 'build.gradle'), _GenerateRootGradle(jinja_processor)) _WriteFile(os.path.join(gradle_output_dir, 'settings.gradle'), _GenerateSettingsGradle(project_entries)) sdk_path = _RebasePath(config_json['android_sdk_root']) _WriteFile(os.path.join(gradle_output_dir, 'local.properties'), _GenerateLocalProperties(sdk_path)) if srcjar_tuples: logging.warning('Building all .srcjar files...') targets = _RebasePath([s[0] for s in srcjar_tuples], output_dir) _RunNinja(output_dir, targets) _ExtractSrcjars(gradle_output_dir, srcjar_tuples) logging.warning('Project created! (%d subprojects)', len(project_entries)) logging.warning('Generated projects work best with Android Studio 2.2') logging.warning('For more tips: https://chromium.googlesource.com/chromium' '/src.git/+/master/docs/android_studio.md')
def _GenerateRootGradle(): """Returns the data for the root project's build.gradle.""" variables = {'template_type': 'root'} processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) return processor.Render(_JINJA_TEMPLATE_PATH, variables)