forked from conan-io/conan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generic.py
262 lines (226 loc) · 10.7 KB
/
generic.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import os
import re
import textwrap
from conans.client.tools import cpu_count
from conans.util.files import load
from conans.errors import ConanException
from conan.tools.cmake.base import CMakeToolchainBase
from conan.tools.cmake.utils import get_generator, is_multi_configuration, architecture_flag
# https://stackoverflow.com/questions/30503631/cmake-in-which-order-are-files-parsed-cache-toolchain-etc
# https://cmake.org/cmake/help/v3.6/manual/cmake-toolchains.7.html
# https://github.com/microsoft/vcpkg/tree/master/scripts/buildsystems
def get_toolset(settings, generator):
compiler = settings.get_safe("compiler")
compiler_base = settings.get_safe("compiler.base")
if compiler == "Visual Studio":
subs_toolset = settings.get_safe("compiler.toolset")
if subs_toolset:
return subs_toolset
elif compiler == "intel" and compiler_base == "Visual Studio" and "Visual" in generator:
compiler_version = settings.get_safe("compiler.version")
if compiler_version:
compiler_version = compiler_version if "." in compiler_version else \
"%s.0" % compiler_version
return "Intel C++ Compiler " + compiler_version
return None
def get_generator_platform(settings, generator):
# Returns the generator platform to be used by CMake
if "CONAN_CMAKE_GENERATOR_PLATFORM" in os.environ:
return os.environ["CONAN_CMAKE_GENERATOR_PLATFORM"]
compiler = settings.get_safe("compiler")
compiler_base = settings.get_safe("compiler.base")
arch = settings.get_safe("arch")
if settings.get_safe("os") == "WindowsCE":
return settings.get_safe("os.platform")
if (compiler in ("Visual Studio", "msvc") or compiler_base == "Visual Studio") and \
generator and "Visual" in generator:
return {"x86": "Win32",
"x86_64": "x64",
"armv7": "ARM",
"armv8": "ARM64"}.get(arch)
return None
class CMakeGenericToolchain(CMakeToolchainBase):
_toolchain_tpl = textwrap.dedent("""
{% extends 'base_toolchain' %}
{% block before_try_compile %}
{{ super() }}
{% if generator_platform %}set(CMAKE_GENERATOR_PLATFORM "{{ generator_platform }}" CACHE STRING "" FORCE){% endif %}
{% if toolset %}set(CMAKE_GENERATOR_TOOLSET "{{ toolset }}" CACHE STRING "" FORCE){% endif %}
{% if compiler %}
set(CMAKE_C_COMPILER {{ compiler }})
set(CMAKE_CXX_COMPILER {{ compiler }})
{%- endif %}
{% endblock %}
{% block main %}
{{ super() }}
{% if fpic -%}
message(STATUS "Conan toolchain: Setting CMAKE_POSITION_INDEPENDENT_CODE=ON (options.fPIC)")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
{%- endif %}
{% if skip_rpath -%}
set(CMAKE_SKIP_RPATH 1 CACHE BOOL "rpaths" FORCE)
# Policy CMP0068
# We want the old behavior, in CMake >= 3.9 CMAKE_SKIP_RPATH won't affect install_name in OSX
set(CMAKE_INSTALL_NAME_DIR "")
{% endif -%}
{% if architecture -%}
set(CONAN_CXX_FLAGS "${CONAN_CXX_FLAGS} {{ architecture }}")
set(CONAN_C_FLAGS "${CONAN_C_FLAGS} {{ architecture }}")
set(CONAN_SHARED_LINKER_FLAGS "${CONAN_SHARED_LINKER_FLAGS} {{ architecture }}")
set(CONAN_EXE_LINKER_FLAGS "${CONAN_EXE_LINKER_FLAGS} {{ architecture }}")
{%- endif %}
{% if set_libcxx -%}
set(CONAN_CXX_FLAGS "${CONAN_CXX_FLAGS} {{ set_libcxx }}")
{%- endif %}
{% if glibcxx -%}
add_definitions(-D_GLIBCXX_USE_CXX11_ABI={{ glibcxx }})
{%- endif %}
{% if vs_runtimes %}
{% set genexpr = namespace(str='') %}
{%- for config, value in vs_runtimes.items() -%}
{%- set genexpr.str = genexpr.str +
'$<$<CONFIG:' + config + '>:' + value|string + '>' %}
{%- endfor -%}
set(CMAKE_MSVC_RUNTIME_LIBRARY "{{ genexpr.str }}")
{% endif %}
{% endblock %}
""")
def __init__(self, conanfile, generator=None, generator_platform=None, build_type=None,
toolset=None, parallel=True):
super(CMakeGenericToolchain, self).__init__(conanfile)
self.fpic = self._deduce_fpic()
self.vs_runtimes = self._runtimes()
self.parallel = parallel
self.generator = generator or get_generator(self._conanfile)
self.generator_platform = (generator_platform or
get_generator_platform(self._conanfile.settings,
self.generator))
self.toolset = toolset or get_toolset(self._conanfile.settings, self.generator)
if (self.generator is not None and "Ninja" in self.generator
and "Visual" in self._conanfile.settings.compiler):
self.compiler = "cl"
else:
self.compiler = None # compiler defined by default
try:
self._build_shared_libs = "ON" if self._conanfile.options.shared else "OFF"
except ConanException:
self._build_shared_libs = None
self.set_libcxx, self.glibcxx = self._get_libcxx()
self.parallel = None
if parallel:
if self.generator and "Visual Studio" in self.generator:
self.parallel = "/MP%s" % cpu_count(output=self._conanfile.output)
self.cppstd, self.cppstd_extensions = self._cppstd()
self.skip_rpath = True if self._conanfile.settings.get_safe("os") == "Macos" else False
self.architecture = self._get_architecture()
# TODO: I would want to have here the path to the compiler too
build_type = build_type or self._conanfile.settings.get_safe("build_type")
self.build_type = build_type if not is_multi_configuration(self.generator) else None
def _get_templates(self):
templates = super(CMakeGenericToolchain, self)._get_templates()
templates.update({
CMakeToolchainBase.filename: self._toolchain_tpl
})
return templates
def _deduce_fpic(self):
fpic = self._conanfile.options.get_safe("fPIC")
if fpic is None:
return None
os_ = self._conanfile.settings.get_safe("os")
if os_ and "Windows" in os_:
self._conanfile.output.warn("Toolchain: Ignoring fPIC option defined for Windows")
return None
shared = self._conanfile.options.get_safe("shared")
if shared:
self._conanfile.output.warn("Toolchain: Ignoring fPIC option defined "
"for a shared library")
return None
return fpic
def _get_architecture(self):
# This should be factorized and make it toolchain-private
return architecture_flag(self._conanfile.settings)
def _runtimes(self):
# Parsing existing toolchain file to get existing configured runtimes
config_dict = {}
if os.path.exists(self.filename):
existing_include = load(self.filename)
msvc_runtime_value = re.search(r"set\(CMAKE_MSVC_RUNTIME_LIBRARY \"([^)]*)\"\)",
existing_include)
if msvc_runtime_value:
capture = msvc_runtime_value.group(1)
matches = re.findall(r"\$<\$<CONFIG:([A-Za-z]*)>:([A-Za-z]*)>", capture)
config_dict = dict(matches)
settings = self._conanfile.settings
compiler = settings.get_safe("compiler")
build_type = settings.get_safe("build_type") # FIXME: change for configuration
runtime = settings.get_safe("compiler.runtime")
if compiler == "Visual Studio":
config_dict[build_type] = {"MT": "MultiThreaded",
"MTd": "MultiThreadedDebug",
"MD": "MultiThreadedDLL",
"MDd": "MultiThreadedDebugDLL"}[runtime]
if compiler == "msvc":
runtime_type = settings.get_safe("compiler.runtime_type")
rt = "MultiThreadedDebug" if runtime_type == "Debug" else "MultiThreaded"
if runtime != "static":
rt += "DLL"
config_dict[build_type] = rt
return config_dict
def _get_libcxx(self):
libcxx = self._conanfile.settings.get_safe("compiler.libcxx")
if not libcxx:
return None, None
compiler = self._conanfile.settings.compiler
lib = glib = None
if compiler == "apple-clang":
# In apple-clang 2 only values atm are "libc++" and "libstdc++"
lib = "-stdlib={}".format(libcxx)
elif compiler == "clang":
if libcxx == "libc++":
lib = "-stdlib=libc++"
elif libcxx == "libstdc++" or libcxx == "libstdc++11":
lib = "-stdlib=libstdc++"
# FIXME, something to do with the other values? Android c++_shared?
elif compiler == "sun-cc":
lib = {"libCstd": "Cstd",
"libstdcxx": "stdcxx4",
"libstlport": "stlport4",
"libstdc++": "stdcpp"
}.get(libcxx)
if lib:
lib = "-library={}".format(lib)
elif compiler == "gcc":
if libcxx == "libstdc++11":
glib = "1"
elif libcxx == "libstdc++":
glib = "0"
return lib, glib
def _cppstd(self):
cppstd = cppstd_extensions = None
compiler_cppstd = self._conanfile.settings.get_safe("compiler.cppstd")
if compiler_cppstd:
if compiler_cppstd.startswith("gnu"):
cppstd = compiler_cppstd[3:]
cppstd_extensions = "ON"
else:
cppstd = compiler_cppstd
cppstd_extensions = "OFF"
return cppstd, cppstd_extensions
def _get_template_context_data(self):
ctxt_toolchain = super(CMakeGenericToolchain, self)._get_template_context_data()
ctxt_toolchain.update({
"generator_platform": self.generator_platform,
"toolset": self.toolset,
"fpic": self.fpic,
"skip_rpath": self.skip_rpath,
"set_libcxx": self.set_libcxx,
"glibcxx": self.glibcxx,
"parallel": self.parallel,
"cppstd": self.cppstd,
"cppstd_extensions": self.cppstd_extensions,
"shared_libs": self._build_shared_libs,
"architecture": self.architecture,
"compiler": self.compiler,
'vs_runtimes': self.vs_runtimes
})
return ctxt_toolchain