1# - Find bit-appropriate program files directories matching a given pattern
2#
3# Requires these CMake modules:
4#  CleanDirectoryList
5#  PrefixListGlob
6#
7# Original Author:
8# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
9# http://academic.cleardefinition.com
10# Iowa State University HCI Graduate Program/VRAC
11#
12# Copyright Iowa State University 2009-2010.
13# Distributed under the Boost Software License, Version 1.0.
14# (See accompanying file LICENSE_1_0.txt or copy at
15# http://www.boost.org/LICENSE_1_0.txt)
16
17include(PrefixListGlob)
18include(CleanDirectoryList)
19
20if(__program_files_glob)
21	return()
22endif()
23set(__program_files_glob YES)
24
25macro(_program_files_glob_var_prep)
26	# caution - ENV{ProgramFiles} on Win64 is adjusted to point to the arch
27	# of the running executable which, since CMake is 32-bit on Windows as
28	# I write this, will always be = $ENV{ProgramFiles(x86)}.
29	# Thus, we only use this environment variable if we are on a 32 machine
30
31	# 32-bit dir on win32, useless to us on win64
32	file(TO_CMAKE_PATH "$ENV{ProgramFiles}" _PROG_FILES)
33
34	# 32-bit dir: only set on win64
35	set(_PF86 "ProgramFiles(x86)")
36	file(TO_CMAKE_PATH "$ENV{${_PF86}}" _PROG_FILES_X86)
37
38	# 64-bit dir: only set on win64
39	file(TO_CMAKE_PATH "$ENV{ProgramW6432}" _PROG_FILES_W6432)
40endmacro()
41
42function(program_files_glob var pattern)
43	_program_files_glob_var_prep()
44	if(CMAKE_SIZEOF_VOID_P MATCHES "8")
45		# 64-bit build on win64
46		set(_PROGFILESDIRS "${_PROG_FILES_W6432}")
47	else()
48		if(_PROG_FILES_W6432)
49			# 32-bit build on win64
50			set(_PROGFILESDIRS "${_PROG_FILES_X86}")
51		else()
52			# 32-bit build on win32
53			set(_PROGFILESDIRS "${_PROG_FILES}")
54		endif()
55	endif()
56
57	prefix_list_glob(_prefixed "${pattern}" ${_PROGFILESDIRS})
58	clean_directory_list(_prefixed)
59	set(${var} ${_prefixed} PARENT_SCOPE)
60endfunction()
61
62function(program_files_fallback_glob var pattern)
63	_program_files_glob_var_prep()
64	if(CMAKE_SIZEOF_VOID_P MATCHES "8")
65		# 64-bit build on win64
66		# look in the "32 bit" (c:\program files (x86)\) directory as a
67		# fallback in case of weird/poorly written installers, like those
68		# that put both 64- and 32-bit libs in the same program files directory
69		set(_PROGFILESDIRS "${_PROG_FILES_W6432}" "${_PROG_FILES_X86}")
70	else()
71		if(_PROG_FILES_W6432)
72			# 32-bit build on win64
73			# look in the "64 bit" (c:\program files\) directory as a fallback
74			# in case of old/weird/poorly written installers
75			set(_PROGFILESDIRS "${_PROG_FILES_X86}" "${_PROG_FILES_W6432}")
76		else()
77			# 32-bit build on win32
78			set(_PROGFILESDIRS "${_PROG_FILES}")
79		endif()
80	endif()
81
82	prefix_list_glob(_prefixed "${pattern}" ${_PROGFILESDIRS})
83	clean_directory_list(_prefixed)
84	set(${var} ${_prefixed} PARENT_SCOPE)
85endfunction()
86