aboutsummaryrefslogtreecommitdiff
path: root/gn/standalone/toolchain/win_find_msvc.py
blob: 0badd1be46f001f65b0675cd97a8af06e3b11656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Finds and prints MSVC and Windows SDK paths.

It outpus:
Line 1: the base path of the Windows SDK.
Line 2: the most recent version of the Windows SDK.
Line 3: the path of the most recent MSVC.

Example:
C:\Program Files (x86)\Windows Kits\10
10.0.19041.0
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333
"""

import os
import subprocess
import sys


def ver_to_tuple(ver_str):
  """Turns '10.1.2' into [10,1,2] so it can be compared using > """
  parts = [int(x) for x in ver_str.split('.')]
  assert (len(parts) == 4)
  return parts


def find_max_subdir(base_dir, filter=lambda x: True):
  """Finds the max subdirectory in base_dir by comparing semantic versions."""
  max_ver = None
  for ver in os.listdir(base_dir) if os.path.exists(base_dir) else []:
    cur = os.path.join(base_dir, ver)
    if not filter(cur):
      continue
    if max_ver is None or ver_to_tuple(ver) > ver_to_tuple(max_ver):
      max_ver = ver
  return max_ver


def main():
  out = [
      '',
      '',
      '',
  ]
  winsdk_base = 'C:\\Program Files (x86)\\Windows Kits\\10'
  if os.path.exists(winsdk_base):
    out[0] = winsdk_base
    lib_base = winsdk_base + '\\Lib'
    filt = lambda x: os.path.exists(os.path.join(x, 'ucrt', 'x64', 'ucrt.lib'))
    out[1] = find_max_subdir(lib_base, filt)

  for version in ['BuildTools', 'Community', 'Professional']:
    msvc_base = ('C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\'
                 '{}\\VC\\Tools\\MSVC').format(version)
    if os.path.exists(msvc_base):
      filt = lambda x: os.path.exists(
          os.path.join(x, 'lib', 'x64', 'libcmt.lib'))
      max_msvc = find_max_subdir(msvc_base, filt)
      if max_msvc is not None:
        out[2] = os.path.join(msvc_base, max_msvc)
      break

  # Don't error in case of failure, GN scripts are supposed to deal with
  # failures and allow the user to override the dirs.

  print('\n'.join(out))
  return 0


if __name__ == '__main__':
  sys.exit(main())