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