Instrumented libraries: fix flaky error in libpci3 build.
[chromium-blink-merge.git] / third_party / instrumented_libraries / download_build_install.py
blob7fddba236d5ec5e0637dc269622f134a93eaa803
1 #!/usr/bin/python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Downloads, builds (with instrumentation) and installs shared libraries."""
8 import argparse
9 import os
10 import platform
11 import re
12 import shlex
13 import shutil
14 import subprocess
15 import sys
17 SCRIPT_ABSOLUTE_PATH = os.path.dirname(os.path.abspath(__file__))
19 class ScopedChangeDirectory(object):
20 """Changes current working directory and restores it back automatically."""
22 def __init__(self, path):
23 self.path = path
24 self.old_path = ''
26 def __enter__(self):
27 self.old_path = os.getcwd()
28 os.chdir(self.path)
29 return self
31 def __exit__(self, exc_type, exc_value, traceback):
32 os.chdir(self.old_path)
34 def get_package_build_dependencies(package):
35 command = 'apt-get -s build-dep %s | grep Inst | cut -d " " -f 2' % package
36 command_result = subprocess.Popen(command, stdout=subprocess.PIPE,
37 shell=True)
38 if command_result.wait():
39 raise Exception('Failed to determine build dependencies for %s' % package)
40 build_dependencies = [l.strip() for l in command_result.stdout]
41 return build_dependencies
44 def check_package_build_dependencies(package):
45 build_dependencies = get_package_build_dependencies(package)
46 if len(build_dependencies):
47 print >> sys.stderr, 'Please, install build-dependencies for %s' % package
48 print >> sys.stderr, 'One-liner for APT:'
49 print >> sys.stderr, 'sudo apt-get -y --no-remove build-dep %s' % package
50 sys.exit(1)
53 def shell_call(command, verbose=False, environment=None):
54 """ Wrapper on subprocess.Popen
56 Calls command with specific environment and verbosity using
57 subprocess.Popen
59 Args:
60 command: Command to run in shell.
61 verbose: If False, hides all stdout and stderr in case of successful build.
62 Otherwise, always prints stdout and stderr.
63 environment: Parameter 'env' for subprocess.Popen.
65 Returns:
66 None
68 Raises:
69 Exception: if return code after call is not zero.
70 """
71 child = subprocess.Popen(
72 command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
73 env=environment, shell=True)
74 stdout, stderr = child.communicate()
75 if verbose or child.returncode:
76 print stdout
77 if child.returncode:
78 raise Exception('Failed to run: %s' % command)
81 def run_shell_commands(commands, verbose=False, environment=None):
82 for command in commands:
83 shell_call(command, verbose, environment)
86 def fix_rpaths(destdir):
87 # TODO(earthdok): reimplement fix_rpaths.sh in Python.
88 shell_call("%s/fix_rpaths.sh %s/lib" % (SCRIPT_ABSOLUTE_PATH, destdir))
91 def destdir_configure_make_install(parsed_arguments, environment,
92 install_prefix):
93 configure_command = './configure %s' % parsed_arguments.extra_configure_flags
94 configure_command += ' --libdir=/lib/'
95 # Installing to a temporary directory allows us to safely clean up the .la
96 # files below.
97 destdir = '%s/debian/instrumented_build' % os.getcwd()
98 # Some makefiles use BUILDROOT or INSTALL_ROOT instead of DESTDIR.
99 make_command = 'make DESTDIR=%s BUILDROOT=%s INSTALL_ROOT=%s' % (destdir,
100 destdir,
101 destdir)
102 build_and_install_in_destdir = [
103 configure_command,
104 '%s -j%s' % (make_command, parsed_arguments.jobs),
105 # Parallel install is flaky for some packages.
106 '%s install -j1' % make_command,
107 # Kill the .la files. They contain absolute paths, and will cause build
108 # errors in dependent libraries.
109 'rm %s/lib/*.la -f' % destdir
111 run_shell_commands(build_and_install_in_destdir,
112 parsed_arguments.verbose, environment)
113 fix_rpaths(destdir)
114 shell_call(
115 # Now move the contents of the temporary destdir to their final place.
116 'cp %s/* %s/ -rdf' % (destdir, install_prefix),
117 parsed_arguments.verbose, environment)
120 def nss_make_and_copy(parsed_arguments, environment, install_prefix):
121 # NSS uses a build system that's different from configure/make/install. All
122 # flags must be passed as arguments to make.
123 make_args = []
124 # Do an optimized build.
125 make_args.append('BUILD_OPT=1')
126 # Set USE_64=1 on x86_64 systems.
127 if platform.architecture()[0] == '64bit':
128 make_args.append('USE_64=1')
129 # Passing C(XX)FLAGS overrides the defaults, and EXTRA_C(XX)FLAGS is not
130 # supported. Append our extra flags to CC/CXX.
131 make_args.append('CC="%s %s"' % (environment['CC'], environment['CFLAGS']))
132 make_args.append('CXX="%s %s"' %
133 (environment['CXX'], environment['CXXFLAGS']))
134 # We need to override ZDEFS_FLAG at least to prevent -Wl,-z,defs.
135 # Might as well use this to pass the linker flags, since ZDEF_FLAG is always
136 # added during linking on Linux.
137 make_args.append('ZDEFS_FLAG="-Wl,-z,nodefs %s"' % environment['LDFLAGS'])
138 make_args.append('NSPR_INCLUDE_DIR=/usr/include/nspr')
139 make_args.append('NSPR_LIB_DIR=%s/lib' % install_prefix)
140 make_args.append('NSS_ENABLE_ECC=1')
141 # Make sure we don't override the default flags.
142 for variable in ['CFLAGS', 'CXXFLAGS', 'LDFLAGS']:
143 del environment[variable]
144 with ScopedChangeDirectory('nss') as cd_nss:
145 # -j is not supported
146 shell_call('make %s' % ' '.join(make_args), parsed_arguments.verbose,
147 environment)
148 fix_rpaths(os.getcwd())
149 # 'make install' is not supported. Copy the DSOs manually.
150 install_dir = '%s/lib/' % install_prefix
151 for (dirpath, dirnames, filenames) in os.walk('./lib/'):
152 for filename in filenames:
153 if filename.endswith('.so'):
154 full_path = os.path.join(dirpath, filename)
155 if parsed_arguments.verbose:
156 print 'download_build_install.py: installing %s' % full_path
157 shutil.copy(full_path, install_dir)
160 def libcap2_make_install(parsed_arguments, environment, install_prefix):
161 # libcap2 doesn't come with a configure script
162 make_args = [
163 '%s="%s"' % (name, environment[name])
164 for name in['CC', 'CXX', 'CFLAGS', 'CXXFLAGS', 'LDFLAGS']]
165 shell_call('make -j%s %s' % (parsed_arguments.jobs, ' '.join(make_args)),
166 parsed_arguments.verbose, environment)
167 destdir = '%s/debian/instrumented_build' % os.getcwd()
168 install_args = [
169 'DESTDIR=%s' % destdir,
170 # Do not install in lib64/.
171 'lib=lib',
172 # Skip a step that requires sudo.
173 'RAISE_SETFCAP=no'
175 shell_call('make -j%s install %s' %
176 (parsed_arguments.jobs, ' '.join(install_args)),
177 parsed_arguments.verbose, environment)
178 fix_rpaths(destdir)
179 shell_call([
180 # Now move the contents of the temporary destdir to their final place.
181 'cp %s/* %s/ -rdf' % (destdir, install_prefix)],
182 parsed_arguments.verbose, environment)
185 def libpci3_make_install(parsed_arguments, environment, install_prefix):
186 # pciutils doesn't have a configure script
187 # This build script follows debian/rules.
189 # Find out the package version. We'll use this when creating symlinks.
190 dir_name = os.path.split(os.getcwd())[-1]
191 match = re.match('pciutils-(\d+\.\d+\.\d+)', dir_name)
192 if match is None:
193 raise Exception(
194 'Unable to guess libpci3 version from directory name: %s' % dir_name)
195 version = match.group(1)
197 # `make install' will create a "$(DESTDIR)-udeb" directory alongside destdir.
198 # We don't want that in our product dir, so we use an intermediate directory.
199 destdir = '%s/debian/pciutils' % os.getcwd()
200 make_args = [
201 '%s="%s"' % (name, environment[name])
202 for name in['CC', 'CXX', 'CFLAGS', 'CXXFLAGS', 'LDFLAGS']]
203 make_args.append('SHARED=yes')
204 # pciutils-3.2.1 (Trusty) fails to build due to unresolved libkmod symbols.
205 # The binary package has no dependencies on libkmod, so it looks like it was
206 # actually built without libkmod support.
207 make_args.append('LIBKMOD=no')
208 paths = [
209 'LIBDIR=/lib/',
210 'PREFIX=/usr',
211 'SBINDIR=/usr/bin',
212 'IDSDIR=/usr/share/misc',
214 install_args = ['DESTDIR=%s' % destdir]
215 run_shell_commands([
216 'mkdir -p %s-udeb/usr/bin' % destdir,
217 'make -j%s %s' % (parsed_arguments.jobs, ' '.join(make_args + paths)),
218 'make -j%s %s install' % (
219 parsed_arguments.jobs,
220 ' '.join(install_args + paths))],
221 parsed_arguments.verbose, environment)
222 fix_rpaths(destdir)
223 # Now move the contents of the temporary destdir to their final place.
224 run_shell_commands([
225 'cp %s/* %s/ -rd' % (destdir, install_prefix),
226 'mkdir -p %s/lib/' % install_prefix,
227 'install -m 644 lib/libpci.so* %s/lib/' % install_prefix,
228 'ln -sf libpci.so.%s %s/lib/libpci.so.3' % (version, install_prefix)],
229 parsed_arguments.verbose, environment)
232 def build_and_install(parsed_arguments, environment, install_prefix):
233 if parsed_arguments.build_method == 'destdir':
234 destdir_configure_make_install(
235 parsed_arguments, environment, install_prefix)
236 elif parsed_arguments.build_method == 'custom_nss':
237 nss_make_and_copy(parsed_arguments, environment, install_prefix)
238 elif parsed_arguments.build_method == 'custom_libcap':
239 libcap2_make_install(parsed_arguments, environment, install_prefix)
240 elif parsed_arguments.build_method == 'custom_libpci3':
241 libpci3_make_install(parsed_arguments, environment, install_prefix)
242 else:
243 raise Exception('Unrecognized build method: %s' %
244 parsed_arguments.build_method)
247 def unescape_flags(s):
248 # GYP escapes the build flags as if they are going to be inserted directly
249 # into the command line. Since we pass them via CFLAGS/LDFLAGS, we must drop
250 # the double quotes accordingly.
251 return ' '.join(shlex.split(s))
254 def build_environment(parsed_arguments, product_directory, install_prefix):
255 environment = os.environ.copy()
256 # The CC/CXX environment variables take precedence over the command line
257 # flags.
258 if 'CC' not in environment and parsed_arguments.cc:
259 environment['CC'] = parsed_arguments.cc
260 if 'CXX' not in environment and parsed_arguments.cxx:
261 environment['CXX'] = parsed_arguments.cxx
263 cflags = unescape_flags(parsed_arguments.cflags)
264 if parsed_arguments.sanitizer_blacklist:
265 cflags += ' -fsanitize-blacklist=%s/%s' % (
266 SCRIPT_ABSOLUTE_PATH,
267 parsed_arguments.sanitizer_blacklist)
268 environment['CFLAGS'] = cflags
269 environment['CXXFLAGS'] = cflags
271 ldflags = unescape_flags(parsed_arguments.ldflags)
272 # Make sure the linker searches the instrumented libraries dir for
273 # library dependencies.
274 environment['LDFLAGS'] = '%s -L%s/lib' % (ldflags, install_prefix)
276 if parsed_arguments.sanitizer_type == 'asan':
277 # Do not report leaks during the build process.
278 environment['ASAN_OPTIONS'] = '%s:detect_leaks=0' % \
279 environment.get('ASAN_OPTIONS', '')
281 # libappindicator1 needs this.
282 environment['CSC'] = '/usr/bin/mono-csc'
283 return environment
287 def download_build_install(parsed_arguments):
288 product_directory = os.path.normpath('%s/%s' % (
289 SCRIPT_ABSOLUTE_PATH,
290 parsed_arguments.product_directory))
292 install_prefix = '%s/instrumented_libraries/%s' % (
293 product_directory,
294 parsed_arguments.sanitizer_type)
296 environment = build_environment(parsed_arguments, product_directory,
297 install_prefix)
299 package_directory = '%s/%s' % (parsed_arguments.intermediate_directory,
300 parsed_arguments.package)
302 # Clobber by default, unless the developer wants to hack on the package's
303 # source code.
304 clobber = (environment.get('INSTRUMENTED_LIBRARIES_NO_CLOBBER', '') != '1')
306 download_source = True
307 if os.path.exists(package_directory):
308 if clobber:
309 shell_call('rm -rf %s' % package_directory, parsed_arguments.verbose)
310 else:
311 download_source = False
312 if download_source:
313 os.makedirs(package_directory)
315 with ScopedChangeDirectory(package_directory) as cd_package:
316 if download_source:
317 shell_call('apt-get source %s' % parsed_arguments.package,
318 parsed_arguments.verbose)
319 # There should be exactly one subdirectory after downloading a package.
320 subdirectories = [d for d in os.listdir('.') if os.path.isdir(d)]
321 if len(subdirectories) != 1:
322 raise Exception('apt-get source %s must create exactly one subdirectory.'
323 % parsed_arguments.package)
324 with ScopedChangeDirectory(subdirectories[0]):
325 # Here we are in the package directory.
326 if download_source:
327 # Patch/run_before_build steps are only done once.
328 if parsed_arguments.patch:
329 shell_call(
330 'patch -p1 -i %s/%s' %
331 (os.path.relpath(cd_package.old_path),
332 parsed_arguments.patch),
333 parsed_arguments.verbose)
334 if parsed_arguments.run_before_build:
335 shell_call(
336 '%s/%s' %
337 (os.path.relpath(cd_package.old_path),
338 parsed_arguments.run_before_build),
339 parsed_arguments.verbose)
340 try:
341 build_and_install(parsed_arguments, environment, install_prefix)
342 except Exception as exception:
343 print exception
344 print 'Failed to build package %s.' % parsed_arguments.package
345 print ('Probably, some of its dependencies are not installed: %s' %
346 ' '.join(get_package_build_dependencies(parsed_arguments.package)))
347 sys.exit(1)
349 # Touch a txt file to indicate package is installed.
350 open('%s/%s.txt' % (install_prefix, parsed_arguments.package), 'w').close()
352 # Remove downloaded package and generated temporary build files.
353 # Failed builds intentionally skip this step, in order to aid in tracking down
354 # build failures.
355 if clobber:
356 shell_call('rm -rf %s' % package_directory, parsed_arguments.verbose)
358 def main():
359 argument_parser = argparse.ArgumentParser(
360 description='Download, build and install instrumented package')
362 argument_parser.add_argument('-j', '--jobs', type=int, default=1)
363 argument_parser.add_argument('-p', '--package', required=True)
364 argument_parser.add_argument(
365 '-i', '--product-directory', default='.',
366 help='Relative path to the directory with chrome binaries')
367 argument_parser.add_argument(
368 '-m', '--intermediate-directory', default='.',
369 help='Relative path to the directory for temporary build files')
370 argument_parser.add_argument('--extra-configure-flags', default='')
371 argument_parser.add_argument('--cflags', default='')
372 argument_parser.add_argument('--ldflags', default='')
373 argument_parser.add_argument('-s', '--sanitizer-type', required=True,
374 choices=['asan', 'msan', 'tsan'])
375 argument_parser.add_argument('-v', '--verbose', action='store_true')
376 argument_parser.add_argument('--check-build-deps', action='store_true')
377 argument_parser.add_argument('--cc')
378 argument_parser.add_argument('--cxx')
379 argument_parser.add_argument('--patch', default='')
380 # This should be a shell script to run before building specific libraries.
381 # This will be run after applying the patch above.
382 argument_parser.add_argument('--run-before-build', default='')
383 argument_parser.add_argument('--build-method', default='destdir')
384 argument_parser.add_argument('--sanitizer-blacklist', default='')
386 # Ignore all empty arguments because in several cases gyp passes them to the
387 # script, but ArgumentParser treats them as positional arguments instead of
388 # ignoring (and doesn't have such options).
389 parsed_arguments = argument_parser.parse_args(
390 [arg for arg in sys.argv[1:] if len(arg) != 0])
391 # Ensure current working directory is this script directory.
392 os.chdir(SCRIPT_ABSOLUTE_PATH)
393 # Ensure all build dependencies are installed.
394 if parsed_arguments.check_build_deps:
395 check_package_build_dependencies(parsed_arguments.package)
397 download_build_install(parsed_arguments)
400 if __name__ == '__main__':
401 main()