add .dylib support too
[make-headers.git] / make-headers.py
blob84ef11d242ad362fbd3a2d0196f78fd56eb5563a
1 #!/usr/bin/env python
2 import os, shutil, re, sys
3 from xml.dom import minidom, XMLNS_NAMESPACE
4 from optparse import OptionParser
6 XMLNS_IFACE = "http://zero-install.sourceforge.net/2004/injector/interface"
7 XMLNS_0COMPILE = 'http://zero-install.sourceforge.net/2006/namespaces/0compile'
9 version = '0.1'
10 parser = OptionParser(usage="usage: %prog [options]")
11 parser.add_option("-k", "--keep", help="keep a file we would normally delete", action='append')
12 parser.add_option("-V", "--version", help="display version information", action='store_true')
13 (options, args) = parser.parse_args()
15 if options.version:
16 print "Make-headers (zero-install) " + version
17 print "Copyright (C) 2007 Thomas Leonard"
18 print "This program comes with ABSOLUTELY NO WARRANTY,"
19 print "to the extent permitted by law."
20 print "You may redistribute copies of this program"
21 print "under the terms of the GNU General Public License."
22 print "For more information about these matters, see the file named COPYING."
23 sys.exit(0)
25 configure = os.path.join(os.environ['SRCDIR'], 'configure')
26 prefix = os.environ['DISTDIR']
28 def run(*argv):
29 if os.spawnvp(os.P_WAIT, argv[0], argv):
30 raise Exception("Command '%s' failed" % repr(argv))
32 run(configure, '--prefix', prefix, *args)
33 run('make', 'install')
35 keep = options.keep or []
37 os.chdir(prefix)
38 for path in ['bin', 'man', 'share']:
39 if path not in keep and os.path.isdir(path):
40 shutil.rmtree(path)
42 # TODO: get 0compile to tell us this instead of guessing
43 xml_files = [x for x in os.listdir('0install') if x.endswith('.xml')]
44 xml_files.remove('build-environment.xml')
45 assert len(xml_files) == 1, xml_files
46 feed_file = os.path.abspath(os.path.join('0install', xml_files[0]))
48 mappings = {}
50 os.chdir('lib')
51 for x in os.listdir('.'):
52 if os.path.isfile(x) or os.path.islink(x):
53 if re.match('^lib.*\.so[.0-9]*$', x) or \
54 re.match('^lib.*[.0-9]*\.dylib$', x) or \
55 re.match('^lib.*\.l?a$', x):
56 if x.endswith('.so') and os.path.islink(x):
57 target = os.readlink(x)
58 mappings[x[3:-3]] = target.split('.so.', 1)[1].split('.', 1)[0]
59 if x.endswith('.dylib') and os.path.islink(x):
60 target = os.readlink(x)
61 mappings[x[3:-6]] = target.split('.dylib.', 1)[0].split('.', 1)[0]
62 os.unlink(x)
63 elif os.path.isdir(x):
64 if re.match('^python.*$', x):
65 shutil.rmtree(x)
67 if mappings:
68 print "Detected library major versions:", mappings
69 doc = minidom.parse(feed_file)
70 impls = doc.getElementsByTagNameNS(XMLNS_IFACE, "implementation")
71 assert len(impls) == 1, impls
72 node = impls[0]
73 # Find the innermost element with some existing mappings, if any
74 while not node.hasAttributeNS(XMLNS_0COMPILE, 'lib-mappings'):
75 if node.parentNode.nodeName != 'group':
76 break
77 node = node.parentNode
78 existing_mappings = node.getAttributeNS(XMLNS_0COMPILE, 'lib-mappings')
79 if existing_mappings:
80 print "Merging existing mappings", existing_mappings
81 node.removeAttributeNS(XMLNS_0COMPILE, 'lib-mappings')
82 for existing_mapping in existing_mappings.split(' '):
83 key, value = existing_mapping.split(':')
84 if key in mappings:
85 if value != mappings[key]:
86 print "WARNING: overwriting detected mapping %s -> %s with %s!" % (key, mappings[key], value)
87 else:
88 print "NOTE: unnecessary mapping given in feed (%s -> %s). I can work it out for myself!" % (key, value)
89 mappings[key] = value
90 lib_mappings = []
91 for key in mappings:
92 lib_mappings.append("%s:%s" % (key, mappings[key]))
93 node.setAttributeNS(XMLNS_0COMPILE, 'compile:lib-mappings', ' '.join(lib_mappings))
94 doc.documentElement.setAttributeNS(XMLNS_NAMESPACE, 'xmlns:compile', XMLNS_0COMPILE)
95 stream = file(feed_file, 'w')
96 doc.writexml(stream)
97 stream.close()
99 os.chdir('pkgconfig')
100 for pc in os.listdir('.'):
101 if pc.endswith('.pc'):
102 data = file(pc).read().split('\n')
103 for i in range(len(data)):
104 if data[i].startswith('prefix='):
105 data[i] = 'prefix=${pcfiledir}/../..'
106 out = file(pc, 'w')
107 out.write('\n'.join(data))
108 out.close()