Changed indents to 4 *spaces*; added Emacs variables to avoid

inserting tabs.  All this (and the previous patch) in preparation for
porting to NT.
This commit is contained in:
Guido van Rossum 1997-08-10 16:56:48 +00:00
parent 9a6e855a27
commit 0b4b8a21ce

View file

@ -10,14 +10,14 @@ Options:
in the Python build directory. in the Python build directory.
(If you never ran this, freeze won't work.) (If you never ran this, freeze won't work.)
The default is whatever sys.prefix evaluates to. The default is whatever sys.prefix evaluates to.
It can also be the top directory of the Python source It can also be the top directory of the Python source
tree; then -P must point to the build tree. tree; then -P must point to the build tree.
-P exec_prefix: Like -p but this is the 'exec_prefix', used to -P exec_prefix: Like -p but this is the 'exec_prefix', used to
install objects etc. The default is whatever sys.exec_prefix install objects etc. The default is whatever sys.exec_prefix
evaluates to, or the -p argument if given. evaluates to, or the -p argument if given.
If -p points to the Python source tree, -P must point If -p points to the Python source tree, -P must point
to the build tree, if different. to the build tree, if different.
-e extension: A directory containing additional .o files that -e extension: A directory containing additional .o files that
may be used to resolve modules. This directory may be used to resolve modules. This directory
@ -31,7 +31,7 @@ Options:
Arguments: Arguments:
script.py: The Python script to be executed by the resulting binary. script.py: The Python script to be executed by the resulting binary.
It *must* end with a .py suffix! It *must* end with a .py suffix!
module ...: Additional Python modules (referenced by pathname) module ...: Additional Python modules (referenced by pathname)
that will be included in the resulting binary. These that will be included in the resulting binary. These
@ -70,245 +70,249 @@ import parsesetup
# Main program # Main program
def main(): def main():
# overridable context # overridable context
prefix = None # settable with -p option prefix = None # settable with -p option
exec_prefix = None # settable with -P option exec_prefix = None # settable with -P option
extensions = [] extensions = []
path = sys.path path = sys.path
odir = '' odir = ''
# output files # output files
frozen_c = 'frozen.c' frozen_c = 'frozen.c'
config_c = 'config.c' config_c = 'config.c'
target = 'a.out' # normally derived from script name target = 'a.out' # normally derived from script name
makefile = 'Makefile' makefile = 'Makefile'
# parse command line # parse command line
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'he:o:p:P:') opts, args = getopt.getopt(sys.argv[1:], 'he:o:p:P:')
except getopt.error, msg: except getopt.error, msg:
usage('getopt error: ' + str(msg)) usage('getopt error: ' + str(msg))
# proces option arguments # proces option arguments
for o, a in opts: for o, a in opts:
if o == '-h': if o == '-h':
print __doc__ print __doc__
return return
if o == '-e': if o == '-e':
extensions.append(a) extensions.append(a)
if o == '-o': if o == '-o':
odir = a odir = a
if o == '-p': if o == '-p':
prefix = a prefix = a
if o == '-P': if o == '-P':
exec_prefix = a exec_prefix = a
# default prefix and exec_prefix # default prefix and exec_prefix
if not exec_prefix: if not exec_prefix:
if prefix: if prefix:
exec_prefix = prefix exec_prefix = prefix
else: else:
exec_prefix = sys.exec_prefix exec_prefix = sys.exec_prefix
if not prefix: if not prefix:
prefix = sys.prefix prefix = sys.prefix
# determine whether -p points to the Python source tree # determine whether -p points to the Python source tree
ishome = os.path.exists(os.path.join(prefix, 'Include', 'pythonrun.h')) ishome = os.path.exists(os.path.join(prefix, 'Include', 'pythonrun.h'))
# locations derived from options # locations derived from options
version = sys.version[:3] version = sys.version[:3]
if ishome: if ishome:
print "(Using Python source directory)" print "(Using Python source directory)"
binlib = exec_prefix binlib = exec_prefix
incldir = os.path.join(prefix, 'Include') incldir = os.path.join(prefix, 'Include')
config_c_in = os.path.join(prefix, 'Modules', 'config.c.in') config_c_in = os.path.join(prefix, 'Modules', 'config.c.in')
frozenmain_c = os.path.join(prefix, 'Modules', 'frozenmain.c') frozenmain_c = os.path.join(prefix, 'Modules', 'frozenmain.c')
makefile_in = os.path.join(exec_prefix, 'Modules', 'Makefile') makefile_in = os.path.join(exec_prefix, 'Modules', 'Makefile')
else: else:
binlib = os.path.join(exec_prefix, binlib = os.path.join(exec_prefix,
'lib', 'python%s' % version, 'config') 'lib', 'python%s' % version, 'config')
incldir = os.path.join(prefix, 'include', 'python%s' % version) incldir = os.path.join(prefix, 'include', 'python%s' % version)
config_c_in = os.path.join(binlib, 'config.c.in') config_c_in = os.path.join(binlib, 'config.c.in')
frozenmain_c = os.path.join(binlib, 'frozenmain.c') frozenmain_c = os.path.join(binlib, 'frozenmain.c')
makefile_in = os.path.join(binlib, 'Makefile') makefile_in = os.path.join(binlib, 'Makefile')
supp_sources = [] supp_sources = []
defines = [] defines = []
includes = ['-I' + incldir, '-I' + binlib] includes = ['-I' + incldir, '-I' + binlib]
# sanity check of directories and files # sanity check of directories and files
for dir in [prefix, exec_prefix, binlib, incldir] + extensions: for dir in [prefix, exec_prefix, binlib, incldir] + extensions:
if not os.path.exists(dir): if not os.path.exists(dir):
usage('needed directory %s not found' % dir) usage('needed directory %s not found' % dir)
if not os.path.isdir(dir): if not os.path.isdir(dir):
usage('%s: not a directory' % dir) usage('%s: not a directory' % dir)
for file in [config_c_in, makefile_in] + supp_sources: for file in [config_c_in, makefile_in] + supp_sources:
if not os.path.exists(file): if not os.path.exists(file):
usage('needed file %s not found' % file) usage('needed file %s not found' % file)
if not os.path.isfile(file): if not os.path.isfile(file):
usage('%s: not a plain file' % file) usage('%s: not a plain file' % file)
for dir in extensions: for dir in extensions:
setup = os.path.join(dir, 'Setup') setup = os.path.join(dir, 'Setup')
if not os.path.exists(setup): if not os.path.exists(setup):
usage('needed file %s not found' % setup) usage('needed file %s not found' % setup)
if not os.path.isfile(setup): if not os.path.isfile(setup):
usage('%s: not a plain file' % setup) usage('%s: not a plain file' % setup)
# check that enough arguments are passed # check that enough arguments are passed
if not args: if not args:
usage('at least one filename argument required') usage('at least one filename argument required')
# check that the script name ends in ".py" # check that the script name ends in ".py"
if args[0][-3:] != ".py": if args[0][-3:] != ".py":
usage('the script name must have a .py suffix') usage('the script name must have a .py suffix')
# check that file arguments exist # check that file arguments exist
for arg in args: for arg in args:
if not os.path.exists(arg): if not os.path.exists(arg):
usage('argument %s not found' % arg) usage('argument %s not found' % arg)
if not os.path.isfile(arg): if not os.path.isfile(arg):
usage('%s: not a plain file' % arg) usage('%s: not a plain file' % arg)
# process non-option arguments # process non-option arguments
scriptfile = args[0] scriptfile = args[0]
modules = args[1:] modules = args[1:]
# derive target name from script name # derive target name from script name
base = os.path.basename(scriptfile) base = os.path.basename(scriptfile)
base, ext = os.path.splitext(base) base, ext = os.path.splitext(base)
if base: if base:
if base != scriptfile: if base != scriptfile:
target = base target = base
else: else:
target = base + '.bin' target = base + '.bin'
# handle -o option # handle -o option
base_frozen_c = frozen_c base_frozen_c = frozen_c
base_config_c = config_c base_config_c = config_c
base_target = target base_target = target
if odir and not os.path.isdir(odir): if odir and not os.path.isdir(odir):
try: try:
os.mkdir(odir) os.mkdir(odir)
print "Created output directory", odir print "Created output directory", odir
except os.error, msg: except os.error, msg:
usage('%s: mkdir failed (%s)' % (odir, str(msg))) usage('%s: mkdir failed (%s)' % (odir, str(msg)))
if odir: if odir:
frozen_c = os.path.join(odir, frozen_c) frozen_c = os.path.join(odir, frozen_c)
config_c = os.path.join(odir, config_c) config_c = os.path.join(odir, config_c)
target = os.path.join(odir, target) target = os.path.join(odir, target)
makefile = os.path.join(odir,makefile) makefile = os.path.join(odir, makefile)
# Actual work starts here... # Actual work starts here...
dict = findmodules.findmodules(scriptfile, modules, path) dict = findmodules.findmodules(scriptfile, modules, path)
names = dict.keys() names = dict.keys()
names.sort() names.sort()
print "Modules being frozen:" print "Modules being frozen:"
for name in names: for name in names:
print '\t', name print '\t', name
backup = frozen_c + '~' backup = frozen_c + '~'
try: try:
os.rename(frozen_c, backup) os.rename(frozen_c, backup)
except os.error: except os.error:
backup = None backup = None
outfp = open(frozen_c, 'w') outfp = open(frozen_c, 'w')
try: try:
makefreeze.makefreeze(outfp, dict) makefreeze.makefreeze(outfp, dict)
finally: finally:
outfp.close() outfp.close()
if backup: if backup:
if cmp.cmp(backup, frozen_c): if cmp.cmp(backup, frozen_c):
sys.stderr.write('%s not changed, not written\n' % sys.stderr.write('%s not changed, not written\n' %
frozen_c) frozen_c)
os.rename(backup, frozen_c) os.rename(backup, frozen_c)
builtins = [] builtins = []
unknown = [] unknown = []
mods = dict.keys() mods = dict.keys()
mods.sort() mods.sort()
for mod in mods: for mod in mods:
if dict[mod] == '<builtin>': if dict[mod] == '<builtin>':
builtins.append(mod) builtins.append(mod)
elif dict[mod] == '<unknown>': elif dict[mod] == '<unknown>':
unknown.append(mod) unknown.append(mod)
addfiles = [] addfiles = []
if unknown: if unknown:
addfiles, addmods = \ addfiles, addmods = \
checkextensions.checkextensions(unknown, extensions) checkextensions.checkextensions(unknown, extensions)
for mod in addmods: for mod in addmods:
unknown.remove(mod) unknown.remove(mod)
builtins = builtins + addmods builtins = builtins + addmods
if unknown: if unknown:
sys.stderr.write('Warning: unknown modules remain: %s\n' % sys.stderr.write('Warning: unknown modules remain: %s\n' %
string.join(unknown)) string.join(unknown))
builtins.sort() builtins.sort()
infp = open(config_c_in) infp = open(config_c_in)
backup = config_c + '~' backup = config_c + '~'
try: try:
os.rename(config_c, backup) os.rename(config_c, backup)
except os.error: except os.error:
backup = None backup = None
outfp = open(config_c, 'w') outfp = open(config_c, 'w')
try: try:
makeconfig.makeconfig(infp, outfp, builtins) makeconfig.makeconfig(infp, outfp, builtins)
finally: finally:
outfp.close() outfp.close()
infp.close() infp.close()
if backup: if backup:
if cmp.cmp(backup, config_c): if cmp.cmp(backup, config_c):
sys.stderr.write('%s not changed, not written\n' % sys.stderr.write('%s not changed, not written\n' %
config_c) config_c)
os.rename(backup, config_c) os.rename(backup, config_c)
cflags = defines + includes + ['$(OPT)'] cflags = defines + includes + ['$(OPT)']
libs = [os.path.join(binlib, 'libpython$(VERSION).a')] libs = [os.path.join(binlib, 'libpython$(VERSION).a')]
makevars = parsesetup.getmakevars(makefile_in) makevars = parsesetup.getmakevars(makefile_in)
somevars = {} somevars = {}
for key in makevars.keys(): for key in makevars.keys():
somevars[key] = makevars[key] somevars[key] = makevars[key]
somevars['CFLAGS'] = string.join(cflags) # override somevars['CFLAGS'] = string.join(cflags) # override
files = ['$(OPT)', '$(LDFLAGS)', base_config_c, base_frozen_c] + \ files = ['$(OPT)', '$(LDFLAGS)', base_config_c, base_frozen_c] + \
supp_sources + addfiles + libs + \ supp_sources + addfiles + libs + \
['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)'] ['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)']
backup = makefile + '~' backup = makefile + '~'
try: try:
os.rename(makefile, backup) os.rename(makefile, backup)
except os.error: except os.error:
backup = None backup = None
outfp = open(makefile, 'w') outfp = open(makefile, 'w')
try: try:
makemakefile.makemakefile(outfp, somevars, files, base_target) makemakefile.makemakefile(outfp, somevars, files, base_target)
finally: finally:
outfp.close() outfp.close()
if backup: if backup:
if not cmp.cmp(backup, makefile): if not cmp.cmp(backup, makefile):
print 'previous Makefile saved as', backup print 'previous Makefile saved as', backup
else: else:
sys.stderr.write('%s not changed, not written\n' % sys.stderr.write('%s not changed, not written\n' %
makefile) makefile)
os.rename(backup, makefile) os.rename(backup, makefile)
# Done! # Done!
if odir: if odir:
print 'Now run "make" in', odir, print 'Now run "make" in', odir,
print 'to build the target:', base_target print 'to build the target:', base_target
else: else:
print 'Now run "make" to build the target:', base_target print 'Now run "make" to build the target:', base_target
# Print usage message and exit # Print usage message and exit
def usage(msg): def usage(msg):
sys.stdout = sys.stderr sys.stdout = sys.stderr
print "Error:", msg print "Error:", msg
print "Use ``%s -h'' for help" % sys.argv[0] print "Use ``%s -h'' for help" % sys.argv[0]
sys.exit(2) sys.exit(2)
main() main()
# Local Variables:
# indent-tabs-mode: nil
# End: