1# - Add flags to compile with profiling support - currently only for GCC
2#
3#  enable_profiling(<targetname>)
4#  globally_enable_profiling() - to modify CMAKE_CXX_FLAGS, etc
5#    to change for all targets declared after the command, instead of per-command
6#
7#
8# Original Author:
9# 2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
10# http://academic.cleardefinition.com
11# Iowa State University HCI Graduate Program/VRAC
12#
13# Copyright Iowa State University 2009-2010.
14# Distributed under the Boost Software License, Version 1.0.
15# (See accompanying file LICENSE_1_0.txt or copy at
16# http://www.boost.org/LICENSE_1_0.txt)
17
18if(__enable_profiling)
19	return()
20endif()
21set(__enable_profiling YES)
22
23macro(_enable_profiling_flags)
24	set(_flags)
25	if(MSVC)
26		# TODO: what kind of flags are needed to profile on MSVC?
27		#set(_flags /W4)
28	elseif(CMAKE_COMPILER_IS_GNUCXX)
29		set(_flags "-p")
30	endif()
31endmacro()
32
33function(enable_profiling _target)
34	_enable_profiling_flags()
35	get_target_property(_origflags ${_target} COMPILE_FLAGS)
36	if(_origflags)
37		set_property(TARGET
38			${_target}
39			PROPERTY
40			COMPILE_FLAGS
41			"${_flags} ${_origflags}")
42	else()
43		set_property(TARGET
44			${_target}
45			PROPERTY
46			COMPILE_FLAGS
47			"${_flags}")
48	endif()
49
50endfunction()
51
52function(globally_enable_profiling)
53	_enable_profiling_flags()
54	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flags}" PARENT_SCOPE)
55	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flags}" PARENT_SCOPE)
56endfunction()
57