author Ted Mielczarek <>
Tue, 05 Apr 2016 10:09:12 -0400
changeset 291748 b65d504944ef45c9dd33ae45b922f5f438c12867
child 305108 9a85c5ad60511900628b7a071f095b2e0fc33dd9
permissions -rw-r--r--
bug 1239083 - use files to build ICU. r=glandium,waldo Also fixes bug 926980 - load ICU data from an archive file. Stop invoking ICU's autoconf build system. Instead, have hand-authored files under config/external/icu to build what we need. In addition, we'll commit a pre-built copy of the ICU data file (currently icudt56l.dat) under config/external/icu/data to avoid having to build ICU host tools to generate it. config/external/icu/data also contains some assembly files which can generate an object file containing the ICU data file contents so that the JS shell (or standalone JS builds) can be linked directly to the data without having to deal with the external data file. This requires yasm or GNU as. Various bits of packaging have been updated to account for the ICU data file. XPCOM initialization now sets the ICU data directory so ICU can locate its data file. The script has been modified to read the list of C/C++ source files out of the ICU Makefiles and update `sources.mozbuild` files under config/external/icu, as well as build a local copy of ICU using its autoconf build system to generate the ICU data file to be committed in-tree. MozReview-Commit-ID: 8Pfkzqt6S1W

#!/usr/bin/env python
# Any copyright is dedicated to the Public Domain.
# Generate SOURCES in sources.mozbuild files from ICU's
# files, and also build a standalone copy of ICU using its build
# system to generate a new copy of the in-tree ICU data file.
# This script expects to be run from `` after the in-tree
# copy of ICU has been updated.

from __future__ import print_function

import glob
import os
import shutil
import subprocess
import sys
import tempfile

def find_source_file(dir, filename):
    base = os.path.splitext(filename)[0]
    for ext in ('.cpp', '.c'):
        f = os.path.join(dir, base + ext)
        if os.path.isfile(f):
            return f
    raise Exception("Couldn't find source file for: %s" % filename)

def get_sources_from_makefile(makefile):
    import pymake.parser
    from pymake.parserdata import SetVariable
    srcdir = os.path.dirname(makefile)
    for statement in pymake.parser.parsefile(makefile):
        if (isinstance(statement, SetVariable) and
                statement.vnameexp.is_static_string and
                statement.vnameexp.s == 'OBJECTS'):
            return sorted((find_source_file(srcdir, s)
                           for s in statement.value.split()),
                          key=lambda x: x.lower())

def write_sources(mozbuild, sources):
    with open(mozbuild, 'wb') as f:
        f.write('# THIS FILE IS GENERATED BY /intl/ ' +
                'DO NOT EDIT\n' +
                'SOURCES += [\n')
        f.write(''.join("   '/%s',\n" % s for s in sources))

def update_sources(topsrcdir):
    print('Updating ICU sources lists...')
    sys.path.append(os.path.join(topsrcdir, 'build/pymake'))
    for d in ['common', 'i18n']:
        makefile = os.path.join(topsrcdir,
                                'intl/icu/source/%s/' % d)
        mozbuild = os.path.join(topsrcdir,
                                'config/external/icu/%s/sources.mozbuild' % d)
        sources = [os.path.relpath(s, topsrcdir)
                   for s in get_sources_from_makefile(makefile)]
        write_sources(mozbuild, sources)

def try_run(name, command, cwd=None, **kwargs):
    with tempfile.NamedTemporaryFile(prefix=name, delete=False) as f:
                           **kwargs) == 0:
            return True
    print('''Error running "{}" in directory {}
See output in {}'''.format(' '.join(command), cwd,,
    return False

def get_data_file(data_dir):
    files = glob.glob(os.path.join(data_dir, 'icudt*.dat'))
    return files[0] if files else None

def update_data_file(topsrcdir):
    objdir = tempfile.mkdtemp(prefix='icu-obj-')
    configure = os.path.join(topsrcdir, 'intl/icu/source/configure')
    env = dict(os.environ)
    # bug 1262101 - these should be shared with the files
                     '-DUCONFIG_NO_LEGACY_CONVERSION ' +
                     '-DUCONFIG_NO_TRANSLITERATION ' +
                     '-DUCONFIG_NO_REGULAR_EXPRESSIONS ' +
                     '-DUCONFIG_NO_BREAK_ITERATION ' +
    print('Running ICU configure...')
    if not try_run(
        return False
    print('Running ICU make...')
    if not try_run('icu-make', ['make'], cwd=objdir):
        return False
    print('Copying ICU data file...')
    tree_data_path = os.path.join(topsrcdir,
    old_data_file = get_data_file(tree_data_path)
    if not old_data_file:
        print('Error: no ICU data file in %s' % tree_data_path,
        return False
    new_data_file = get_data_file(os.path.join(objdir, 'data/out'))
    if not new_data_file:
        print('Error: no ICU data in ICU objdir', file=sys.stderr)
        return False
    if os.path.basename(old_data_file) != os.path.basename(new_data_file):
        # Data file name has the major version number embedded.
    shutil.copy(new_data_file, tree_data_path)
    return True

def main():
    if len(sys.argv) != 2:
        print('Usage: <mozilla topsrcdir>',

    topsrcdir = os.path.abspath(sys.argv[1])
    if not update_data_file(topsrcdir):
        print('Error updating ICU data file', file=sys.stderr)

if __name__ == '__main__':