-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTargetModules.cmake
More file actions
88 lines (66 loc) · 2.85 KB
/
TargetModules.cmake
File metadata and controls
88 lines (66 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright © 2019 Damien Flament
# This file is part of CMake Modules.
#[=======================================================================[.rst:
TargetModules
-------------
This module allows to declare required modules using the
:command:`require_module` command and define modules required by a target using
the :command:`target_required_modules` command.
The implementation rely on the availability of the ``pkg-config`` tool.
#]=======================================================================]
include_guard(GLOBAL)
#[=======================================================================[.rst:
.. command:: require_module
Checks if a ``module`` is available using pkg-config. ::
require_module(<module>
[MINIMUM <version>])
When the ``MINIMUM`` argument is given, module will match only if its
version is from ``version`` or later.
#]=======================================================================]
function(require_module module)
find_package(PkgConfig REQUIRED)
cmake_parse_arguments(PARSE_ARGV 0 ""
""
"MINIMUM"
""
)
set(specification "${module}")
if (_MINIMUM)
string(APPEND specification ">=" ${_MINIMUM})
endif()
pkg_check_modules(_TargetModules_${module} REQUIRED ${specification})
endfunction()
#[=======================================================================[.rst:
.. command:: target_required_modules
Specify include directories, compile options and link flags needed to compile
and link the ``target`` using the specified modules. ::
target_required_modules(<target>
<INTERFACE|PUBLIC|PRIVATE> <module> [module...]
[<INTERFACE|PUBLIC|PRIVATE> <module> [module...] ...])
When specifying the ``module``, no version specification is allowed as it
needs to be done using :command:`require_module`.
#]=======================================================================]
function(target_required_modules target)
cmake_parse_arguments(PARSE_ARGV 0 ""
""
""
"INTERFACE;PUBLIC;PRIVATE"
)
if(NOT(_INTERFACE OR _PUBLIC OR _PRIVATE))
message(FATAL_ERROR "The required modules must be specified.")
endif()
foreach(scope INTERFACE PUBLIC PRIVATE)
foreach(module ${_${scope}})
if(NOT DEFINED _TargetModules_${module}_FOUND)
message(FATAL_ERROR "The module `${module}` has not been required.")
endif()
if(NOT _TargetModules_${module}_FOUND)
message(FATAL_ERROR "The module `${module}` has not been found.")
endif()
target_include_directories(${target} ${scope} ${_TargetModules_${module}_INCLUDE_DIRS})
target_compile_options(${target} ${scope} ${_TargetModules_${module}_CFLAGS})
target_link_libraries(${target} ${_TargetModules_${module}_LIBRARIES})
endforeach(module)
endforeach()
endfunction()