| 1 | n/a | """Extension management for Windows. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Under Windows it is unlikely the .obj files are of use, as special compiler options |
|---|
| 4 | n/a | are needed (primarily to toggle the behavior of "public" symbols. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | I don't consider it worth parsing the MSVC makefiles for compiler options. Even if |
|---|
| 7 | n/a | we get it just right, a specific freeze application may have specific compiler |
|---|
| 8 | n/a | options anyway (eg, to enable or disable specific functionality) |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | So my basic strategy is: |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | * Have some Windows INI files which "describe" one or more extension modules. |
|---|
| 13 | n/a | (Freeze comes with a default one for all known modules - but you can specify |
|---|
| 14 | n/a | your own). |
|---|
| 15 | n/a | * This description can include: |
|---|
| 16 | n/a | - The MSVC .dsp file for the extension. The .c source file names |
|---|
| 17 | n/a | are extracted from there. |
|---|
| 18 | n/a | - Specific compiler/linker options |
|---|
| 19 | n/a | - Flag to indicate if Unicode compilation is expected. |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | At the moment the name and location of this INI file is hardcoded, |
|---|
| 22 | n/a | but an obvious enhancement would be to provide command line options. |
|---|
| 23 | n/a | """ |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | import os, sys |
|---|
| 26 | n/a | try: |
|---|
| 27 | n/a | import win32api |
|---|
| 28 | n/a | except ImportError: |
|---|
| 29 | n/a | win32api = None # User has already been warned |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | class CExtension: |
|---|
| 32 | n/a | """An abstraction of an extension implemented in C/C++ |
|---|
| 33 | n/a | """ |
|---|
| 34 | n/a | def __init__(self, name, sourceFiles): |
|---|
| 35 | n/a | self.name = name |
|---|
| 36 | n/a | # A list of strings defining additional compiler options. |
|---|
| 37 | n/a | self.sourceFiles = sourceFiles |
|---|
| 38 | n/a | # A list of special compiler options to be applied to |
|---|
| 39 | n/a | # all source modules in this extension. |
|---|
| 40 | n/a | self.compilerOptions = [] |
|---|
| 41 | n/a | # A list of .lib files the final .EXE will need. |
|---|
| 42 | n/a | self.linkerLibs = [] |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def GetSourceFiles(self): |
|---|
| 45 | n/a | return self.sourceFiles |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | def AddCompilerOption(self, option): |
|---|
| 48 | n/a | self.compilerOptions.append(option) |
|---|
| 49 | n/a | def GetCompilerOptions(self): |
|---|
| 50 | n/a | return self.compilerOptions |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def AddLinkerLib(self, lib): |
|---|
| 53 | n/a | self.linkerLibs.append(lib) |
|---|
| 54 | n/a | def GetLinkerLibs(self): |
|---|
| 55 | n/a | return self.linkerLibs |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def checkextensions(unknown, extra_inis, prefix): |
|---|
| 58 | n/a | # Create a table of frozen extensions |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | defaultMapName = os.path.join( os.path.split(sys.argv[0])[0], "extensions_win32.ini") |
|---|
| 61 | n/a | if not os.path.isfile(defaultMapName): |
|---|
| 62 | n/a | sys.stderr.write("WARNING: %s can not be found - standard extensions may not be found\n" % defaultMapName) |
|---|
| 63 | n/a | else: |
|---|
| 64 | n/a | # must go on end, so other inis can override. |
|---|
| 65 | n/a | extra_inis.append(defaultMapName) |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | ret = [] |
|---|
| 68 | n/a | for mod in unknown: |
|---|
| 69 | n/a | for ini in extra_inis: |
|---|
| 70 | n/a | # print "Looking for", mod, "in", win32api.GetFullPathName(ini),"...", |
|---|
| 71 | n/a | defn = get_extension_defn( mod, ini, prefix ) |
|---|
| 72 | n/a | if defn is not None: |
|---|
| 73 | n/a | # print "Yay - found it!" |
|---|
| 74 | n/a | ret.append( defn ) |
|---|
| 75 | n/a | break |
|---|
| 76 | n/a | # print "Nope!" |
|---|
| 77 | n/a | else: # For not broken! |
|---|
| 78 | n/a | sys.stderr.write("No definition of module %s in any specified map file.\n" % (mod)) |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | return ret |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def get_extension_defn(moduleName, mapFileName, prefix): |
|---|
| 83 | n/a | if win32api is None: return None |
|---|
| 84 | n/a | os.environ['PYTHONPREFIX'] = prefix |
|---|
| 85 | n/a | dsp = win32api.GetProfileVal(moduleName, "dsp", "", mapFileName) |
|---|
| 86 | n/a | if dsp=="": |
|---|
| 87 | n/a | return None |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | # We allow environment variables in the file name |
|---|
| 90 | n/a | dsp = win32api.ExpandEnvironmentStrings(dsp) |
|---|
| 91 | n/a | # If the path to the .DSP file is not absolute, assume it is relative |
|---|
| 92 | n/a | # to the description file. |
|---|
| 93 | n/a | if not os.path.isabs(dsp): |
|---|
| 94 | n/a | dsp = os.path.join( os.path.split(mapFileName)[0], dsp) |
|---|
| 95 | n/a | # Parse it to extract the source files. |
|---|
| 96 | n/a | sourceFiles = parse_dsp(dsp) |
|---|
| 97 | n/a | if sourceFiles is None: |
|---|
| 98 | n/a | return None |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | module = CExtension(moduleName, sourceFiles) |
|---|
| 101 | n/a | # Put the path to the DSP into the environment so entries can reference it. |
|---|
| 102 | n/a | os.environ['dsp_path'] = os.path.split(dsp)[0] |
|---|
| 103 | n/a | os.environ['ini_path'] = os.path.split(mapFileName)[0] |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | cl_options = win32api.GetProfileVal(moduleName, "cl", "", mapFileName) |
|---|
| 106 | n/a | if cl_options: |
|---|
| 107 | n/a | module.AddCompilerOption(win32api.ExpandEnvironmentStrings(cl_options)) |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | exclude = win32api.GetProfileVal(moduleName, "exclude", "", mapFileName) |
|---|
| 110 | n/a | exclude = exclude.split() |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | if win32api.GetProfileVal(moduleName, "Unicode", 0, mapFileName): |
|---|
| 113 | n/a | module.AddCompilerOption('/D UNICODE /D _UNICODE') |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | libs = win32api.GetProfileVal(moduleName, "libs", "", mapFileName).split() |
|---|
| 116 | n/a | for lib in libs: |
|---|
| 117 | n/a | module.AddLinkerLib(win32api.ExpandEnvironmentStrings(lib)) |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | for exc in exclude: |
|---|
| 120 | n/a | if exc in module.sourceFiles: |
|---|
| 121 | n/a | module.sourceFiles.remove(exc) |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | return module |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | # Given an MSVC DSP file, locate C source files it uses |
|---|
| 126 | n/a | # returns a list of source files. |
|---|
| 127 | n/a | def parse_dsp(dsp): |
|---|
| 128 | n/a | # print "Processing", dsp |
|---|
| 129 | n/a | # For now, only support |
|---|
| 130 | n/a | ret = [] |
|---|
| 131 | n/a | dsp_path, dsp_name = os.path.split(dsp) |
|---|
| 132 | n/a | try: |
|---|
| 133 | n/a | lines = open(dsp, "r").readlines() |
|---|
| 134 | n/a | except IOError as msg: |
|---|
| 135 | n/a | sys.stderr.write("%s: %s\n" % (dsp, msg)) |
|---|
| 136 | n/a | return None |
|---|
| 137 | n/a | for line in lines: |
|---|
| 138 | n/a | fields = line.strip().split("=", 2) |
|---|
| 139 | n/a | if fields[0]=="SOURCE": |
|---|
| 140 | n/a | if os.path.splitext(fields[1])[1].lower() in ['.cpp', '.c']: |
|---|
| 141 | n/a | ret.append( win32api.GetFullPathName(os.path.join(dsp_path, fields[1] ) ) ) |
|---|
| 142 | n/a | return ret |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | def write_extension_table(fname, modules): |
|---|
| 145 | n/a | fp = open(fname, "w") |
|---|
| 146 | n/a | try: |
|---|
| 147 | n/a | fp.write (ext_src_header) |
|---|
| 148 | n/a | # Write fn protos |
|---|
| 149 | n/a | for module in modules: |
|---|
| 150 | n/a | # bit of a hack for .pyd's as part of packages. |
|---|
| 151 | n/a | name = module.name.split('.')[-1] |
|---|
| 152 | n/a | fp.write('extern void init%s(void);\n' % (name) ) |
|---|
| 153 | n/a | # Write the table |
|---|
| 154 | n/a | fp.write (ext_tab_header) |
|---|
| 155 | n/a | for module in modules: |
|---|
| 156 | n/a | name = module.name.split('.')[-1] |
|---|
| 157 | n/a | fp.write('\t{"%s", init%s},\n' % (name, name) ) |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | fp.write (ext_tab_footer) |
|---|
| 160 | n/a | fp.write(ext_src_footer) |
|---|
| 161 | n/a | finally: |
|---|
| 162 | n/a | fp.close() |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | ext_src_header = """\ |
|---|
| 166 | n/a | #include "Python.h" |
|---|
| 167 | n/a | """ |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | ext_tab_header = """\ |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | static struct _inittab extensions[] = { |
|---|
| 172 | n/a | """ |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | ext_tab_footer = """\ |
|---|
| 175 | n/a | /* Sentinel */ |
|---|
| 176 | n/a | {0, 0} |
|---|
| 177 | n/a | }; |
|---|
| 178 | n/a | """ |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | ext_src_footer = """\ |
|---|
| 181 | n/a | extern DL_IMPORT(int) PyImport_ExtendInittab(struct _inittab *newtab); |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | int PyInitFrozenExtensions() |
|---|
| 184 | n/a | { |
|---|
| 185 | n/a | return PyImport_ExtendInittab(extensions); |
|---|
| 186 | n/a | } |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | """ |
|---|