Test releasing a C program
[0release.git] / compile.py
blobfd72a667977905b1ad409fe9ed5b1b7ce471fd27
1 # Copyright (C) 2009, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import tempfile, shutil, subprocess, os
5 import ConfigParser
6 from zeroinstall.injector import model
8 import support
10 COMPILE = 'http://0install.net/2006/interfaces/0compile.xml'
11 RELEASE = 'http://0install.net/2007/interfaces/0release.xml'
13 class Compiler:
14 def __init__(self, options, src_feed_name):
15 self.src_feed_name = src_feed_name
16 self.src_feed = support.load_feed(src_feed_name)
17 self.archive_dir_public_url = options.archive_dir_public_url
19 self.src_impl = support.get_singleton_impl(self.src_feed)
20 if self.src_impl.arch and self.src_impl.arch.endswith('-src'):
21 self.targets = ['host']
22 else:
23 self.targets = []
25 # We run the build in a sub-process. The idea is that the build may need to run
26 # on a different machine.
27 def build_binaries(self):
28 if not self.targets: return
30 print "Source package, so generating binaries..."
32 archive_file = support.get_archive_basename(self.src_impl)
34 for arch in self.targets:
35 if arch == 'host':
36 command = ['0launch', '--not-before', '0.8-post', RELEASE, '--build-slave']
37 else:
38 command = ['0release-build', arch]
40 binary_feed = 'binary-' + arch + '.xml'
41 if os.path.exists(binary_feed):
42 print "Feed %s already exists; not rebuilding" % binary_feed
43 else:
44 print "\nBuilding binary for %s architecture...\n" % arch
45 subprocess.check_call(command + [self.src_feed_name, archive_file, self.archive_dir_public_url, binary_feed + '.new'])
46 bin_feed = support.load_feed(binary_feed + '.new')
47 bin_impl = support.get_singleton_impl(bin_feed)
48 bin_archive_file = support.get_archive_basename(bin_impl)
49 bin_size = bin_impl.download_sources[0].size
51 assert os.path.exists(bin_archive_file), "Compiled binary '%s' not found!" % os.path.abspath(bin_archive_file)
52 assert os.path.getsize(bin_archive_file) == bin_size, "Compiled binary '%s' has wrong size!" % os.path.abspath(bin_archive_file)
54 os.rename(binary_feed + '.new', binary_feed)
56 def get_binary_feeds(self):
57 return ['binary-%s.xml' % arch for arch in self.targets]
59 # This is the actual build process, running on the build machine
60 def build_slave(src_feed, archive_file, archive_dir_public_url, target_feed):
61 feed = support.load_feed(src_feed)
63 archive_file = os.path.abspath(archive_file)
64 target_feed = os.path.abspath(target_feed)
66 impl, = feed.implementations.values()
68 tmpdir = tempfile.mkdtemp(prefix = '0release-')
69 try:
70 os.chdir(tmpdir)
71 depdir = os.path.join(tmpdir, 'dependencies')
72 os.mkdir(depdir)
74 support.unpack_tarball(archive_file)
75 os.rename(impl.download_sources[0].extract, os.path.join(depdir, impl.id))
77 config = ConfigParser.RawConfigParser()
78 config.add_section('compile')
79 config.set('compile', 'download-base-url', archive_dir_public_url)
80 config.set('compile', 'version-modifier', '')
81 config.set('compile', 'interface', src_feed)
82 config.set('compile', 'selections', '')
83 config.set('compile', 'metadir', '0install')
84 stream = open(os.path.join(tmpdir, '0compile.properties'), 'w')
85 try:
86 config.write(stream)
87 finally:
88 stream.close()
90 subprocess.check_call(['0launch', COMPILE, 'build'], cwd = tmpdir)
91 subprocess.check_call(['0launch', COMPILE, 'publish', '--target-feed', target_feed], cwd = tmpdir)
93 # TODO: run unit-tests
95 feed = support.load_feed(target_feed)
96 impl = support.get_singleton_impl(feed)
97 archive_file = support.get_archive_basename(impl)
99 shutil.move(archive_file, os.path.join(os.path.dirname(target_feed), archive_file))
100 except:
101 print "\nLeaving temporary directory %s for inspection...\n" % tmpdir
102 raise
103 else:
104 shutil.rmtree(tmpdir)