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