forked from conan-io/conan
/
target_configuration.py
213 lines (165 loc) · 12 KB
/
target_configuration.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import textwrap
from conan.tools.cmake.cmakedeps.templates import CMakeDepsFileTemplate
"""
FooTarget-release.cmake
"""
class TargetConfigurationTemplate(CMakeDepsFileTemplate):
@property
def filename(self):
name = "" if not self.generating_module else "module-"
name += "{}-Target-{}.cmake".format(self.file_name, self.cmakedeps.configuration.lower())
return name
@property
def context(self):
deps_targets_names = self.get_deps_targets_names() \
if not self.conanfile.is_build_context else []
components_targets_names = self.get_declared_components_targets_names()
components_names = [(components_target_name.replace("::", "_"), components_target_name)
for components_target_name in components_targets_names]
is_win = self.conanfile.settings.get_safe("os") == "Windows"
auto_link = self.conanfile.cpp_info.get_property("cmake_set_interface_link_directories")
return {"pkg_name": self.pkg_name,
"root_target_name": self.root_target_name,
"config_suffix": self.config_suffix,
"config": self.configuration.upper(),
"deps_targets_names": ";".join(deps_targets_names),
"components_names": components_names,
"configuration": self.cmakedeps.configuration,
"set_interface_link_directories": auto_link and is_win}
@property
def template(self):
return textwrap.dedent("""\
{%- macro tvalue(pkg_name, comp_name, var, config_suffix) -%}
{{'${'+pkg_name+'_'+comp_name+'_'+var+config_suffix+'}'}}
{%- endmacro -%}
########### VARIABLES #######################################################################
#############################################################################################
set({{ pkg_name }}_COMPILE_OPTIONS{{ config_suffix }}
"$<$<COMPILE_LANGUAGE:CXX>{{ ':${' }}{{ pkg_name }}_COMPILE_OPTIONS_CXX{{ config_suffix }}}>"
"$<$<COMPILE_LANGUAGE:C>{{ ':${' }}{{ pkg_name }}_COMPILE_OPTIONS_C{{ config_suffix }}}>")
set({{ pkg_name }}_LINKER_FLAGS{{ config_suffix }}
"$<$<STREQUAL{{ ':$' }}<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>{{ ':${' }}{{ pkg_name }}_SHARED_LINK_FLAGS{{ config_suffix }}}>"
"$<$<STREQUAL{{ ':$' }}<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>{{ ':${' }}{{ pkg_name }}_SHARED_LINK_FLAGS{{ config_suffix }}}>"
"$<$<STREQUAL{{ ':$' }}<TARGET_PROPERTY:TYPE>,EXECUTABLE>{{ ':${' }}{{ pkg_name }}_EXE_LINK_FLAGS{{ config_suffix }}}>")
set({{ pkg_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "") # Will be filled later
conan_find_apple_frameworks({{ pkg_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "{{ '${' }}{{ pkg_name }}_FRAMEWORKS{{ config_suffix }}}" "{{ '${' }}{{ pkg_name }}_FRAMEWORK_DIRS{{ config_suffix }}}")
# Gather all the libraries that should be linked to the targets (do not touch existing variables)
set(_{{ pkg_name }}_DEPENDENCIES{{ config_suffix }} "{{ '${' }}{{ pkg_name }}_FRAMEWORKS_FOUND{{ config_suffix }}} {{ '${' }}{{ pkg_name }}_SYSTEM_LIBS{{ config_suffix }}} {{ deps_targets_names }}")
set({{ pkg_name }}_LIBRARIES_TARGETS{{ config_suffix }} "") # Will be filled later
conan_package_library_targets("{{ '${' }}{{ pkg_name }}_LIBS{{ config_suffix }}}" # libraries
"{{ '${' }}{{ pkg_name }}_LIB_DIRS{{ config_suffix }}}" # package_libdir
"{{ '${' }}_{{ pkg_name }}_DEPENDENCIES{{ config_suffix }}}" # deps
{{ pkg_name }}_LIBRARIES_TARGETS # out_libraries_targets
"{{ config }}" # DEBUG, RELEASE ...
"{{ pkg_name }}") # package_name
foreach(_FRAMEWORK {{ '${' }}{{ pkg_name }}_FRAMEWORKS_FOUND{{ config_suffix }}})
list(APPEND {{ pkg_name }}_LIBRARIES_TARGETS ${_FRAMEWORK})
endforeach()
foreach(_SYSTEM_LIB {{ '${' }}{{ pkg_name }}_SYSTEM_LIBS{{ config_suffix }}})
list(APPEND {{ pkg_name }}_LIBRARIES_TARGETS ${_SYSTEM_LIB})
endforeach()
# We need to add our requirements too
set({{ pkg_name }}_LIBRARIES_TARGETS {{ '"${' }}{{ pkg_name }}_LIBRARIES_TARGETS{{ '};' }}{{ deps_targets_names }}")
# This is a copy of the variable above
set({{ pkg_name }}_LIBRARIES{{ config_suffix }} {{ '${' }}{{ pkg_name }}_LIBRARIES_TARGETS})
# FIXME: What is the result of this for multi-config? All configs adding themselves to path?
set(CMAKE_MODULE_PATH {{ '${' }}{{ pkg_name }}_BUILD_DIRS{{ config_suffix }}} {{ '${' }}CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH {{ '${' }}{{ pkg_name }}_BUILD_DIRS{{ config_suffix }}} {{ '${' }}CMAKE_PREFIX_PATH})
{%- for comp_variable_name, comp_target_name in components_names %}
########## COMPONENT {{ comp_target_name }} FIND LIBRARIES & FRAMEWORKS / DYNAMIC VARS #############
set({{ pkg_name }}_{{ comp_variable_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "")
conan_find_apple_frameworks({{ pkg_name }}_{{ comp_variable_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "{{ '${'+pkg_name+'_'+comp_variable_name+'_FRAMEWORKS'+config_suffix+'}' }}" "{{ '${'+pkg_name+'_'+comp_variable_name+'_FRAMEWORK_DIRS'+config_suffix+'}' }}")
set({{ pkg_name }}_{{ comp_variable_name }}_LIB_TARGETS{{ config_suffix }} "")
set({{ pkg_name }}_{{ comp_variable_name }}_LIBS_FRAMEWORKS_DEPS{{ config_suffix }} {{ '${'+pkg_name+'_'+comp_variable_name+'_FRAMEWORKS_FOUND'+config_suffix+'}' }} {{ '${'+pkg_name+'_'+comp_variable_name+'_SYSTEM_LIBS'+config_suffix+'}' }} {{ '${'+pkg_name+'_'+comp_variable_name+'_DEPENDENCIES'+config_suffix+'}' }})
conan_package_library_targets("{{ '${'+pkg_name+'_'+comp_variable_name+'_LIBS'+config_suffix+'}' }}"
"{{ '${'+pkg_name+'_'+comp_variable_name+'_LIB_DIRS'+config_suffix+'}' }}"
"{{ '${'+pkg_name+'_'+comp_variable_name+'_LIBS_FRAMEWORKS_DEPS'+config_suffix+'}' }}"
{{ pkg_name }}_{{ comp_variable_name }}_LIB_TARGETS{{ config_suffix }}
"{{ config }}" # DEBUG, RELEASE...
"{{ pkg_name }}_{{ comp_variable_name }}")
set({{ pkg_name }}_{{ comp_variable_name }}_LINK_LIBS{{ config_suffix }} {{ '${'+pkg_name+'_'+comp_variable_name+'_LIB_TARGETS'+config_suffix+'}' }} {{ '${'+pkg_name+'_'+comp_variable_name+'_LIBS_FRAMEWORKS_DEPS'+config_suffix+'}' }})
{%- endfor %}
{% if not components_names %}
########## GLOBAL TARGET PROPERTIES {{ configuration }} ########################################
set_property(TARGET {{root_target_name}}
PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_OBJECTS{{config_suffix}}}>
${{'{'}}{{pkg_name}}_LIBRARIES_TARGETS} APPEND)
set_property(TARGET {{root_target_name}}
PROPERTY INTERFACE_LINK_OPTIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LINKER_FLAGS{{config_suffix}}}> APPEND)
set_property(TARGET {{root_target_name}}
PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_INCLUDE_DIRS{{config_suffix}}}> APPEND)
set_property(TARGET {{root_target_name}}
PROPERTY INTERFACE_COMPILE_DEFINITIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_COMPILE_DEFINITIONS{{config_suffix}}}> APPEND)
set_property(TARGET {{root_target_name}}
PROPERTY INTERFACE_COMPILE_OPTIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_COMPILE_OPTIONS{{config_suffix}}}> APPEND)
{%- if set_interface_link_directories %}
# This is only used for '#pragma comment(lib, "foo")' (automatic link)
set_property(TARGET {{root_target_name}}
PROPERTY INTERFACE_LINK_DIRECTORIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LIB_DIRS{{config_suffix}}}> APPEND)
{%- endif %}
{%- else %}
########## COMPONENTS TARGET PROPERTIES {{ configuration }} ########################################
{%- for comp_variable_name, comp_target_name in components_names %}
########## COMPONENT {{ comp_target_name }} TARGET PROPERTIES ######################################
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'OBJECTS', config_suffix)}}
{{tvalue(pkg_name, comp_variable_name, 'LINK_LIBS', config_suffix)}}> APPEND)
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_LINK_OPTIONS
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'LINKER_FLAGS', config_suffix)}}> APPEND)
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'INCLUDE_DIRS', config_suffix)}}> APPEND)
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_COMPILE_DEFINITIONS
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'COMPILE_DEFINITIONS', config_suffix)}}> APPEND)
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_COMPILE_OPTIONS
$<$<CONFIG:{{ configuration }}>:
{{tvalue(pkg_name, comp_variable_name, 'COMPILE_OPTIONS_C', config_suffix)}}
{{tvalue(pkg_name, comp_variable_name, 'COMPILE_OPTIONS_CXX', config_suffix)}}> APPEND)
set({{ pkg_name }}_{{ comp_variable_name }}_TARGET_PROPERTIES TRUE)
{%- if set_interface_link_directories %}
# This is only used for '#pragma comment(lib, "foo")' (automatic link)
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_LINK_DIRECTORIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'LIB_DIRS', config_suffix)}}> APPEND)
{%- endif %}
{%- endfor %}
########## AGGREGATED GLOBAL TARGET WITH THE COMPONENTS #####################
{%- for comp_variable_name, comp_target_name in components_names %}
target_link_libraries({{root_target_name}} INTERFACE {{ comp_target_name }})
{%- endfor %}
{%- endif %}
""")
def get_declared_components_targets_names(self):
"""Returns a list of component_name"""
ret = []
sorted_comps = self.conanfile.cpp_info.get_sorted_components()
for comp_name, comp in sorted_comps.items():
ret.append(self.get_component_alias(self.conanfile, comp_name))
ret.reverse()
return ret
def get_deps_targets_names(self):
"""
- [{foo}::{bar}, ] of the required
"""
ret = []
# Get a list of dependencies target names
# Declared cppinfo.requires or .components[].requires
visible_host = self.conanfile.dependencies.filter({"build": False, "visible": True})
visible_host_direct = visible_host.filter({"direct": True})
if self.conanfile.cpp_info.required_components:
for dep_name, component_name in self.conanfile.cpp_info.required_components:
if not dep_name:
# Internal dep (no another component)
req = self.conanfile
else:
req = visible_host[dep_name]
component_name = self.get_component_alias(req, component_name)
ret.append(component_name)
elif visible_host_direct:
# Regular external "conanfile.requires" declared, not cpp_info requires
ret = [self.get_root_target_name(r) for r in visible_host_direct.values()]
return ret