OLD | NEW |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
6 import re | 6 import re |
7 import sys | 7 import sys |
8 | 8 |
9 def ExtractImportantEnvironment(): | 9 def ExtractImportantEnvironment(): |
10 """Extracts environment variables required for the toolchain from the | 10 """Extracts environment variables required for the toolchain from the |
(...skipping 17 matching lines...) Expand all Loading... |
28 result[envvar.upper()] = os.path.dirname(sys.executable) + \ | 28 result[envvar.upper()] = os.path.dirname(sys.executable) + \ |
29 os.pathsep + os.environ[envvar] | 29 os.pathsep + os.environ[envvar] |
30 else: | 30 else: |
31 result[envvar.upper()] = os.environ[envvar] | 31 result[envvar.upper()] = os.environ[envvar] |
32 for required in ('SYSTEMROOT', 'TEMP', 'TMP'): | 32 for required in ('SYSTEMROOT', 'TEMP', 'TMP'): |
33 if required not in result: | 33 if required not in result: |
34 raise Exception('Environment variable "%s" ' | 34 raise Exception('Environment variable "%s" ' |
35 'required to be set to valid path' % required) | 35 'required to be set to valid path' % required) |
36 return result | 36 return result |
37 | 37 |
| 38 |
| 39 # VC setup will add a path like this in 32-bit mode: |
| 40 # c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN |
| 41 # And this in 64-bit mode: |
| 42 # c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64 |
| 43 # Note that in 64-bit it's duplicated but the 64-bit one comes first. |
| 44 # |
| 45 # What we get as the path when running this will depend on which VS setup |
| 46 # script you've run. The following two functions try to do this. |
| 47 |
| 48 # For 32-bit compiles remove anything that ends in "\VC\WIN\amd64". |
| 49 def FixupPath32(path): |
| 50 find_64 = re.compile("VC\\\\BIN\\\\amd64\\\\*$", flags=re.IGNORECASE) |
| 51 |
| 52 for i in range(len(path)): |
| 53 if find_64.search(path[i]): |
| 54 # Found 32-bit path, insert the 64-bit one immediately before it. |
| 55 dir_64 = path[i].rstrip("\\") |
| 56 dir_64 = dir_64[:len(dir_64) - 6] # Trim off "\amd64". |
| 57 path[i] = dir_64 |
| 58 break |
| 59 return path |
| 60 |
| 61 # For 64-bit compiles, append anything ending in "\VC\BIN" with "\amd64" as |
| 62 # long as that thing isn't already in the list, and append it immediately |
| 63 # before the non-amd64-one. |
| 64 def FixupPath64(path): |
| 65 find_32 = re.compile("VC\\\\BIN\\\\*$", flags=re.IGNORECASE) |
| 66 |
| 67 for i in range(len(path)): |
| 68 if find_32.search(path[i]): |
| 69 # Found 32-bit path, insert the 64-bit one immediately before it. |
| 70 dir_32 = path[i] |
| 71 if dir_32[len(dir_32) - 1] == '\\': |
| 72 dir_64 = dir_32 + "amd64" |
| 73 else: |
| 74 dir_64 = dir_32 + "\\amd64" |
| 75 path.insert(i, dir_64) |
| 76 break |
| 77 |
| 78 return path |
| 79 |
| 80 |
38 def FormatAsEnvironmentBlock(envvar_dict): | 81 def FormatAsEnvironmentBlock(envvar_dict): |
39 """Format as an 'environment block' directly suitable for CreateProcess. | 82 """Format as an 'environment block' directly suitable for CreateProcess. |
40 Briefly this is a list of key=value\0, terminated by an additional \0. See | 83 Briefly this is a list of key=value\0, terminated by an additional \0. See |
41 CreateProcess documentation for more details.""" | 84 CreateProcess documentation for more details.""" |
42 block = '' | 85 block = '' |
43 nul = '\0' | 86 nul = '\0' |
44 for key, value in envvar_dict.iteritems(): | 87 for key, value in envvar_dict.iteritems(): |
45 block += key + '=' + value + nul | 88 block += key + '=' + value + nul |
46 block += nul | 89 block += nul |
47 return block | 90 return block |
48 | 91 |
49 def CopyTool(source_path): | 92 def CopyTool(source_path): |
50 """Copies the given tool to the current directory, including a warning not | 93 """Copies the given tool to the current directory, including a warning not |
51 to edit it.""" | 94 to edit it.""" |
52 with open(source_path) as source_file: | 95 with open(source_path) as source_file: |
53 tool_source = source_file.readlines() | 96 tool_source = source_file.readlines() |
54 | 97 |
55 # Add header and write it out to the current directory (which should be the | 98 # Add header and write it out to the current directory (which should be the |
56 # root build dir). | 99 # root build dir). |
57 with open("gyp-win-tool", 'w') as tool_file: | 100 with open("gyp-win-tool", 'w') as tool_file: |
58 tool_file.write(''.join([tool_source[0], | 101 tool_file.write(''.join([tool_source[0], |
59 '# Generated by setup_toolchain.py do not edit.\n'] | 102 '# Generated by setup_toolchain.py do not edit.\n'] |
60 + tool_source[1:])) | 103 + tool_source[1:])) |
61 | 104 |
| 105 |
62 # Find the tool source, it's the first argument, and copy it. | 106 # Find the tool source, it's the first argument, and copy it. |
63 if len(sys.argv) != 2: | 107 if len(sys.argv) != 2: |
64 print "Need one argument (win_tool source path)." | 108 print "Need one argument (win_tool source path)." |
65 sys.exit(1) | 109 sys.exit(1) |
66 CopyTool(sys.argv[1]) | 110 CopyTool(sys.argv[1]) |
67 | 111 |
68 # Write the environment file to the current directory. | 112 important_env_vars = ExtractImportantEnvironment() |
69 environ = FormatAsEnvironmentBlock(ExtractImportantEnvironment()) | 113 path = important_env_vars["PATH"].split(";") |
| 114 |
| 115 important_env_vars["PATH"] = ";".join(FixupPath32(path)) |
| 116 environ = FormatAsEnvironmentBlock(important_env_vars) |
70 with open('environment.x86', 'wb') as env_file: | 117 with open('environment.x86', 'wb') as env_file: |
71 env_file.write(environ) | 118 env_file.write(environ) |
72 | 119 |
| 120 important_env_vars["PATH"] = ";".join(FixupPath64(path)) |
| 121 environ = FormatAsEnvironmentBlock(important_env_vars) |
| 122 with open('environment.x64', 'wb') as env_file: |
| 123 env_file.write(environ) |
OLD | NEW |