Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 3 | """Freeze a Python script into a binary. |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 1e07403 | 1998-03-05 04:05:38 +0000 | [diff] [blame] | 5 | usage: freeze [options...] script [module]... |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 7 | Options: |
Guido van Rossum | 9a6e855 | 1997-08-10 16:47:17 +0000 | [diff] [blame] | 8 | -p prefix: This is the prefix used when you ran ``make install'' |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 9 | in the Python build directory. |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 10 | (If you never ran this, freeze won't work.) |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 11 | The default is whatever sys.prefix evaluates to. |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 12 | It can also be the top directory of the Python source |
| 13 | tree; then -P must point to the build tree. |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 150316e | 1995-08-08 14:21:07 +0000 | [diff] [blame] | 15 | -P exec_prefix: Like -p but this is the 'exec_prefix', used to |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 150316e | 1995-08-08 14:21:07 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 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. |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 24 | On Windows, the name of a .INI file describing one |
| 25 | or more extensions is passed. |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 26 | More than one -e option may be given. |
| 27 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 28 | -o dir: Directory where the output files are created; default '.'. |
| 29 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 30 | -m: Additional arguments are module names instead of filenames. |
| 31 | |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 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. |
| 36 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 37 | -l file: Pass the file to the linker (windows only) |
| 38 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 39 | -d: Debugging mode for the module finder. |
| 40 | |
| 41 | -q: Make the module finder totally quiet. |
| 42 | |
Guido van Rossum | 9a6e855 | 1997-08-10 16:47:17 +0000 | [diff] [blame] | 43 | -h: Print this help message. |
| 44 | |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 45 | -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 Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 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) |
| 59 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 60 | -s subsystem: Specify the subsystem (For Windows only.); |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 61 | 'console' (default), 'windows', 'service' or 'com_dll' |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 63 | -w: Toggle Windows (NT or 95) behavior. |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 64 | (For debugging only -- on a win32 platform, win32 behavior |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 65 | is automatic.) |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 67 | -r prefix=f: Replace path prefix. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 68 | Replace prefix with f in the source path references |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 69 | contained in the resulting binary. |
| 70 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 71 | Arguments: |
| 72 | |
Guido van Rossum | 1e07403 | 1998-03-05 04:05:38 +0000 | [diff] [blame] | 73 | script: The Python script to be executed by the resulting binary. |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 74 | |
| 75 | module ...: Additional Python modules (referenced by pathname) |
| 76 | that will be included in the resulting binary. These |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 77 | may be .py or .pyc files. If -m is specified, these are |
| 78 | module names that are search in the path instead. |
Guido van Rossum | 150316e | 1995-08-08 14:21:07 +0000 | [diff] [blame] | 79 | |
| 80 | NOTES: |
| 81 | |
| 82 | In order to use freeze successfully, you must have built Python and |
Guido van Rossum | d4cc04c | 1996-06-17 17:49:13 +0000 | [diff] [blame] | 83 | installed it ("make install"). |
Guido van Rossum | 150316e | 1995-08-08 14:21:07 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 85 | The script should not use modules provided only as shared libraries; |
| 86 | if it does, the resulting binary is not self-contained. |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 87 | """ |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 88 | |
| 89 | |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 90 | # Import standard modules |
| 91 | |
Gustavo Niemeyer | 7b4abbb | 2003-05-26 23:52:30 +0000 | [diff] [blame] | 92 | import modulefinder |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 93 | import getopt |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 94 | import os |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 95 | import sys |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 98 | # Import the freeze-private modules |
| 99 | |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 100 | import checkextensions |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 101 | import makeconfig |
| 102 | import makefreeze |
| 103 | import makemakefile |
| 104 | import parsesetup |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 105 | import bkfile |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 107 | |
| 108 | # Main program |
| 109 | |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 110 | def main(): |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 111 | # overridable context |
| 112 | prefix = None # settable with -p option |
| 113 | exec_prefix = None # settable with -P option |
| 114 | extensions = [] |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 115 | exclude = [] # settable with -x option |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 116 | addn_link = [] # settable with -l, but only honored under Windows. |
Guido van Rossum | 1e07403 | 1998-03-05 04:05:38 +0000 | [diff] [blame] | 117 | path = sys.path[:] |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 118 | modargs = 0 |
| 119 | debug = 1 |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 120 | odir = '' |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 121 | win = sys.platform[:3] == 'win' |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 122 | replace_paths = [] # settable with -r option |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 123 | error_if_any_missing = 0 |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 125 | # default the exclude list for each platform |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 126 | if win: exclude = exclude + [ |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 127 | 'dos', 'dospath', 'mac', 'macpath', 'macfs', 'MACFS', 'posix', |
Jesus Cea | b176203 | 2012-10-05 02:27:40 +0200 | [diff] [blame] | 128 | 'ce', |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 129 | ] |
| 130 | |
| 131 | fail_import = exclude[:] |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 133 | # output files |
| 134 | frozen_c = 'frozen.c' |
| 135 | config_c = 'config.c' |
| 136 | target = 'a.out' # normally derived from script name |
| 137 | makefile = 'Makefile' |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 138 | subsystem = 'console' |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 140 | # parse command line by first replacing any "-i" options with the |
| 141 | # file contents. |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 142 | pos = 1 |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 143 | while pos < len(sys.argv)-1: |
| 144 | # last option can not be "-i", so this ensures "pos+1" is in range! |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 145 | if sys.argv[pos] == '-i': |
| 146 | try: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 147 | options = open(sys.argv[pos+1]).read().split() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 148 | except IOError as why: |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 149 | usage("File name '%s' specified with the -i option " |
| 150 | "can not be read - %s" % (sys.argv[pos+1], why) ) |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 151 | # Replace the '-i' and the filename with the read params. |
| 152 | sys.argv[pos:pos+2] = options |
| 153 | pos = pos + len(options) - 1 # Skip the name and the included args. |
| 154 | pos = pos + 1 |
| 155 | |
| 156 | # Now parse the command line with the extras inserted. |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 157 | try: |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 158 | opts, args = getopt.getopt(sys.argv[1:], 'r:a:dEe:hmo:p:P:qs:wX:x:l:') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 159 | except getopt.error as msg: |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 160 | usage('getopt error: ' + str(msg)) |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 162 | # proces option arguments |
| 163 | for o, a in opts: |
| 164 | if o == '-h': |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 165 | print(__doc__) |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 166 | return |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 167 | if o == '-d': |
| 168 | debug = debug + 1 |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 169 | if o == '-e': |
| 170 | extensions.append(a) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 171 | if o == '-m': |
| 172 | modargs = 1 |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 173 | if o == '-o': |
| 174 | odir = a |
| 175 | if o == '-p': |
| 176 | prefix = a |
| 177 | if o == '-P': |
| 178 | exec_prefix = a |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 179 | if o == '-q': |
| 180 | debug = 0 |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 181 | if o == '-w': |
| 182 | win = not win |
| 183 | if o == '-s': |
| 184 | if not win: |
| 185 | usage("-s subsystem option only on Windows") |
| 186 | subsystem = a |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 187 | if o == '-x': |
| 188 | exclude.append(a) |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 189 | if o == '-X': |
| 190 | exclude.append(a) |
| 191 | fail_import.append(a) |
| 192 | if o == '-E': |
| 193 | error_if_any_missing = 1 |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 194 | if o == '-l': |
| 195 | addn_link.append(a) |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 196 | if o == '-a': |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 197 | modulefinder.AddPackagePath(*a.split("=", 2)) |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 198 | if o == '-r': |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 199 | f,r = a.split("=", 2) |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 200 | replace_paths.append( (f,r) ) |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 201 | |
Just van Rossum | e9e20a9 | 2003-03-08 19:50:38 +0000 | [diff] [blame] | 202 | # modules that are imported by the Python runtime |
| 203 | implicits = [] |
Georg Brandl | 0063958 | 2010-08-02 22:25:16 +0000 | [diff] [blame] | 204 | for module in ('site', 'warnings', 'encodings.utf_8', 'encodings.latin_1'): |
Just van Rossum | e9e20a9 | 2003-03-08 19:50:38 +0000 | [diff] [blame] | 205 | if module not in exclude: |
| 206 | implicits.append(module) |
| 207 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 208 | # default prefix and exec_prefix |
| 209 | if not exec_prefix: |
| 210 | if prefix: |
| 211 | exec_prefix = prefix |
| 212 | else: |
| 213 | exec_prefix = sys.exec_prefix |
| 214 | if not prefix: |
| 215 | prefix = sys.prefix |
Guido van Rossum | 9a6e855 | 1997-08-10 16:47:17 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 217 | # determine whether -p points to the Python source tree |
Guido van Rossum | e039425 | 1998-03-05 05:39:50 +0000 | [diff] [blame] | 218 | ishome = os.path.exists(os.path.join(prefix, 'Python', 'ceval.c')) |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 220 | # locations derived from options |
| 221 | version = sys.version[:3] |
Guido van Rossum | 5e32a77 | 1998-07-07 22:47:38 +0000 | [diff] [blame] | 222 | if win: |
| 223 | extensions_c = 'frozen_extensions.c' |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 224 | if ishome: |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 225 | print("(Using Python source directory)") |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 226 | binlib = exec_prefix |
| 227 | incldir = os.path.join(prefix, 'Include') |
Guido van Rossum | 590fc2c | 1998-06-12 14:09:03 +0000 | [diff] [blame] | 228 | config_h_dir = exec_prefix |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 229 | config_c_in = os.path.join(prefix, 'Modules', 'config.c.in') |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 230 | frozenmain_c = os.path.join(prefix, 'Python', 'frozenmain.c') |
Gustavo Niemeyer | ffa5a50 | 2004-05-08 17:59:43 +0000 | [diff] [blame] | 231 | makefile_in = os.path.join(exec_prefix, 'Makefile') |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 232 | if win: |
| 233 | frozendllmain_c = os.path.join(exec_prefix, 'Pc\\frozen_dllmain.c') |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 234 | else: |
| 235 | binlib = os.path.join(exec_prefix, |
| 236 | 'lib', 'python%s' % version, 'config') |
| 237 | incldir = os.path.join(prefix, 'include', 'python%s' % version) |
Guido van Rossum | 590fc2c | 1998-06-12 14:09:03 +0000 | [diff] [blame] | 238 | config_h_dir = os.path.join(exec_prefix, 'include', |
| 239 | 'python%s' % version) |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 240 | config_c_in = os.path.join(binlib, 'config.c.in') |
| 241 | frozenmain_c = os.path.join(binlib, 'frozenmain.c') |
| 242 | makefile_in = os.path.join(binlib, 'Makefile') |
Guido van Rossum | 5e32a77 | 1998-07-07 22:47:38 +0000 | [diff] [blame] | 243 | frozendllmain_c = os.path.join(binlib, 'frozen_dllmain.c') |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 244 | supp_sources = [] |
| 245 | defines = [] |
Guido van Rossum | 590fc2c | 1998-06-12 14:09:03 +0000 | [diff] [blame] | 246 | includes = ['-I' + incldir, '-I' + config_h_dir] |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 248 | # sanity check of directories and files |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 249 | check_dirs = [prefix, exec_prefix, binlib, incldir] |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 250 | if not win: |
| 251 | # These are not directories on Windows. |
| 252 | check_dirs = check_dirs + extensions |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 253 | for dir in check_dirs: |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 254 | if not os.path.exists(dir): |
| 255 | usage('needed directory %s not found' % dir) |
| 256 | if not os.path.isdir(dir): |
| 257 | usage('%s: not a directory' % dir) |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 258 | if win: |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 259 | files = supp_sources + extensions # extensions are files on Windows. |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 260 | else: |
| 261 | files = [config_c_in, makefile_in] + supp_sources |
| 262 | for file in supp_sources: |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 263 | if not os.path.exists(file): |
| 264 | usage('needed file %s not found' % file) |
| 265 | if not os.path.isfile(file): |
| 266 | usage('%s: not a plain file' % file) |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 267 | if not win: |
| 268 | for dir in extensions: |
| 269 | setup = os.path.join(dir, 'Setup') |
| 270 | if not os.path.exists(setup): |
| 271 | usage('needed file %s not found' % setup) |
| 272 | if not os.path.isfile(setup): |
| 273 | usage('%s: not a plain file' % setup) |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 275 | # check that enough arguments are passed |
| 276 | if not args: |
| 277 | usage('at least one filename argument required') |
Guido van Rossum | d4cc04c | 1996-06-17 17:49:13 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 279 | # check that file arguments exist |
| 280 | for arg in args: |
Guido van Rossum | 1e07403 | 1998-03-05 04:05:38 +0000 | [diff] [blame] | 281 | if arg == '-m': |
| 282 | break |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 283 | # if user specified -m on the command line before _any_ |
| 284 | # file names, then nothing should be checked (as the |
| 285 | # very first file should be a module name) |
| 286 | if modargs: |
| 287 | break |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 288 | if not os.path.exists(arg): |
| 289 | usage('argument %s not found' % arg) |
| 290 | if not os.path.isfile(arg): |
| 291 | usage('%s: not a plain file' % arg) |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 293 | # process non-option arguments |
| 294 | scriptfile = args[0] |
| 295 | modules = args[1:] |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 297 | # derive target name from script name |
| 298 | base = os.path.basename(scriptfile) |
| 299 | base, ext = os.path.splitext(base) |
| 300 | if base: |
| 301 | if base != scriptfile: |
| 302 | target = base |
| 303 | else: |
| 304 | target = base + '.bin' |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 306 | # handle -o option |
| 307 | base_frozen_c = frozen_c |
| 308 | base_config_c = config_c |
| 309 | base_target = target |
| 310 | if odir and not os.path.isdir(odir): |
| 311 | try: |
| 312 | os.mkdir(odir) |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 313 | print("Created output directory", odir) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 314 | except os.error as msg: |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 315 | usage('%s: mkdir failed (%s)' % (odir, str(msg))) |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 316 | base = '' |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 317 | if odir: |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 318 | base = os.path.join(odir, '') |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 319 | frozen_c = os.path.join(odir, frozen_c) |
| 320 | config_c = os.path.join(odir, config_c) |
| 321 | target = os.path.join(odir, target) |
| 322 | makefile = os.path.join(odir, makefile) |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 323 | if win: extensions_c = os.path.join(odir, extensions_c) |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 325 | # Handle special entry point requirements |
| 326 | # (on Windows, some frozen programs do not use __main__, but |
| 327 | # import the module directly. Eg, DLLs, Services, etc |
| 328 | custom_entry_point = None # Currently only used on Windows |
| 329 | python_entry_is_main = 1 # Is the entry point called __main__? |
| 330 | # handle -s option on Windows |
| 331 | if win: |
| 332 | import winmakemakefile |
| 333 | try: |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 334 | custom_entry_point, python_entry_is_main = \ |
| 335 | winmakemakefile.get_custom_entry_point(subsystem) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 336 | except ValueError as why: |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 337 | usage(why) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 340 | # Actual work starts here... |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 342 | # collect all modules of the program |
Guido van Rossum | 1e07403 | 1998-03-05 04:05:38 +0000 | [diff] [blame] | 343 | dir = os.path.dirname(scriptfile) |
| 344 | path[0] = dir |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 345 | mf = modulefinder.ModuleFinder(path, debug, exclude, replace_paths) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 347 | if win and subsystem=='service': |
| 348 | # If a Windows service, then add the "built-in" module. |
| 349 | mod = mf.add_module("servicemanager") |
| 350 | mod.__file__="dummy.pyd" # really built-in to the resulting EXE |
| 351 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 352 | for mod in implicits: |
| 353 | mf.import_hook(mod) |
| 354 | for mod in modules: |
| 355 | if mod == '-m': |
| 356 | modargs = 1 |
| 357 | continue |
| 358 | if modargs: |
| 359 | if mod[-2:] == '.*': |
| 360 | mf.import_hook(mod[:-2], None, ["*"]) |
| 361 | else: |
| 362 | mf.import_hook(mod) |
| 363 | else: |
| 364 | mf.load_file(mod) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 365 | |
| 366 | # Add the main script as either __main__, or the actual module name. |
| 367 | if python_entry_is_main: |
| 368 | mf.run_script(scriptfile) |
| 369 | else: |
Guido van Rossum | 4b1235c | 2000-05-06 03:18:08 +0000 | [diff] [blame] | 370 | mf.load_file(scriptfile) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 371 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 372 | if debug > 0: |
| 373 | mf.report() |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 374 | print() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 375 | dict = mf.modules |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 377 | if error_if_any_missing: |
| 378 | missing = mf.any_missing() |
| 379 | if missing: |
| 380 | sys.exit("There are some missing modules: %r" % missing) |
| 381 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 382 | # generate output for frozen modules |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 383 | files = makefreeze.makefreeze(base, dict, debug, custom_entry_point, |
| 384 | fail_import) |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | a0e1835 | 1998-03-07 04:51:03 +0000 | [diff] [blame] | 386 | # look for unfrozen modules (builtin and of unknown origin) |
| 387 | builtins = [] |
| 388 | unknown = [] |
Guido van Rossum | 5397039 | 2007-06-12 00:28:30 +0000 | [diff] [blame] | 389 | mods = sorted(dict.keys()) |
Guido van Rossum | a0e1835 | 1998-03-07 04:51:03 +0000 | [diff] [blame] | 390 | for mod in mods: |
| 391 | if dict[mod].__code__: |
| 392 | continue |
| 393 | if not dict[mod].__file__: |
| 394 | builtins.append(mod) |
| 395 | else: |
| 396 | unknown.append(mod) |
| 397 | |
| 398 | # search for unknown modules in extensions directories (not on Windows) |
| 399 | addfiles = [] |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 400 | frozen_extensions = [] # Windows list of modules. |
Guido van Rossum | a937afd | 1998-04-23 14:39:24 +0000 | [diff] [blame] | 401 | if unknown or (not win and builtins): |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 402 | if not win: |
| 403 | addfiles, addmods = \ |
Guido van Rossum | a937afd | 1998-04-23 14:39:24 +0000 | [diff] [blame] | 404 | checkextensions.checkextensions(unknown+builtins, |
| 405 | extensions) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 406 | for mod in addmods: |
Guido van Rossum | a937afd | 1998-04-23 14:39:24 +0000 | [diff] [blame] | 407 | if mod in unknown: |
| 408 | unknown.remove(mod) |
| 409 | builtins.append(mod) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 410 | else: |
| 411 | # Do the windows thang... |
| 412 | import checkextensions_win32 |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 413 | # Get a list of CExtension instances, each describing a module |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 414 | # (including its source files) |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 415 | frozen_extensions = checkextensions_win32.checkextensions( |
Guido van Rossum | f67c238 | 2000-07-13 15:45:17 +0000 | [diff] [blame] | 416 | unknown, extensions, prefix) |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 417 | for mod in frozen_extensions: |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 418 | unknown.remove(mod.name) |
Guido van Rossum | a0e1835 | 1998-03-07 04:51:03 +0000 | [diff] [blame] | 419 | |
| 420 | # report unknown modules |
| 421 | if unknown: |
| 422 | sys.stderr.write('Warning: unknown modules remain: %s\n' % |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 423 | ' '.join(unknown)) |
Guido van Rossum | a0e1835 | 1998-03-07 04:51:03 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 425 | # windows gets different treatment |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 426 | if win: |
| 427 | # Taking a shortcut here... |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 428 | import winmakemakefile, checkextensions_win32 |
| 429 | checkextensions_win32.write_extension_table(extensions_c, |
| 430 | frozen_extensions) |
| 431 | # Create a module definition for the bootstrap C code. |
| 432 | xtras = [frozenmain_c, os.path.basename(frozen_c), |
Guido van Rossum | 7039f50 | 1999-03-12 22:07:05 +0000 | [diff] [blame] | 433 | frozendllmain_c, os.path.basename(extensions_c)] + files |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 434 | maindefn = checkextensions_win32.CExtension( '__main__', xtras ) |
| 435 | frozen_extensions.append( maindefn ) |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 436 | outfp = open(makefile, 'w') |
| 437 | try: |
| 438 | winmakemakefile.makemakefile(outfp, |
| 439 | locals(), |
Guido van Rossum | e35c601 | 1998-05-18 20:25:54 +0000 | [diff] [blame] | 440 | frozen_extensions, |
Guido van Rossum | 31d53ed | 1998-03-07 04:08:04 +0000 | [diff] [blame] | 441 | os.path.basename(target)) |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 442 | finally: |
| 443 | outfp.close() |
| 444 | return |
| 445 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 446 | # generate config.c and Makefile |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 447 | builtins.sort() |
| 448 | infp = open(config_c_in) |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 449 | outfp = bkfile.open(config_c, 'w') |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 450 | try: |
| 451 | makeconfig.makeconfig(infp, outfp, builtins) |
| 452 | finally: |
| 453 | outfp.close() |
| 454 | infp.close() |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 455 | |
Neil Schemenauer | 89e90d6 | 2001-06-02 06:16:02 +0000 | [diff] [blame] | 456 | cflags = ['$(OPT)'] |
| 457 | cppflags = defines + includes |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 458 | libs = [os.path.join(binlib, 'libpython$(VERSION).a')] |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 459 | |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 460 | somevars = {} |
Guido van Rossum | 345df17 | 1997-11-22 22:10:01 +0000 | [diff] [blame] | 461 | if os.path.exists(makefile_in): |
| 462 | makevars = parsesetup.getmakevars(makefile_in) |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 463 | for key in makevars: |
Gustavo Niemeyer | ffa5a50 | 2004-05-08 17:59:43 +0000 | [diff] [blame] | 464 | somevars[key] = makevars[key] |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 465 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 466 | somevars['CFLAGS'] = ' '.join(cflags) # override |
| 467 | somevars['CPPFLAGS'] = ' '.join(cppflags) # override |
Marc-André Lemburg | 64b4f27 | 2002-04-04 16:15:41 +0000 | [diff] [blame] | 468 | files = [base_config_c, base_frozen_c] + \ |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 469 | files + supp_sources + addfiles + libs + \ |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 470 | ['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)'] |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 472 | outfp = bkfile.open(makefile, 'w') |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 473 | try: |
| 474 | makemakefile.makemakefile(outfp, somevars, files, base_target) |
| 475 | finally: |
| 476 | outfp.close() |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 477 | |
| 478 | # Done! |
| 479 | |
| 480 | if odir: |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 481 | print('Now run "make" in', odir, end=' ') |
| 482 | print('to build the target:', base_target) |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 483 | else: |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 484 | print('Now run "make" to build the target:', base_target) |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 486 | |
| 487 | # Print usage message and exit |
| 488 | |
Guido van Rossum | 9a6e855 | 1997-08-10 16:47:17 +0000 | [diff] [blame] | 489 | def usage(msg): |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 490 | sys.stdout = sys.stderr |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 491 | print("Error:", msg) |
| 492 | print("Use ``%s -h'' for help" % sys.argv[0]) |
Guido van Rossum | 0b4b8a2 | 1997-08-10 16:56:48 +0000 | [diff] [blame] | 493 | sys.exit(2) |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 494 | |
| 495 | |
Guido van Rossum | dbaf332 | 1994-10-03 10:25:54 +0000 | [diff] [blame] | 496 | main() |