blob: 136d3052656e7dccdb447feb36280d8dc3a23ce5 [file] [log] [blame]
José Fonsecab215d7d2008-05-28 01:24:06 +09001"""msvc_sa
José Fonseca58a3d7d2008-02-23 19:49:08 +09002
José Fonsecab215d7d2008-05-28 01:24:06 +09003Tool-specific initialization for Microsoft Visual C/C++.
José Fonseca6ac14882008-02-27 16:05:57 +09004
José Fonsecab215d7d2008-05-28 01:24:06 +09005Based on SCons.Tool.msvc, without the MSVS detection.
José Fonseca58a3d7d2008-02-23 19:49:08 +09006
7"""
8
9#
José Fonsecab215d7d2008-05-28 01:24:06 +090010# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
José Fonseca58a3d7d2008-02-23 19:49:08 +090011#
12# Permission is hereby granted, free of charge, to any person obtaining
13# a copy of this software and associated documentation files (the
14# "Software"), to deal in the Software without restriction, including
15# without limitation the rights to use, copy, modify, merge, publish,
16# distribute, sublicense, and/or sell copies of the Software, and to
17# permit persons to whom the Software is furnished to do so, subject to
18# the following conditions:
19#
20# The above copyright notice and this permission notice shall be included
21# in all copies or substantial portions of the Software.
22#
23# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
24# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
25# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30#
31
José Fonseca58a3d7d2008-02-23 19:49:08 +090032import os.path
33import re
34import string
35
36import SCons.Action
37import SCons.Builder
38import SCons.Errors
39import SCons.Platform.win32
40import SCons.Tool
José Fonseca58a3d7d2008-02-23 19:49:08 +090041import SCons.Util
42import SCons.Warnings
43
44CSuffixes = ['.c', '.C']
45CXXSuffixes = ['.cc', '.cpp', '.cxx', '.c++', '.C++']
46
José Fonseca58a3d7d2008-02-23 19:49:08 +090047def validate_vars(env):
48 """Validate the PCH and PCHSTOP construction variables."""
49 if env.has_key('PCH') and env['PCH']:
50 if not env.has_key('PCHSTOP'):
51 raise SCons.Errors.UserError, "The PCHSTOP construction must be defined if PCH is defined."
52 if not SCons.Util.is_String(env['PCHSTOP']):
53 raise SCons.Errors.UserError, "The PCHSTOP construction variable must be a string: %r"%env['PCHSTOP']
54
55def pch_emitter(target, source, env):
56 """Adds the object file target."""
57
58 validate_vars(env)
59
60 pch = None
61 obj = None
62
63 for t in target:
64 if SCons.Util.splitext(str(t))[1] == '.pch':
65 pch = t
66 if SCons.Util.splitext(str(t))[1] == '.obj':
67 obj = t
68
69 if not obj:
70 obj = SCons.Util.splitext(str(pch))[0]+'.obj'
71
72 target = [pch, obj] # pch must be first, and obj second for the PCHCOM to work
73
74 return (target, source)
75
76def object_emitter(target, source, env, parent_emitter):
77 """Sets up the PCH dependencies for an object file."""
78
79 validate_vars(env)
80
81 parent_emitter(target, source, env)
82
83 if env.has_key('PCH') and env['PCH']:
84 env.Depends(target, env['PCH'])
85
86 return (target, source)
87
88def static_object_emitter(target, source, env):
89 return object_emitter(target, source, env,
90 SCons.Defaults.StaticObjectEmitter)
91
92def shared_object_emitter(target, source, env):
93 return object_emitter(target, source, env,
94 SCons.Defaults.SharedObjectEmitter)
95
96pch_action = SCons.Action.Action('$PCHCOM', '$PCHCOMSTR')
97pch_builder = SCons.Builder.Builder(action=pch_action, suffix='.pch',
98 emitter=pch_emitter,
99 source_scanner=SCons.Tool.SourceFileScanner)
100res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
101res_builder = SCons.Builder.Builder(action=res_action,
102 src_suffix='.rc',
103 suffix='.res',
104 src_builder=[],
105 source_scanner=SCons.Tool.SourceFileScanner)
106SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
107
108def generate(env):
109 """Add Builders and construction variables for MSVC++ to an Environment."""
110 static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
111
112 for suffix in CSuffixes:
113 static_obj.add_action(suffix, SCons.Defaults.CAction)
114 shared_obj.add_action(suffix, SCons.Defaults.ShCAction)
115 static_obj.add_emitter(suffix, static_object_emitter)
116 shared_obj.add_emitter(suffix, shared_object_emitter)
117
118 for suffix in CXXSuffixes:
119 static_obj.add_action(suffix, SCons.Defaults.CXXAction)
120 shared_obj.add_action(suffix, SCons.Defaults.ShCXXAction)
121 static_obj.add_emitter(suffix, static_object_emitter)
122 shared_obj.add_emitter(suffix, shared_object_emitter)
123
124 env['CCPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Z7") or ""}'])
125 env['CCPCHFLAGS'] = SCons.Util.CLVar(['${(PCH and "/Yu%s /Fp%s"%(PCHSTOP or "",File(PCH))) or ""}'])
126 env['CCCOMFLAGS'] = '$CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS /c $SOURCES /Fo$TARGET $CCPCHFLAGS $CCPDBFLAGS'
127 env['CC'] = 'cl'
128 env['CCFLAGS'] = SCons.Util.CLVar('/nologo')
129 env['CFLAGS'] = SCons.Util.CLVar('')
130 env['CCCOM'] = '$CC $CFLAGS $CCFLAGS $CCCOMFLAGS'
131 env['SHCC'] = '$CC'
132 env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
133 env['SHCFLAGS'] = SCons.Util.CLVar('$CFLAGS')
134 env['SHCCCOM'] = '$SHCC $SHCFLAGS $SHCCFLAGS $CCCOMFLAGS'
135 env['CXX'] = '$CC'
136 env['CXXFLAGS'] = SCons.Util.CLVar('$CCFLAGS $( /TP $)')
137 env['CXXCOM'] = '$CXX $CXXFLAGS $CCCOMFLAGS'
138 env['SHCXX'] = '$CXX'
139 env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
140 env['SHCXXCOM'] = '$SHCXX $SHCXXFLAGS $CCCOMFLAGS'
141 env['CPPDEFPREFIX'] = '/D'
142 env['CPPDEFSUFFIX'] = ''
143 env['INCPREFIX'] = '/I'
144 env['INCSUFFIX'] = ''
145# env.Append(OBJEMITTER = [static_object_emitter])
146# env.Append(SHOBJEMITTER = [shared_object_emitter])
147 env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
148
149 env['RC'] = 'rc'
150 env['RCFLAGS'] = SCons.Util.CLVar('')
151 env['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS $RCFLAGS /fo$TARGET $SOURCES'
152 env['BUILDERS']['RES'] = res_builder
153 env['OBJPREFIX'] = ''
154 env['OBJSUFFIX'] = '.obj'
155 env['SHOBJPREFIX'] = '$OBJPREFIX'
156 env['SHOBJSUFFIX'] = '$OBJSUFFIX'
157
José Fonseca58a3d7d2008-02-23 19:49:08 +0900158 env['CFILESUFFIX'] = '.c'
159 env['CXXFILESUFFIX'] = '.cc'
160
161 env['PCHPDBFLAGS'] = SCons.Util.CLVar(['${(PDB and "/Yd") or ""}'])
162 env['PCHCOM'] = '$CXX $CXXFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS /c $SOURCES /Fo${TARGETS[1]} /Yc$PCHSTOP /Fp${TARGETS[0]} $CCPDBFLAGS $PCHPDBFLAGS'
163 env['BUILDERS']['PCH'] = pch_builder
164
José Fonseca58a3d7d2008-02-23 19:49:08 +0900165 if not env.has_key('ENV'):
166 env['ENV'] = {}
José Fonsecab215d7d2008-05-28 01:24:06 +0900167 if not env['ENV'].has_key('SystemRoot'): # required for dlls in the winsxs folders
168 env['ENV']['SystemRoot'] = SCons.Platform.win32.get_system_root()
José Fonseca58a3d7d2008-02-23 19:49:08 +0900169
170def exists(env):
171 return env.Detect('cl')
172
José Fonsecab215d7d2008-05-28 01:24:06 +0900173# vim:set ts=4 sw=4 et: