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 | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 53 | print "# Makefile for Windows (NT or 95) generated by freeze.py script" |
| 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 | 77b3008 | 1997-08-14 20:13:46 +0000 | [diff] [blame] | 57 | # XXX The following line is fishy and may need manual fixing |
Guido van Rossum | 2addd2a | 1998-03-07 05:10:00 +0000 | [diff] [blame] | 58 | print 'pythonlib = "%s"' % (vars['exec_prefix'] + |
| 59 | "/pcbuild/release/python15.lib") |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 60 | |
| 61 | # We only ever write one "entry point" symbol - either |
| 62 | # "main" or "WinMain". Therefore, there is no need to |
| 63 | # pass a subsystem switch to the linker as it works it |
| 64 | # out all by itself. However, the subsystem _does_ determine |
| 65 | # the file extension and additional linker flags. |
| 66 | target_link_flags = "" |
| 67 | target_ext = ".exe" |
| 68 | if subsystem_details[vars['subsystem']][2]: |
| 69 | target_link_flags = "-dll" |
| 70 | target_ext = ".dll" |
| 71 | |
| 72 | print "cdl = /MD" # XXX - Should this come from vars? User may have specific requirements... |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 73 | print |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 74 | print "all: $(target)%s" % (target_ext) |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 75 | print |
| 76 | |
| 77 | objects = [] |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 78 | libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib"] |
| 79 | for moddefn in moddefns: |
| 80 | print "# Module", moddefn.name |
| 81 | for file in moddefn.sourceFiles: |
| 82 | base = os.path.basename(file) |
| 83 | base, ext = os.path.splitext(base) |
| 84 | objects.append(base + ".obj") |
| 85 | print '%s.obj: "%s"' % (base, file) |
| 86 | print "\t@$(CC) -c -nologo $(cdl) /D BUILD_FREEZE", |
| 87 | print "-I$(pythonhome)/Include -I$(pythonhome)/PC \\" |
| 88 | print "\t\t$(cflags) $(cdebug) $(cinclude) \\" |
| 89 | extra = moddefn.GetCompilerOptions() |
| 90 | if extra: |
| 91 | print "\t\t%s \\" % (string.join(extra),) |
| 92 | print '\t\t"%s"' % file |
| 93 | print |
Guido van Rossum | 58a5948 | 1997-08-14 01:45:33 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 95 | # Add .lib files this module needs |
| 96 | for modlib in moddefn.GetLinkerLibs(): |
| 97 | if modlib not in libs: |
| 98 | libs.append(modlib) |
| 99 | |
| 100 | print "ADDN_LINK_FILES=", |
| 101 | for addn in vars['addn_link']: print '"%s"' % (addn), |
| 102 | print ; print |
| 103 | |
| 104 | print "OBJS=", |
| 105 | for obj in objects: print '"%s"' % (obj), |
| 106 | print ; print |
| 107 | |
| 108 | print "LIBS=", |
| 109 | for lib in libs: print '"%s"' % (lib), |
| 110 | print ; print |
| 111 | |
| 112 | print "$(target)%s: $(OBJS)" % (target_ext) |
| 113 | print "\tlink -out:$(target)%s %s" % (target_ext, target_link_flags), |
| 114 | print "\t$(OBJS) \\" |
| 115 | print "\t$(LIBS) \\" |
| 116 | print "\t$(ADDN_LINK_FILES) \\" |
| 117 | print "\t\t$(pythonlib) $(lcustom)\\" |
| 118 | print "\t\t$(resources)" |
Guido van Rossum | 2addd2a | 1998-03-07 05:10:00 +0000 | [diff] [blame] | 119 | print |
| 120 | print "clean:" |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 121 | print "\t-rm -f *.obj" |
| 122 | print "\t-rm -f $(target).exe" |