1#!/usr/bin/env python3
2#
3# Copyright (c) 2016 The Khronos Group Inc.
4# Copyright (c) 2016 Valve Corporation
5# Copyright (c) 2016 LunarG, Inc.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#     http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# Author: Mark Young <marky@lunarg.com>
20
21import sys
22import os
23import subprocess
24
25# Following function code snippet was found on StackOverflow (with a change to lower
26# camel-case on the variable names):
27#   http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
28def find_executable(program):
29    def is_exe(fPath):
30        return os.path.isfile(fPath) and os.access(fPath, os.X_OK)
31
32    fPath, fName = os.path.split(program)
33    if fPath:
34        if is_exe(program):
35            return program
36    else:
37        for path in os.environ["PATH"].split(os.pathsep):
38            path = path.strip('"')
39            exe_file = os.path.join(path, program)
40            if is_exe(exe_file):
41                return exe_file
42
43    return None
44
45def determine_year(version):
46    if version == 8:
47        return 2005
48    elif version == 9:
49        return 2008
50    elif version == 10:
51        return 2010
52    elif version == 11:
53        return 2012
54    elif version == 12:
55        return 2013
56    elif version == 14:
57        return 2015
58    elif version == 15:
59        return 2017
60    else:
61        return 0000
62
63# Determine if msbuild is in the path, then call it to determine the version and parse
64# it into a format we can use, which is "<version_num> <version_year>".
65if __name__ == '__main__':
66    exeName     = 'msbuild.exe'
67    arguments   = '/ver'
68
69    # Determine if the executable exists in the path, this is critical.
70    #
71    foundExeName = find_executable(exeName)
72
73    # If not found, return an invalid number but in the appropriate format so it will
74    # fail if the program above tries to use it.
75    if foundExeName == None:
76        print('00 0000')
77        print('Executable ' + exeName + ' not found in PATH!')
78    else:
79        proc = subprocess.Popen([exeName, arguments], stdout=subprocess.PIPE)
80        sysCallOut = proc.stdout.readline().decode('iso-8859-1').rstrip()
81
82        version = None
83
84        # Split around any spaces first
85        spaceList  = sysCallOut.split(' ')
86        for spaceString in spaceList:
87
88            # If we've already found it, bail.
89            if version != None:
90                break
91
92            # Now split around line feeds
93            lineList = spaceString.split('\n')
94            for curLine in lineList:
95
96                # If we've already found it, bail.
97                if version != None:
98                    break
99
100                # We only want to continue if there's a period in the list
101                if '.' not in curLine:
102                    continue
103
104                # Get the first element and determine if it is a number, if so, we've
105                # got our number.
106                splitAroundPeriod = curLine.split('.')
107                if splitAroundPeriod[0].isdigit():
108                    version = int (splitAroundPeriod[0])
109                    break
110
111        # Failsafe to return a number in the proper format, but one that will fail.
112        if version == None:
113            version = 00
114
115        # Determine the year associated with that version
116        year = determine_year(version)
117
118        # Output the string we need for Cmake to properly build for this version
119        print(str(version) + ' ' + str(year))
120