| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright 2008, Google Inc. |
| 3 # All rights reserved. |
| 4 # |
| 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are |
| 7 # met: |
| 8 # |
| 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. |
| 11 # * Redistributions in binary form must reproduce the above |
| 12 # copyright notice, this list of conditions and the following disclaimer |
| 13 # in the documentation and/or other materials provided with the |
| 14 # distribution. |
| 15 # * Neither the name of Google Inc. nor the names of its |
| 16 # contributors may be used to endorse or promote products derived from |
| 17 # this software without specific prior written permission. |
| 18 # |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 """Build tool setup for Windows. |
| 32 |
| 33 This module is a SCons tool which should be include in the topmost windows |
| 34 environment. |
| 35 It is used as follows: |
| 36 env = base_env.Clone(tools = ['component_setup']) |
| 37 win_env = base_env.Clone(tools = ['target_platform_windows']) |
| 38 """ |
| 39 |
| 40 |
| 41 import os |
| 42 import time |
| 43 import command_output |
| 44 import SCons.Script |
| 45 |
| 46 |
| 47 def WaitForWritable(target, source, env): |
| 48 """Waits for the target to become writable. |
| 49 |
| 50 Args: |
| 51 target: List of target nodes. |
| 52 source: List of source nodes. |
| 53 env: Environment context. |
| 54 |
| 55 Returns: |
| 56 Zero if success, nonzero if error. |
| 57 |
| 58 This is a necessary hack on Windows, where antivirus software can lock exe |
| 59 files briefly after they're written. This can cause subsequent reads of the |
| 60 file by env.Install() to fail. To prevent these failures, wait for the file |
| 61 to be writable. |
| 62 """ |
| 63 target_path = target[0].abspath |
| 64 if not os.path.exists(target_path): |
| 65 return 0 # Nothing to wait for |
| 66 |
| 67 for retries in range(10): |
| 68 try: |
| 69 f = open(target_path, 'a+b') |
| 70 f.close() |
| 71 return 0 # Successfully opened file for write, so we're done |
| 72 except (IOError, OSError): |
| 73 print 'Waiting for access to %s...' % target_path |
| 74 time.sleep(1) |
| 75 |
| 76 # If we're still here, fail |
| 77 print 'Timeout waiting for access to %s.' % target_path |
| 78 return 1 |
| 79 |
| 80 |
| 81 def RunManifest(target, source, env, resource_num): |
| 82 """Run the Microsoft Visual Studio manifest tool (mt.exe). |
| 83 |
| 84 Args: |
| 85 target: List of target nodes. |
| 86 source: List of source nodes. |
| 87 env: Environment context. |
| 88 resource_num: Resource number to modify in target (1=exe, 2=dll). |
| 89 |
| 90 Returns: |
| 91 Zero if success, nonzero if error. |
| 92 |
| 93 The mt.exe tool seems to experience intermittent failures trying to write to |
| 94 .exe or .dll files. Antivirus software makes this worse, but the problem |
| 95 can still occur even if antivirus software is disabled. The failures look |
| 96 like: |
| 97 |
| 98 mt.exe : general error c101008d: Failed to write the updated manifest to |
| 99 the resource of file "(name of exe)". Access is denied. |
| 100 |
| 101 with mt.exe returning an errorlevel (return code) of 31. The workaround is |
| 102 to retry running mt.exe after a short delay. |
| 103 """ |
| 104 |
| 105 cmdline = env.subst( |
| 106 'mt.exe -nologo -manifest ${TARGET}.manifest -outputresource:$TARGET;%d' |
| 107 % resource_num, |
| 108 target=target, source=source) |
| 109 print cmdline |
| 110 |
| 111 for retry in range(5): |
| 112 # If this is a retry, print a message and delay first |
| 113 if retry: |
| 114 # mt.exe failed to write to the target file. Print a warning message, |
| 115 # delay 3 seconds, and retry. |
| 116 print 'Warning: mt.exe failed to write to %s; retrying.' % target[0] |
| 117 time.sleep(3) |
| 118 |
| 119 return_code, output = command_output.RunCommand( |
| 120 cmdline, env=env['ENV'], echo_output=False) |
| 121 if return_code != 31: # Something other than the intermittent error |
| 122 break |
| 123 |
| 124 # Pass through output (if any) and return code from manifest |
| 125 if output: |
| 126 print output |
| 127 return return_code |
| 128 |
| 129 |
| 130 def RunManifestExe(target, source, env): |
| 131 """Calls RunManifest for updating an executable (resource_num=1).""" |
| 132 return RunManifest(target, source, env, resource_num=1) |
| 133 |
| 134 |
| 135 def RunManifestDll(target, source, env): |
| 136 """Calls RunManifest for updating a dll (resource_num=2).""" |
| 137 return RunManifest(target, source, env, resource_num=2) |
| 138 |
| 139 |
| 140 def ComponentPlatformSetup(env, builder_name): |
| 141 """Hook to allow platform to modify environment inside a component builder. |
| 142 |
| 143 Args: |
| 144 env: Environment to modify |
| 145 builder_name: Name of the builder |
| 146 """ |
| 147 if env.get('ENABLE_EXCEPTIONS'): |
| 148 env.FilterOut( |
| 149 CPPDEFINES=['_HAS_EXCEPTIONS=0'], |
| 150 # There are problems with LTCG when some files are compiled with |
| 151 # exceptions and some aren't (the v-tables for STL and BOOST classes |
| 152 # don't match). Therefore, turn off LTCG when exceptions are enabled. |
| 153 CCFLAGS=['/GL'], |
| 154 LINKFLAGS=['/LTCG'], |
| 155 ARFLAGS=['/LTCG'], |
| 156 ) |
| 157 env.Append(CCFLAGS=['/EHsc']) |
| 158 |
| 159 if builder_name in ('ComponentObject', 'ComponentLibrary'): |
| 160 if env.get('COMPONENT_STATIC'): |
| 161 env.Append(CPPDEFINES=['_LIB']) |
| 162 else: |
| 163 env.Append(CPPDEFINES=['_USRDLL', '_WINDLL']) |
| 164 |
| 165 if builder_name == 'ComponentTestProgram': |
| 166 env.FilterOut( |
| 167 CPPDEFINES=['_WINDOWS'], |
| 168 LINKFLAGS=['/SUBSYSTEM:WINDOWS'], |
| 169 ) |
| 170 env.Append( |
| 171 CPPDEFINES=['_CONSOLE'], |
| 172 LINKFLAGS=['/SUBSYSTEM:CONSOLE'], |
| 173 ) |
| 174 |
| 175 #------------------------------------------------------------------------------ |
| 176 |
| 177 |
| 178 def generate(env): |
| 179 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 180 """SCons entry point for this tool.""" |
| 181 |
| 182 # Set up environment paths first |
| 183 |
| 184 # Load various Visual Studio related tools. |
| 185 env.Tool('as') |
| 186 env.Tool('msvs') |
| 187 env.Tool('windows_hard_link') |
| 188 |
| 189 pre_msvc_env = env['ENV'].copy() |
| 190 |
| 191 env.Tool('msvc') |
| 192 env.Tool('mslib') |
| 193 env.Tool('mslink') |
| 194 |
| 195 # The msvc, mslink, and mslib tools search the registry for installed copies |
| 196 # of Visual Studio and prepends them to the PATH, INCLUDE, and LIB |
| 197 # environment variables. Block these changes if necessary. |
| 198 if env.get('MSVC_BLOCK_ENVIRONMENT_CHANGES'): |
| 199 env['ENV'] = pre_msvc_env |
| 200 |
| 201 # Declare bits |
| 202 DeclareBit('windows', 'Target platform is windows.', |
| 203 exclusive_groups=('target_platform')) |
| 204 env.SetBits('windows') |
| 205 |
| 206 env.Replace( |
| 207 TARGET_PLATFORM='WINDOWS', |
| 208 COMPONENT_PLATFORM_SETUP=ComponentPlatformSetup, |
| 209 |
| 210 # A better rebuild command (actually cleans, then rebuild) |
| 211 MSVSREBUILDCOM=''.join(['$MSVSSCONSCOM -c "$MSVSBUILDTARGET" && ', |
| 212 '$MSVSSCONSCOM "$MSVSBUILDTARGET"']), |
| 213 ) |
| 214 |
| 215 env.Append( |
| 216 HOST_PLATFORMS=['WINDOWS'], |
| 217 CPPDEFINES=['OS_WINDOWS=OS_WINDOWS'], |
| 218 |
| 219 # Turn up the warning level |
| 220 CCFLAGS=['/W3'], |
| 221 |
| 222 # Force x86 platform for now |
| 223 LINKFLAGS=['/MACHINE:X86'], |
| 224 ARFLAGS=['/MACHINE:X86'], |
| 225 |
| 226 # Settings for debug |
| 227 CCFLAGS_DEBUG=[ |
| 228 '/Od', # disable optimizations |
| 229 '/RTC1', # enable fast checks |
| 230 '/MTd', # link with LIBCMTD.LIB debug lib |
| 231 ], |
| 232 LINKFLAGS_DEBUG=['/DEBUG'], |
| 233 |
| 234 # Settings for optimized |
| 235 CCFLAGS_OPTIMIZED=[ |
| 236 '/O1', # optimize for size |
| 237 '/MT', # link with LIBCMT.LIB (multi-threaded, static linked crt) |
| 238 '/GS', # enable security checks |
| 239 ], |
| 240 |
| 241 # Settings for component_builders |
| 242 COMPONENT_LIBRARY_LINK_SUFFIXES=['.lib'], |
| 243 COMPONENT_LIBRARY_DEBUG_SUFFIXES=['.pdb'], |
| 244 ) |
| 245 |
| 246 # Add manifests to EXEs and DLLs |
| 247 wait_action = SCons.Script.Action(WaitForWritable, |
| 248 lambda target, source, env: ''), |
| 249 env['LINKCOM'] = [ |
| 250 env['LINKCOM'], |
| 251 SCons.Script.Action(RunManifestExe, lambda target, source, env: ''), |
| 252 SCons.Script.Delete('${TARGET}.manifest'), |
| 253 wait_action, |
| 254 ] |
| 255 env['SHLINKCOM'] = [ |
| 256 env['SHLINKCOM'], |
| 257 SCons.Script.Action(RunManifestDll, lambda target, source, env: ''), |
| 258 SCons.Script.Delete('${TARGET}.manifest'), |
| 259 wait_action, |
| 260 ] |
| 261 env['WINDOWS_INSERT_MANIFESTS'] = True |
| 262 env.Append(LINKFLAGS=['-manifest']) |
| OLD | NEW |