blob: 3dcf9027e5cadc3f4413b646abfd549b1b8c10ec [file] [log] [blame]
José Fonseca58a3d7d2008-02-23 19:49:08 +09001"""engine.SCons.Tool.msvc
2
3Tool-specific initialization for Microsoft Visual C/C++.
4
5There normally shouldn't be any need to import this module directly.
6It will usually be imported through the generic SCons.Tool.Tool()
7selection method.
8
9"""
10
11#
12# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 The SCons Foundation
13#
14# Permission is hereby granted, free of charge, to any person obtaining
15# a copy of this software and associated documentation files (the
16# "Software"), to deal in the Software without restriction, including
17# without limitation the rights to use, copy, modify, merge, publish,
18# distribute, sublicense, and/or sell copies of the Software, and to
19# permit persons to whom the Software is furnished to do so, subject to
20# the following conditions:
21#
22# The above copyright notice and this permission notice shall be included
23# in all copies or substantial portions of the Software.
24#
25# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
26# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
27# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32#
33
34__revision__ = "src/engine/SCons/Tool/msvc.py 2523 2007/12/12 09:37:41 knight"
35
36import os.path
37import re
38import string
39
40import SCons.Action
41import SCons.Builder
42import SCons.Errors
43import SCons.Platform.win32
44import SCons.Tool
45import SCons.Tool.mslib
46import SCons.Tool.mslink
47import SCons.Util
48import SCons.Warnings
49
50CSuffixes = ['.c', '.C']
51CXXSuffixes = ['.cc', '.cpp', '.cxx', '.c++', '.C++']
52
53def get_winddk_paths(env, version=None):
54 """Return a 3-tuple of (INCLUDE, LIB, PATH) as the values
55 of those three environment variables that should be set
56 in order to execute the MSVC tools properly."""
57
58 WINDDKdir = None
59 exe_paths = []
60 lib_paths = []
61 include_paths = []
62
63 if 'BASEDIR' in os.environ:
64 WINDDKdir = os.environ['BASEDIR']
65 else:
José Fonsecaefd33682008-02-25 14:46:53 +090066 #WINDDKdir = "C:\\WINDDK\\3790.1830"
67 WINDDKdir = "C:/WINDDK/3790.1830"
José Fonseca58a3d7d2008-02-23 19:49:08 +090068
69 exe_paths.append( os.path.join(WINDDKdir, 'bin') )
José Fonsecaefd33682008-02-25 14:46:53 +090070 exe_paths.append( os.path.join(WINDDKdir, 'bin/x86') )
71 include_paths.append( os.path.join(WINDDKdir, 'inc/wxp') )
José Fonseca58a3d7d2008-02-23 19:49:08 +090072 lib_paths.append( os.path.join(WINDDKdir, 'lib') )
73
José Fonsecaefd33682008-02-25 14:46:53 +090074 target_os = 'wxp'
75 target_cpu = 'i386'
76
77 env['SDK_INC_PATH'] = os.path.join(WINDDKdir, 'inc', target_os)
78 env['CRT_INC_PATH'] = os.path.join(WINDDKdir, 'inc/crt')
79 env['DDK_INC_PATH'] = os.path.join(WINDDKdir, 'inc/ddk', target_os)
80 env['WDM_INC_PATH'] = os.path.join(WINDDKdir, 'inc/ddk/wdm', target_os)
81
82 env['SDK_LIB_PATH'] = os.path.join(WINDDKdir, 'lib', target_os, target_cpu)
83 env['CRT_LIB_PATH'] = os.path.join(WINDDKdir, 'lib/crt', target_cpu)
84 env['DDK_LIB_PATH'] = os.path.join(WINDDKdir, 'lib', target_os, target_cpu)
85 env['WDM_LIB_PATH'] = os.path.join(WINDDKdir, 'lib', target_os, target_cpu)
86
José Fonseca58a3d7d2008-02-23 19:49:08 +090087 include_path = string.join( include_paths, os.pathsep )
88 lib_path = string.join(lib_paths, os.pathsep )
89 exe_path = string.join(exe_paths, os.pathsep )
90 return (include_path, lib_path, exe_path)
91
92def validate_vars(env):
93 """Validate the PCH and PCHSTOP construction variables."""
94 if env.has_key('PCH') and env['PCH']:
95 if not env.has_key('PCHSTOP'):
96 raise SCons.Errors.UserError, "The PCHSTOP construction must be defined if PCH is defined."
97 if not SCons.Util.is_String(env['PCHSTOP']):
98 raise SCons.Errors.UserError, "The PCHSTOP construction variable must be a string: %r"%env['PCHSTOP']
99
100def pch_emitter(target, source, env):
101 """Adds the object file target."""
102
103 validate_vars(env)
104
105 pch = None
106 obj = None
107
108 for t in target:
109 if SCons.Util.splitext(str(t))[1] == '.pch':
110 pch = t
111 if SCons.Util.splitext(str(t))[1] == '.obj':
112 obj = t
113
114 if not obj:
115 obj = SCons.Util.splitext(str(pch))[0]+'.obj'
116
117 target = [pch, obj] # pch must be first, and obj second for the PCHCOM to work
118
119 return (target, source)
120
121def object_emitter(target, source, env, parent_emitter):
122 """Sets up the PCH dependencies for an object file."""
123
124 validate_vars(env)
125
126 parent_emitter(target, source, env)
127
128 if env.has_key('PCH') and env['PCH']:
129 env.Depends(target, env['PCH'])
130
131 return (target, source)
132
133def static_object_emitter(target, source, env):
134 return object_emitter(target, source, env,
135 SCons.Defaults.StaticObjectEmitter)
136
137def shared_object_emitter(target, source, env):
138 return object_emitter(target, source, env,
139 SCons.Defaults.SharedObjectEmitter)
140
141pch_action = SCons.Action.Action('$PCHCOM', '$PCHCOMSTR')
142pch_builder = SCons.Builder.Builder(action=pch_action, suffix='.pch',
143 emitter=pch_emitter,
144 source_scanner=SCons.Tool.SourceFileScanner)
145res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
146res_builder = SCons.Builder.Builder(action=res_action,
147 src_suffix='.rc',
148 suffix='.res',
149 src_builder=[],
150 source_scanner=SCons.Tool.SourceFileScanner)
151SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
152
153def generate(env):
154 """Add Builders and construction variables for MSVC++ to an Environment."""
155 static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
156
157 for suffix in CSuffixes:
158 static_obj.add_action(suffix, SCons.Defaults.CAction)
159 shared_obj.add_action(suffix, SCons.Defaults.ShCAction)
160 static_obj.add_emitter(suffix, static_object_emitter)
161 shared_obj.add_emitter(suffix, shared_object_emitter)
162
163 for suffix in CXXSuffixes:
164 static_obj.add_action(suffix, SCons.Defaults.CXXAction)
165 shared_obj.add_action(suffix, SCons.Defaults.ShCXXAction)
166 static_obj.add_emitter(suffix, static_object_emitter)
167 shared_obj.add_emitter(suffix, shared_object_emitter)
168
169 env['CCPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Z7") or ""}'])
170 env['CCPCHFLAGS'] = SCons.Util.CLVar(['${(PCH and "/Yu%s /Fp%s"%(PCHSTOP or "",File(PCH))) or ""}'])
171 env['CCCOMFLAGS'] = '$CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS /c $SOURCES /Fo$TARGET $CCPCHFLAGS $CCPDBFLAGS'
172 env['CC'] = 'cl'
173 env['CCFLAGS'] = SCons.Util.CLVar('/nologo')
174 env['CFLAGS'] = SCons.Util.CLVar('')
175 env['CCCOM'] = '$CC $CFLAGS $CCFLAGS $CCCOMFLAGS'
176 env['SHCC'] = '$CC'
177 env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
178 env['SHCFLAGS'] = SCons.Util.CLVar('$CFLAGS')
179 env['SHCCCOM'] = '$SHCC $SHCFLAGS $SHCCFLAGS $CCCOMFLAGS'
180 env['CXX'] = '$CC'
181 env['CXXFLAGS'] = SCons.Util.CLVar('$CCFLAGS $( /TP $)')
182 env['CXXCOM'] = '$CXX $CXXFLAGS $CCCOMFLAGS'
183 env['SHCXX'] = '$CXX'
184 env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
185 env['SHCXXCOM'] = '$SHCXX $SHCXXFLAGS $CCCOMFLAGS'
186 env['CPPDEFPREFIX'] = '/D'
187 env['CPPDEFSUFFIX'] = ''
188 env['INCPREFIX'] = '/I'
189 env['INCSUFFIX'] = ''
190# env.Append(OBJEMITTER = [static_object_emitter])
191# env.Append(SHOBJEMITTER = [shared_object_emitter])
192 env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
193
194 env['RC'] = 'rc'
195 env['RCFLAGS'] = SCons.Util.CLVar('')
196 env['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS $RCFLAGS /fo$TARGET $SOURCES'
197 env['BUILDERS']['RES'] = res_builder
198 env['OBJPREFIX'] = ''
199 env['OBJSUFFIX'] = '.obj'
200 env['SHOBJPREFIX'] = '$OBJPREFIX'
201 env['SHOBJSUFFIX'] = '$OBJSUFFIX'
202
203 try:
204 include_path, lib_path, exe_path = get_winddk_paths(env)
205
206 # since other tools can set these, we just make sure that the
207 # relevant stuff from MSVS is in there somewhere.
208 env.PrependENVPath('INCLUDE', include_path)
209 env.PrependENVPath('LIB', lib_path)
210 env.PrependENVPath('PATH', exe_path)
211 except (SCons.Util.RegError, SCons.Errors.InternalError):
212 pass
213
214 env['CFILESUFFIX'] = '.c'
215 env['CXXFILESUFFIX'] = '.cc'
216
217 env['PCHPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Yd") or ""}'])
218 env['PCHCOM'] = '$CXX $CXXFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS /c $SOURCES /Fo${TARGETS[1]} /Yc$PCHSTOP /Fp${TARGETS[0]} $CCPDBFLAGS $PCHPDBFLAGS'
219 env['BUILDERS']['PCH'] = pch_builder
220
221 env['AR'] = 'lib'
222 env['ARFLAGS'] = SCons.Util.CLVar('/nologo')
223 env['ARCOM'] = "${TEMPFILE('$AR $ARFLAGS /OUT:$TARGET $SOURCES')}"
224 env['LIBPREFIX'] = ''
225 env['LIBSUFFIX'] = '.lib'
226
227 SCons.Tool.mslink.generate(env)
228
José Fonsecae70a4312008-02-24 16:43:07 +0900229 # See also:
José Fonseca2bebeef2008-02-24 17:58:18 +0900230 # - WINDDK's bin/makefile.new i386mk.inc for more info.
José Fonsecae70a4312008-02-24 16:43:07 +0900231 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
232 env.Append(CPPDEFINES = [
233 'WIN32',
234 '_WINDOWS',
235 ('i386', '1'),
236 ('_X86_', '1'),
237 'STD_CALL',
238 ('CONDITION_HANDLING', '1'),
239 ('NT_INST', '0'),
240 ('_NT1X_', '100'),
241 ('WINNT', '1'),
242 ('_WIN32_WINNT', '0x0500'), # minimum required OS version
243 ('WIN32_LEAN_AND_MEAN', '1'),
244 ('DEVL', '1'),
245 ('FPO', '1'),
246 ])
247 cflags = [
248 '/GF', # Enable String Pooling
José Fonseca2bebeef2008-02-24 17:58:18 +0900249 '/GX-', # Disable C++ Exceptions
José Fonsecae70a4312008-02-24 16:43:07 +0900250 '/Zp8', # 8bytes struct member alignment
251 #'/GS-', # No Buffer Security Check
José Fonseca2bebeef2008-02-24 17:58:18 +0900252 '/GR-', # Disable Run-Time Type Info
José Fonsecae70a4312008-02-24 16:43:07 +0900253 '/Gz', # __stdcall Calling convention
254 ]
255 env.Append(CFLAGS = cflags)
256 env.Append(CXXFLAGS = cflags)
257
258 env.Append(LINKFLAGS = [
259 '/DEBUG',
260 '/NODEFAULTLIB',
261 '/SUBSYSTEM:NATIVE',
262 '/INCREMENTAL:NO',
263 #'/DRIVER',
264
265 #'-subsystem:native,4.00',
266 '-base:0x10000',
267
268 '-entry:DrvEnableDriver',
269 ])
270
José Fonseca58a3d7d2008-02-23 19:49:08 +0900271 if not env.has_key('ENV'):
272 env['ENV'] = {}
273 if not env['ENV'].has_key('SystemRoot'): # required for dlls in the winsxs folders
274 env['ENV']['SystemRoot'] = SCons.Platform.win32.get_system_root()
275
276def exists(env):
277 return env.Detect('cl')
278