OSDN Git Service

BuildSystem:
[handbrake-jp/handbrake-jp-git.git] / make / configure.py
1 import fnmatch
2 import optparse
3 import os
4 import platform
5 import re
6 import subprocess
7 import sys
8 import time
9
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
15
16 ###############################################################################
17
18 def errf( format, *args ):
19     stderr.write( ('ERROR: ' + format + '\n') % args )
20     sys.exit( 1 )
21
22 def outf( format, *args ):
23     stdout.write( (format + '\n') % args )
24
25 ###############################################################################
26
27 ## Expand values of iterable object into a decent string representation.
28 ##
29 def expandValues( obj ):
30     buf = ''
31     for v in obj:
32         buf += ', ' + v
33     return '{ ' + buf[2:] + ' }'
34
35 ###############################################################################
36
37 ## Find executable by searching path.
38 ## On success, returns full pathname of executable.
39 ## On fail, returns None.
40 ##
41 def findExecutable( name ):
42     if len( os.path.split(name)[0] ):
43         return name if os.access( name, os.X_OK ) else None
44
45     if not os.environ.has_key( 'PATH' ) or os.environ[ 'PATH' ] == '':
46         path = os.defpath
47     else:
48         path = os.environ['PATH']
49
50     for dir in path.split( os.pathsep ):
51         f = os.path.join( dir, name )
52         if os.access( f, os.X_OK ):
53             return f
54     return None
55
56 ###############################################################################
57
58 def computeDefaultMakeJobs():
59     ## good for darwin9.6.0 and linux
60     try:
61         n = os.sysconf( 'SC_NPROCESSORS_ONLN' )
62         if n < 1:
63             n = 1
64         return n
65     except:
66         pass
67     ## windows
68     try:
69         n = int( os.environ['NUMBER_OF_PROCESSORS'] )
70         if n < 1:
71             n = 1
72         return n
73     except:
74         pass
75     return 1
76
77 ###############################################################################
78
79 ## taken from python2.6 -- we need it
80 def relpath(path, start=os.path.curdir):
81     """Return a relative version of a path"""
82
83     if not path:
84         raise ValueError("no path specified")
85
86     start_list = os.path.abspath(start).split(os.sep)
87     path_list = os.path.abspath(path).split(os.sep)
88
89     # Work out how much of the filepath is shared by start and path.
90     i = len(os.path.commonprefix([start_list, path_list]))
91
92     rel_list = [os.pardir] * (len(start_list)-i) + path_list[i:]
93     if not rel_list:
94         return os.path.curdir
95     return os.path.join(*rel_list)
96
97 ###############################################################################
98
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] )
102 for i in range( 2 ):
103     project_dir = os.path.dirname( project_dir )
104 if len( project_dir ) == 0:
105     project_dir = os.curdir
106
107 ###############################################################################
108
109 ## model gnu-autotools platform guess
110 ##
111 ## native format:
112 ##   (PROC)-(VENDOR)-(SYSTEM)(RELEASE)-(EXTRA)
113 ##
114 ## examples:
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)
119 ##
120 class Guess:
121     def __init__( self ):
122         self.machine = 'unknown'
123         self.vendor  = 'unknown'
124         self.system  = 'unknown'
125         self.systemc = 'Unknown'
126         self.release = '0.0.0'
127         self.extra   = ''
128
129         p_system    = platform.system().lower()
130         p_systemc   = platform.system()
131         p_release   = platform.release().lower()
132         p_processor = platform.processor().lower()
133         p_machine   = platform.machine().lower()
134
135         if re.match( 'cygwin', p_system ):
136             self.machine = p_machine
137             self.vendor  = 'pc'
138             self.system  = 'cygwin'
139             self.systemc = 'Cygwin'
140             self.release = ''
141             self.extra   = ''
142         elif re.match( 'darwin', p_system ):
143             self.machine = p_machine
144             self.vendor  = 'apple'
145             self.system  = p_system
146             self.systemc = p_systemc
147             self.release = p_release
148             self.extra   = ''
149         elif re.match( 'linux', p_system ):
150             self.machine = p_machine
151             self.vendor  = 'unknown'
152             self.system  = p_system
153             self.systemc = p_systemc
154             self.release = ''
155             self.extra   = 'gnu'
156             self.title   = 'Linux %s' % (p_machine)
157         else:
158             errf( 'unrecognized host system: %s', p_system )
159
160     def __str__( self ):
161         if len(self.extra):
162             return '%s-%s-%s%s-%s' % (self.machine,self.vendor,self.system,self.release,self.extra)
163         else:
164             return '%s-%s-%s%s' % (self.machine,self.vendor,self.system,self.release)
165
166     def match( self, spec ):
167         return fnmatch.fnmatch( str(self), spec )
168
169 ###############################################################################
170
171 # a tool represents a command-line tool which may be searched for in PATH
172 class Tool:
173     def __init__( self, parent, optional, var, *pool ):
174         self.name     = pool[0]
175         self.optional = optional
176         self.var      = var
177         self.pool     = pool
178         self.found    = None
179         if parent:
180             parent.register( self )
181
182     def addToConfig( self, config ):
183         config.add( self.var, self.found )
184
185     def addToGroup( self, group ):
186         group.add_option( '', '--' + self.name, help='specify %s location' % (self.name), default=None, metavar='EXE' )
187
188     def locate( self, options ):
189         spec = options.__dict__[self.name]
190         pool = self.pool if not spec else [spec]
191         for p in pool:
192             self.found = findExecutable( p )
193             if self.found:
194                 outf( 'located %s: %s', self.name, self.found )
195                 return
196         if self.optional:
197             outf( 'missing: %s (optional)', self.name )
198         else:
199             errf( 'unable to locate tool: %s', self.name )
200
201 ## a select tool picks first found from a list of tools
202 class SelectTool( Tool ):
203     def __init__( self, parent, var, name, *pool ):
204         self.var     = var
205         self.name    = name
206         self.pool    = pool
207         self.found   = None
208
209         self.poolMap = {}
210         for p in self.pool:
211             self.poolMap[p.name] = p
212         if parent:
213             parent.register( self )
214
215     def addToConfig( self, config ):
216         config.add( self.var, self.found )
217
218     def addToGroup( self, group ):
219         group.add_option( '', '--' + self.name, help='select %s mode: %s' % (self.name,expandValues(self.poolMap)),
220             default=self.name, metavar='MODE' )
221
222     def locate( self, options ):
223         spec = options.__dict__[self.name]
224         if spec in self.poolMap:
225             self.found = spec
226             return
227         for p in self.pool:
228             if p.found:
229                 self.found = p.name
230                 outf( 'selected %s: %s', self.name, self.found )
231                 return
232         errf( 'require at least one location of: %s', expandValues( self.poolMap ))
233
234 ###############################################################################
235
236 class ToolSet:
237     def __init__( self ):
238         self.items = []
239         Tool( self, False, 'AR.exe',    'ar' )
240         Tool( self, False, 'CP.exe',    'cp' )
241         Tool( self, True,  'CURL.exe',  'curl' )
242         Tool( self, False, 'GCC.gcc',   'gcc', 'gcc-4' )
243         Tool( self, False, 'M4.exe',    'm4' )
244         Tool( self, False, 'MKDIR.exe', 'mkdir' )
245         Tool( self, False, 'PATCH.exe', 'patch' )
246         Tool( self, False, 'RM.exe',    'rm' )
247         Tool( self, False, 'TAR.exe',   'tar' )
248         Tool( self, True,  'WGET.exe',  'wget' )
249
250         SelectTool( self, 'FETCH.select', 'fetch', self.wget, self.curl )
251
252     def register( self, item ):
253         self.__dict__[item.name] = item
254         self.items.append( item )
255
256 ###############################################################################
257
258 class OptionMode( list ):
259     def __init__( self, default, *items ):
260         super( OptionMode, self ).__init__( items )
261         self.default = items[default]
262         self.mode = self.default
263
264     def __str__( self ):
265         return ' '.join( self ).replace( self.mode, '*'+self.mode )
266
267     def addToGroup( self, group, option, name ):
268         group.add_option( '', option, help='select %s mode: %s' % (name,self), default=self.mode, metavar='MODE' )
269
270     def setFromOption( self, name, mode ):
271         if mode not in self:
272             errf( 'invalid %s mode: %s', name, mode )
273         self.mode = mode
274
275 ###############################################################################
276
277 ## create singletons
278 guessHost  = Guess()
279 guessBuild = Guess()
280
281 makeTool = Tool( None, False, 'CONF.make', 'gmake', 'make' )
282 tools = ToolSet()
283
284 debugMode    = OptionMode( 0, 'none', 'min', 'std', 'max' )
285 optimizeMode = OptionMode( 1, 'none', 'speed', 'size' )
286
287 ## populate platform-specific architecture modes
288 if guessHost.match( 'i386-*-darwin8.*' ):
289     archMode = OptionMode( 0, 'i386', 'ppc' )
290 elif guessHost.match( 'powerpc-*-darwin8.*' ):
291     archMode = OptionMode( 1, 'i386', 'ppc' )
292 elif guessHost.match( 'i386-*-darwin9.*' ):
293     archMode = OptionMode( 0, 'i386', 'x86_64', 'ppc', 'ppc64' )
294 elif guessHost.match( 'powerpc-*-darwin9.*' ):
295     archMode = OptionMode( 2, 'i386', 'x86_64', 'ppc', 'ppc64' )
296 else:
297     archMode = OptionMode( 0, guessHost.machine )
298
299 if guessHost.match( '*-*-darwin*' ):
300     d_prefix = '/Applications'
301 else: 
302     d_prefix = '/usr/local'
303
304 ## create parser
305 parser = OptionParser( 'Usage: %prog' )
306
307 group = OptionGroup( parser, 'Installation Options' )
308 group.add_option( '', '--prefix', default=d_prefix, action='store',
309     help='specify destination for final products (%s)' % (d_prefix) )
310 parser.add_option_group( group )
311
312 group = OptionGroup( parser, 'Feature Options' )
313 group.add_option( '', '--disable-xcode', default=False, action='store_true',
314     help='disable Xcode (Darwin only)' )
315 group.add_option( '', '--disable-gtk', default=False, action='store_true',
316     help='disable GTK GUI (Linux only)' )
317 parser.add_option_group( group )
318
319 ## add launch options
320 group = OptionGroup( parser, 'Launch Options' )
321 group.add_option( '', '--launch', default=False, action='store_true',
322     help='launch build, capture log and wait for completion' )
323 group.add_option( '', '--launch-jobs', default=1, action='store', metavar='N',
324     help='allow N jobs at once; 0 to match CPU count (1)' )
325 group.add_option( '', '--launch-args', default=None, action='store', metavar='ARGS',
326     help='specify additional ARGS for launch command' )
327 group.add_option( '', '--launch-dir', default='build', action='store', metavar='DIR',
328     help='specify scratch DIR to use for build (build)' )
329 group.add_option( '', '--launch-force', default=False, action='store_true',
330     help='force use of scratch directory even if exists' )
331 group.add_option( '', '--launch-log', default='log.txt', action='store', metavar='FILE',
332     help='specify log FILE (log.txt)' )
333 group.add_option( '', '--launch-quiet', default=False, action='store_true',
334     help='do not echo build output' )
335 parser.add_option_group( group )
336
337 ## add compile options
338 group = OptionGroup( parser, 'Compiler Options' )
339 debugMode.addToGroup( group, '--debug', 'debug' )
340 optimizeMode.addToGroup( group, '--optimize', 'optimize' )
341 archMode.addToGroup( group, '--arch', 'architecutre' )
342 parser.add_option_group( group )
343
344 ## add tool options
345 group = OptionGroup( parser, 'Tool Options' )
346 makeTool.addToGroup( group )
347 for tool in tools.items:
348     tool.addToGroup( group )
349 parser.add_option_group( group )
350
351 (options, args) = parser.parse_args()
352
353 ## recompute values when launch mode
354 if options.launch:
355     options.launch_jobs = int(options.launch_jobs)
356     build_dir = options.launch_dir
357     if os.path.isabs( build_dir ):
358         project_dir = os.getcwd() 
359     else:
360         project_dir = os.path.normpath( relpath( project_dir, build_dir ))
361     if options.launch_jobs == 0:
362         options.launch_jobs = computeDefaultMakeJobs()
363     if options.launch_jobs < 1:
364         options.launch_jobs = 1
365     elif options.launch_jobs > 8:
366         options.launch_jobs = 8
367
368 ## make sure configure does not run in source root
369 if os.path.abspath( project_dir ) == os.path.abspath( build_dir ):
370     errf( 'scratch (build) directory must not be the same as source root' )
371
372 ## validate modes
373 debugMode.setFromOption( 'debug', options.debug )
374 optimizeMode.setFromOption( 'optimize', options.optimize )
375 archMode.setFromOption( 'architecture', options.arch )
376
377 ## update guessBuild as per architecture mode
378 if guessHost.match( '*-*-darwin*' ):
379     if archMode.mode == 'i386':
380         guessBuild.machine = 'i386'
381     elif archMode.mode == 'x86_64':
382         guessBuild.machine = 'x86_64'
383     elif archMode.mode == 'ppc':
384         guessBuild.machine = 'powerpc'
385     elif archMode.mode == 'ppc64':
386         guessBuild.machine = 'powerpc64'
387 else:
388     guessBuild.machine = archMode.mode
389 guessBuild.cross = 0 if archMode.default == archMode.mode else 1
390
391 # locate tools
392 makeTool.locate( options )
393 for tool in tools.items:
394     tool.locate( options )
395
396 ###############################################################################
397
398 ## Repository object.
399 ## Holds information gleaned from subversion working dir.
400 ##
401 ## Builds are classed into one of the following types:
402 ##
403 ##  release
404 ##      must be built from official svn with '/tags/' in the url
405 ##  developer
406 ##      must be built from official svn but is not a release
407 ##  unofficial
408 ##      all other builds
409 ##
410 class Repository:
411     def __init__( self ):
412         self.url       = 'svn://nowhere.com/project/unknown'
413         self.root      = 'svn://nowhere.com/project'
414         self.branch    = 'unknown'
415         self.uuid      = '00000000-0000-0000-0000-000000000000';
416         self.rev       = 0
417         self.date      = '0000-00-00 00:00:00 -0000'
418         self.wcversion = 'exported'
419         self.official  = 0
420         self.type      = 'unofficial'
421
422         # parse output: svnversion PROJECT_DIR
423         cmd = 'svnversion ' + project_dir
424         print 'running: %s' % (cmd)
425         try:
426             p = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
427             p.wait();
428             if p.returncode == 0:
429                 self.wcversion = p.stdout.readline().rstrip()
430         except:
431             pass
432
433         # parse output: svn info PROJECT_DIR
434         cmd = 'svn info ' + project_dir
435         print 'running: %s' % (cmd)
436         try:
437             p = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
438             p.wait();
439             if p.returncode == 0:
440                 for line in p.stdout:
441                     (name,value) = re.match( '([^:]+):\\s+(.+)', line.rstrip() ).groups()
442                     if name == 'URL':
443                         self.url = value
444                     elif name == 'Repository Root':
445                         self.root = value
446                     elif name == 'Repository UUID':
447                         self.uuid = value
448                     elif name == 'Revision':
449                         self.rev = int( value )
450                     elif name == 'Last Changed Date':
451                         # strip chars in parens
452                         if value.find( ' (' ):
453                             self.date = value[0:value.find(' (')]
454                         else:
455                             self.date = value
456         except:
457             pass
458
459         i = self.url.rfind( '/' )
460         if i != -1 and i < len(self.url)-1:
461             self.branch = self.url[i+1:]
462
463         # official UUID behavior
464         if self.uuid == 'b64f7644-9d1e-0410-96f1-a4d463321fa5':
465             self.official = 1
466             m = re.match( '([^:]+)://([^/]+)/(.+)', self.url )
467             if m and re.match( 'tags/', m.group( 3 )):
468                 self.type = 'release'
469             else:
470                 self.type = 'developer'
471
472 ###############################################################################
473
474 ## Project object.
475 ## Contains manually updated version numbers consistent with HB releases
476 ## and other project metadata.
477 ##
478 class Project:
479     def __init__( self ):
480         if repo.type == 'unofficial':
481             self.name          = 'NoNameBrand'
482             self.acro_lower    = 'nnb'
483             self.acro_upper    = 'NNB'
484             self.url_website   = 'http://nonamebrand.com'
485             self.url_community = 'http://forum.nonamebrand.com'
486             self.url_irc       = 'irc://irc.freenode.net/nonamebrand'
487         else:
488             self.name          = 'HandBrake'
489             self.acro_lower    = 'hb'
490             self.acro_upper    = 'HB'
491             self.url_website   = 'http://handbrake.fr'
492             self.url_community = 'http://forum.handbrake.fr'
493             self.url_irc       = 'irc://irc.freenode.net/handbrake'
494
495         self.name_lower = self.name.lower()
496         self.name_upper = self.name.upper()
497
498         self.vmajor = 0
499         self.vminor = 9
500         self.vpoint = 4
501
502         appcastfmt = 'http://handbrake.fr/appcast%s.xml'
503
504         if repo.type == 'release':
505             self.version = '%d.%d.%d' % (self.vmajor,self.vminor,self.vpoint)
506             self.url_appcast = appcastfmt % ('')
507             self.build = time.strftime('%Y%m%d') + '00'
508             self.title = '%s %s (%s)' % (self.name,self.version,self.build)
509         elif repo.type == 'developer':
510             self.version = 'svn%d' % (repo.rev)
511             self.url_appcast = appcastfmt % ('_unstable')
512             self.build = time.strftime('%Y%m%d') + '01'
513             self.title = '%s svn%d (%s)' % (self.name,repo.rev,self.build)
514         else:
515             self.version = 'svn%d' % (repo.rev)
516             self.version = '%d.%d.%d' % (self.vmajor,self.vminor,self.vpoint)
517             self.url_appcast = appcastfmt % ('_unofficial')
518             self.build = time.strftime('%Y%m%d') + '99'
519             self.title = 'Unofficial svn%d (%s)' % (repo.rev,self.build)
520
521 ###############################################################################
522
523 ## Config object used to output gnu-make or gnu-m4 output.
524 ##
525 ## Use add() to add NAME/VALUE pairs suitable for both make/m4.
526 ## Use addBlank() to add a linefeed for both make/m4.
527 ## Use addMake() to add a make-specific line.
528 ## Use addM4() to add a m4-specific line.
529 ##
530 class Config:
531     def __init__( self ):
532         self._items = []
533
534     def add( self, name, value ):
535         self._items.append( (name,value) )
536
537     def addBlank( self ):
538         self._items.append( None )
539
540     def addComment( self, format, *args ):
541         self.addMake( '## ' + format % args )
542         self.addM4( 'dnl ' + format % args )
543
544     def addMake( self, line ):
545         self._items.append( ('?make',line) )
546
547     def addM4( self, line ):
548         self._items.append( ('?m4',line) )
549
550     def output( self, file, type ):
551         namelen = 0
552         for item in self._items:
553             if item == None or item[0].find( '?' ) == 0:
554                 continue
555             if len(item[0]) > namelen:
556                 namelen = len(item[0])
557         for item in self._items:
558             if item == None:
559                 if type == 'm4':
560                     file.write( 'dnl\n' )
561                 else:
562                     file.write( '\n' )
563                 continue
564             if item[0].find( '?' ) == 0:
565                 if item[0].find( type, 1 ) == 1:
566                     file.write( '%s\n' % (item[1]) )
567                 continue
568
569             if type == 'm4':
570                 self._outputM4( file, namelen, item[0], item[1] )
571             else:
572                 self._outputMake( file, namelen, item[0], item[1] )
573
574     def _outputMake( self, file, namelen, name, value ):
575         file.write( '%-*s = %s\n' % (namelen, name, value ))
576
577     def _outputM4( self, file, namelen, name, value ):
578         namelen += 7
579         name = '<<__%s>>,' % name.replace( '.', '_' )
580         file.write( 'define(%-*s  <<%s>>)dnl\n' % (namelen, name, value ))
581
582 ###############################################################################
583
584 ## create configure line, stripping arg --launch, quoting others
585 configure = []
586 for arg in sys.argv[1:]:
587     #if arg.find( '--launch' ) == 0:
588     #    continue
589     if arg == '--launch':
590         continue
591     configure.append( '"%s"' % (arg.replace('"', '\\"')) )
592
593 ## create singletones
594 repo = Repository()
595 project = Project()
596 config  = Config()
597
598 config.addComment( 'generated by configure on %s', time.strftime( '%c' ))
599
600 config.addBlank()
601 config.add( 'CONF.args', ' '.join( configure ))
602
603 config.addBlank()
604 config.add( 'HB.title',         project.title )
605 config.add( 'HB.name',          project.name )
606 config.add( 'HB.name.lower',    project.name_lower )
607 config.add( 'HB.name.upper',    project.name_upper )
608 config.add( 'HB.acro.lower',    project.acro_lower )
609 config.add( 'HB.acro.upper',    project.acro_upper )
610
611 config.add( 'HB.url.website',   project.url_website )
612 config.add( 'HB.url.community', project.url_community )
613 config.add( 'HB.url.irc',       project.url_irc )
614 config.add( 'HB.url.appcast',   project.url_appcast )
615
616 config.add( 'HB.version.major',  project.vmajor )
617 config.add( 'HB.version.minor',  project.vminor )
618 config.add( 'HB.version.point',  project.vpoint )
619 config.add( 'HB.version',        project.version )
620 config.add( 'HB.version.hex',    '%04x%02x%02x%02x%06x' % (project.vmajor,project.vminor,project.vpoint,0,repo.rev) )
621
622 config.add( 'HB.build', project.build )
623
624 config.add( 'HB.repo.url',       repo.url )
625 config.add( 'HB.repo.root',      repo.root )
626 config.add( 'HB.repo.branch',    repo.branch )
627 config.add( 'HB.repo.uuid',      repo.uuid )
628 config.add( 'HB.repo.rev',       repo.rev )
629 config.add( 'HB.repo.date',      repo.date )
630 config.add( 'HB.repo.wcversion', repo.wcversion )
631 config.add( 'HB.repo.official',  repo.official )
632 config.add( 'HB.repo.type',      repo.type )
633
634 config.addBlank()
635 config.add( 'HOST.spec',    guessHost )
636 config.add( 'HOST.machine', guessHost.machine )
637 config.add( 'HOST.vendor',  guessHost.vendor )
638 config.add( 'HOST.system',  guessHost.system )
639 config.add( 'HOST.systemc', guessHost.systemc )
640 config.add( 'HOST.release', guessHost.release )
641 config.add( 'HOST.title',   '%s %s' % (guessHost.systemc,archMode.default) )
642 config.add( 'HOST.extra',   guessHost.extra )
643
644 config.addBlank()
645 config.add( 'BUILD.spec',    guessBuild )
646 config.add( 'BUILD.machine', guessBuild.machine )
647 config.add( 'BUILD.vendor',  guessBuild.vendor )
648 config.add( 'BUILD.system',  guessBuild.system )
649 config.add( 'BUILD.systemc', guessBuild.systemc )
650 config.add( 'BUILD.release', guessBuild.release )
651 config.add( 'BUILD.title',   '%s %s' % (guessBuild.systemc,archMode.mode) )
652 config.add( 'BUILD.extra',   guessBuild.extra )
653 config.add( 'BUILD.cross',   guessBuild.cross )
654 config.add( 'BUILD.date',    time.strftime('%c') )
655 config.add( 'BUILD.arch',    archMode.mode )
656
657 config.addBlank()
658 config.add( 'BUILD/',   os.curdir + os.sep )
659 config.add( 'PROJECT/', project_dir + os.sep )
660
661 config.addBlank()
662 config.add( 'INSTALL.prefix', options.prefix )
663 config.add( 'INSTALL.prefix/', '$(INSTALL.prefix)/' )
664
665 config.addBlank()
666 config.add( 'FEATURE.xcode', 0 if options.disable_xcode else 1 )
667 config.add( 'FEATURE.gtk',   0 if options.disable_gtk   else 1 )
668
669 config.addMake( '' )
670 config.addMake( '## include main definitions' )
671 config.addMake( 'include $(PROJECT/)make/include/main.defs' )
672
673 config.addBlank()
674 for tool in tools.items:
675     tool.addToConfig( config )
676
677 config.addBlank()
678 config.add( 'GCC.archs', archMode.mode if guessBuild.cross else '' )
679 config.add( 'GCC.g', options.debug )
680 config.add( 'GCC.O', options.optimize )
681
682 config.addMake( '' )
683 config.addMake( '## include (optional) customization file' )
684 config.addMake( '-include $(BUID/)GNUmakefile.custom' )
685
686 config.addMake( '' )
687 config.addMake( '## include main rules' )
688 config.addMake( 'include $(PROJECT/)make/include/main.rules' )
689
690 ###############################################################################
691
692 # generate make or m4 file
693 def generate( type ):
694     if type == 'make':
695         fname = 'GNUmakefile'
696     elif type == 'm4':
697         fname = os.path.join( 'project', project.name_lower + '.m4' )
698     else:
699         raise ValueError, 'unknown file type: ' + type
700
701     ftmp  = fname + '.tmp'
702
703     pdir = os.path.dirname( fname )
704     if pdir:
705         if not os.path.exists( pdir ):
706             os.makedirs( pdir )
707
708     try:
709         try:
710             outf( 'generating %s', fname )
711             file = open( ftmp, 'w' )
712             config.output( file, type )
713         finally:
714             try:
715                 file.close()
716             except:
717                 pass
718     except Exception, x:
719         try:
720             os.remove( ftmp )
721         except Exception, x:
722             pass
723         errf( 'failed writing to %s\n%s', ftmp, x )
724
725     try:
726         os.rename( ftmp, fname )
727     except Exception, x:
728         errf( 'failed writing to %s\n%s', fname, x )
729
730 ###############################################################################
731
732 if not options.launch:
733     generate( 'make' )
734     generate( 'm4' )
735     sys.exit( 0 )
736
737 ###############################################################################
738
739 if os.path.exists( options.launch_dir ):
740     if not options.launch_force:
741         errf( 'scratch directory already exists: %s', options.launch_dir )
742 else:
743     outf( 'creating %s', options.launch_dir )
744     os.makedirs( options.launch_dir )    
745
746 outf( 'chdir %s', options.launch_dir )
747 os.chdir( options.launch_dir )
748 generate( 'make' )
749 generate( 'm4' )
750
751 outf( 'opening %s', options.launch_log )
752 try:
753     log = open( options.launch_log, 'w' )
754 except Exception, x:
755     errf( 'open failure: %s', x )
756
757 cmd = '%s -j%d' % (makeTool.found,options.launch_jobs)
758 if options.launch_args:
759     cmd += ' ' + options.launch_args
760
761 ## record begin
762 timeBegin = time.time()
763 s = '###\n### TIME: %s\n### launch: %s\n###\n' % (time.asctime(),cmd)
764 stdout.write( s ); stdout.flush()
765 log.write( s ); log.flush()
766
767 ## launch/pipe
768 try:
769     pipe = subprocess.Popen( cmd, shell=True, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
770 except Exception, x:
771     errf( 'launch failure: %s', x )
772 for line in pipe.stdout:
773     if not options.launch_quiet:
774         stdout.write( line ); stdout.flush()
775     log.write( line ); log.flush()
776 pipe.wait()
777
778 ## record end
779 timeEnd = time.time()
780 elapsed = timeEnd - timeBegin
781 result = '%s (exit code %d)' % ('success' if pipe.returncode == 0 else 'failed',pipe.returncode)
782 s = '###\n### TIME: %s\n### finished: %.2f seconds\n### %s\n###\n' % (time.asctime(),elapsed,result)
783 stdout.write( s ); stdout.flush()
784 log.write( s ); log.flush()
785
786 log.close()
787 sys.exit( 0 )