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# Copyright (c) 2016 Google Inc.
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and/or associated documentation files (the "Materials"), to
10# deal in the Materials without restriction, including without limitation the
11# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12# sell copies of the Materials, and to permit persons to whom the Materials
13# are furnished to do so, subject to the following conditions:
14#
15# The above copyright notice(s) and this permission notice shall be included
16# in all copies or substantial portions of the Materials.
17#
18# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21#
22# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
25# USE OR OTHER DEALINGS IN THE MATERIALS
26#
27# Author: Mark Young <marky@lunarg.com>
28
29import sys
30import os
31
32# Following function code snippet was found on StackOverflow (with a change to lower
33# camel-case on the variable names):
34#   http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
35def find_executable(program):
36    def is_exe(fPath):
37        return os.path.isfile(fPath) and os.access(fPath, os.X_OK)
38
39    fPath, fName = os.path.split(program)
40    if fPath:
41        if is_exe(program):
42            return program
43    else:
44        for path in os.environ["PATH"].split(os.pathsep):
45            path = path.strip('"')
46            exe_file = os.path.join(path, program)
47            if is_exe(exe_file):
48                return exe_file
49
50    return None
51
52def determine_year(version):
53	if version == 8:
54		return 2005
55	elif version == 9:
56		return 2008
57	elif version == 10:
58		return 2010
59	elif version == 11:
60		return 2012
61	elif version == 12:
62		return 2013
63	elif version == 14:
64		return 2015
65	else:
66		return 0000
67
68# Determine if msbuild is in the path, then call it to determine the version and parse
69# it into a format we can use, which is "<version_num> <version_year>".
70if __name__ == '__main__':
71	exeName     = 'msbuild.exe'
72	versionCall = exeName + ' /ver'
73
74	# Determine if the executable exists in the path, this is critical.
75	#
76	foundExeName = find_executable(exeName)
77
78	# If not found, return an invalid number but in the appropriate format so it will
79	# fail if the program above tries to use it.
80	if foundExeName == None:
81		print('00 0000')
82		print('Executable ' + exeName + ' not found in PATH!')
83	else:
84		sysCallOut = os.popen(versionCall).read()
85
86		version = None
87
88		# Split around any spaces first
89		spaceList  = sysCallOut.split(' ')
90		for spaceString in spaceList:
91
92			# If we've already found it, bail.
93			if version != None:
94				break
95
96			# Now split around line feeds
97			lineList = spaceString.split('\n')
98			for curLine in lineList:
99
100				# If we've already found it, bail.
101				if version != None:
102					break
103
104				# We only want to continue if there's a period in the list
105				if '.' not in curLine:
106					continue
107
108				# Get the first element and determine if it is a number, if so, we've
109				# got our number.
110				splitAroundPeriod = curLine.split('.')
111				if splitAroundPeriod[0].isdigit():
112					version = int (splitAroundPeriod[0])
113					break
114
115		# Failsafe to return a number in the proper format, but one that will fail.
116		if version == None:
117			version = 00
118
119		# Determine the year associated with that version
120		year = determine_year(version)
121
122		# Output the string we need for Cmake to properly build for this version
123		print(str(version) + ' ' + str(year))
124