Welcome to mirror list, hosted at ThFree Co, Russian Federation.

profile.py « bockbuild - github.com/mono/bockbuild.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8bac28b96543fe2952ff5f73264ce11da522851e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os
from optparse import OptionParser
from util.util import *
from util.csproj import *
from environment import Environment
from package import *
import collections
import hashlib

class Profile:
	def __init__ (self, prefix = False):
		self.name = 'bockbuild'
		self.root = os.getcwd ()
		self.resource_root = os.path.realpath (os.path.join('..', '..', 'packages'))
		self.build_root = os.path.join (self.root, 'build-root')
		self.staged_prefix = os.path.join (self.root, 'stage-root')
		self.toolchain_root = os.path.join (self.root, 'toolchain-root')
		self.prefix = prefix if prefix else os.path.join (self.root, 'install-root')
                self.source_cache = os.getenv('BOCKBUILD_SOURCE_CACHE') or os.path.realpath (os.path.join (self.root, 'cache'))
                self.cpu_count = get_cpu_count ()
		self.host = get_host ()
		self.uname = backtick ('uname -a')

		self.env = Environment (self)
		self.env.set ('BUILD_PREFIX', '%{prefix}')
		self.env.set ('BUILD_ARCH', '%{arch}')
		self.env.set ('BOCKBUILD_ENV', '1')
		self.packages = []
		self.profile_name = self.__class__.__name__

		find_git (self)

		self.bockbuild_revision = git_get_revision(self)

		loginit ('bockbuild rev. %s %s' % (self.bockbuild_revision, "" or "(branch: %s)" % git_get_branch(self)))
		info ('cmd: %s' % ' '.join(sys.argv))

		self.parse_options ()

		packages_to_build = self.cmd_args
		self.verbose = self.cmd_options.verbose
		self.run_phases = self.default_run_phases
		self.arch = self.cmd_options.arch
		self.unsafe = self.cmd_options.unsafe

	def parse_options (self):
		self.default_run_phases = ['prep', 'build', 'install']
		parser = OptionParser (usage = 'usage: %prog [options] [package_names...]')
		parser.add_option ('-b', '--build',
			action = 'store_true', dest = 'do_build', default = False,
			help = 'build the profile')
		parser.add_option ('-P', '--package',
			action = 'store_true', dest = 'do_package', default = False,
			help = 'package the profile')
		parser.add_option ('-z', '--bundle',
			action = 'store_true', dest = 'do_bundle', default = False,
			help = 'create a distributable bundle from a build')
		parser.add_option ('-o', '--output-dir',
			default = None, action = 'store', dest = 'output_dir',
			help = 'output directory for housing the bundle (--bundle|-z)')
		parser.add_option ('-k', '--skeleton-dir',
			default = None, action = 'store',  dest = 'skeleton_dir',
			help = 'skeleton directory containing misc files to copy into bundle (--bundle|-z)')
		parser.add_option ('-v', '--verbose',
			action = 'store_true', dest = 'verbose', default = False,
			help = 'show all build output (e.g. configure, make)')
		parser.add_option ('-i', '--include-phase',
			action = 'append', dest = 'include_run_phases', default = [],
			help = 'explicitly include a build phase to run %s' % self.default_run_phases)
		parser.add_option ('-x', '--exclude-phase',
			action = 'append', dest = 'exclude_run_phases', default = [],
			help = 'explicitly exclude a build phase from running %s' % self.default_run_phases)
		parser.add_option ('-s', '--only-sources',
			action = 'store_true', dest = 'only_sources', default = False,
			help = 'only fetch sources, do not run any build phases')
		parser.add_option ('-d', '--debug', default = False,
			action = 'store_true', dest = 'debug',
			help = 'Build with debug flags enabled')
		parser.add_option ('-e', '--environment', default = False,
			action = 'store_true', dest = 'dump_environment',
			help = 'Dump the profile environment as a shell-sourceable list of exports ')
		parser.add_option ('-r', '--release', default = False,
			action = 'store_true', dest = 'release_build',
			help = 'Whether or not this build is a release build')
		parser.add_option ('', '--csproj-env', default = False,
			action = 'store_true', dest = 'dump_environment_csproj',
			help = 'Dump the profile environment xml formarted for use in .csproj files')
		parser.add_option ('', '--csproj-insert', default = None,
			action = 'store', dest = 'csproj_file',
			help = 'Inserts the profile environment variables into VS/MonoDevelop .csproj files')
		parser.add_option ('', '--arch', default = 'default',
			action = 'store', dest = 'arch',
			help = 'Select the target architecture(s) for the package')
		parser.add_option ('', '--shell', default = False,
			action = 'store_true', dest = 'shell',
			help = 'Get an shell with the package environment')
		parser.add_option ('', '--unsafe', default = False,
			action = 'store_true', dest = 'unsafe',
			help = 'Prevents full rebuilds when a build environment change is detected. Useful for debugging.')

		self.parser = parser
		self.cmd_options, self.cmd_args = parser.parse_args ()

	def make_package (self, output_dir):
		sys.exit ("Package support not implemented for this profile")

	def bundle (self, output_dir):
		sys.exit ('Bundle support not implemented for this profile')

	def build (self):
		if self.cmd_options.dump_environment:
			self.env.compile ()
			self.env.dump ()
			sys.exit (0)

		if self.cmd_options.dump_environment_csproj:
			# specify to use our GAC, else MonoDevelop would
			# use its own 
			self.env.set ('MONO_GAC_PREFIX', self.staged_prefix)

			self.env.compile ()
			self.env.dump_csproj ()
			sys.exit (0)

		if self.cmd_options.csproj_file is not None:
			self.env.set ('MONO_GAC_PREFIX', self.staged_prefix)
			self.env.compile ()
			self.env.write_csproj (self.cmd_options.csproj_file)
			sys.exit (0)

		if not self.cmd_options.include_run_phases == []:
			self.run_phases = self.cmd_options.include_run_phases
		for exclude_phase in self.cmd_options.exclude_run_phases:
			self.run_phases.remove (exclude_phase)
		if self.cmd_options.only_sources:
			self.run_phases = []

		for phase_set in [self.run_phases,
			self.cmd_options.include_run_phases, self.cmd_options.exclude_run_phases]:
			for phase in phase_set:
				if phase not in self.default_run_phases:
					sys.exit ('Invalid run phase \'%s\'' % phase)

		log (0, 'Loaded profile \'%s\' (arch: %s)' % (self.name, self.arch))
		log (0, 'Setting environment variables')

		Profile.setup (self)
		self.setup ()

		self.full_rebuild = self.track_env ()

		if self.full_rebuild:
			warn ('Build environment changed')
			for d in os.listdir (self.build_root):
				if d.endswith ('.cache') or d.endswith ('.artifact'):
					os.remove (os.path.join(self.build_root, d))


		if self.cmd_options.shell:
			title ('Shell')
			self.shell ()

		if self.cmd_options.do_build:

			ensure_dir (self.staged_prefix, True)
			ensure_dir (self.toolchain_root, True)

			title ('Building toolchain')
			for package in self.toolchain_packages.values ():
				package.staged_profile = self.toolchain_root
				package.package_prefix = self.toolchain_root
				package.start_build ('darwin-32')

			title ('Building release')
			for package in self.release_packages.values ():
				package.staged_profile = self.staged_prefix
				package.package_prefix = self.prefix
				package.start_build (self.arch)

		if self.cmd_options.do_bundle:
			if not self.cmd_options.output_dir == None:
				self.bundle_output_dir = os.path.join (os.getcwd (), 'bundle')
			if not self.cmd_options.skeleton_dir == None:
				self.bundle_skeleton_dir = os.path.join (os.getcwd (), 'skeleton')
			self.bundle ()
			return

		if self.cmd_options.do_package:
			title ('Packaging')
			self.process_release ()
			self.package ()

	def load_package (self, path):
		Package.last_instance = None
		exec compile (open (path).read (), path, 'exec')
		if Package.last_instance == None:
			error ('%s does not provide a valid package.' % path)

		new_package = Package.last_instance
		new_package._path = path
		return new_package

	def track_env (self):
		tracked_env = []

		if self.unsafe:
			warn ('Running with --unsafe, build environment not checked for changes')

		self.env.compile ()
		self.env.export ()
		tracked_env.extend (self.env.serialize ())

		changed = False if self.unsafe else update (tracked_env, os.path.join (self.root, 'global.env'), show_diff = True)



		self.envfile = os.path.join (self.root, self.profile_name) + '_env.sh'
		self.env.dump (self.envfile)
		os.chmod (self.envfile, 0755)

		return changed

	def setup (self):
		progress ('Setting up packages')
		ensure_dir (self.source_cache, False)
		ensure_dir (self.build_root, False)

		self.toolchain_packages = collections.OrderedDict()
		self.release_packages = collections.OrderedDict()
		Package.profile = self

		for path in self.packages:
			package = self.load_package (path)

			Profile.setup_package (self, package)
			Profile.fetch_package (self, package)

			if package.build_dependency:
				self.toolchain_packages[package.name] = package
			else:
				self.release_packages[package.name] = package

	def fetch_package (self, package):
		clean_func = None
		def fetch ():
			return package._fetch_sources (self.build_root,
				package.workspace, self.resource_root, self.source_cache)

		clean_func = retry(fetch)
		package.clean = clean_func

	def setup_package (self, package):
		if package.build_dependency == True:
			package.staged_profile = self.toolchain_root
			package.package_prefix = self.toolchain_root
		else:
			package.staged_profile = self.staged_prefix
			package.package_prefix = self.prefix

		expand_macros (package.sources, package)
		package.source_dir_name = expand_macros (package.source_dir_name, package)
		package.workspace = os.path.join (self.build_root, package.source_dir_name)
		package.build_artifact = os.path.join (self.build_root, package.name + '.artifact')

	class FileProcessor (object):
		def __init__ (self, harness = None, extra_files = None):
			self.harness = harness
			self.files = list (extra_files) if extra_files else list ()
			self.root = None

		def relpath (self, path):
			return os.path.relpath (path, self.root)

		def process (self, path):
			return

		def run (self):
			for path in self.files:
				self.harness (path, self.process)
		def end (self):
			return


	def postprocess (self, processors, directory, filefilter = None):
		def simple_harness (path, func):
			hash = hashlib.sha1(open(path).read()).hexdigest()
			func (path)
			if not os.path.exists (path):
				warn ('Removed file: %s' % path)
			if hash != hashlib.sha1(open(path).read()).hexdigest():
				warn ('Changed file: %s' % path)

		for path in filter (filefilter, iterate_dir (directory)):
			filetype = get_filetype (path)
			for proc in processors:
				proc.root = directory
				if proc.match (path, filetype) == True:
					trace ('%s  matched %s / %s' % (proc.__class__.__name__, os.path.basename(path), filetype) )
					proc.files.append (path)

		for proc in processors:
			if proc.harness == None:
				proc.harness = simple_harness
			trace ('%s: %s items' % (proc.__class__.__name__ , len (proc.files)))
			proc.run ()


		for proc in processors:
			proc.end ()
			proc.harness = None
			proc.files = []


class Bockbuild:
	def main ():
		profile.prep_options ()
		profile.build ()
		profile.package ()