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
23
24# Following function code snippet was found on StackOverflow (with a change to lower
25# camel-case on the variable names):
26#   http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
27def find_executable(program):
28    def is_exe(fPath):
29        return os.path.isfile(fPath) and os.access(fPath, os.X_OK)
30
31    fPath, fName = os.path.split(program)
32    if fPath:
33        if is_exe(program):
34            return program
35    else:
36        for path in os.environ["PATH"].split(os.pathsep):
37            path = path.strip('"')
38            exe_file = os.path.join(path, program)
39            if is_exe(exe_file):
40                return exe_file
41
42    return None
43
44def determine_year(version):
45    if version == 8:
46        return 2005
47    elif version == 9:
48        return 2008
49    elif version == 10:
50        return 2010
51    elif version == 11:
52        return 2012
53    elif version == 12:
54        return 2013
55    elif version == 14:
56        return 2015
57    else:
58        return 0000
59
60# Determine if msbuild is in the path, then call it to determine the version and parse
61# it into a format we can use, which is "<version_num> <version_year>".
62if __name__ == '__main__':
63    exeName     = 'msbuild.exe'
64    versionCall = exeName + ' /ver'
65
66    # Determine if the executable exists in the path, this is critical.
67    #
68    foundExeName = find_executable(exeName)
69
70    # If not found, return an invalid number but in the appropriate format so it will
71    # fail if the program above tries to use it.
72    if foundExeName == None:
73        print('00 0000')
74        print('Executable ' + exeName + ' not found in PATH!')
75    else:
76        sysCallOut = os.popen(versionCall).read()
77
78        version = None
79
80        # Split around any spaces first
81        spaceList  = sysCallOut.split(' ')
82        for spaceString in spaceList:
83
84            # If we've already found it, bail.
85            if version != None:
86                break
87
88            # Now split around line feeds
89            lineList = spaceString.split('\n')
90            for curLine in lineList:
91
92                # If we've already found it, bail.
93                if version != None:
94                    break
95
96                # We only want to continue if there's a period in the list
97                if '.' not in curLine:
98                    continue
99
100                # Get the first element and determine if it is a number, if so, we've
101                # got our number.
102                splitAroundPeriod = curLine.split('.')
103                if splitAroundPeriod[0].isdigit():
104                    version = int (splitAroundPeriod[0])
105                    break
106
107        # Failsafe to return a number in the proper format, but one that will fail.
108        if version == None:
109            version = 00
110
111        # Determine the year associated with that version
112        year = determine_year(version)
113
114        # Output the string we need for Cmake to properly build for this version
115        print(str(version) + ' ' + str(year))
116