Switch to OptionParser

main
Michael Hayes 15 years ago
parent b7ad5ed76a
commit 3ec5297548

@ -41,12 +41,12 @@ There are special strings that are replaced in the template file:
# http://www.cs.umd.edu/~nspring/software/style-check-readme.html # http://www.cs.umd.edu/~nspring/software/style-check-readme.html
import sys import sys
import getopt
import re import re
import os import os
import subprocess import subprocess
from os import pathsep from os import pathsep
import os.path import os.path
from optparse import OptionParser
def unique (list): def unique (list):
@ -77,7 +77,7 @@ def file_search (filename, search_path, debug):
return None return None
def hfiles_get (cfile, filedeps, mopts): def hfiles_get (cfile, filedeps, options):
deps = filedeps[cfile] deps = filedeps[cfile]
@ -89,12 +89,12 @@ def hfiles_get (cfile, filedeps, mopts):
for hfile in filedeps[cfile]: for hfile in filedeps[cfile]:
if hfile[-2:] == '.h': if hfile[-2:] == '.h':
if mopts['relpath']: if options.relpath:
hfile = os.path.relpath (hfile) hfile = os.path.relpath (hfile)
hfilelist.append (hfile) hfilelist.append (hfile)
for hfile in filedeps[cfile]: for hfile in filedeps[cfile]:
hfilelist.extend (hfiles_get (hfile, filedeps, mopts)) hfilelist.extend (hfiles_get (hfile, filedeps, options))
return unique (hfilelist) return unique (hfilelist)
@ -127,17 +127,12 @@ def file_parse (pathname, indent, debug):
return hfilelist return hfilelist
def makefile_print (mopts, template, maincfilename, filedeps, def makefile_print (options, template, maincfilename, filedeps,
search_list, debug): search_list):
basecfilelist = [] cfilelist = cfiles_get (filedeps)
cfilelist = []
for target in filedeps:
if target[-2:] == '.c':
cfilelist.append (target)
basecfilelist.append (os.path.basename (target))
basecfilelist.sort ()
cfilelist.sort () cfilelist.sort ()
basecfilelist = [os.path.basename (cfile) for cfile in cfilelist]
project = os.path.splitext (os.path.basename (maincfilename)) project = os.path.splitext (os.path.basename (maincfilename))
project = project[0] project = project[0]
@ -152,13 +147,13 @@ def makefile_print (mopts, template, maincfilename, filedeps,
src = ' '.join (basecfilelist) src = ' '.join (basecfilelist)
obj = src obj = src
if mopts['builddir'] != '': if options.builddir != '':
objfilelist = [os.path.join (mopts['builddir'], obj1) for obj1 in basecfilelist] objfilelist = [os.path.join (options.builddir, obj1) for obj1 in basecfilelist]
objfilelist.sort () objfilelist.sort ()
obj = ' '.join (objfilelist) obj = ' '.join (objfilelist)
project = os.path.join (mopts['builddir'], project) project = os.path.join (options.builddir, project)
obj = re.sub (r'([a-zA-Z0-9/.-_]*)[.]c', r'\1' + mopts['objext'], obj) obj = re.sub (r'([a-zA-Z0-9/.-_]*)[.]c', r'\1' + options.objext, obj)
text = re.sub (r'@PROJECT@', project, text) text = re.sub (r'@PROJECT@', project, text)
text = re.sub (r'@VPATH@', vpath, text) text = re.sub (r'@VPATH@', vpath, text)
@ -175,21 +170,21 @@ def makefile_print (mopts, template, maincfilename, filedeps,
for cfile in cfilelist: for cfile in cfilelist:
cfilebase = os.path.basename (cfile) cfilebase = os.path.basename (cfile)
if mopts['relpath']: if options.relpath:
cfile1 = os.path.relpath (cfile) cfile1 = os.path.relpath (cfile)
else: else:
cfile1 = cfilebase cfile1 = cfilebase
if mopts['builddir'] != '': if options.builddir != '':
rules = rules + os.path.join (mopts['builddir'], '') rules = rules + os.path.join (options.builddir, '')
rules = rules + re.sub ('([a-zA-Z0-9/.-_]*)[.]c', r'\1' + mopts['objext'], cfilebase) + ': ' + cfile1 rules = rules + re.sub ('([a-zA-Z0-9/.-_]*)[.]c', r'\1' + options.objext, cfilebase) + ': ' + cfile1
hfilelist = hfiles_get (cfile, filedeps, mopts) hfilelist = hfiles_get (cfile, filedeps, options)
hfilelist.sort () hfilelist.sort ()
if debug: if options.debug:
print >> sys.stderr, 'Need hfiles', hfilelist, 'for', cfile print >> sys.stderr, 'Need hfiles', hfilelist, 'for', cfile
for hfile in hfilelist: for hfile in hfilelist:
@ -308,23 +303,23 @@ def files_find (gcc, filepath, search_path, filedeps, moduledeps, indent, debug)
files_find (gcc, file, search_path, filedeps, moduledeps, indent + ' ', debug) files_find (gcc, file, search_path, filedeps, moduledeps, indent + ' ', debug)
def alldeps_print (depsdir, mopts): def alldeps_print (depsdir, options):
for target in depsdir.keys (): for target in depsdir.keys ():
targetbase = os.path.basename (target) targetbase = os.path.basename (target)
if targetbase in mopts['exclude']: if targetbase in options.exclude:
continue continue
deps = depsdir[target] deps = depsdir[target]
deps = [dep for dep in deps if os.path.basename (dep) not in mopts['exclude']] deps = [dep for dep in deps if os.path.basename (dep) not in options.exclude]
if mopts['relpath']: if options.relpath:
deps = [os.path.relpath (dep) for dep in deps] deps = [os.path.relpath (dep) for dep in deps]
print os.path.relpath (target) + ': ' + ' '.join (deps) + '\n' print os.path.relpath (target) + ': ' + ' '.join (deps) + '\n'
def deps_print (target, depsdir, mopts, record = {}): def deps_print (target, depsdir, options, record = {}):
if record.has_key (target): if record.has_key (target):
return return
@ -332,11 +327,11 @@ def deps_print (target, depsdir, mopts, record = {}):
return return
deps = depsdir[target] deps = depsdir[target]
deps = [dep for dep in deps if os.path.basename (dep) not in mopts['exclude']] deps = [dep for dep in deps if os.path.basename (dep) not in options.exclude]
for dep in deps: for dep in deps:
deps_print (dep, depsdir, mopts, record) deps_print (dep, depsdir, options, record)
if mopts['relpath']: if options.relpath:
deps = [os.path.relpath (dep) for dep in deps] deps = [os.path.relpath (dep) for dep in deps]
record[target] = True record[target] = True
@ -353,136 +348,139 @@ class Usage (Exception):
def main(argv = None): def main(argv = None):
if argv is None: if argv is None:
argv = sys.argv argv = sys.argv
try:
try: version = __doc__.split ('\n')[0]
opts, args = getopt.gnu_getopt (argv[1:], "?h", \
["help", "builddir=", "objext=", parser = OptionParser (usage = '%prog', version = version,
"exeext=", description = __doc__)
"relpath", "debug", "template=",
"files", "modules", "exclude=", parser.add_option('--showops', action = 'store_true',
"calls"]) dest = 'showops', default = False,
except getopt.error, msg: help = 'show operations')
raise Usage (msg)
parser.add_option('--modules', action = 'store_true',
if not opts and not args: dest = 'modules', default = False,
print __doc__ help = 'generate module dependencies')
sys.exit (0)
parser.add_option('--calls', action = 'store_true',
if len (args) < 1: dest = 'calls', default = False,
print __doc__ help = 'generate callgraph')
sys.exit (0)
parser.add_option('--files', action = 'store_true',
mopts = {} dest = 'files', default = False,
mopts['builddir'] = '' help = 'generate file dependencies')
mopts['objext'] = '.o'
mopts['exeext'] = '.out' parser.add_option('--debug', action = 'store_true',
mopts['relpath'] = False dest = 'debug', default = False,
mopts['template'] = None help = 'enable debugging')
mopts['files'] = False
mopts['modules'] = False parser.add_option('--exclude', dest = 'exclude',
mopts['calls'] = False default = '',
mopts['exclude'] = [] help = 'files to exclude')
debug = False
parser.add_option('--objext', dest = 'objext',
# Process options default = '.o',
for o, a in opts: help = 'object file extension')
if o in ("-?", "-h", "--help"):
print __doc__ parser.add_option('--exeext', dest = 'exeext',
sys.exit (0) default = '.out',
elif o == "--builddir": help = 'executable file extension')
mopts['builddir'] = a
elif o == "--objext": parser.add_option('--relpath', action = 'store_true',
mopts['objext'] = a dest = 'relpath', default = False,
elif o == "--exeext": help = 'use relative paths')
mopts['exeext'] = a
elif o == "--relpath": parser.add_option('--outfile', dest = 'outfilename',
mopts['relpath'] = True default = None,
elif o == "--debug": help = 'output filename')
debug = True
elif o == "--template": parser.add_option('--builddir', dest = 'builddir',
mopts['template'] = a default = '',
elif o == "--files": help = 'build dirname')
mopts['files'] = True
elif o == "--modules": parser.add_option('--template', dest = 'template',
mopts['modules'] = True default = None,
elif o == "--calls": help = 'template filename')
mopts['calls'] = True
elif o == "--exclude": (options, args) = parser.parse_args ()
mopts['exclude'] = a.split (' ')
if len (args) < 1:
maincfilename = args[0] print __doc__
sys.exit (0)
search_list = []
if len (args) > 1:
search_list.extend (args[1:len (args)]) if ',' in options.exclude:
options.exclude = options.exclude.split(',')
if debug: else:
print >> sys.stderr, search_list options.exclude = options.exclude.split()
search_path = pathsep.join (search_list)
if debug: maincfilename = args[0]
print >> sys.stderr, 'template', mopts['template']
print >> sys.stderr, 'cfile', maincfilename search_list = []
print >> sys.stderr, 'search_path', search_path if len (args) > 1:
print >> sys.stderr, 'CWD = ', os.getcwd() search_list.extend (args[1:len (args)])
if os.path.isdir (maincfilename): if options.debug:
if debug: print >> sys.stderr, search_list
print >> sys.stderr, 'Searching ' + maincfilename search_path = pathsep.join (search_list)
maincfilename = maincfilename_find (maincfilename) if options.debug:
if not maincfilename: print >> sys.stderr, 'template', options.template
sys.exit (1) print >> sys.stderr, 'cfile', maincfilename
print >> sys.stderr, 'search_path', search_path
if debug: print >> sys.stderr, 'CWD = ', os.getcwd()
print >> sys.stderr, 'Found C file ' + maincfilename
if os.path.isdir (maincfilename):
includes = '-I' + ' -I'.join (search_list) if options.debug:
cflags = '-mmcu=atmega32u2' print >> sys.stderr, 'Searching ' + maincfilename
opts = '-Os' maincfilename = maincfilename_find (maincfilename)
gcc = 'avr-gcc' + ' ' + cflags + ' ' + opts + ' ' + includes if not maincfilename:
sys.exit (1)
# Search main c file looking for header files included with #include
# and any header files included by the header files if options.debug:
print >> sys.stderr, 'Found C file ' + maincfilename
filedeps = {}
moduledeps = {} includes = '-I' + ' -I'.join (search_list)
functiondeps = {} cflags = '-mmcu=atmega32u2'
files_find (gcc, maincfilename, search_path, filedeps, moduledeps, '', debug) opts = '-Os'
gcc = 'avr-gcc' + ' ' + cflags + ' ' + opts + ' ' + includes
cfilelist = cfiles_get (filedeps)
ofilelist = [cfile[:-2] + mopts['objext'] for cfile in cfilelist] # Search main c file looking for header files included with #include
outfile = maincfilename[:-2] + mopts['exeext'] # and any header files included by the header files
filedeps[outfile] = ofilelist
for ofile in ofilelist: filedeps = {}
deps = [] moduledeps = {}
deps.append (ofile[:-2] + '.c') functiondeps = {}
filedeps[ofile] = deps files_find (gcc, maincfilename, search_path, filedeps, moduledeps, '', options.debug)
# print >> sys.stderr, moduledeps cfilelist = cfiles_get (filedeps)
# print >> sys.stderr, filedeps ofilelist = [cfile[:-2] + options.objext for cfile in cfilelist]
outfile = maincfilename[:-2] + options.exeext
if mopts['calls']: filedeps[outfile] = ofilelist
for cfile in cfilelist: for ofile in ofilelist:
functions_find (gcc, cfile, functiondeps) deps = []
deps_print ('main', functiondeps, mopts) deps.append (ofile[:-2] + '.c')
filedeps[ofile] = deps
if mopts['files']:
deps_print (outfile, filedeps, mopts) # print >> sys.stderr, moduledeps
# print >> sys.stderr, filedeps
if mopts['modules']:
target, ext = os.path.splitext (os.path.basename (maincfilename)) if options.calls:
deps_print (target, moduledeps, mopts) for cfile in cfilelist:
functions_find (gcc, cfile, functiondeps)
if mopts['template']: deps_print ('main', functiondeps, options)
makefile_print (mopts, mopts['template'], maincfilename, filedeps,
search_list, debug) if options.files:
deps_print (outfile, filedeps, options)
return 0
if options.modules:
target, ext = os.path.splitext (os.path.basename (maincfilename))
except Usage, err: deps_print (target, moduledeps, options)
print >> sys.stderr, err.msg
print >> sys.stderr, "for help use --help" if options.template:
return 2 makefile_print (options, options.template, maincfilename, filedeps,
search_list)
return 0
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save