Esempio n. 1
0
def _CreateInfoFile(java_files, jar_path, chromium_code, srcjar_files,
                    classes_dir, generated_java_dir, excluded_globs):
    """Writes a .jar.info file.

  This maps fully qualified names for classes to either the java file that they
  are defined in or the path of the srcjar that they came from.
  """
    output_path = jar_path + '.info'
    logging.info('Start creating info file: %s', output_path)
    javac_generated_sources = _MoveGeneratedJavaFilesToGenDir(
        classes_dir, generated_java_dir)
    logging.info('Finished moving generated java files: %s', output_path)
    # 2 processes saves ~0.9s, 3 processes saves ~1.2s, 4 processes saves ~1.2s.
    pool = multiprocessing.Pool(processes=3)
    results = pool.imap_unordered(_ProcessJavaFileForInfo,
                                  itertools.chain(java_files,
                                                  javac_generated_sources),
                                  chunksize=10)
    pool.close()
    all_info_data = {}
    for java_file, package_name, class_names in results:
        source = srcjar_files.get(java_file, java_file)
        for fully_qualified_name in _ProcessInfo(java_file, package_name,
                                                 class_names, source,
                                                 chromium_code):
            if _ShouldIncludeInJarInfo(fully_qualified_name, excluded_globs):
                all_info_data[fully_qualified_name] = java_file
    logging.info('Writing info file: %s', output_path)
    with build_utils.AtomicOutput(output_path) as f:
        jar_info_utils.WriteJarInfoFile(f, all_info_data, srcjar_files)
    logging.info('Completed info file: %s', output_path)
Esempio n. 2
0
def _CreateInfoFile(java_files, options, srcjar_files):
  """Writes a .jar.info file.

  This maps fully qualified names for classes to either the java file that they
  are defined in or the path of the srcjar that they came from.

  For apks this also produces a coalesced .apk.jar.info file combining all the
  .jar.info files of its transitive dependencies.
  """
  info_data = dict()
  for java_file in java_files:
    package_name, class_names = _ParsePackageAndClassNames(java_file)
    for class_name in class_names:
      fully_qualified_name = '{}.{}'.format(package_name, class_name)
      info_data[fully_qualified_name] = java_file
    # Skip aidl srcjars since they don't indent code correctly.
    source = srcjar_files.get(java_file, java_file)
    if '_aidl.srcjar' in source:
      continue
    assert not options.chromium_code or len(class_names) == 1, (
        'Chromium java files must only have one class: {}'.format(source))
    if options.chromium_code:
      _CheckPathMatchesClassName(java_file, package_name, class_names[0])
  with build_utils.AtomicOutput(options.jar_path + '.info') as f:
    jar_info_utils.WriteJarInfoFile(f.name, info_data, srcjar_files)
Esempio n. 3
0
def _MergeInfoFiles(output, jar_paths):
    """Merge several .jar.info files to generate an .apk.jar.info.

  Args:
    output: output file path.
    jar_paths: List of .jar file paths for the target apk.
  """
    info_data = dict()
    for jar_path in jar_paths:
        # android_java_prebuilt adds jar files in the src directory (relative to
        #     the output directory, usually ../../third_party/example.jar).
        # android_aar_prebuilt collects jar files in the aar file and uses the
        #     java_prebuilt rule to generate gen/example/classes.jar files.
        # We scan these prebuilt jars to parse each class path for the FQN. This
        #     allows us to later map these classes back to their respective src
        #     directories.
        jar_info_path = jar_path + '.info'
        if os.path.exists(jar_info_path):
            info_data.update(
                jar_info_utils.ParseJarInfoFile(jar_path + '.info'))
        else:
            with zipfile.ZipFile(jar_path) as zip_info:
                for path in zip_info.namelist():
                    fully_qualified_name = _FullJavaNameFromClassFilePath(path)
                    if fully_qualified_name:
                        info_data[fully_qualified_name] = jar_path

    jar_info_utils.WriteJarInfoFile(output, info_data)
Esempio n. 4
0
def _MergeJarInfoFiles(output, inputs):
    """Merge several .jar.info files to generate an .apk.jar.info.

  Args:
    output: output file path.
    inputs: List of .info.jar or .jar files.
  """
    info_data = dict()
    for path in inputs:
        # android_java_prebuilt adds jar files in the src directory (relative to
        #     the output directory, usually ../../third_party/example.jar).
        # android_aar_prebuilt collects jar files in the aar file and uses the
        #     java_prebuilt rule to generate gen/example/classes.jar files.
        # We scan these prebuilt jars to parse each class path for the FQN. This
        #     allows us to later map these classes back to their respective src
        #     directories.
        # TODO(agrieve): This should probably also check that the mtime of the .info
        #     is newer than that of the .jar, or change prebuilts to always output
        #     .info files so that they always exist (and change the depfile to
        #     depend directly on them).
        if path.endswith('.info'):
            info_data.update(jar_info_utils.ParseJarInfoFile(path))
        else:
            with zipfile.ZipFile(path) as zip_info:
                for name in zip_info.namelist():
                    fully_qualified_name = _FullJavaNameFromClassFilePath(name)
                    if fully_qualified_name:
                        info_data[fully_qualified_name] = '{}/{}'.format(
                            path, name)

    # only_if_changed=False since no build rules depend on this as an input.
    with build_utils.AtomicOutput(output, only_if_changed=False) as f:
        jar_info_utils.WriteJarInfoFile(f, info_data)
def _MergeInfoFiles(output, jar_paths):
    """Merge several .jar.info files to generate an .apk.jar.info.

  Args:
    output: output file path.
    jar_paths: List of .jar file paths for the target apk.
  """
    info_data = dict()
    for jar_path in jar_paths:
        # android_java_prebuilt adds jar files in the src directory (relative to
        #     the output directory, usually ../../third_party/example.jar).
        # android_aar_prebuilt collects jar files in the aar file and uses the
        #     java_prebuilt rule to generate gen/example/classes.jar files.
        # We scan these prebuilt jars to parse each class path for the FQN. This
        #     allows us to later map these classes back to their respective src
        #     directories.
        jar_info_path = jar_path + '.info'
        # TODO(agrieve): This should probably also check that the mtime of the .info
        #     is newer than that of the .jar, or change prebuilts to always output
        #     .info files so that they always exist (and change the depfile to
        #     depend directly on them).
        if os.path.exists(jar_info_path):
            info_data.update(
                jar_info_utils.ParseJarInfoFile(jar_path + '.info'))
        else:
            with zipfile.ZipFile(jar_path) as zip_info:
                for path in zip_info.namelist():
                    fully_qualified_name = _FullJavaNameFromClassFilePath(path)
                    if fully_qualified_name:
                        info_data[fully_qualified_name] = '{}/{}'.format(
                            jar_path, path)

    jar_info_utils.WriteJarInfoFile(output, info_data)
Esempio n. 6
0
  def Commit(self, output_path):
    """Writes a .jar.info file.

    Maps fully qualified names for classes to either the java file that they
    are defined in or the path of the srcjar that they came from.
    """
    logging.info('Collecting info file entries')
    entries = self._Collect()

    logging.info('Writing info file: %s', output_path)
    with build_utils.AtomicOutput(output_path) as f:
      jar_info_utils.WriteJarInfoFile(f, entries, self._srcjar_files)
    logging.info('Completed info file: %s', output_path)
def _MergeJarInfoFiles(output, inputs):
    """Merge several .jar.info files to generate an .apk.jar.info.

  Args:
    output: output file path.
    inputs: List of .jar.info or .jar files.
  """
    info_data = dict()
    for path in inputs:
        # For non-prebuilts: .jar.info files are written by compile_java.py and map
        # .class files to .java source paths.
        #
        # For prebuilts: No .jar.info file exists, we scan the .jar files here and
        # map .class files to the .jar.
        #
        # For .aar files: We look for a "source.info" file in the containing
        # directory in order to map classes back to the .aar (rather than mapping
        # them to the extracted .jar file).
        if path.endswith('.info'):
            info_data.update(jar_info_utils.ParseJarInfoFile(path))
        else:
            attributed_path = path
            if not path.startswith('..'):
                parent_path = os.path.dirname(path)
                # See if it's an sub-jar within the .aar.
                if os.path.basename(parent_path) == 'libs':
                    parent_path = os.path.dirname(parent_path)
                aar_source_info_path = os.path.join(parent_path, 'source.info')
                # source.info files exist only for jars from android_aar_prebuilt().
                # E.g. Could have an java_prebuilt() pointing to a generated .jar.
                if os.path.exists(aar_source_info_path):
                    attributed_path = jar_info_utils.ReadAarSourceInfo(
                        aar_source_info_path)

            with zipfile.ZipFile(path) as zip_info:
                for name in zip_info.namelist():
                    fully_qualified_name = _FullJavaNameFromClassFilePath(name)
                    if fully_qualified_name:
                        info_data[fully_qualified_name] = _TransformAarPaths(
                            '{}/{}'.format(attributed_path, name))

    # only_if_changed=False since no build rules depend on this as an input.
    with build_utils.AtomicOutput(output, only_if_changed=False) as f:
        jar_info_utils.WriteJarInfoFile(f, info_data)