1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
12 Copies the given "win tool" (which the toolchain uses to wrap compiler
13 invocations) and the environment blocks for the 32-bit and 64-bit builds on
14 Windows to the build directory.
16 The arguments are the visual studio install location and the location of the
17 win tool. The script assumes that the root build directory is the current dir
18 and the files will be written to the current directory.
22 def ExtractImportantEnvironment():
23 """Extracts environment variables required for the toolchain from the
24 current environment."""
26 'goma_.*', # TODO(scottmg): This is ugly, but needed for goma.
27 'include', # Needed by midl compiler.
35 for envvar
in envvars_to_save
:
36 if envvar
in os
.environ
:
37 envvar
= envvar
.lower()
39 # Our own rules (for running gyp-win-tool) and other actions in
40 # Chromium rely on python being in the path. Add the path to this
41 # python here so that if it's not in the path when ninja is run
42 # later, python will still be found.
43 result
[envvar
.upper()] = os
.path
.dirname(sys
.executable
) + \
44 os
.pathsep
+ os
.environ
[envvar
]
46 result
[envvar
.upper()] = os
.environ
[envvar
]
47 for required
in ('SYSTEMROOT', 'TEMP', 'TMP'):
48 if required
not in result
:
49 raise Exception('Environment variable "%s" '
50 'required to be set to valid path' % required
)
54 def FormatAsEnvironmentBlock(envvar_dict
):
55 """Format as an 'environment block' directly suitable for CreateProcess.
56 Briefly this is a list of key=value\0, terminated by an additional \0. See
57 CreateProcess documentation for more details."""
60 for key
, value
in envvar_dict
.iteritems():
61 block
+= key
+ '=' + value
+ nul
66 def CopyTool(source_path
):
67 """Copies the given tool to the current directory, including a warning not
69 with
open(source_path
) as source_file
:
70 tool_source
= source_file
.readlines()
72 # Add header and write it out to the current directory (which should be the
74 with
open("gyp-win-tool", 'w') as tool_file
:
75 tool_file
.write(''.join([tool_source
[0],
76 '# Generated by setup_toolchain.py do not edit.\n']
79 if len(sys
.argv
) != 4:
80 print('Usage setup_toolchain.py '
81 '<visual studio path> <win tool path> <win sdk path>')
84 tool_source
= sys
.argv
[2]
85 win_sdk_path
= sys
.argv
[3]
89 important_env_vars
= ExtractImportantEnvironment()
90 path
= important_env_vars
["PATH"].split(";")
92 # Add 32-bit compiler path to the beginning and write the block.
93 path32
= [os
.path
.join(vs_path
, "VC\\BIN")] + \
94 [os
.path
.join(win_sdk_path
, "bin\\x86")] + \
96 important_env_vars
["PATH"] = ";".join(path32
)
97 environ
= FormatAsEnvironmentBlock(important_env_vars
)
98 with
open('environment.x86', 'wb') as env_file
:
99 env_file
.write(environ
)
101 # Add 64-bit compiler path to the beginning and write the block.
102 path64
= [os
.path
.join(vs_path
, "VC\\BIN\\amd64")] + \
103 [os
.path
.join(win_sdk_path
, "bin\\x64")] + \
105 important_env_vars
["PATH"] = ";".join(path64
)
106 environ
= FormatAsEnvironmentBlock(important_env_vars
)
107 with
open('environment.x64', 'wb') as env_file
:
108 env_file
.write(environ
)