1# - Do a version-dependent check and auto-include backported modules dirs
2#
3# Name your module directories cmake-*-modules where * is the full
4# (maj.min.patch) version number that they came from.  You can use
5# subdirectories within those directories, if you like - all directories
6# inside of a cmake-*-modules dir for a newer version of CMake that what
7# we're running, that contain one or more .cmake files, will be appended
8# to the CMAKE_MODULE_PATH.
9#
10# When backporting modules, be sure to test them and follow copyright
11# instructions (usually updating copyright notices)
12#
13# Requires these CMake modules:
14#  CleanDirectoryList
15#
16# Original Author:
17# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
18# http://academic.cleardefinition.com
19# Iowa State University HCI Graduate Program/VRAC
20#
21# Copyright Iowa State University 2009-2010.
22# Distributed under the Boost Software License, Version 1.0.
23# (See accompanying file LICENSE_1_0.txt or copy at
24# http://www.boost.org/LICENSE_1_0.txt)
25
26if(NOT CMAKE_VERSION)	# defined in >=2.6.3
27	set(_cmver
28		"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")
29else()
30	set(_cmver "${CMAKE_VERSION}")
31endif()
32
33include(CleanDirectoryList)
34
35# No debugging output please
36set(USE_BACKPORTED_MODULES_VERBOSE NO)
37
38get_filename_component(_moddir ${CMAKE_CURRENT_LIST_FILE} PATH)
39file(GLOB _globbed "${_moddir}/cmake-*-modules")
40
41if(USE_BACKPORTED_MODULES_VERBOSE)
42	message(STATUS
43		"UseBackportedModules: Detected use of CMake version ${_cmver}")
44	message(STATUS "Checking these base directories: ${_globbed}")
45endif()
46
47foreach(_dir ${_globbed})
48	string(REGEX
49		MATCH
50		"cmake-[0-9].[0-9].[0-9]-modules"
51		_dirname
52		"${_dir}")
53	string(REGEX
54		REPLACE
55		"cmake-([0-9].[0-9].[0-9])-modules"
56		"\\1"
57		_ver
58		"${_dirname}")
59	string(REGEX
60		REPLACE
61		"cmake-([0-9]).([0-9]).([0-9])-modules"
62		"\\1_\\2_\\3"
63		_ver_clean
64		"${_dirname}")
65
66	if(USE_BACKPORTED_MODULES_VERBOSE)
67		message(STATUS "${_dir}: ${_ver} ${_ver_clean}")
68	endif()
69
70	if("${_cmver}" VERSION_LESS "${_ver}")
71		list(APPEND _upgradever "${_ver_clean}")
72		file(GLOB_RECURSE _modules "${_dir}/*.cmake")
73
74		foreach(_mod ${_modules})
75			get_filename_component(_path "${_mod}" PATH)
76			list(APPEND _paths_${_ver_clean} "${_path}")
77		endforeach()
78
79	endif()
80endforeach()
81
82
83# Autoinclude files from oldest version to newest version
84if(_upgradever)
85	set(_save_cmake_module_path ${CMAKE_MODULE_PATH})
86	list(REMOVE_DUPLICATES _upgradever)
87	list(SORT _upgradever)
88	foreach(_ver_clean ${_upgradever})
89		clean_directory_list(_paths_${_ver_clean})
90		foreach(_dir ${_paths_${_ver_clean}})
91			set(CMAKE_MODULE_PATH ${_dir} ${_save_cmake_module_path})
92			include("${_dir}/autoinclude.cmake" OPTIONAL RESULT_VARIABLE _result)
93			if(USE_BACKPORTED_MODULES_VERBOSE)
94				message(STATUS "${_dir} - Autoincluded: ${_result}")
95			endif()
96		endforeach()
97	endforeach()
98	set(CMAKE_MODULE_PATH ${_save_cmake_module_path})
99endif()
100
101# Add the module path from newest version to oldest version
102set(_added_module_path)
103if(_upgradever)
104	list(REVERSE _upgradever)
105	foreach(_ver_clean ${_upgradever})
106		list(APPEND _added_module_path ${_paths_${_ver_clean}})
107	endforeach()
108endif()
109
110list(APPEND CMAKE_MODULE_PATH ${_added_module_path})
111
112if(USE_BACKPORTED_MODULES_VERBOSE)
113	message(STATUS "New module path: ${CMAKE_MODULE_PATH}")
114endif()
115