- Got rid of newmodule.c
[python/dscho.git] / Tools / freeze / freeze.py
blob0b9b20639fc131a1df2cf10c366a1b628b8d6a1c
1 #! /usr/bin/env python
3 """Freeze a Python script into a binary.
5 usage: freeze [options...] script [module]...
7 Options:
8 -p prefix: This is the prefix used when you ran ``make install''
9 in the Python build directory.
10 (If you never ran this, freeze won't work.)
11 The default is whatever sys.prefix evaluates to.
12 It can also be the top directory of the Python source
13 tree; then -P must point to the build tree.
15 -P exec_prefix: Like -p but this is the 'exec_prefix', used to
16 install objects etc. The default is whatever sys.exec_prefix
17 evaluates to, or the -p argument if given.
18 If -p points to the Python source tree, -P must point
19 to the build tree, if different.
21 -e extension: A directory containing additional .o files that
22 may be used to resolve modules. This directory
23 should also have a Setup file describing the .o files.
24 On Windows, the name of a .INI file describing one
25 or more extensions is passed.
26 More than one -e option may be given.
28 -o dir: Directory where the output files are created; default '.'.
30 -m: Additional arguments are module names instead of filenames.
32 -a package=dir: Additional directories to be added to the package's
33 __path__. Used to simulate directories added by the
34 package at runtime (eg, by OpenGL and win32com).
35 More than one -a option may be given for each package.
37 -l file: Pass the file to the linker (windows only)
39 -d: Debugging mode for the module finder.
41 -q: Make the module finder totally quiet.
43 -h: Print this help message.
45 -x module Exclude the specified module. It will still be imported
46 by the frozen binary if it exists on the host system.
48 -X module Like -x, except the module can never be imported by
49 the frozen binary.
51 -E: Freeze will fail if any modules can't be found (that
52 were not excluded using -x or -X).
54 -i filename: Include a file with additional command line options. Used
55 to prevent command lines growing beyond the capabilities of
56 the shell/OS. All arguments specified in filename
57 are read and the -i option replaced with the parsed
58 params (note - quoting args in this file is NOT supported)
60 -s subsystem: Specify the subsystem (For Windows only.);
61 'console' (default), 'windows', 'service' or 'com_dll'
63 -w: Toggle Windows (NT or 95) behavior.
64 (For debugging only -- on a win32 platform, win32 behavior
65 is automatic.)
67 -r prefix=f: Replace path prefix.
68 Replace prefix with f in the source path references
69 contained in the resulting binary.
71 Arguments:
73 script: The Python script to be executed by the resulting binary.
75 module ...: Additional Python modules (referenced by pathname)
76 that will be included in the resulting binary. These
77 may be .py or .pyc files. If -m is specified, these are
78 module names that are search in the path instead.
80 NOTES:
82 In order to use freeze successfully, you must have built Python and
83 installed it ("make install").
85 The script should not use modules provided only as shared libraries;
86 if it does, the resulting binary is not self-contained.
87 """
90 # Import standard modules
92 import getopt
93 import os
94 import string
95 import sys
98 # Import the freeze-private modules
100 import checkextensions
101 import modulefinder
102 import makeconfig
103 import makefreeze
104 import makemakefile
105 import parsesetup
106 import bkfile
109 # Main program
111 def main():
112 # overridable context
113 prefix = None # settable with -p option
114 exec_prefix = None # settable with -P option
115 extensions = []
116 exclude = [] # settable with -x option
117 addn_link = [] # settable with -l, but only honored under Windows.
118 path = sys.path[:]
119 modargs = 0
120 debug = 1
121 odir = ''
122 win = sys.platform[:3] == 'win'
123 replace_paths = [] # settable with -r option
124 error_if_any_missing = 0
126 # default the exclude list for each platform
127 if win: exclude = exclude + [
128 'dos', 'dospath', 'mac', 'macpath', 'macfs', 'MACFS', 'posix',
129 'os2', 'ce', 'riscos', 'riscosenviron', 'riscospath',
132 fail_import = exclude[:]
134 # modules that are imported by the Python runtime
135 implicits = ["site", "exceptions"]
137 # output files
138 frozen_c = 'frozen.c'
139 config_c = 'config.c'
140 target = 'a.out' # normally derived from script name
141 makefile = 'Makefile'
142 subsystem = 'console'
144 # parse command line by first replacing any "-i" options with the
145 # file contents.
146 pos = 1
147 while pos < len(sys.argv)-1:
148 # last option can not be "-i", so this ensures "pos+1" is in range!
149 if sys.argv[pos] == '-i':
150 try:
151 options = string.split(open(sys.argv[pos+1]).read())
152 except IOError, why:
153 usage("File name '%s' specified with the -i option "
154 "can not be read - %s" % (sys.argv[pos+1], why) )
155 # Replace the '-i' and the filename with the read params.
156 sys.argv[pos:pos+2] = options
157 pos = pos + len(options) - 1 # Skip the name and the included args.
158 pos = pos + 1
160 # Now parse the command line with the extras inserted.
161 try:
162 opts, args = getopt.getopt(sys.argv[1:], 'r:a:dEe:hmo:p:P:qs:wX:x:l:')
163 except getopt.error, msg:
164 usage('getopt error: ' + str(msg))
166 # proces option arguments
167 for o, a in opts:
168 if o == '-h':
169 print __doc__
170 return
171 if o == '-d':
172 debug = debug + 1
173 if o == '-e':
174 extensions.append(a)
175 if o == '-m':
176 modargs = 1
177 if o == '-o':
178 odir = a
179 if o == '-p':
180 prefix = a
181 if o == '-P':
182 exec_prefix = a
183 if o == '-q':
184 debug = 0
185 if o == '-w':
186 win = not win
187 if o == '-s':
188 if not win:
189 usage("-s subsystem option only on Windows")
190 subsystem = a
191 if o == '-x':
192 exclude.append(a)
193 if o == '-X':
194 exclude.append(a)
195 fail_import.append(a)
196 if o == '-E':
197 error_if_any_missing = 1
198 if o == '-l':
199 addn_link.append(a)
200 if o == '-a':
201 apply(modulefinder.AddPackagePath, tuple(string.split(a,"=", 2)))
202 if o == '-r':
203 f,r = string.split(a,"=", 2)
204 replace_paths.append( (f,r) )
206 # default prefix and exec_prefix
207 if not exec_prefix:
208 if prefix:
209 exec_prefix = prefix
210 else:
211 exec_prefix = sys.exec_prefix
212 if not prefix:
213 prefix = sys.prefix
215 # determine whether -p points to the Python source tree
216 ishome = os.path.exists(os.path.join(prefix, 'Python', 'ceval.c'))
218 # locations derived from options
219 version = sys.version[:3]
220 if win:
221 extensions_c = 'frozen_extensions.c'
222 if ishome:
223 print "(Using Python source directory)"
224 binlib = exec_prefix
225 incldir = os.path.join(prefix, 'Include')
226 config_h_dir = exec_prefix
227 config_c_in = os.path.join(prefix, 'Modules', 'config.c.in')
228 frozenmain_c = os.path.join(prefix, 'Python', 'frozenmain.c')
229 makefile_in = os.path.join(exec_prefix, 'Modules', 'Makefile')
230 if win:
231 frozendllmain_c = os.path.join(exec_prefix, 'Pc\\frozen_dllmain.c')
232 else:
233 binlib = os.path.join(exec_prefix,
234 'lib', 'python%s' % version, 'config')
235 incldir = os.path.join(prefix, 'include', 'python%s' % version)
236 config_h_dir = os.path.join(exec_prefix, 'include',
237 'python%s' % version)
238 config_c_in = os.path.join(binlib, 'config.c.in')
239 frozenmain_c = os.path.join(binlib, 'frozenmain.c')
240 makefile_in = os.path.join(binlib, 'Makefile')
241 frozendllmain_c = os.path.join(binlib, 'frozen_dllmain.c')
242 supp_sources = []
243 defines = []
244 includes = ['-I' + incldir, '-I' + config_h_dir]
246 # sanity check of directories and files
247 check_dirs = [prefix, exec_prefix, binlib, incldir]
248 if not win:
249 # These are not directories on Windows.
250 check_dirs = check_dirs + extensions
251 for dir in check_dirs:
252 if not os.path.exists(dir):
253 usage('needed directory %s not found' % dir)
254 if not os.path.isdir(dir):
255 usage('%s: not a directory' % dir)
256 if win:
257 files = supp_sources + extensions # extensions are files on Windows.
258 else:
259 files = [config_c_in, makefile_in] + supp_sources
260 for file in supp_sources:
261 if not os.path.exists(file):
262 usage('needed file %s not found' % file)
263 if not os.path.isfile(file):
264 usage('%s: not a plain file' % file)
265 if not win:
266 for dir in extensions:
267 setup = os.path.join(dir, 'Setup')
268 if not os.path.exists(setup):
269 usage('needed file %s not found' % setup)
270 if not os.path.isfile(setup):
271 usage('%s: not a plain file' % setup)
273 # check that enough arguments are passed
274 if not args:
275 usage('at least one filename argument required')
277 # check that file arguments exist
278 for arg in args:
279 if arg == '-m':
280 break
281 # if user specified -m on the command line before _any_
282 # file names, then nothing should be checked (as the
283 # very first file should be a module name)
284 if modargs:
285 break
286 if not os.path.exists(arg):
287 usage('argument %s not found' % arg)
288 if not os.path.isfile(arg):
289 usage('%s: not a plain file' % arg)
291 # process non-option arguments
292 scriptfile = args[0]
293 modules = args[1:]
295 # derive target name from script name
296 base = os.path.basename(scriptfile)
297 base, ext = os.path.splitext(base)
298 if base:
299 if base != scriptfile:
300 target = base
301 else:
302 target = base + '.bin'
304 # handle -o option
305 base_frozen_c = frozen_c
306 base_config_c = config_c
307 base_target = target
308 if odir and not os.path.isdir(odir):
309 try:
310 os.mkdir(odir)
311 print "Created output directory", odir
312 except os.error, msg:
313 usage('%s: mkdir failed (%s)' % (odir, str(msg)))
314 base = ''
315 if odir:
316 base = os.path.join(odir, '')
317 frozen_c = os.path.join(odir, frozen_c)
318 config_c = os.path.join(odir, config_c)
319 target = os.path.join(odir, target)
320 makefile = os.path.join(odir, makefile)
321 if win: extensions_c = os.path.join(odir, extensions_c)
323 # Handle special entry point requirements
324 # (on Windows, some frozen programs do not use __main__, but
325 # import the module directly. Eg, DLLs, Services, etc
326 custom_entry_point = None # Currently only used on Windows
327 python_entry_is_main = 1 # Is the entry point called __main__?
328 # handle -s option on Windows
329 if win:
330 import winmakemakefile
331 try:
332 custom_entry_point, python_entry_is_main = \
333 winmakemakefile.get_custom_entry_point(subsystem)
334 except ValueError, why:
335 usage(why)
338 # Actual work starts here...
340 # collect all modules of the program
341 dir = os.path.dirname(scriptfile)
342 path[0] = dir
343 mf = modulefinder.ModuleFinder(path, debug, exclude, replace_paths)
345 if win and subsystem=='service':
346 # If a Windows service, then add the "built-in" module.
347 mod = mf.add_module("servicemanager")
348 mod.__file__="dummy.pyd" # really built-in to the resulting EXE
350 for mod in implicits:
351 mf.import_hook(mod)
352 for mod in modules:
353 if mod == '-m':
354 modargs = 1
355 continue
356 if modargs:
357 if mod[-2:] == '.*':
358 mf.import_hook(mod[:-2], None, ["*"])
359 else:
360 mf.import_hook(mod)
361 else:
362 mf.load_file(mod)
364 # Add the main script as either __main__, or the actual module name.
365 if python_entry_is_main:
366 mf.run_script(scriptfile)
367 else:
368 mf.load_file(scriptfile)
370 if debug > 0:
371 mf.report()
372 print
373 dict = mf.modules
375 if error_if_any_missing:
376 missing = mf.any_missing()
377 if missing:
378 sys.exit("There are some missing modules: %r" % missing)
380 # generate output for frozen modules
381 files = makefreeze.makefreeze(base, dict, debug, custom_entry_point,
382 fail_import)
384 # look for unfrozen modules (builtin and of unknown origin)
385 builtins = []
386 unknown = []
387 mods = dict.keys()
388 mods.sort()
389 for mod in mods:
390 if dict[mod].__code__:
391 continue
392 if not dict[mod].__file__:
393 builtins.append(mod)
394 else:
395 unknown.append(mod)
397 # search for unknown modules in extensions directories (not on Windows)
398 addfiles = []
399 frozen_extensions = [] # Windows list of modules.
400 if unknown or (not win and builtins):
401 if not win:
402 addfiles, addmods = \
403 checkextensions.checkextensions(unknown+builtins,
404 extensions)
405 for mod in addmods:
406 if mod in unknown:
407 unknown.remove(mod)
408 builtins.append(mod)
409 else:
410 # Do the windows thang...
411 import checkextensions_win32
412 # Get a list of CExtension instances, each describing a module
413 # (including its source files)
414 frozen_extensions = checkextensions_win32.checkextensions(
415 unknown, extensions, prefix)
416 for mod in frozen_extensions:
417 unknown.remove(mod.name)
419 # report unknown modules
420 if unknown:
421 sys.stderr.write('Warning: unknown modules remain: %s\n' %
422 string.join(unknown))
424 # windows gets different treatment
425 if win:
426 # Taking a shortcut here...
427 import winmakemakefile, checkextensions_win32
428 checkextensions_win32.write_extension_table(extensions_c,
429 frozen_extensions)
430 # Create a module definition for the bootstrap C code.
431 xtras = [frozenmain_c, os.path.basename(frozen_c),
432 frozendllmain_c, os.path.basename(extensions_c)] + files
433 maindefn = checkextensions_win32.CExtension( '__main__', xtras )
434 frozen_extensions.append( maindefn )
435 outfp = open(makefile, 'w')
436 try:
437 winmakemakefile.makemakefile(outfp,
438 locals(),
439 frozen_extensions,
440 os.path.basename(target))
441 finally:
442 outfp.close()
443 return
445 # generate config.c and Makefile
446 builtins.sort()
447 infp = open(config_c_in)
448 outfp = bkfile.open(config_c, 'w')
449 try:
450 makeconfig.makeconfig(infp, outfp, builtins)
451 finally:
452 outfp.close()
453 infp.close()
455 cflags = ['$(OPT)']
456 cppflags = defines + includes
457 libs = [os.path.join(binlib, 'libpython$(VERSION).a')]
459 somevars = {}
460 if os.path.exists(makefile_in):
461 makevars = parsesetup.getmakevars(makefile_in)
462 for key in makevars.keys():
463 somevars[key] = makevars[key]
465 somevars['CFLAGS'] = string.join(cflags) # override
466 somevars['CPPFLAGS'] = string.join(cppflags) # override
467 files = [base_config_c, base_frozen_c] + \
468 files + supp_sources + addfiles + libs + \
469 ['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)']
471 outfp = bkfile.open(makefile, 'w')
472 try:
473 makemakefile.makemakefile(outfp, somevars, files, base_target)
474 finally:
475 outfp.close()
477 # Done!
479 if odir:
480 print 'Now run "make" in', odir,
481 print 'to build the target:', base_target
482 else:
483 print 'Now run "make" to build the target:', base_target
486 # Print usage message and exit
488 def usage(msg):
489 sys.stdout = sys.stderr
490 print "Error:", msg
491 print "Use ``%s -h'' for help" % sys.argv[0]
492 sys.exit(2)
495 main()