1# - Utility function to return a human-useful-only string ID'ing the compiler
2#
3#  get_compiler_info_string(<resultvar>)
4#
5# and some helper functions:
6#  get_gcc_version(<resultvar>)
7#  get_vs_short_version_string(<generator> <resultvar>)
8#
9# You might consider using it when setting up CTest options, for example:
10#  include(GetCompilerInfoString)
11#  get_compiler_info_string(COMPILERID)
12#  set(CTEST_BUILD_NAME "${CMAKE_SYSTEM}-${CMAKE_SYSTEM_PROCESSOR}-${COMPILERID}")
13#
14# Requires these CMake modules:
15#  no additional modules required
16#
17# Original Author:
18# 2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
19# http://academic.cleardefinition.com
20# Iowa State University HCI Graduate Program/VRAC
21#
22# Copyright Iowa State University 2009-2010.
23# Distributed under the Boost Software License, Version 1.0.
24# (See accompanying file LICENSE_1_0.txt or copy at
25# http://www.boost.org/LICENSE_1_0.txt)
26#
27# Some functions based on cmake-2.8.0 modules FindBoost.cmake and CTest.cmake
28#=============================================================================
29# Copyright 2006-2009 Kitware, Inc.
30# Copyright 2006-2008 Andreas Schneider <mail@cynapses.org>
31# Copyright 2007      Wengo
32# Copyright 2007      Mike Jackson
33# Copyright 2008      Andreas Pakulat <apaku@gmx.de>
34# Copyright 2008-2009 Philip Lowman <philip@yhbt.com>
35# Copyright 2010      Iowa State University (Ryan Pavlik <abiryan@ryand.net>)
36#
37# Distributed under the OSI-approved BSD License (the "License");
38# see accompanying file Copyright.txt for details.
39#
40# This software is distributed WITHOUT ANY WARRANTY; without even the
41# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
42# See the License for more information.
43#=============================================================================
44# CMake - Cross Platform Makefile Generator
45# Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
46# All rights reserved.
47#
48# Redistribution and use in source and binary forms, with or without
49# modification, are permitted provided that the following conditions
50# are met:
51#
52# * Redistributions of source code must retain the above copyright
53#   notice, this list of conditions and the following disclaimer.
54#
55# * Redistributions in binary form must reproduce the above copyright
56#   notice, this list of conditions and the following disclaimer in the
57#   documentation and/or other materials provided with the distribution.
58#
59# * Neither the names of Kitware, Inc., the Insight Software Consortium,
60#   nor the names of their contributors may be used to endorse or promote
61#   products derived from this software without specific prior written
62#   permission.
63#
64# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
65# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
66# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
67# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
68# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
69# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
70# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
71# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
72# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
73# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
74# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75#=============================================================================
76
77if(__get_compiler_info_string)
78	return()
79endif()
80set(__get_compiler_info_string YES)
81
82
83function(get_compiler_info_string _var)
84	set(_out)
85
86	if(CTEST_CMAKE_GENERATOR AND NOT CMAKE_GENERATOR)
87		# We're running in CTest - use that generator.
88		set(CMAKE_GENERATOR ${CTEST_CMAKE_GENERATOR})
89	endif()
90
91	if(NOT CMAKE_CXX_COMPILER)
92		# Also for use in CTest scripts
93		include(CMakeDetermineCXXCompiler)
94	endif()
95
96	if(MSVC)
97		# Parse version for Visual Studio
98		get_vs_short_version_string("${CMAKE_GENERATOR}" _verstring)
99		if(${CMAKE_GENERATOR} MATCHES "Win64")
100			set(_verstring "${_verstring}win64")
101		endif()
102
103	elseif(CMAKE_COMPILER_IS_GNUCXX)
104		# Parse version for GCC
105		get_gcc_version(_gccver)
106		set(_verstring "gcc${_gccver}")
107
108	else()
109		# Some other compiler we don't handle in more detail yet.
110		string(REGEX REPLACE " " "_" _verstring "${CMAKE_GENERATOR}")
111		set(_verstring "${CMAKE_CXX_COMPILER_ID}:generator:${_verstring}")
112	endif()
113
114	# Return _verstring
115	set(${_var} "${_verstring}" PARENT_SCOPE)
116endfunction()
117
118## Based on a function in FindBoost.cmake from CMake 2.8.0
119#-------------------------------------------------------------------------------
120#
121# Runs compiler with "-dumpversion" and parses major/minor
122# version with a regex.
123#
124function(get_gcc_version _var)
125	exec_program(${CMAKE_CXX_COMPILER}
126		ARGS
127		${CMAKE_CXX_COMPILER_ARG1}
128		-dumpversion
129		OUTPUT_VARIABLE
130		_compilerinfo_COMPILER_VERSION)
131
132	string(REGEX
133		MATCH
134		"([.0-9]+)"
135		"\\1"
136		_compilerinfo_COMPILER_VERSION
137		"${_compilerinfo_COMPILER_VERSION}")
138
139	set(${_var} ${_compilerinfo_COMPILER_VERSION} PARENT_SCOPE)
140endfunction()
141
142## Based on a function in CTest.cmake from CMake 2.8.0
143#-------------------------------------------------------------------------------
144#
145# function to turn generator name into a version string
146# like vs7 vs71 vs8 vs9
147#
148function(get_vs_short_version_string _generator _var)
149	set(_ver_string)
150	if("${_generator}" MATCHES "Visual Studio")
151		string(REGEX
152			REPLACE
153			"Visual Studio ([0-9][0-9]?)($|.*)"
154			"\\1"
155			_vsver
156			"${_generator}")
157		if("${_generator}" MATCHES "Visual Studio 7 .NET 2003")
158			# handle the weird one
159			set(_ver_string "vs71")
160		else()
161			set(_ver_string "vs${_vsver}")
162		endif()
163	elseif(MSVC)
164		if(MSVC71)
165			set(_ver_string "vs71")
166		else()
167			foreach(_ver 6 7 8 9 10)
168				if(MSVC${_ver}0)
169					set(_ver_string "vs${_ver}")
170					break()
171				endif()
172			endforeach()
173		endif()
174	endif()
175
176	if(_ver_string)
177		set(${_var} ${_ver_string} PARENT_SCOPE)
178	endif()
179endfunction()
180