• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2
3# Copyright 2017 The Chromium Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import os
9import os.path
10import shutil
11import subprocess
12import sys
13import stat
14import tempfile
15
16# How to patch libxml2 in Chromium:
17#
18# 1. Write a .patch file and add it to third_party/libxml/chromium.
19# 2. Apply the patch in src: patch -p1 <../chromium/foo.patch
20# 3. Add the patch to the list of patches in this file.
21# 4. Update README.chromium with the provenance of the patch.
22# 5. Upload a change with the modified documentation, roll script,
23#    patch, applied patch and any other relevant changes like
24#    regression tests. Go through the usual review and commit process.
25#
26# How to roll libxml2 in Chromium:
27#
28# Prerequisites:
29#
30# 1. Check out Chromium somewhere on Linux, Mac and Windows.
31# 2. On Linux:
32#    a. sudo apt-get install libicu-dev
33#    b. git clone https://github.com/GNOME/libxml2.git somewhere
34# 3. On Mac, install these packages with brew:
35#      autoconf automake libtool pkgconfig icu4c
36#
37# Procedure:
38#
39# Warning: This process is destructive. Run it on a clean branch.
40#
41# 1. On Linux, in the libxml2 repo directory:
42#    a. git remote update origin
43#    b. git checkout origin/master
44#
45#    This will be the upstream version of libxml you are rolling to.
46#
47# 2. On Linux, in the Chromium src director:
48#    a. third_party/libxml/chromium/roll.py --linux /path/to/libxml2
49#
50#    If this fails, it may be a patch no longer applies. Reset to
51#    head; modify the patch files, this script, and
52#    README.chromium; then commit the result and run it again.
53#
54#    b. Upload a CL, but do not Start Review.
55#
56# 2. On Windows, in the Chromium src directory:
57#    a. git cl patch <Gerrit Issue ID>
58#    b. third_party\libxml\chromium\roll.py --win32
59#    c. git cl upload
60#
61# 3. On Mac, in the Chromium src directory:
62#    a. git cl patch <Gerrit Issue ID>
63#    b. third_party/libxml/chromium/roll.py --mac --icu4c_path=~/homebrew/opt/icu4c
64#    c. Make and commit any final changes to README.chromium, BUILD.gn, etc.
65#    d. git cl upload
66#    e. Complete the review as usual
67
68PATCHES = [
69    'libxml2-2.9.4-security-xpath-nodetab-uaf.patch',
70    'undo-sax-deprecation.patch',
71]
72
73
74# See libxml2 configure.ac and win32/configure.js to learn what
75# options are available. We include every option here to more easily track
76# changes from one version to the next, and to be sure we only include what
77# we need.
78# These two sets of options should be in sync. You can check the
79# generated #defines in (win32|mac|linux)/include/libxml/xmlversion.h to confirm
80# this.
81# We would like to disable python but it introduces a host of build errors
82SHARED_XML_CONFIGURE_OPTIONS = [
83    # These options are turned ON
84    ('--with-html', 'html=yes'),
85    ('--with-icu', 'icu=yes'),
86    ('--with-output', 'output=yes'),
87    ('--with-push', 'push=yes'),
88    ('--with-python', 'python=yes'),
89    ('--with-reader', 'reader=yes'),
90    ('--with-sax1', 'sax1=yes'),
91    ('--with-threads', 'threads=yes'),
92    ('--with-tree', 'tree=yes'),
93    ('--with-writer', 'writer=yes'),
94    ('--with-xpath', 'xpath=yes'),
95    # These options are turned OFF
96    ('--without-c14n', 'c14n=no'),
97    ('--without-catalog', 'catalog=no'),
98    ('--without-debug', 'xml_debug=no'),
99    ('--without-ftp', 'ftp=no'),
100    ('--without-http', 'http=no'),
101    ('--without-iconv', 'iconv=no'),
102    ('--without-iso8859x', 'iso8859x=no'),
103    ('--without-legacy', 'legacy=no'),
104    ('--without-lzma', 'lzma=no'),
105    ('--without-mem-debug', 'mem_debug=no'),
106    ('--without-modules', 'modules=no'),
107    ('--without-pattern', 'pattern=no'),
108    ('--without-regexps', 'regexps=no'),
109    ('--without-schemas', 'schemas=no'),
110    ('--without-schematron', 'schematron=no'),
111    ('--without-valid', 'valid=no'),
112    ('--without-xinclude', 'xinclude=no'),
113    ('--without-xptr', 'xptr=no'),
114    ('--without-xptr-locs', 'xptr_locs=no'),
115    ('--without-zlib', 'zlib=no'),
116]
117
118
119# These options are only available in configure.ac for Linux and Mac.
120EXTRA_NIX_XML_CONFIGURE_OPTIONS = [
121    '--without-fexceptions',
122    '--without-minimum',
123    '--without-readline',
124    '--without-history',
125]
126
127
128# These options are only available in win32/configure.js for Windows.
129EXTRA_WIN32_XML_CONFIGURE_OPTIONS = [
130    'trio=no',
131    'walker=no',
132]
133
134
135XML_CONFIGURE_OPTIONS = (
136    [option[0] for option in SHARED_XML_CONFIGURE_OPTIONS] +
137    EXTRA_NIX_XML_CONFIGURE_OPTIONS)
138
139
140XML_WIN32_CONFIGURE_OPTIONS = (
141    [option[1] for option in SHARED_XML_CONFIGURE_OPTIONS] +
142    EXTRA_WIN32_XML_CONFIGURE_OPTIONS)
143
144
145FILES_TO_REMOVE = [
146    'src/DOCBparser.c',
147    'src/HACKING',
148    'src/INSTALL',
149    'src/INSTALL.libxml2',
150    'src/MAINTAINERS',
151    'src/Makefile.in',
152    'src/Makefile.win',
153    'src/README.cvs-commits',
154    # This is unneeded "legacy" SAX API, even though we enable SAX1.
155    'src/SAX.c',
156    'src/VxWorks',
157    'src/autogen.sh',
158    'src/autom4te.cache',
159    'src/bakefile',
160    'src/build_glob.py',
161    'src/c14n.c',
162    'src/catalog.c',
163    'src/compile',
164    'src/config.guess',
165    'src/config.sub',
166    'src/configure',
167    'src/chvalid.def',
168    'src/debugXML.c',
169    'src/depcomp',
170    'src/doc',
171    'src/example',
172    'src/fuzz',
173    'src/genChRanges.py',
174    'src/global.data',
175    'src/include/libxml/Makefile.in',
176    'src/include/libxml/xmlversion.h',
177    'src/include/libxml/xmlwin32version.h',
178    'src/include/libxml/xmlwin32version.h.in',
179    'src/include/Makefile.in',
180    'src/install-sh',
181    'src/legacy.c',
182    'src/libxml2.doap',
183    'src/ltmain.sh',
184    'src/m4',
185    'src/macos/libxml2.mcp.xml.sit.hqx',
186    'src/missing',
187    'src/optim',
188    'src/os400',
189    'src/python',
190    'src/relaxng.c',
191    'src/result',
192    'src/rngparser.c',
193    'src/schematron.c',
194    'src/test',
195    'src/testOOM.c',
196    'src/testOOMlib.c',
197    'src/testOOMlib.h',
198    'src/trio.c',
199    'src/trio.h',
200    'src/triop.h',
201    'src/triostr.c',
202    'src/triostr.h',
203    'src/vms',
204    'src/win32/VC10/config.h',
205    'src/win32/wince',
206    'src/xinclude.c',
207    'src/xlink.c',
208    'src/xml2-config.in',
209    'src/xmlcatalog.c',
210    'src/xmllint.c',
211    'src/xmlmodule.c',
212    'src/xmlregexp.c',
213    'src/xmlschemas.c',
214    'src/xmlschemastypes.c',
215    'src/xpointer.c',
216    'src/xstc',
217    'src/xzlib.c',
218    'linux/.deps',
219    'linux/doc',
220    'linux/example',
221    'linux/fuzz',
222    'linux/include/private',
223    'linux/python',
224    'linux/xstc',
225]
226
227
228THIRD_PARTY_LIBXML_SRC = 'third_party/libxml/src'
229
230
231class WorkingDir(object):
232    """"Changes the working directory and resets it on exit."""
233    def __init__(self, path):
234        self.prev_path = os.getcwd()
235        self.path = path
236
237    def __enter__(self):
238        os.chdir(self.path)
239
240    def __exit__(self, exc_type, exc_value, traceback):
241        if exc_value:
242            print('was in %s; %s before that' % (self.path, self.prev_path))
243        os.chdir(self.prev_path)
244
245
246def git(*args):
247    """Runs a git subcommand.
248
249    On Windows this uses the shell because there's a git wrapper
250    batch file in depot_tools.
251
252    Arguments:
253        args: The arguments to pass to git.
254    """
255    command = ['git'] + list(args)
256    subprocess.check_call(command, shell=(os.name == 'nt'))
257
258
259def remove_tracked_and_local_dir(path):
260    """Removes the contents of a directory from git, and the filesystem.
261
262    Arguments:
263        path: The path to remove.
264    """
265    remove_tracked_files([path])
266    shutil.rmtree(path, ignore_errors=True)
267    os.mkdir(path)
268
269
270def remove_tracked_files(files_to_remove):
271    """Removes tracked files from git.
272
273    Arguments:
274        files_to_remove: The files to remove.
275    """
276    files_to_remove = [f for f in files_to_remove if os.path.exists(f)]
277    if files_to_remove:
278        git('rm', '-rf', *files_to_remove)
279
280
281def sed_in_place(input_filename, program):
282    """Replaces text in a file.
283
284    Arguments:
285        input_filename: The file to edit.
286        program: The sed program to perform edits on the file.
287    """
288    # OS X's sed requires -e
289    subprocess.check_call(['sed', '-i', '-e', program, input_filename])
290
291
292def check_copying(full_path_to_third_party_libxml_src):
293    path = os.path.join(full_path_to_third_party_libxml_src, 'COPYING')
294    if not os.path.exists(path):
295        return
296    with open(path) as f:
297        s = f.read()
298        if 'GNU' in s:
299            raise Exception('check COPYING')
300
301
302def prepare_libxml_distribution(src_path, libxml2_repo_path, temp_dir):
303    """Makes a libxml2 distribution.
304
305    Args:
306        src_path: The path to the Chromium checkout.
307        libxml2_repo_path: The path to the local clone of the libxml2 repo.
308        temp_dir: A temporary directory to stage the distribution to.
309
310    Returns: A tuple of commit hash and full path to the archive.
311    """
312    # If it was necessary to push from a distribution prepared upstream,
313    # this is the point to inject it: Return the version string and the
314    # distribution tar file.
315
316    # The libxml2 repo we're pulling changes from should not have
317    # local changes. This *should* be a commit that's publicly visible
318    # in the upstream repo; reviewers should check this.
319    check_clean(libxml2_repo_path)
320
321    temp_config_path = os.path.join(temp_dir, 'config')
322    os.mkdir(temp_config_path)
323    temp_src_path = os.path.join(temp_dir, 'src')
324    os.mkdir(temp_src_path)
325
326    with WorkingDir(libxml2_repo_path):
327        commit = subprocess.check_output(
328            ['git', 'log', '-n', '1', '--pretty=format:%H',
329             'HEAD']).decode('ascii')
330        subprocess.check_call(
331            'git archive HEAD | tar -x -C "%s"' % temp_src_path,
332            shell=True)
333    with WorkingDir(temp_src_path):
334        os.remove('.gitignore')
335        for patch in PATCHES:
336            print('applying %s' % patch)
337            subprocess.check_call(
338                'patch -p1 --fuzz=0 < %s' % os.path.join(
339                    src_path, THIRD_PARTY_LIBXML_SRC, '..', 'chromium', patch),
340                shell=True)
341
342    with WorkingDir(temp_config_path):
343        print('../src/autogen.sh %s' % XML_CONFIGURE_OPTIONS)
344        subprocess.check_call(['../src/autogen.sh'] + XML_CONFIGURE_OPTIONS)
345        subprocess.check_call(['make', 'dist-all'])
346
347        # Work out what it is called
348        tar_file = subprocess.check_output(
349            '''awk '/PACKAGE =/ {p=$3} /VERSION =/ {v=$3} '''
350            '''END {printf("%s-%s.tar.xz", p, v)}' Makefile''',
351            shell=True).decode('ascii')
352        return commit, os.path.abspath(tar_file)
353
354
355def roll_libxml_linux(src_path, libxml2_repo_path):
356    with WorkingDir(src_path):
357        # Export the upstream git repo.
358        try:
359            temp_dir = tempfile.mkdtemp()
360            print('temporary directory: %s' % temp_dir)
361
362            commit, tar_file = prepare_libxml_distribution(
363                src_path, libxml2_repo_path, temp_dir)
364
365            # Remove all of the old libxml to ensure only desired cruft
366            # accumulates
367            remove_tracked_and_local_dir(THIRD_PARTY_LIBXML_SRC)
368
369            # Update the libxml repo and export it to the Chromium tree
370            with WorkingDir(THIRD_PARTY_LIBXML_SRC):
371                subprocess.check_call(
372                    'tar xJf %s --strip-components=1' % tar_file,
373                    shell=True)
374        finally:
375            shutil.rmtree(temp_dir)
376
377        with WorkingDir(THIRD_PARTY_LIBXML_SRC):
378            # Put the version number is the README file
379            sed_in_place('../README.chromium',
380                         's/Version: .*$/Version: %s/' % commit)
381
382            with WorkingDir('../linux'):
383                subprocess.check_call(
384                    ['../src/autogen.sh'] + XML_CONFIGURE_OPTIONS)
385                check_copying(os.getcwd())
386                sed_in_place('config.h', 's/#define HAVE_RAND_R 1//')
387
388            # Add *everything*
389            with WorkingDir('../src'):
390                git('add', '*')
391                git('commit', '-am', '%s libxml, linux' % commit)
392    print('Now push to Windows and run steps there.')
393
394
395def roll_libxml_win32(src_path):
396    with WorkingDir(src_path):
397        # Run the configure script.
398        with WorkingDir(os.path.join(THIRD_PARTY_LIBXML_SRC, 'win32')):
399            subprocess.check_call(
400                ['cscript', '//E:jscript', 'configure.js', 'compiler=msvc'] +
401                XML_WIN32_CONFIGURE_OPTIONS)
402
403            # Add and commit the result.
404            shutil.move('../config.h', '../../win32/config.h')
405            git('add', '../../win32/config.h')
406            shutil.move('../include/libxml/xmlversion.h',
407                        '../../win32/include/libxml/xmlversion.h')
408            git('add', '../../win32/include/libxml/xmlversion.h')
409            git('commit', '--allow-empty', '-m', 'Windows')
410            git('clean', '-f')
411    print('Now push to Mac and run steps there.')
412
413
414def roll_libxml_mac(src_path, icu4c_path):
415    icu4c_path = os.path.abspath(os.path.expanduser(icu4c_path))
416    os.environ["LDFLAGS"] = "-L" + os.path.join(icu4c_path, 'lib')
417    os.environ["CPPFLAGS"] = "-I" + os.path.join(icu4c_path, 'include')
418    os.environ["PKG_CONFIG_PATH"] = os.path.join(icu4c_path, 'lib/pkgconfig')
419
420    full_path_to_third_party_libxml = os.path.join(
421        src_path, THIRD_PARTY_LIBXML_SRC, '..')
422
423    with WorkingDir(os.path.join(full_path_to_third_party_libxml, 'mac')):
424        subprocess.check_call(['autoreconf', '-i', '../src'])
425        os.chmod('../src/configure',
426                 os.stat('../src/configure').st_mode | stat.S_IXUSR)
427        subprocess.check_call(['../src/configure'] + XML_CONFIGURE_OPTIONS)
428        sed_in_place('config.h', 's/#define HAVE_RAND_R 1//')
429
430    with WorkingDir(full_path_to_third_party_libxml):
431        commit = subprocess.check_output(
432            ['awk', '/Version:/ {print $2}',
433             'README.chromium']).decode('ascii')
434        remove_tracked_files(FILES_TO_REMOVE)
435        commit_message = 'Roll libxml to %s' % commit
436        git('commit', '-am', commit_message)
437    print('Now upload for review, etc.')
438
439
440def check_clean(path):
441    with WorkingDir(path):
442        status = subprocess.check_output(['git', 'status',
443                                          '-s']).decode('ascii')
444        if len(status) > 0:
445            raise Exception('repository at %s is not clean' % path)
446
447
448def main():
449    src_dir = os.getcwd()
450    if not os.path.exists(os.path.join(src_dir, 'third_party')):
451        print('error: run this script from the Chromium src directory')
452        sys.exit(1)
453
454    parser = argparse.ArgumentParser(
455        description='Roll the libxml2 dependency in Chromium')
456    platform = parser.add_mutually_exclusive_group(required=True)
457    platform.add_argument('--linux', action='store_true')
458    platform.add_argument('--win32', action='store_true')
459    platform.add_argument('--mac', action='store_true')
460    parser.add_argument(
461        'libxml2_repo_path',
462        type=str,
463        nargs='?',
464        help='The path to the local clone of the libxml2 git repo.')
465    parser.add_argument(
466        '--icu4c_path',
467        help='The path to the homebrew installation of icu4c.')
468    args = parser.parse_args()
469
470    if args.linux:
471        libxml2_repo_path = args.libxml2_repo_path
472        if not libxml2_repo_path:
473            print('Specify the path to the local libxml2 repo clone.')
474            sys.exit(1)
475        libxml2_repo_path = os.path.abspath(libxml2_repo_path)
476        roll_libxml_linux(src_dir, libxml2_repo_path)
477    elif args.win32:
478        roll_libxml_win32(src_dir)
479    elif args.mac:
480        icu4c_path = args.icu4c_path
481        if not icu4c_path:
482            print('Specify the path to the homebrew installation of icu4c with --icu4c_path.')
483            print('  ex: roll.py --mac --icu4c_path=~/homebrew/opt/icu4c')
484            sys.exit(1)
485        roll_libxml_mac(src_dir, icu4c_path)
486
487
488if __name__ == '__main__':
489    main()
490