1#.rst:
2# GetMSVCVersion
3# --------------
4#
5#
6#
7# get_msvc_major_version(<var>)
8# get_msvc_minor_version(<var>)
9# get_msvc_combined_major_minor_version(<var>)
10# get_msvc_major_minor_version(<major_var> <minor_var>)
11# get_msvc_unique_decimal_version(<var>)
12#
13# This family of functions is designed to be used to convert
14# MSVC_VERSION from the compiler version number to the Visual Studio
15# decimal version number (2012 is 11.0, 2015 is 14.0). All take a name
16# of a variable in <var> to return their results in.
17#
18# Consider Visual Studio 2013, which reports 1800 in MSVC_VERSION (and
19# the _MSC_VER preprocessor macro). It is also known as VS 12 or 12.0.
20# (Minor versions are rarely used, except in the case of 7.1 aka
21# VS.NET 2003) The functions would return this output for 2013:
22#
23# get_msvc_major_version: 12
24# get_msvc_minor_version: 0
25# get_msvc_combined_major_minor_version: 120
26# get_msvc_major_minor_version: 12 in <major_var>, 0 in <minor_var>
27# get_msvc_unique_decimal_version: 12  (this returns with a decimal and
28# minor when needed to be precise, e.g. 7.1)
29#
30# The variable is not modified if not building with MSVC.
31
32#=============================================================================
33# Copyright 2015 Ryan Pavlik <ryan.pavlik@gmail.com>
34#
35# Distributed under the OSI-approved BSD License (the "License");
36# see below.
37#
38# This software is distributed WITHOUT ANY WARRANTY; without even the
39# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
40# See the License for more information.
41#=============================================================================
42#
43# Redistribution and use in source and binary forms, with or without
44# modification, are permitted provided that the following conditions
45# are met:
46#
47# * Redistributions of source code must retain the above copyright
48#   notice, this list of conditions and the following disclaimer.
49#
50# * Redistributions in binary form must reproduce the above copyright
51#   notice, this list of conditions and the following disclaimer in the
52#   documentation and/or other materials provided with the distribution.
53#
54# * Neither the names of Kitware, Inc., the Insight Software Consortium,
55#   nor the names of their contributors may be used to endorse or promote
56#   products derived from this software without specific prior written
57#   permission.
58#
59# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
60# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
61# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
62# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
63# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
64# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
65# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70#=============================================================================
71
72# This function serves as the main implementation, with the others just
73# tweaking the result.
74function(get_msvc_combined_major_minor_version _var)
75    if(NOT MSVC)
76        return()
77    endif()
78    math(EXPR _vs_ver "${MSVC_VERSION} / 10 - 60")
79
80    # VS 2015 changed the pattern because they skipped VS 13
81    if(_vs_ver GREATER 120)
82        math(EXPR _vs_ver "${_vs_ver} + 10")
83    endif()
84    set(${_var} ${_vs_ver} PARENT_SCOPE)
85endfunction()
86
87# This function is also used as backend to some implementation, though
88# its contents are simpler, no real business logic to speak of.
89function(get_msvc_major_minor_version _major_var _minor_var)
90    if(NOT MSVC)
91        return()
92    endif()
93    get_msvc_combined_major_minor_version(_vs_ver)
94
95    math(EXPR _vs_minor "${_vs_ver} % 10")
96    math(EXPR _vs_major "(${_vs_ver} - ${_vs_minor}) / 10")
97    set(${_major_var} ${_vs_major} PARENT_SCOPE)
98    set(${_minor_var} ${_vs_minor} PARENT_SCOPE)
99endfunction()
100
101function(get_msvc_major_version _var)
102    if(NOT MSVC)
103        return()
104    endif()
105    get_msvc_major_minor_version(_vs_ver _dummy)
106    set(${_var} ${_vs_ver} PARENT_SCOPE)
107endfunction()
108
109function(get_msvc_minor_version _var)
110    if(NOT MSVC)
111        return()
112    endif()
113    get_msvc_major_minor_version(_dummy _vs_ver)
114    set(${_var} ${_vs_ver} PARENT_SCOPE)
115endfunction()
116
117function(get_msvc_unique_decimal_version _var)
118    if(NOT MSVC)
119        return()
120    endif()
121    get_msvc_major_minor_version(_vs_major _vs_minor)
122    set(_vs_ver ${_vs_major})
123    if(_vs_minor GREATER 0)
124        set(_vs_ver "${_vs_ver}.${_vs_minor}")
125    endif()
126    set(${_var} ${_vs_ver} PARENT_SCOPE)
127endfunction()
128