Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 1 | import sys, os, string |
| 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: |
| 42 | raise ValueError, "The subsystem %s is not known" % subsystem |
| 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): |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 54 | print "# Makefile for Microsoft Visual C++ generated by freeze.py script" |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 55 | print |
Guido van Rossum | 2addd2a | 1998-03-07 05:10:00 +0000 | [diff] [blame] | 56 | print 'target = %s' % target |
Guido van Rossum | 4312890 | 1999-06-21 22:36:53 +0000 | [diff] [blame] | 57 | print 'pythonhome = %s' % vars['prefix'] |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 58 | print |
| 59 | print 'DEBUG=0 # Set to 1 to use the _d versions of Python.' |
| 60 | print '!IF $(DEBUG)' |
| 61 | print 'debug_suffix=_d' |
| 62 | print 'c_debug=/Zi /Od /DDEBUG /D_DEBUG' |
| 63 | print 'l_debug=/DEBUG' |
| 64 | print 'temp_dir=Build\\Debug' |
| 65 | print '!ELSE' |
| 66 | print 'debug_suffix=' |
| 67 | print 'c_debug=/Ox' |
| 68 | print 'l_debug=' |
| 69 | print 'temp_dir=Build\\Release' |
| 70 | print '!ENDIF' |
| 71 | print |
| 72 | |
| 73 | print '# The following line assumes you have built Python using the standard instructions' |
| 74 | print '# Otherwise fix the following line to point to the library.' |
| 75 | print 'pythonlib = "$(pythonhome)/pcbuild/python15$(debug_suffix).lib"' |
| 76 | print |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 77 | |
| 78 | # We only ever write one "entry point" symbol - either |
| 79 | # "main" or "WinMain". Therefore, there is no need to |
| 80 | # pass a subsystem switch to the linker as it works it |
| 81 | # out all by itself. However, the subsystem _does_ determine |
| 82 | # the file extension and additional linker flags. |
| 83 | target_link_flags = "" |
| 84 | target_ext = ".exe" |
| 85 | if subsystem_details[vars['subsystem']][2]: |
| 86 | target_link_flags = "-dll" |
| 87 | target_ext = ".dll" |
| 88 | |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 89 | |
| 90 | print "# As the target uses Python15.dll, we must use this compiler option!" |
| 91 | print "cdl = /MD" |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 92 | print |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 93 | print "all: $(target)$(debug_suffix)%s" % (target_ext) |
| 94 | print |
| 95 | |
| 96 | print '$(temp_dir):' |
| 97 | print ' if not exist $(temp_dir)\. mkdir $(temp_dir)' |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 98 | print |
| 99 | |
| 100 | objects = [] |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 101 | libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"] |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 102 | for moddefn in moddefns: |
| 103 | print "# Module", moddefn.name |
| 104 | for file in moddefn.sourceFiles: |
| 105 | base = os.path.basename(file) |
| 106 | base, ext = os.path.splitext(base) |
| 107 | objects.append(base + ".obj") |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 108 | print '$(temp_dir)\%s.obj: "%s"' % (base, file) |
| 109 | print "\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE", |
Guido van Rossum | 4312890 | 1999-06-21 22:36:53 +0000 | [diff] [blame] | 110 | print '"-I$(pythonhome)/Include" "-I$(pythonhome)/PC" \\' |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 111 | print "\t\t$(cflags) $(cdebug) $(cinclude) \\" |
| 112 | extra = moddefn.GetCompilerOptions() |
| 113 | if extra: |
| 114 | print "\t\t%s \\" % (string.join(extra),) |
| 115 | print '\t\t"%s"' % file |
| 116 | print |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 118 | # Add .lib files this module needs |
| 119 | for modlib in moddefn.GetLinkerLibs(): |
| 120 | if modlib not in libs: |
| 121 | libs.append(modlib) |
| 122 | |
| 123 | print "ADDN_LINK_FILES=", |
| 124 | for addn in vars['addn_link']: print '"%s"' % (addn), |
| 125 | print ; print |
| 126 | |
| 127 | print "OBJS=", |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 128 | for obj in objects: print '"$(temp_dir)\%s"' % (obj), |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 129 | print ; print |
| 130 | |
| 131 | print "LIBS=", |
| 132 | for lib in libs: print '"%s"' % (lib), |
| 133 | print ; print |
| 134 | |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 135 | print "$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext) |
| 136 | print "\tlink -out:$(target)$(debug_suffix)%s %s" % (target_ext, target_link_flags), |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 137 | print "\t$(OBJS) \\" |
| 138 | print "\t$(LIBS) \\" |
| 139 | print "\t$(ADDN_LINK_FILES) \\" |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 140 | print "\t$(pythonlib) $(lcustom) $(l_debug)\\" |
| 141 | print "\t$(resources)" |
Guido van Rossum | 2addd2a | 1998-03-07 05:10:00 +0000 | [diff] [blame] | 142 | print |
| 143 | print "clean:" |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 144 | print "\t-rm -f *.obj" |
| 145 | print "\t-rm -f $(target).exe" |