Commit f26567c4 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

strip installed binaries.

parent 651071c2
......@@ -7,6 +7,7 @@ import os
import pkg_resources
from platform import machine as platform_machine
import re
import stat
import shutil
import subprocess
import sys
......@@ -50,6 +51,8 @@ class Recipe(object):
options['url'] = options.get('url', '').strip()
options['path'] = options.get('path', '').strip()
options['promises'] = options.get('promises', '')
default_strip = buildout['buildout'].query_bool('strip', 'true')
options['strip'] = options.query_bool('strip', default_strip and 'true' or 'false')
# Check dependencies, all the dependent parts will be installed first. It
# seems once part is referenced, for example, self.buildout[part], it will
......@@ -386,4 +389,75 @@ class Recipe(object):
if self.options['share'] == '':
parts.append(self.options['default-location'])
if self.options['strip']:
self.strip(parts)
return parts
def strip(self, path_list):
# Same logic as Debian's dh_strip script.
log = logging.getLogger(self.name)
args = ['find',] + list(path_list) + ['-type', 'f']
try:
p = subprocess.Popen(args, stdout=subprocess.PIPE)
file_list, _ = p.communicate()
retcode = p.returncode
if retcode < 0:
log.error('Command received signal %s: %s' % (-retcode, args))
raise zc.buildout.UserError('System error')
elif retcode > 0:
log.error('Command failed with exit code %s: %s' % (retcode, args))
raise zc.buildout.UserError('System error')
shared_lib_list = []
executable_list = []
static_lib_list = []
for path in file_list.splitlines():
file_name = os.path.basename(path)
if re.match('.*\.(so(\..*)?|cmxs)?$', file_name):
shared_lib_list.append(path)
elif os.stat(path).st_mode & stat.S_IEXEC:
try:
args = ['file', path]
p = subprocess.Popen(args, stdout=subprocess.PIPE)
result, _ = p.communicate()
if re.match('.*ELF.*(executable|shared).*', result):
executable_list.append(path)
except OSError, e:
log.warning('Command failed: %s: %s' % (e, args))
elif re.match('lib.*\.a$', file_name) and not re.match('.*_g\.a$', file_name):
static_lib_list.append(path)
for path in shared_lib_list:
strip_args = [
'--remove-section=.comment',
'--remove-section=.note',
'--strip-unneeded',
]
self.run_strip(path, strip_args)
for path in executable_list:
strip_args = [
'--remove-section=.comment',
'--remove-section=.note',
]
self.run_strip(path, strip_args)
for path in shared_lib_list:
strip_args = [
'--strip-debug',
]
self.run_strip(path, strip_args)
except OSError, e:
log.warning('Command failed: %s: %s' % (e, args))
def run_strip(self, path, strip_args):
log = logging.getLogger(self.name)
mode = os.stat(path).st_mode
writable_mode = mode | stat.S_IWUSR
if mode != writable_mode:
os.chmod(path, writable_mode)
try:
args = ['strip',] + strip_args + [path,]
p = subprocess.Popen(args, stdout=subprocess.PIPE)
result, _ = p.communicate()
except OSError, e:
log.warning('Command failed: %s: %s' % (e, args))
if mode != writable_mode:
os.chmod(path, mode)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment