1# PkgConfigEx.cmake
2#
3# Extends CMake's PkgConfig module with commands:
4#
5# pkg_check_modules_for_option(_option_name _option_description _prefix _module0)
6#
7#    which calls `pkg_check_modules(_prefix _module0)` and if <_prefix>_FOUND is False,
8#    then prints an error with a hint to disaable the _option_name if needed.
9#
10# pkg_check_exists(_output_name _pkg)
11#
12#    calls pkg-config --exists for _pkg and stores the result to _output_name.
13#
14# pkg_check_at_least_version(_output_name _pkg _version)
15#
16#    calls pkg-config --at-least-version=_version for _pkg and stores the result to _output_name.
17#
18# pkg_check_exact_version(_output_name _pkg _version)
19#
20#    calls pkg-config --exact-version=_version for _pkg and stores the result to _output_name.
21#
22# pkg_check_variable(_output_name _pkg _name)
23#
24#    gets a variable named _name from package _pkg and stores the result into _output_name
25
26find_package(PkgConfig REQUIRED)
27
28macro(pkg_check_modules_for_option _option_name _option_description _prefix _module0)
29	pkg_check_modules(${_prefix} ${_module0} ${ARGN})
30
31	if(NOT ${_prefix}_FOUND)
32		message(FATAL_ERROR "Necessary libraries not found or not enough version. If you want to disable ${_option_description}, please use -D${_option_name}=OFF argument to cmake command.")
33	endif(NOT ${_prefix}_FOUND)
34endmacro()
35
36macro(pkg_check_exists _output_name _pkg)
37	execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --exists ${_pkg}
38			RESULT_VARIABLE ${_output_name})
39
40	# Negate the result, because 0 means 'found'
41	if(${_output_name})
42		set(${_output_name} OFF)
43	else(${_output_name})
44		set(${_output_name} ON)
45	endif(${_output_name})
46endmacro()
47
48macro(pkg_check_at_least_version _output_name _pkg _version)
49	execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --atleast-version=${_version} ${_pkg}
50			RESULT_VARIABLE ${_output_name})
51
52	# Negate the result, because 0 means 'found'
53	if(${_output_name})
54		set(${_output_name} OFF)
55	else(${_output_name})
56		set(${_output_name} ON)
57	endif(${_output_name})
58endmacro()
59
60macro(pkg_check_exact_version _output_name _pkg _version)
61	execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --exact-version=${_version} ${_pkg}
62			RESULT_VARIABLE ${_output_name})
63
64	# Negate the result, because 0 means 'found'
65	if(${_output_name})
66		set(${_output_name} OFF)
67	else(${_output_name})
68		set(${_output_name} ON)
69	endif(${_output_name})
70endmacro()
71
72function(pkg_check_variable _output_name _pkg _name)
73    execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=${_name} ${_pkg}
74                    OUTPUT_VARIABLE _pkg_result
75                    OUTPUT_STRIP_TRAILING_WHITESPACE)
76
77    set("${_output_name}" "${_pkg_result}" CACHE STRING "pkg-config variable ${_name} of ${_pkg}")
78endfunction()
79
80macro(add_pkgconfig_file _input _output)
81	configure_file(
82		${CMAKE_CURRENT_SOURCE_DIR}/${_input}
83		${CMAKE_CURRENT_BINARY_DIR}/${_output}
84		@ONLY
85	)
86
87	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${_output}
88		DESTINATION ${LIB_INSTALL_DIR}/pkgconfig
89	)
90endmacro()
91