Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 1 | import sys, os |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 3 | # Template used then the program is a GUI program |
Guido van Rossum | f888350 | 1998-03-04 18:12:39 +0000 | [diff] [blame] | 4 | WINMAINTEMPLATE = """ |
| 5 | #include <windows.h> |
| 6 | |
| 7 | int WINAPI WinMain( |
| 8 | HINSTANCE hInstance, // handle to current instance |
| 9 | HINSTANCE hPrevInstance, // handle to previous instance |
| 10 | LPSTR lpCmdLine, // pointer to command line |
| 11 | int nCmdShow // show state of window |
| 12 | ) |
| 13 | { |
Guido van Rossum | cfd76a2 | 1999-11-02 15:44:40 +0000 | [diff] [blame] | 14 | extern int Py_FrozenMain(int, char **); |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 15 | PyImport_FrozenModules = _PyImport_FrozenModules; |
| 16 | return Py_FrozenMain(__argc, __argv); |
Guido van Rossum | f888350 | 1998-03-04 18:12:39 +0000 | [diff] [blame] | 17 | } |
| 18 | """ |
| 19 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 20 | SERVICETEMPLATE = """ |
| 21 | extern int PythonService_main(int, char **); |
| 22 | |
| 23 | int main( int argc, char **argv) |
| 24 | { |
| 25 | PyImport_FrozenModules = _PyImport_FrozenModules; |
| 26 | return PythonService_main(argc, argv); |
| 27 | } |
| 28 | """ |
| 29 | |
| 30 | subsystem_details = { |
| 31 | # -s flag : (C entry point template), (is it __main__?), (is it a DLL?) |
| 32 | 'console' : (None, 1, 0), |
| 33 | 'windows' : (WINMAINTEMPLATE, 1, 0), |
| 34 | 'service' : (SERVICETEMPLATE, 0, 0), |
| 35 | 'com_dll' : ("", 0, 1), |
| 36 | } |
| 37 | |
| 38 | def get_custom_entry_point(subsystem): |
| 39 | try: |
| 40 | return subsystem_details[subsystem][:2] |
| 41 | except KeyError: |
Collin Winter | a817e58 | 2007-08-22 23:05:06 +0000 | [diff] [blame] | 42 | raise ValueError("The subsystem %s is not known" % subsystem) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 43 | |
| 44 | |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 45 | def makemakefile(outfp, vars, files, target): |
| 46 | save = sys.stdout |
| 47 | try: |
| 48 | sys.stdout = outfp |
| 49 | realwork(vars, files, target) |
| 50 | finally: |
| 51 | sys.stdout = save |
| 52 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 53 | def realwork(vars, moddefns, target): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 54 | version_suffix = "%r%r" % sys.version_info[:2] |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 55 | print("# Makefile for Microsoft Visual C++ generated by freeze.py script") |
| 56 | print() |
| 57 | print('target = %s' % target) |
| 58 | print('pythonhome = %s' % vars['prefix']) |
| 59 | print() |
| 60 | print('DEBUG=0 # Set to 1 to use the _d versions of Python.') |
| 61 | print('!IF $(DEBUG)') |
| 62 | print('debug_suffix=_d') |
| 63 | print('c_debug=/Zi /Od /DDEBUG /D_DEBUG') |
| 64 | print('l_debug=/DEBUG') |
| 65 | print('temp_dir=Build\\Debug') |
| 66 | print('!ELSE') |
| 67 | print('debug_suffix=') |
| 68 | print('c_debug=/Ox') |
| 69 | print('l_debug=') |
| 70 | print('temp_dir=Build\\Release') |
| 71 | print('!ENDIF') |
| 72 | print() |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 74 | print('# The following line assumes you have built Python using the standard instructions') |
| 75 | print('# Otherwise fix the following line to point to the library.') |
| 76 | print('pythonlib = "$(pythonhome)/pcbuild/python%s$(debug_suffix).lib"' % version_suffix) |
| 77 | print() |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 78 | |
| 79 | # We only ever write one "entry point" symbol - either |
| 80 | # "main" or "WinMain". Therefore, there is no need to |
| 81 | # pass a subsystem switch to the linker as it works it |
| 82 | # out all by itself. However, the subsystem _does_ determine |
| 83 | # the file extension and additional linker flags. |
| 84 | target_link_flags = "" |
| 85 | target_ext = ".exe" |
| 86 | if subsystem_details[vars['subsystem']][2]: |
| 87 | target_link_flags = "-dll" |
| 88 | target_ext = ".dll" |
| 89 | |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 91 | print("# As the target uses Python%s.dll, we must use this compiler option!" % version_suffix) |
| 92 | print("cdl = /MD") |
| 93 | print() |
| 94 | print("all: $(target)$(debug_suffix)%s" % (target_ext)) |
| 95 | print() |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 97 | print('$(temp_dir):') |
| 98 | print(' if not exist $(temp_dir)\. mkdir $(temp_dir)') |
| 99 | print() |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 100 | |
| 101 | objects = [] |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 102 | libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"] |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 103 | for moddefn in moddefns: |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 104 | print("# Module", moddefn.name) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 105 | for file in moddefn.sourceFiles: |
| 106 | base = os.path.basename(file) |
| 107 | base, ext = os.path.splitext(base) |
| 108 | objects.append(base + ".obj") |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 109 | print('$(temp_dir)\%s.obj: "%s"' % (base, file)) |
| 110 | print("\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE", end=' ') |
| 111 | print('"-I$(pythonhome)/Include" "-I$(pythonhome)/PC" \\') |
| 112 | print("\t\t$(cflags) $(cdebug) $(cinclude) \\") |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 113 | extra = moddefn.GetCompilerOptions() |
| 114 | if extra: |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 115 | print("\t\t%s \\" % (' '.join(extra),)) |
| 116 | print('\t\t"%s"' % file) |
| 117 | print() |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 119 | # Add .lib files this module needs |
| 120 | for modlib in moddefn.GetLinkerLibs(): |
| 121 | if modlib not in libs: |
| 122 | libs.append(modlib) |
| 123 | |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 124 | print("ADDN_LINK_FILES=", end=' ') |
| 125 | for addn in vars['addn_link']: print('"%s"' % (addn), end=' ') |
| 126 | print() ; print() |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 128 | print("OBJS=", end=' ') |
| 129 | for obj in objects: print('"$(temp_dir)\%s"' % (obj), end=' ') |
| 130 | print() ; print() |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 132 | print("LIBS=", end=' ') |
| 133 | for lib in libs: print('"%s"' % (lib), end=' ') |
| 134 | print() ; print() |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 136 | print("$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext)) |
| 137 | print("\tlink -out:$(target)$(debug_suffix)%s %s" % (target_ext, target_link_flags), end=' ') |
| 138 | print("\t$(OBJS) \\") |
| 139 | print("\t$(LIBS) \\") |
| 140 | print("\t$(ADDN_LINK_FILES) \\") |
| 141 | print("\t$(pythonlib) $(lcustom) $(l_debug)\\") |
| 142 | print("\t$(resources)") |
| 143 | print() |
| 144 | print("clean:") |
| 145 | print("\t-rm -f *.obj") |
| 146 | print("\t-rm -f $(target).exe") |