10 from optparse import OptionGroup
11 from optparse import OptionGroup
12 from optparse import OptionParser
13 from sys import stderr
14 from sys import stdout
16 ###############################################################################
18 def errf( format, *args ):
19 stderr.write( ('ERROR: ' + format + '\n') % args )
22 def outf( format, *args ):
23 stdout.write( (format + '\n') % args )
25 ###############################################################################
27 ## Expand values of iterable object into a decent string representation.
29 def expandValues( obj ):
33 return '{ ' + buf[2:] + ' }'
35 ###############################################################################
37 ## Find executable by searching path.
38 ## On success, returns full pathname of executable.
39 ## On fail, returns None.
41 def findExecutable( name ):
42 if len( os.path.split(name)[0] ):
43 return name if os.access( name, os.X_OK ) else None
45 if not os.environ.has_key( 'PATH' ) or os.environ[ 'PATH' ] == '':
48 path = os.environ['PATH']
50 for dir in path.split( os.pathsep ):
51 f = os.path.join( dir, name )
52 if os.access( f, os.X_OK ):
56 ###############################################################################
58 def computeDefaultMakeJobs():
59 ## good for darwin9.6.0 and linux
61 n = os.sysconf( 'SC_NPROCESSORS_ONLN' )
69 n = int( os.environ['NUMBER_OF_PROCESSORS'] )
77 ###############################################################################
79 ## taken from python2.6 -- we need it
80 def relpath(path, start=os.path.curdir):
81 """Return a relative version of a path"""
84 raise ValueError("no path specified")
86 start_list = os.path.abspath(start).split(os.sep)
87 path_list = os.path.abspath(path).split(os.sep)
89 # Work out how much of the filepath is shared by start and path.
90 i = len(os.path.commonprefix([start_list, path_list]))
92 rel_list = [os.pardir] * (len(start_list)-i) + path_list[i:]
95 return os.path.join(*rel_list)
97 ###############################################################################
99 # compute project dir which should be 2 dirs below this script
100 build_dir = os.curdir
101 project_dir = os.path.normpath( sys.argv[0] )
103 project_dir = os.path.dirname( project_dir )
104 if len( project_dir ) == 0:
105 project_dir = os.curdir
107 ###############################################################################
109 ## model gnu-autotools platform guess
112 ## (PROC)-(VENDOR)-(SYSTEM)(RELEASE)-(EXTRA)
115 ## i386-apple-darwin9.6.0 (Mac OS X 10.5.6 Intel)
116 ## powerpc-apple-darwin9.6.0 (Mac OS X 10.5.6 PPC)
117 ## i686-pc-cygwin (Cygwin, Microsoft Vista)
118 ## x86_64-unknown-linux-gnu (Linux, Fedora 10 x86_64)
121 def __init__( self ):
122 self.proc = 'unknown'
123 self.vendor = 'unknown'
124 self.system = 'unknown'
125 self.release = '0.0.0'
128 p_system = platform.system().lower()
129 p_release = platform.release().lower()
130 p_processor = platform.processor().lower()
131 p_machine = platform.machine().lower()
133 if re.match( 'cygwin', p_system ):
134 self.proc = p_machine
136 self.system = 'cygwin'
139 elif re.match( 'darwin', p_system ):
140 self.proc = p_machine
141 self.vendor = 'apple'
142 self.system = p_system
143 self.release = p_release
145 elif re.match( 'linux', p_system ):
146 self.proc = p_machine
147 self.vendor = 'unknown'
148 self.system = p_system
152 errf( 'unrecognized host system: %s', p_system )
156 return '%s-%s-%s%s-%s' % (self.proc,self.vendor,self.system,self.release,self.extra)
158 return '%s-%s-%s%s' % (self.proc,self.vendor,self.system,self.release)
160 def match( self, spec ):
161 return fnmatch.fnmatch( str(self), spec )
163 ###############################################################################
165 # a tool represents a command-line tool which may be searched for in PATH
167 def __init__( self, parent, optional, var, *pool ):
169 self.optional = optional
174 parent.register( self )
176 def addToConfig( self, config ):
177 config.add( self.var, self.found )
179 def addToGroup( self, group ):
180 group.add_option( '', '--' + self.name, help='specify %s location' % (self.name), default=None, metavar='EXE' )
182 def locate( self, options ):
183 spec = options.__dict__[self.name]
184 pool = self.pool if not spec else [spec]
186 self.found = findExecutable( p )
188 outf( 'located %s: %s', self.name, self.found )
191 outf( 'missing: %s (optional)', self.name )
193 errf( 'unable to locate tool: %s', self.name )
195 ## a select tool picks first found from a list of tools
196 class SelectTool( Tool ):
197 def __init__( self, parent, var, name, *pool ):
205 self.poolMap[p.name] = p
207 parent.register( self )
209 def addToConfig( self, config ):
210 config.add( self.var, self.found )
212 def addToGroup( self, group ):
213 group.add_option( '', '--' + self.name, help='select %s mode: %s' % (self.name,expandValues(self.poolMap)),
214 default=self.name, metavar='MODE' )
216 def locate( self, options ):
217 spec = options.__dict__[self.name]
218 if spec in self.poolMap:
224 outf( 'selected %s: %s', self.name, self.found )
226 errf( 'require at least one location of: %s', expandValues( self.poolMap ))
228 ###############################################################################
231 def __init__( self ):
233 Tool( self, False, 'AR.exe', 'ar' )
234 Tool( self, False, 'CP.exe', 'cp' )
235 Tool( self, True, 'CURL.exe', 'curl' )
236 Tool( self, False, 'GCC.gcc', 'gcc', 'gcc-4' )
237 Tool( self, False, 'M4.exe', 'm4' )
238 Tool( self, False, 'MKDIR.exe', 'mkdir' )
239 Tool( self, False, 'PATCH.exe', 'patch' )
240 Tool( self, False, 'RM.exe', 'rm' )
241 Tool( self, False, 'TAR.exe', 'tar' )
242 Tool( self, True, 'WGET.exe', 'wget' )
244 SelectTool( self, 'FETCH.select', 'fetch', self.wget, self.curl )
246 def register( self, item ):
247 self.__dict__[item.name] = item
248 self.items.append( item )
250 ###############################################################################
252 class OptionMode( list ):
253 def __init__( self, default, *items ):
254 super( OptionMode, self ).__init__( items )
255 self.default = items[default]
256 self.mode = self.default
259 return ' '.join( self ).replace( self.mode, '*'+self.mode )
261 def addToGroup( self, group, option, name ):
262 group.add_option( '', option, help='select %s mode: %s' % (name,self), default=self.mode, metavar='MODE' )
264 def setFromOption( self, name, mode ):
266 errf( 'invalid %s mode: %s', name, mode )
269 ###############################################################################
275 makeTool = Tool( None, False, 'CONF.make', 'gmake', 'make' )
278 debugMode = OptionMode( 0, 'none', 'min', 'std', 'max' )
279 optimizeMode = OptionMode( 1, 'none', 'speed', 'size' )
281 ## populate platform-specific architecture modes
282 if guessHost.match( 'i386-*-darwin8.*' ):
283 archMode = OptionMode( 0, 'i386', 'ppc' )
284 elif guessHost.match( 'powerpc-*-darwin8.*' ):
285 archMode = OptionMode( 1, 'i386', 'ppc' )
286 elif guessHost.match( 'i386-*-darwin9.*' ):
287 archMode = OptionMode( 0, 'i386', 'x86_64', 'ppc', 'ppc64' )
288 elif guessHost.match( 'powerpc-*-darwin9.*' ):
289 archMode = OptionMode( 2, 'i386', 'x86_64', 'ppc', 'ppc64' )
291 archMode = OptionMode( 0, guessHost.proc )
293 if guessHost.match( '*-*-darwin*' ):
294 d_prefix = '/Applications'
296 d_prefix = '/usr/local'
299 parser = OptionParser( 'Usage: %prog' )
301 group = OptionGroup( parser, 'Installation Options' )
302 group.add_option( '', '--prefix', default=d_prefix, action='store',
303 help='install destination for final products (%s)' % (d_prefix) )
304 parser.add_option_group( group )
306 group = OptionGroup( parser, 'Feature Options' )
307 group.add_option( '', '--disable-xcode', default=False, action='store_true',
308 help='disable Xcode (Darwin only)' )
309 group.add_option( '', '--disable-gtk', default=False, action='store_true',
310 help='disable GTK GUI (Linux only)' )
311 parser.add_option_group( group )
313 ## add launch options
314 group = OptionGroup( parser, 'Launch Options' )
315 group.add_option( '', '--launch', default=False, action='store_true',
316 help='launch build, capture log and wait for completion' )
317 group.add_option( '', '--launch-jobs', default=1, action='store', metavar='N',
318 help='allow N jobs at once; 0 to match CPU count (1)' )
319 group.add_option( '', '--launch-args', default=None, action='store', metavar='ARGS',
320 help='specify additional ARGS for launch command' )
321 group.add_option( '', '--launch-dir', default='build', action='store', metavar='DIR',
322 help='specify scratch DIR to use for build (build)' )
323 group.add_option( '', '--launch-force', default=False, action='store_true',
324 help='force use of scratch directory even if exists' )
325 group.add_option( '', '--launch-log', default='log.txt', action='store', metavar='FILE',
326 help='specify log FILE (log.txt)' )
327 group.add_option( '', '--launch-quiet', default=False, action='store_true',
328 help='do not echo build output' )
329 parser.add_option_group( group )
331 ## add compile options
332 group = OptionGroup( parser, 'Compiler Options' )
333 debugMode.addToGroup( group, '--debug', 'debug' )
334 optimizeMode.addToGroup( group, '--optimize', 'optimize' )
335 archMode.addToGroup( group, '--arch', 'architecutre' )
336 parser.add_option_group( group )
339 group = OptionGroup( parser, 'Tool Options' )
340 makeTool.addToGroup( group )
341 for tool in tools.items:
342 tool.addToGroup( group )
343 parser.add_option_group( group )
345 (options, args) = parser.parse_args()
347 ## recompute values when launch mode
349 options.launch_jobs = int(options.launch_jobs)
350 build_dir = options.launch_dir
351 if os.path.isabs( build_dir ):
352 project_dir = os.getcwd()
354 project_dir = os.path.normpath( relpath( project_dir, build_dir ))
355 if options.launch_jobs == 0:
356 options.launch_jobs = computeDefaultMakeJobs()
357 if options.launch_jobs < 1:
358 options.launch_jobs = 1
359 elif options.launch_jobs > 8:
360 options.launch_jobs = 8
362 ## make sure configure does not run in source root
363 if os.path.abspath( project_dir ) == os.path.abspath( build_dir ):
364 errf( 'scratch (build) directory must not be the same as source root' )
367 debugMode.setFromOption( 'debug', options.debug )
368 optimizeMode.setFromOption( 'optimize', options.optimize )
369 archMode.setFromOption( 'architecture', options.arch )
371 ## update guessBuild as per architecture mode
372 if guessHost.match( '*-*-darwin*' ):
373 if archMode.mode == 'i386':
374 guessBuild.proc = 'i386'
375 elif archMode.mode == 'x86_64':
376 guessBuild.proc = 'x86_64'
377 elif archMode.mode == 'ppc':
378 guessBuild.proc = 'powerpc'
379 elif archMode.mode == 'ppc64':
380 guessBuild.proc = 'powerpc64'
382 guessBuild.proc = archMode.mode
383 guessBuild.cross = 0 if archMode.default == archMode.mode else 1
386 makeTool.locate( options )
387 for tool in tools.items:
388 tool.locate( options )
390 ###############################################################################
392 ## Repository object.
393 ## Holds information gleaned from subversion working dir.
395 ## Builds are classed into one of the following types:
398 ## must be built from official svn with '/tags/' in the url
400 ## must be built from official svn but is not a release
405 def __init__( self ):
406 self.url = 'svn://nowhere.com/project/unknown'
407 self.root = 'svn://nowhere.com/project'
408 self.branch = 'unknown'
409 self.uuid = '00000000-0000-0000-0000-000000000000';
411 self.date = '0000-00-00 00:00:00 -0000'
412 self.wcversion = 'exported'
414 self.type = 'unofficial'
416 # parse output: svnversion PROJECT_DIR
417 cmd = 'svnversion ' + project_dir
418 print 'running: %s' % (cmd)
420 p = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
422 if p.returncode == 0:
423 self.wcversion = p.stdout.readline().rstrip()
427 # parse output: svn info PROJECT_DIR
428 cmd = 'svn info ' + project_dir
429 print 'running: %s' % (cmd)
431 p = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
433 if p.returncode == 0:
434 for line in p.stdout:
435 (name,value) = re.match( '([^:]+):\\s+(.+)', line.rstrip() ).groups()
438 elif name == 'Repository Root':
440 elif name == 'Repository UUID':
442 elif name == 'Revision':
443 self.rev = int( value )
444 elif name == 'Last Changed Date':
445 # strip chars in parens
446 if value.find( ' (' ):
447 self.date = value[0:value.find(' (')]
453 i = self.url.rfind( '/' )
454 if i != -1 and i < len(self.url)-1:
455 self.branch = self.url[i+1:]
457 # official UUID behavior
458 if self.uuid == 'b64f7644-9d1e-0410-96f1-a4d463321fa5':
460 m = re.match( '([^:]+)://([^/]+)/(.+)', self.url )
461 if m and re.match( 'tags/', m.group( 3 )):
462 self.type = 'release'
464 self.type = 'developer'
466 ###############################################################################
469 ## Contains manually updated version numbers consistent with HB releases
470 ## and other project metadata.
473 def __init__( self ):
474 self.name = 'HandBrake'
475 self.name_lower = self.name.lower()
476 self.name_upper = self.name.upper()
477 self.acro_lower = 'hb'
478 self.acro_upper = 'HB'
479 self.url_website = 'http://handbrake.fr'
480 self.url_community = 'http://forum.handbrake.fr'
481 self.url_irc = 'irc://irc.freenode.net/handbrake'
487 self.version = '%d.%d.%d' % (self.vmajor,self.vminor,self.vpoint)
488 appcastfmt = 'http://handbrake.fr/appcast%s.xml'
490 if repo.type == 'release':
491 self.version_formal = '%s Release' % (self.version)
492 self.url_appcast = appcastfmt % ('')
493 elif repo.type == 'developer':
494 self.version_formal = '%s Developer ' % (self.version)
495 self.url_appcast = appcastfmt % ('_unstable')
497 self.version_formal = '%s Unnofficial ' % (self.version)
498 self.url_appcast = appcastfmt % ('_unofficial')
500 self.title = '%s %s' % (self.name,self.version)
501 self.build = time.strftime('%Y%m%d') + '01'
503 ###############################################################################
505 ## Config object used to output gnu-make or gnu-m4 output.
507 ## Use add() to add NAME/VALUE pairs suitable for both make/m4.
508 ## Use addBlank() to add a linefeed for both make/m4.
509 ## Use addMake() to add a make-specific line.
510 ## Use addM4() to add a m4-specific line.
513 def __init__( self ):
516 def add( self, name, value ):
517 self._items.append( (name,value) )
519 def addBlank( self ):
520 self._items.append( None )
522 def addComment( self, format, *args ):
523 self.addMake( '## ' + format % args )
524 self.addM4( 'dnl ' + format % args )
526 def addMake( self, line ):
527 self._items.append( ('?make',line) )
529 def addM4( self, line ):
530 self._items.append( ('?m4',line) )
532 def output( self, file, type ):
534 for item in self._items:
535 if item == None or item[0].find( '?' ) == 0:
537 if len(item[0]) > namelen:
538 namelen = len(item[0])
539 for item in self._items:
542 file.write( 'dnl\n' )
546 if item[0].find( '?' ) == 0:
547 if item[0].find( type, 1 ) == 1:
548 file.write( '%s\n' % (item[1]) )
552 self._outputM4( file, namelen, item[0], item[1] )
554 self._outputMake( file, namelen, item[0], item[1] )
556 def _outputMake( self, file, namelen, name, value ):
557 file.write( '%-*s = %s\n' % (namelen, name, value ))
559 def _outputM4( self, file, namelen, name, value ):
561 name = '<<__%s>>,' % name.replace( '.', '_' )
562 file.write( 'define(%-*s <<%s>>)dnl\n' % (namelen, name, value ))
564 ###############################################################################
566 ## create configure line, stripping arg --launch, quoting others
568 for arg in sys.argv[1:]:
569 #if arg.find( '--launch' ) == 0:
571 if arg == '--launch':
573 configure.append( '"%s"' % (arg.replace('"', '\\"')) )
575 ## create singletones
580 config.addComment( 'generated by configure on %s', time.strftime( '%c' ))
583 config.add( 'CONF.args', ' '.join( configure ))
586 config.add( 'HB.title', project.title )
587 config.add( 'HB.name', project.name )
588 config.add( 'HB.name.lower', project.name_lower )
589 config.add( 'HB.name.upper', project.name_upper )
590 config.add( 'HB.acro.lower', project.acro_lower )
591 config.add( 'HB.acro.upper', project.acro_upper )
593 config.add( 'HB.url.website', project.url_website )
594 config.add( 'HB.url.community', project.url_community )
595 config.add( 'HB.url.irc', project.url_irc )
596 config.add( 'HB.url.appcast', project.url_appcast )
598 config.add( 'HB.version.major', project.vmajor )
599 config.add( 'HB.version.minor', project.vminor )
600 config.add( 'HB.version.point', project.vpoint )
601 config.add( 'HB.version', project.version )
602 config.add( 'HB.version.formal', project.version_formal )
603 config.add( 'HB.version.hex', '%04x%02x%02x%02x%06x' % (project.vmajor,project.vminor,project.vpoint,0,repo.rev) )
605 config.add( 'HB.build', project.build )
607 config.add( 'HB.repo.url', repo.url )
608 config.add( 'HB.repo.root', repo.root )
609 config.add( 'HB.repo.branch', repo.branch )
610 config.add( 'HB.repo.uuid', repo.uuid )
611 config.add( 'HB.repo.rev', repo.rev )
612 config.add( 'HB.repo.date', repo.date )
613 config.add( 'HB.repo.wcversion', repo.wcversion )
614 config.add( 'HB.repo.official', repo.official )
615 config.add( 'HB.repo.type', repo.type )
618 config.add( 'HOST.spec', guessHost )
619 config.add( 'HOST.proc', guessHost.proc )
620 config.add( 'HOST.vendor', guessHost.vendor )
621 config.add( 'HOST.system', guessHost.system )
622 config.add( 'HOST.release', guessHost.release )
623 config.add( 'HOST.extra', guessHost.extra )
626 config.add( 'BUILD.spec', guessBuild )
627 config.add( 'BUILD.proc', guessBuild.proc )
628 config.add( 'BUILD.vendor', guessBuild.vendor )
629 config.add( 'BUILD.system', guessBuild.system )
630 config.add( 'BUILD.release', guessBuild.release )
631 config.add( 'BUILD.extra', guessBuild.extra )
632 config.add( 'BUILD.cross', guessBuild.cross )
633 config.add( 'BUILD.date', time.strftime('%c') )
634 config.add( 'BUILD.arch', archMode.mode )
637 config.add( 'BUILD/', os.curdir + os.sep )
638 config.add( 'PROJECT/', project_dir + os.sep )
641 config.add( 'INSTALL.prefix', options.prefix )
642 config.add( 'INSTALL.prefix/', '$(INSTALL.prefix)/' )
645 config.add( 'FEATURE.xcode', 0 if options.disable_xcode else 1 )
646 config.add( 'FEATURE.gtk', 0 if options.disable_gtk else 1 )
649 config.addMake( '## include main definitions' )
650 config.addMake( 'include $(PROJECT/)make/include/main.defs' )
653 for tool in tools.items:
654 tool.addToConfig( config )
657 config.add( 'GCC.archs', archMode.mode if guessBuild.cross else '' )
658 config.add( 'GCC.g', options.debug )
659 config.add( 'GCC.O', options.optimize )
662 config.addMake( '## include (optional) customization file' )
663 config.addMake( '-include $(BUID/)GNUmakefile.custom' )
666 config.addMake( '## include main rules' )
667 config.addMake( 'include $(PROJECT/)make/include/main.rules' )
669 ###############################################################################
671 # generate make or m4 file
672 def generate( type ):
674 fname = 'GNUmakefile'
676 fname = os.path.join( 'project', project.name_lower + '.m4' )
678 raise ValueError, 'unknown file type: ' + type
680 ftmp = fname + '.tmp'
682 pdir = os.path.dirname( fname )
684 if not os.path.exists( pdir ):
689 outf( 'generating %s', fname )
690 file = open( ftmp, 'w' )
691 config.output( file, type )
702 errf( 'failed writing to %s\n%s', ftmp, x )
705 os.rename( ftmp, fname )
707 errf( 'failed writing to %s\n%s', fname, x )
709 ###############################################################################
711 if not options.launch:
716 ###############################################################################
718 if os.path.exists( options.launch_dir ):
719 if not options.launch_force:
720 errf( 'scratch directory already exists: %s', options.launch_dir )
722 outf( 'creating %s', options.launch_dir )
723 os.makedirs( options.launch_dir )
725 outf( 'chdir %s', options.launch_dir )
726 os.chdir( options.launch_dir )
730 outf( 'opening %s', options.launch_log )
732 log = open( options.launch_log, 'w' )
734 errf( 'open failure: %s', x )
736 cmd = '%s -j%d' % (makeTool.found,options.launch_jobs)
737 if options.launch_args:
738 cmd += ' ' + options.launch_args
741 timeBegin = time.time()
742 s = '###\n### TIME: %s\n### launch: %s\n###\n' % (time.asctime(),cmd)
743 stdout.write( s ); stdout.flush()
744 log.write( s ); log.flush()
748 pipe = subprocess.Popen( cmd, shell=True, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
750 errf( 'launch failure: %s', x )
751 for line in pipe.stdout:
752 if not options.launch_quiet:
753 stdout.write( line ); stdout.flush()
754 log.write( line ); log.flush()
758 timeEnd = time.time()
759 elapsed = timeEnd - timeBegin
760 result = '%s (exit code %d)' % ('success' if pipe.returncode == 0 else 'failed',pipe.returncode)
761 s = '###\n### TIME: %s\n### finished: %.2f seconds\n### %s\n###\n' % (time.asctime(),elapsed,result)
762 stdout.write( s ); stdout.flush()
763 log.write( s ); log.flush()