blob: 3ab56fd0fe1d041b70eb317cc6e66529fc5aa2dd [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Guido van Rossumdbaf3321994-10-03 10:25:54 +00002
Guido van Rossum96c4dd91996-08-26 05:14:20 +00003"""Freeze a Python script into a binary.
Guido van Rossumdbaf3321994-10-03 10:25:54 +00004
Guido van Rossum1e074031998-03-05 04:05:38 +00005usage: freeze [options...] script [module]...
Guido van Rossum00ff4331994-10-03 16:33:08 +00006
Guido van Rossum96c4dd91996-08-26 05:14:20 +00007Options:
Guido van Rossum9a6e8551997-08-10 16:47:17 +00008-p prefix: This is the prefix used when you ran ``make install''
Guido van Rossum96c4dd91996-08-26 05:14:20 +00009 in the Python build directory.
Guido van Rossumd8336c21994-10-05 16:13:01 +000010 (If you never ran this, freeze won't work.)
Guido van Rossum96c4dd91996-08-26 05:14:20 +000011 The default is whatever sys.prefix evaluates to.
Guido van Rossum0b4b8a21997-08-10 16:56:48 +000012 It can also be the top directory of the Python source
13 tree; then -P must point to the build tree.
Guido van Rossumd8336c21994-10-05 16:13:01 +000014
Guido van Rossum150316e1995-08-08 14:21:07 +000015-P exec_prefix: Like -p but this is the 'exec_prefix', used to
Guido van Rossum0b4b8a21997-08-10 16:56:48 +000016 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.
Guido van Rossum150316e1995-08-08 14:21:07 +000020
Guido van Rossumd8336c21994-10-05 16:13:01 +000021-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.
Guido van Rossumbaf06031998-08-25 14:06:55 +000024 On Windows, the name of a .INI file describing one
25 or more extensions is passed.
Guido van Rossumd8336c21994-10-05 16:13:01 +000026 More than one -e option may be given.
27
Guido van Rossum96c4dd91996-08-26 05:14:20 +000028-o dir: Directory where the output files are created; default '.'.
29
Guido van Rossum75dc4961998-03-05 03:42:00 +000030-m: Additional arguments are module names instead of filenames.
31
Guido van Rossume35c6011998-05-18 20:25:54 +000032-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.
36
Guido van Rossum78fc3631998-03-20 17:37:24 +000037-l file: Pass the file to the linker (windows only)
38
Guido van Rossum75dc4961998-03-05 03:42:00 +000039-d: Debugging mode for the module finder.
40
41-q: Make the module finder totally quiet.
42
Guido van Rossum9a6e8551997-08-10 16:47:17 +000043-h: Print this help message.
44
Guido van Rossum03f7f082001-10-18 19:15:32 +000045-x module Exclude the specified module. It will still be imported
46 by the frozen binary if it exists on the host system.
47
48-X module Like -x, except the module can never be imported by
49 the frozen binary.
50
51-E: Freeze will fail if any modules can't be found (that
52 were not excluded using -x or -X).
Guido van Rossum78fc3631998-03-20 17:37:24 +000053
Guido van Rossumbaf06031998-08-25 14:06:55 +000054-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)
59
Tim Peters182b5ac2004-07-18 06:16:08 +000060-s subsystem: Specify the subsystem (For Windows only.);
Guido van Rossum78fc3631998-03-20 17:37:24 +000061 'console' (default), 'windows', 'service' or 'com_dll'
Tim Peters182b5ac2004-07-18 06:16:08 +000062
Guido van Rossumbaf06031998-08-25 14:06:55 +000063-w: Toggle Windows (NT or 95) behavior.
Thomas Wouters7e474022000-07-16 12:04:32 +000064 (For debugging only -- on a win32 platform, win32 behavior
Guido van Rossumbaf06031998-08-25 14:06:55 +000065 is automatic.)
Guido van Rossum58a59481997-08-14 01:45:33 +000066
Guido van Rossum6b767ac2001-03-20 20:43:34 +000067-r prefix=f: Replace path prefix.
Tim Peters182b5ac2004-07-18 06:16:08 +000068 Replace prefix with f in the source path references
Guido van Rossum6b767ac2001-03-20 20:43:34 +000069 contained in the resulting binary.
70
Guido van Rossum96c4dd91996-08-26 05:14:20 +000071Arguments:
72
Guido van Rossum1e074031998-03-05 04:05:38 +000073script: The Python script to be executed by the resulting binary.
Guido van Rossumd8336c21994-10-05 16:13:01 +000074
75module ...: Additional Python modules (referenced by pathname)
76 that will be included in the resulting binary. These
Guido van Rossum75dc4961998-03-05 03:42:00 +000077 may be .py or .pyc files. If -m is specified, these are
78 module names that are search in the path instead.
Guido van Rossum150316e1995-08-08 14:21:07 +000079
80NOTES:
81
82In order to use freeze successfully, you must have built Python and
Guido van Rossumd4cc04c1996-06-17 17:49:13 +000083installed it ("make install").
Guido van Rossum150316e1995-08-08 14:21:07 +000084
Guido van Rossum96c4dd91996-08-26 05:14:20 +000085The script should not use modules provided only as shared libraries;
86if it does, the resulting binary is not self-contained.
Guido van Rossumd8336c21994-10-05 16:13:01 +000087"""
Guido van Rossum00ff4331994-10-03 16:33:08 +000088
89
Guido van Rossum00ff4331994-10-03 16:33:08 +000090# Import standard modules
91
Gustavo Niemeyer7b4abbb2003-05-26 23:52:30 +000092import modulefinder
Guido van Rossumdbaf3321994-10-03 10:25:54 +000093import getopt
Guido van Rossum00ff4331994-10-03 16:33:08 +000094import os
Guido van Rossum00ff4331994-10-03 16:33:08 +000095import sys
Guido van Rossumdbaf3321994-10-03 10:25:54 +000096
Guido van Rossumdbaf3321994-10-03 10:25:54 +000097
Guido van Rossum00ff4331994-10-03 16:33:08 +000098# Import the freeze-private modules
99
Guido van Rossumd8336c21994-10-05 16:13:01 +0000100import checkextensions
Guido van Rossum00ff4331994-10-03 16:33:08 +0000101import makeconfig
102import makefreeze
103import makemakefile
104import parsesetup
Guido van Rossumbaf06031998-08-25 14:06:55 +0000105import bkfile
Guido van Rossum00ff4331994-10-03 16:33:08 +0000106
Guido van Rossum00ff4331994-10-03 16:33:08 +0000107
108# Main program
109
Guido van Rossumdbaf3321994-10-03 10:25:54 +0000110def main():
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000111 # overridable context
112 prefix = None # settable with -p option
113 exec_prefix = None # settable with -P option
114 extensions = []
Guido van Rossum78fc3631998-03-20 17:37:24 +0000115 exclude = [] # settable with -x option
Guido van Rossume35c6011998-05-18 20:25:54 +0000116 addn_link = [] # settable with -l, but only honored under Windows.
Guido van Rossum1e074031998-03-05 04:05:38 +0000117 path = sys.path[:]
Guido van Rossum75dc4961998-03-05 03:42:00 +0000118 modargs = 0
119 debug = 1
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000120 odir = ''
Guido van Rossum58a59481997-08-14 01:45:33 +0000121 win = sys.platform[:3] == 'win'
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000122 replace_paths = [] # settable with -r option
Guido van Rossum03f7f082001-10-18 19:15:32 +0000123 error_if_any_missing = 0
Guido van Rossum00ff4331994-10-03 16:33:08 +0000124
Guido van Rossum78fc3631998-03-20 17:37:24 +0000125 # default the exclude list for each platform
Guido van Rossumbaf06031998-08-25 14:06:55 +0000126 if win: exclude = exclude + [
Victor Stinnerd7538dd2018-12-14 13:37:26 +0100127 'dos', 'dospath', 'mac', 'macfs', 'MACFS', 'posix', ]
Guido van Rossum03f7f082001-10-18 19:15:32 +0000128
129 fail_import = exclude[:]
Guido van Rossum78fc3631998-03-20 17:37:24 +0000130
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000131 # output files
132 frozen_c = 'frozen.c'
133 config_c = 'config.c'
134 target = 'a.out' # normally derived from script name
135 makefile = 'Makefile'
Guido van Rossum58a59481997-08-14 01:45:33 +0000136 subsystem = 'console'
Guido van Rossum00ff4331994-10-03 16:33:08 +0000137
Guido van Rossum03f7f082001-10-18 19:15:32 +0000138 # parse command line by first replacing any "-i" options with the
139 # file contents.
Guido van Rossumbaf06031998-08-25 14:06:55 +0000140 pos = 1
Guido van Rossum03f7f082001-10-18 19:15:32 +0000141 while pos < len(sys.argv)-1:
142 # last option can not be "-i", so this ensures "pos+1" is in range!
Guido van Rossumbaf06031998-08-25 14:06:55 +0000143 if sys.argv[pos] == '-i':
144 try:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000145 options = open(sys.argv[pos+1]).read().split()
Guido van Rossumb940e112007-01-10 16:19:56 +0000146 except IOError as why:
Guido van Rossum03f7f082001-10-18 19:15:32 +0000147 usage("File name '%s' specified with the -i option "
148 "can not be read - %s" % (sys.argv[pos+1], why) )
Guido van Rossumbaf06031998-08-25 14:06:55 +0000149 # Replace the '-i' and the filename with the read params.
150 sys.argv[pos:pos+2] = options
151 pos = pos + len(options) - 1 # Skip the name and the included args.
152 pos = pos + 1
153
154 # Now parse the command line with the extras inserted.
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000155 try:
Guido van Rossum03f7f082001-10-18 19:15:32 +0000156 opts, args = getopt.getopt(sys.argv[1:], 'r:a:dEe:hmo:p:P:qs:wX:x:l:')
Guido van Rossumb940e112007-01-10 16:19:56 +0000157 except getopt.error as msg:
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000158 usage('getopt error: ' + str(msg))
Guido van Rossum00ff4331994-10-03 16:33:08 +0000159
Martin Pantereb995702016-07-28 01:11:04 +0000160 # process option arguments
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000161 for o, a in opts:
162 if o == '-h':
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000163 print(__doc__)
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000164 return
Guido van Rossum75dc4961998-03-05 03:42:00 +0000165 if o == '-d':
166 debug = debug + 1
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000167 if o == '-e':
168 extensions.append(a)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000169 if o == '-m':
170 modargs = 1
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000171 if o == '-o':
172 odir = a
173 if o == '-p':
174 prefix = a
175 if o == '-P':
176 exec_prefix = a
Guido van Rossum75dc4961998-03-05 03:42:00 +0000177 if o == '-q':
178 debug = 0
Guido van Rossum58a59481997-08-14 01:45:33 +0000179 if o == '-w':
180 win = not win
181 if o == '-s':
182 if not win:
183 usage("-s subsystem option only on Windows")
184 subsystem = a
Guido van Rossum78fc3631998-03-20 17:37:24 +0000185 if o == '-x':
186 exclude.append(a)
Guido van Rossum03f7f082001-10-18 19:15:32 +0000187 if o == '-X':
188 exclude.append(a)
189 fail_import.append(a)
190 if o == '-E':
191 error_if_any_missing = 1
Guido van Rossum78fc3631998-03-20 17:37:24 +0000192 if o == '-l':
193 addn_link.append(a)
Guido van Rossume35c6011998-05-18 20:25:54 +0000194 if o == '-a':
Neal Norwitzd9108552006-03-17 08:00:19 +0000195 modulefinder.AddPackagePath(*a.split("=", 2))
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000196 if o == '-r':
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000197 f,r = a.split("=", 2)
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000198 replace_paths.append( (f,r) )
Guido van Rossumdbaf3321994-10-03 10:25:54 +0000199
Just van Rossume9e20a92003-03-08 19:50:38 +0000200 # modules that are imported by the Python runtime
201 implicits = []
Georg Brandl00639582010-08-02 22:25:16 +0000202 for module in ('site', 'warnings', 'encodings.utf_8', 'encodings.latin_1'):
Just van Rossume9e20a92003-03-08 19:50:38 +0000203 if module not in exclude:
204 implicits.append(module)
205
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000206 # 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
Guido van Rossum9a6e8551997-08-10 16:47:17 +0000214
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000215 # determine whether -p points to the Python source tree
Guido van Rossume0394251998-03-05 05:39:50 +0000216 ishome = os.path.exists(os.path.join(prefix, 'Python', 'ceval.c'))
Guido van Rossumdbaf3321994-10-03 10:25:54 +0000217
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000218 # locations derived from options
Serhiy Storchaka885bdc42016-02-11 13:10:36 +0200219 version = '%d.%d' % sys.version_info[:2]
AraHaana7987e72019-03-23 12:29:49 -0400220 if hasattr(sys, 'abiflags'):
221 flagged_version = version + sys.abiflags
222 else:
223 flagged_version = version
Guido van Rossum5e32a771998-07-07 22:47:38 +0000224 if win:
225 extensions_c = 'frozen_extensions.c'
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000226 if ishome:
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000227 print("(Using Python source directory)")
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000228 binlib = exec_prefix
229 incldir = os.path.join(prefix, 'Include')
Guido van Rossum590fc2c1998-06-12 14:09:03 +0000230 config_h_dir = exec_prefix
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000231 config_c_in = os.path.join(prefix, 'Modules', 'config.c.in')
Guido van Rossum58a59481997-08-14 01:45:33 +0000232 frozenmain_c = os.path.join(prefix, 'Python', 'frozenmain.c')
Gustavo Niemeyerffa5a502004-05-08 17:59:43 +0000233 makefile_in = os.path.join(exec_prefix, 'Makefile')
Guido van Rossume35c6011998-05-18 20:25:54 +0000234 if win:
235 frozendllmain_c = os.path.join(exec_prefix, 'Pc\\frozen_dllmain.c')
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000236 else:
237 binlib = os.path.join(exec_prefix,
Martin v. Löwisa7fcd922014-03-30 20:28:52 +0200238 'lib', 'python%s' % version,
239 'config-%s' % flagged_version)
240 incldir = os.path.join(prefix, 'include', 'python%s' % flagged_version)
Guido van Rossum590fc2c1998-06-12 14:09:03 +0000241 config_h_dir = os.path.join(exec_prefix, 'include',
Martin v. Löwisa7fcd922014-03-30 20:28:52 +0200242 'python%s' % flagged_version)
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000243 config_c_in = os.path.join(binlib, 'config.c.in')
244 frozenmain_c = os.path.join(binlib, 'frozenmain.c')
245 makefile_in = os.path.join(binlib, 'Makefile')
Guido van Rossum5e32a771998-07-07 22:47:38 +0000246 frozendllmain_c = os.path.join(binlib, 'frozen_dllmain.c')
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000247 supp_sources = []
248 defines = []
Guido van Rossum590fc2c1998-06-12 14:09:03 +0000249 includes = ['-I' + incldir, '-I' + config_h_dir]
Guido van Rossumd8336c21994-10-05 16:13:01 +0000250
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000251 # sanity check of directories and files
Guido van Rossumbaf06031998-08-25 14:06:55 +0000252 check_dirs = [prefix, exec_prefix, binlib, incldir]
Guido van Rossum03f7f082001-10-18 19:15:32 +0000253 if not win:
254 # These are not directories on Windows.
255 check_dirs = check_dirs + extensions
Guido van Rossumbaf06031998-08-25 14:06:55 +0000256 for dir in check_dirs:
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000257 if not os.path.exists(dir):
258 usage('needed directory %s not found' % dir)
259 if not os.path.isdir(dir):
260 usage('%s: not a directory' % dir)
Guido van Rossum58a59481997-08-14 01:45:33 +0000261 if win:
Guido van Rossumbaf06031998-08-25 14:06:55 +0000262 files = supp_sources + extensions # extensions are files on Windows.
Guido van Rossum58a59481997-08-14 01:45:33 +0000263 else:
264 files = [config_c_in, makefile_in] + supp_sources
265 for file in supp_sources:
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000266 if not os.path.exists(file):
267 usage('needed file %s not found' % file)
268 if not os.path.isfile(file):
269 usage('%s: not a plain file' % file)
Guido van Rossum58a59481997-08-14 01:45:33 +0000270 if not win:
271 for dir in extensions:
272 setup = os.path.join(dir, 'Setup')
273 if not os.path.exists(setup):
274 usage('needed file %s not found' % setup)
275 if not os.path.isfile(setup):
276 usage('%s: not a plain file' % setup)
Guido van Rossum00ff4331994-10-03 16:33:08 +0000277
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000278 # check that enough arguments are passed
279 if not args:
280 usage('at least one filename argument required')
Guido van Rossumd4cc04c1996-06-17 17:49:13 +0000281
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000282 # check that file arguments exist
283 for arg in args:
Guido van Rossum1e074031998-03-05 04:05:38 +0000284 if arg == '-m':
285 break
Guido van Rossum78fc3631998-03-20 17:37:24 +0000286 # if user specified -m on the command line before _any_
287 # file names, then nothing should be checked (as the
288 # very first file should be a module name)
289 if modargs:
290 break
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000291 if not os.path.exists(arg):
292 usage('argument %s not found' % arg)
293 if not os.path.isfile(arg):
294 usage('%s: not a plain file' % arg)
Guido van Rossum00ff4331994-10-03 16:33:08 +0000295
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000296 # process non-option arguments
297 scriptfile = args[0]
298 modules = args[1:]
Guido van Rossum00ff4331994-10-03 16:33:08 +0000299
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000300 # derive target name from script name
301 base = os.path.basename(scriptfile)
302 base, ext = os.path.splitext(base)
303 if base:
304 if base != scriptfile:
305 target = base
306 else:
307 target = base + '.bin'
Guido van Rossum00ff4331994-10-03 16:33:08 +0000308
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000309 # handle -o option
310 base_frozen_c = frozen_c
311 base_config_c = config_c
312 base_target = target
313 if odir and not os.path.isdir(odir):
314 try:
315 os.mkdir(odir)
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000316 print("Created output directory", odir)
Andrew Svetlov8b33dd82012-12-24 19:58:48 +0200317 except OSError as msg:
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000318 usage('%s: mkdir failed (%s)' % (odir, str(msg)))
Guido van Rossumbaf06031998-08-25 14:06:55 +0000319 base = ''
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000320 if odir:
Guido van Rossumbaf06031998-08-25 14:06:55 +0000321 base = os.path.join(odir, '')
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000322 frozen_c = os.path.join(odir, frozen_c)
323 config_c = os.path.join(odir, config_c)
324 target = os.path.join(odir, target)
325 makefile = os.path.join(odir, makefile)
Guido van Rossume35c6011998-05-18 20:25:54 +0000326 if win: extensions_c = os.path.join(odir, extensions_c)
Guido van Rossum00ff4331994-10-03 16:33:08 +0000327
Guido van Rossum78fc3631998-03-20 17:37:24 +0000328 # Handle special entry point requirements
329 # (on Windows, some frozen programs do not use __main__, but
330 # import the module directly. Eg, DLLs, Services, etc
331 custom_entry_point = None # Currently only used on Windows
332 python_entry_is_main = 1 # Is the entry point called __main__?
333 # handle -s option on Windows
334 if win:
335 import winmakemakefile
336 try:
Guido van Rossume35c6011998-05-18 20:25:54 +0000337 custom_entry_point, python_entry_is_main = \
338 winmakemakefile.get_custom_entry_point(subsystem)
Guido van Rossumb940e112007-01-10 16:19:56 +0000339 except ValueError as why:
Guido van Rossum78fc3631998-03-20 17:37:24 +0000340 usage(why)
Tim Peters182b5ac2004-07-18 06:16:08 +0000341
Guido van Rossum78fc3631998-03-20 17:37:24 +0000342
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000343 # Actual work starts here...
Guido van Rossumd8336c21994-10-05 16:13:01 +0000344
Guido van Rossum75dc4961998-03-05 03:42:00 +0000345 # collect all modules of the program
Guido van Rossum1e074031998-03-05 04:05:38 +0000346 dir = os.path.dirname(scriptfile)
347 path[0] = dir
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000348 mf = modulefinder.ModuleFinder(path, debug, exclude, replace_paths)
Tim Peters182b5ac2004-07-18 06:16:08 +0000349
Guido van Rossum78fc3631998-03-20 17:37:24 +0000350 if win and subsystem=='service':
351 # If a Windows service, then add the "built-in" module.
352 mod = mf.add_module("servicemanager")
353 mod.__file__="dummy.pyd" # really built-in to the resulting EXE
354
Guido van Rossum75dc4961998-03-05 03:42:00 +0000355 for mod in implicits:
356 mf.import_hook(mod)
357 for mod in modules:
358 if mod == '-m':
359 modargs = 1
360 continue
361 if modargs:
362 if mod[-2:] == '.*':
363 mf.import_hook(mod[:-2], None, ["*"])
364 else:
365 mf.import_hook(mod)
366 else:
367 mf.load_file(mod)
Guido van Rossum78fc3631998-03-20 17:37:24 +0000368
Martin v. Löwisc00d39e2014-03-30 21:07:25 +0200369 # Alias "importlib._bootstrap" to "_frozen_importlib" so that the
Eric Snow32439d62015-05-02 19:15:18 -0600370 # import machinery can bootstrap. Do the same for
371 # importlib._bootstrap_external.
Martin v. Löwisc00d39e2014-03-30 21:07:25 +0200372 mf.modules["_frozen_importlib"] = mf.modules["importlib._bootstrap"]
Eric Snow32439d62015-05-02 19:15:18 -0600373 mf.modules["_frozen_importlib_external"] = mf.modules["importlib._bootstrap_external"]
Martin v. Löwisc00d39e2014-03-30 21:07:25 +0200374
Guido van Rossum78fc3631998-03-20 17:37:24 +0000375 # Add the main script as either __main__, or the actual module name.
376 if python_entry_is_main:
377 mf.run_script(scriptfile)
378 else:
Guido van Rossum4b1235c2000-05-06 03:18:08 +0000379 mf.load_file(scriptfile)
Guido van Rossum78fc3631998-03-20 17:37:24 +0000380
Guido van Rossum75dc4961998-03-05 03:42:00 +0000381 if debug > 0:
382 mf.report()
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000383 print()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000384 dict = mf.modules
Guido van Rossumdbaf3321994-10-03 10:25:54 +0000385
Guido van Rossum03f7f082001-10-18 19:15:32 +0000386 if error_if_any_missing:
387 missing = mf.any_missing()
388 if missing:
389 sys.exit("There are some missing modules: %r" % missing)
390
Guido van Rossum75dc4961998-03-05 03:42:00 +0000391 # generate output for frozen modules
Guido van Rossum03f7f082001-10-18 19:15:32 +0000392 files = makefreeze.makefreeze(base, dict, debug, custom_entry_point,
393 fail_import)
Guido van Rossumdbaf3321994-10-03 10:25:54 +0000394
Guido van Rossuma0e18351998-03-07 04:51:03 +0000395 # look for unfrozen modules (builtin and of unknown origin)
396 builtins = []
397 unknown = []
Guido van Rossum53970392007-06-12 00:28:30 +0000398 mods = sorted(dict.keys())
Guido van Rossuma0e18351998-03-07 04:51:03 +0000399 for mod in mods:
400 if dict[mod].__code__:
401 continue
402 if not dict[mod].__file__:
403 builtins.append(mod)
404 else:
405 unknown.append(mod)
406
407 # search for unknown modules in extensions directories (not on Windows)
408 addfiles = []
Guido van Rossume35c6011998-05-18 20:25:54 +0000409 frozen_extensions = [] # Windows list of modules.
Guido van Rossuma937afd1998-04-23 14:39:24 +0000410 if unknown or (not win and builtins):
Guido van Rossum78fc3631998-03-20 17:37:24 +0000411 if not win:
412 addfiles, addmods = \
Guido van Rossuma937afd1998-04-23 14:39:24 +0000413 checkextensions.checkextensions(unknown+builtins,
414 extensions)
Guido van Rossum78fc3631998-03-20 17:37:24 +0000415 for mod in addmods:
Guido van Rossuma937afd1998-04-23 14:39:24 +0000416 if mod in unknown:
417 unknown.remove(mod)
418 builtins.append(mod)
Guido van Rossum78fc3631998-03-20 17:37:24 +0000419 else:
420 # Do the windows thang...
421 import checkextensions_win32
Tim Peters182b5ac2004-07-18 06:16:08 +0000422 # Get a list of CExtension instances, each describing a module
Guido van Rossum78fc3631998-03-20 17:37:24 +0000423 # (including its source files)
Guido van Rossume35c6011998-05-18 20:25:54 +0000424 frozen_extensions = checkextensions_win32.checkextensions(
Guido van Rossumf67c2382000-07-13 15:45:17 +0000425 unknown, extensions, prefix)
Guido van Rossume35c6011998-05-18 20:25:54 +0000426 for mod in frozen_extensions:
Guido van Rossum78fc3631998-03-20 17:37:24 +0000427 unknown.remove(mod.name)
Guido van Rossuma0e18351998-03-07 04:51:03 +0000428
429 # report unknown modules
430 if unknown:
431 sys.stderr.write('Warning: unknown modules remain: %s\n' %
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000432 ' '.join(unknown))
Guido van Rossuma0e18351998-03-07 04:51:03 +0000433
Guido van Rossum75dc4961998-03-05 03:42:00 +0000434 # windows gets different treatment
Guido van Rossum58a59481997-08-14 01:45:33 +0000435 if win:
436 # Taking a shortcut here...
Guido van Rossume35c6011998-05-18 20:25:54 +0000437 import winmakemakefile, checkextensions_win32
438 checkextensions_win32.write_extension_table(extensions_c,
439 frozen_extensions)
440 # Create a module definition for the bootstrap C code.
441 xtras = [frozenmain_c, os.path.basename(frozen_c),
Guido van Rossum7039f501999-03-12 22:07:05 +0000442 frozendllmain_c, os.path.basename(extensions_c)] + files
Guido van Rossume35c6011998-05-18 20:25:54 +0000443 maindefn = checkextensions_win32.CExtension( '__main__', xtras )
444 frozen_extensions.append( maindefn )
Serhiy Storchaka53c3fb12015-03-20 09:21:59 +0200445 with open(makefile, 'w') as outfp:
Guido van Rossum58a59481997-08-14 01:45:33 +0000446 winmakemakefile.makemakefile(outfp,
447 locals(),
Guido van Rossume35c6011998-05-18 20:25:54 +0000448 frozen_extensions,
Guido van Rossum31d53ed1998-03-07 04:08:04 +0000449 os.path.basename(target))
Guido van Rossum58a59481997-08-14 01:45:33 +0000450 return
451
Guido van Rossum75dc4961998-03-05 03:42:00 +0000452 # generate config.c and Makefile
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000453 builtins.sort()
Serhiy Storchaka53c3fb12015-03-20 09:21:59 +0200454 with open(config_c_in) as infp, bkfile.open(config_c, 'w') as outfp:
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000455 makeconfig.makeconfig(infp, outfp, builtins)
Guido van Rossumdbaf3321994-10-03 10:25:54 +0000456
Neil Schemenauer89e90d62001-06-02 06:16:02 +0000457 cflags = ['$(OPT)']
458 cppflags = defines + includes
Martin v. Löwisa7fcd922014-03-30 20:28:52 +0200459 libs = [os.path.join(binlib, '$(LDLIBRARY)')]
Guido van Rossum00ff4331994-10-03 16:33:08 +0000460
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000461 somevars = {}
Guido van Rossum345df171997-11-22 22:10:01 +0000462 if os.path.exists(makefile_in):
463 makevars = parsesetup.getmakevars(makefile_in)
Georg Brandlbf82e372008-05-16 17:02:34 +0000464 for key in makevars:
Gustavo Niemeyerffa5a502004-05-08 17:59:43 +0000465 somevars[key] = makevars[key]
Guido van Rossum00ff4331994-10-03 16:33:08 +0000466
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000467 somevars['CFLAGS'] = ' '.join(cflags) # override
468 somevars['CPPFLAGS'] = ' '.join(cppflags) # override
Marc-André Lemburg64b4f272002-04-04 16:15:41 +0000469 files = [base_config_c, base_frozen_c] + \
Guido van Rossumbaf06031998-08-25 14:06:55 +0000470 files + supp_sources + addfiles + libs + \
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000471 ['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)']
Guido van Rossum00ff4331994-10-03 16:33:08 +0000472
Serhiy Storchaka53c3fb12015-03-20 09:21:59 +0200473 with bkfile.open(makefile, 'w') as outfp:
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000474 makemakefile.makemakefile(outfp, somevars, files, base_target)
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000475
476 # Done!
477
478 if odir:
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000479 print('Now run "make" in', odir, end=' ')
480 print('to build the target:', base_target)
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000481 else:
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000482 print('Now run "make" to build the target:', base_target)
Guido van Rossum00ff4331994-10-03 16:33:08 +0000483
Guido van Rossumd8336c21994-10-05 16:13:01 +0000484
485# Print usage message and exit
486
Guido van Rossum9a6e8551997-08-10 16:47:17 +0000487def usage(msg):
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000488 sys.stdout = sys.stderr
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000489 print("Error:", msg)
490 print("Use ``%s -h'' for help" % sys.argv[0])
Guido van Rossum0b4b8a21997-08-10 16:56:48 +0000491 sys.exit(2)
Guido van Rossumd8336c21994-10-05 16:13:01 +0000492
493
Guido van Rossumdbaf3321994-10-03 10:25:54 +0000494main()