1# - try to find Flagpoll application, and offer package-finding services
2#  FLAGPOLL, the executable: if not defined, do not try to use Flagpoll.
3#
4# Useful configuration variables you might want to add to your cache:
5#  FLAGPOLL_ROOT_DIR  - A directory prefix to search for the app
6#                       (a path that contains bin/ as a subdirectory)
7#
8# VR Juggler requires this package, so this Find script takes that into
9# account when determining where to search for the desired files.
10# The VJ_BASE_DIR environment variable is searched (preferentially)
11# when searching for this package, so most sane VR Juggler build environments
12# should "just work."  Note that you need to manually re-run CMake if you
13# change this environment variable, because it cannot auto-detect this change
14# and trigger an automatic re-run.
15#
16# You can use Flagpoll to provide directories to use as HINTS for find_*
17# These are the provided macros:
18#  flagpoll_get_include_dirs
19#  flagpoll_get_library_dirs
20#  flagpoll_get_library_names
21#  flagpoll_get_extra_libs
22# All take the name of the desired package, optionally NO_DEPS to pass --no-deps
23# to Flagpoll, and return yourpkgname_FLAGPOLL_INCLUDE_DIRS(etc. for the other
24# macros).
25#
26# Example usage:
27# 	flagpoll_get_include_dirs(vpr NO_DEPS)
28# 	find_path(VPR20_INCLUDE_DIRS vpr/vpr.h
29#   	  HINTS  ${vpr_FLAGPOLL_INCLUDE_DIRS})
30#
31# Original Author:
32# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
33# http://academic.cleardefinition.com
34# Iowa State University HCI Graduate Program/VRAC
35#
36# Copyright Iowa State University 2009-2010.
37# Distributed under the Boost Software License, Version 1.0.
38# (See accompanying file LICENSE_1_0.txt or copy at
39# http://www.boost.org/LICENSE_1_0.txt)
40
41
42###
43# Flagpoll detection
44###
45set(Flagpoll_FIND_QUIETLY true)
46find_program(FLAGPOLL
47	NAMES
48	flagpoll
49	flagpoll.exe
50	PATHS
51	"${FLAGPOLL_ROOT_DIR}"
52	"${VRJUGGLER22_ROOT_DIR}"
53	PATH_SUFFIXES
54	bin)
55
56# handle the QUIETLY and REQUIRED arguments and set xxx_FOUND to TRUE if
57# all listed variables are TRUE
58include(FindPackageHandleStandardArgs)
59find_package_handle_standard_args(Flagpoll DEFAULT_MSG FLAGPOLL)
60
61mark_as_advanced(FLAGPOLL)
62
63###
64# Macro for internal use - shared workings between all the public macros below.
65###
66macro(_flagpoll_get_results _package _arg _flag _output)
67	if(FLAGPOLL)
68
69		# If the CMakeLists that called the flagpoll macro passed NO_DEPS,
70		# we won't return the results for dependencies
71		if("${ARGN}" MATCHES "NO_DEPS")
72			set(_FLAGPOLL_NODEP "--no-deps")
73		else()
74			set(_FLAGPOLL_NODEP "")
75		endif()
76
77		# Run flagpoll
78		execute_process(COMMAND
79			${FLAGPOLL}
80			${_package}
81			${_arg}
82			${_FLAGPOLL_NODEP}
83			OUTPUT_VARIABLE
84			_FLAGPOLL_OUTPUT
85			ERROR_QUIET
86			OUTPUT_STRIP_TRAILING_WHITESPACE)
87
88		if(_FLAGPOLL_OUTPUT)
89			# Remove -I and /I(or equivalent for other flags
90			string(REGEX
91				REPLACE
92				"[-/]${_flag}"
93				""
94				_FLAGPOLL_OUTPUT
95				${_FLAGPOLL_OUTPUT})
96
97			# Remove extra spaces
98			string(REGEX REPLACE " +" " " _FLAGPOLL_OUTPUT ${_FLAGPOLL_OUTPUT})
99
100			# Make a CMake list, standardize paths, and append only what we want to our final list
101			separate_arguments(_FLAGPOLL_OUTPUT)
102			foreach(_RESULT ${_FLAGPOLL_OUTPUT})
103				string(REGEX MATCH "^-" _BAD ${_RESULT})
104				if(_RESULT AND NOT _BAD)
105					file(TO_CMAKE_PATH "${_RESULT}" _RESULT_CLEAN)
106					list(APPEND ${_output} ${_RESULT_CLEAN})
107				endif()
108			endforeach()
109		endif()
110
111	endif()
112endmacro()
113
114###
115# "Public" macros - to use flagpoll to give you HINTS directories when finding things
116###
117macro(flagpoll_get_include_dirs _package)
118	# Passing ARGN along so if they specified NO_DEPS we actually do it.
119	_flagpoll_get_results(${_package}
120		"--cflags-only-I"
121		I
122		${_package}_FLAGPOLL_INCLUDE_DIRS
123		${ARGN})
124endmacro()
125
126macro(flagpoll_get_library_dirs _package)
127	# Passing ARGN along so if they specified NO_DEPS we actually do it.
128	_flagpoll_get_results(${_package}
129		"--libs-only-L"
130		L
131		${_package}_FLAGPOLL_LIBRARY_DIRS
132		${ARGN})
133endmacro()
134
135macro(flagpoll_get_library_names _package)
136	# Passing ARGN along so if they specified NO_DEPS we actually do it.
137	_flagpoll_get_results(${_package}
138		"--libs-only-l"
139		l
140		${_package}_FLAGPOLL_LIBRARY_NAMES
141		${ARGN})
142endmacro()
143
144macro(flagpoll_get_extra_libs _package)
145	# Passing ARGN along so if they specified NO_DEPS we actually do it.
146	_flagpoll_get_results(${_package}
147		"--get-extra-libs"
148		l
149		${_package}_FLAGPOLL_EXTRA_LIBS
150		${ARGN})
151endmacro()
152