-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathconanfile.py
257 lines (222 loc) · 12.6 KB
/
conanfile.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
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import os
import textwrap
class ProtobufConan(ConanFile):
name = "protobuf"
description = "Protocol Buffers - Google's data interchange format"
topics = ("conan", "protobuf", "protocol-buffers",
"protocol-compiler", "serialization", "rpc", "protocol-compiler")
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/protocolbuffers/protobuf"
license = "BSD-3-Clause"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"with_zlib": [True, False],
"with_rtti": [True, False],
"lite": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"with_zlib": True,
"with_rtti": True,
"lite": False,
}
short_paths = True
exports_sources = ["CMakeLists.txt", "patches/*"]
generators = "cmake"
_cmake = None
@property
def _source_subfolder(self):
return "source_subfolder"
@property
def _build_subfolder(self):
return "build_subfolder"
@property
def _is_clang_x86(self):
return self.settings.compiler == "clang" and self.settings.arch == "x86"
@property
def _can_disable_rtti(self):
return tools.Version(self.version) >= "3.15.4"
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
if not self._can_disable_rtti:
del self.options.with_rtti
def configure(self):
if self.options.shared:
del self.options.fPIC
if str(self.settings.compiler.get_safe("runtime")) in ["MT", "MTd", "static"]:
raise ConanInvalidConfiguration("Protobuf can't be built with shared + MT(d) runtimes")
if self.settings.compiler == "Visual Studio":
if tools.Version(self.settings.compiler.version) < "14":
raise ConanInvalidConfiguration("On Windows Protobuf can only be built with "
"Visual Studio 2015 or higher.")
if self.settings.compiler == "clang":
if tools.Version(self.version) >= "3.15.4" and tools.Version(self.settings.compiler.version) < "4":
raise ConanInvalidConfiguration("protobuf {} doesn't support clang < 4".format(self.version))
def requirements(self):
if self.options.with_zlib:
self.requires("zlib/1.2.11")
def source(self):
tools.get(**self.conan_data["sources"][self.version])
extracted_folder = self.name + "-" + self.version
os.rename(extracted_folder, self._source_subfolder)
@property
def _cmake_install_base_path(self):
return os.path.join("lib", "cmake", "protobuf")
def _configure_cmake(self):
if not self._cmake:
self._cmake = CMake(self)
self._cmake.definitions["CMAKE_INSTALL_CMAKEDIR"] = self._cmake_install_base_path.replace("\\", "/")
self._cmake.definitions["protobuf_WITH_ZLIB"] = self.options.with_zlib
self._cmake.definitions["protobuf_BUILD_TESTS"] = False
self._cmake.definitions["protobuf_BUILD_PROTOC_BINARIES"] = True
if tools.Version(self.version) >= "3.14.0":
self._cmake.definitions["protobuf_BUILD_LIBPROTOC"] = True
if self._can_disable_rtti:
self._cmake.definitions["protobuf_DISABLE_RTTI"] = not self.options.with_rtti
if self.settings.compiler.get_safe("runtime"):
self._cmake.definitions["protobuf_MSVC_STATIC_RUNTIME"] = str(self.settings.compiler.runtime) in ["MT", "MTd", "static"]
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake
def _patch_sources(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
# Provide relocatable protobuf::protoc target and Protobuf_PROTOC_EXECUTABLE cache variable
# TODO: some of the following logic might be disabled when conan will
# allow to create executable imported targets in package_info()
protobuf_config_cmake = os.path.join(self._source_subfolder, "cmake", "protobuf-config.cmake.in")
tools.replace_in_file(
protobuf_config_cmake,
"@_protobuf_FIND_ZLIB@",
"# BEGIN CONAN PATCH\n#_protobuf_FIND_ZLIB@\n# END CONAN PATCH"
)
exe_ext = ".exe" if self.settings.os == "Windows" else ""
protoc_filename = "protoc" + exe_ext
module_folder_depth = len(os.path.normpath(self._cmake_install_base_path).split(os.path.sep))
protoc_rel_path = "{}bin/{}".format("".join(["../"] * module_folder_depth), protoc_filename)
protoc_target = textwrap.dedent("""\
if(NOT TARGET protobuf::protoc)
if(CMAKE_CROSSCOMPILING)
find_program(PROTOC_PROGRAM protoc PATHS ENV PATH NO_DEFAULT_PATH)
endif()
if(NOT PROTOC_PROGRAM)
set(PROTOC_PROGRAM \"${{CMAKE_CURRENT_LIST_DIR}}/{protoc_rel_path}\")
endif()
get_filename_component(PROTOC_PROGRAM \"${{PROTOC_PROGRAM}}\" ABSOLUTE)
set(Protobuf_PROTOC_EXECUTABLE ${{PROTOC_PROGRAM}} CACHE FILEPATH \"The protoc compiler\")
add_executable(protobuf::protoc IMPORTED)
set_property(TARGET protobuf::protoc PROPERTY IMPORTED_LOCATION ${{Protobuf_PROTOC_EXECUTABLE}})
endif()
""".format(protoc_rel_path=protoc_rel_path))
tools.replace_in_file(
protobuf_config_cmake,
"include(\"${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake\")",
protoc_target
)
# Set DYLD_LIBRARY_PATH in command line to avoid issues with shared protobuf
# (even with virtualrunenv, this fix might be required due to SIP)
# Only works with cmake, cmake_find_package or cmake_find_package_multi generators
if tools.is_apple_os(self.settings.os):
tools.replace_in_file(
protobuf_config_cmake,
"add_custom_command(",
("set(CUSTOM_DYLD_LIBRARY_PATH ${CONAN_LIB_DIRS} ${Protobuf_LIB_DIRS} ${Protobuf_LIB_DIRS_RELEASE} ${Protobuf_LIB_DIRS_DEBUG} ${Protobuf_LIB_DIRS_RELWITHDEBINFO} ${Protobuf_LIB_DIRS_MINSIZEREL})\n"
"string(REPLACE \";\" \":\" CUSTOM_DYLD_LIBRARY_PATH \"${CUSTOM_DYLD_LIBRARY_PATH}\")\n"
"add_custom_command(")
)
tools.replace_in_file(
protobuf_config_cmake,
"COMMAND protobuf::protoc",
"COMMAND ${CMAKE_COMMAND} -E env \"DYLD_LIBRARY_PATH=${CUSTOM_DYLD_LIBRARY_PATH}\" $<TARGET_FILE:protobuf::protoc>"
)
# Disable a potential warning in protobuf-module.cmake.in
# TODO: remove this patch? Is it really useful?
protobuf_module_cmake = os.path.join(self._source_subfolder, "cmake", "protobuf-module.cmake.in")
tools.replace_in_file(
protobuf_module_cmake,
"if(DEFINED Protobuf_SRC_ROOT_FOLDER)",
"if(0)\nif(DEFINED Protobuf_SRC_ROOT_FOLDER)",
)
tools.replace_in_file(
protobuf_module_cmake,
"# Define upper case versions of output variables",
"endif()",
)
def build(self):
self._patch_sources()
cmake = self._configure_cmake()
cmake.build()
def package(self):
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
os.unlink(os.path.join(self.package_folder, self._cmake_install_base_path, "protobuf-config-version.cmake"))
os.unlink(os.path.join(self.package_folder, self._cmake_install_base_path, "protobuf-targets.cmake"))
os.unlink(os.path.join(self.package_folder, self._cmake_install_base_path, "protobuf-targets-{}.cmake".format(str(self.settings.build_type).lower())))
os.rename(os.path.join(self.package_folder, self._cmake_install_base_path, "protobuf-config.cmake"),
os.path.join(self.package_folder, self._cmake_install_base_path, "protobuf-generate.cmake"))
if not self.options.lite:
tools.remove_files_by_mask(os.path.join(self.package_folder, "lib"), "libprotobuf-lite.*")
tools.remove_files_by_mask(os.path.join(self.package_folder, "bin"), "libprotobuf-lite.*")
def package_info(self):
# The module name is Protobuf while the config name is protobuf
self.cpp_info.filenames["cmake_find_package"] = "Protobuf"
self.cpp_info.filenames["cmake_find_package_multi"] = "protobuf"
self.cpp_info.names["cmake_find_package"] = "protobuf"
self.cpp_info.names["cmake_find_package_multi"] = "protobuf"
self.cpp_info.names["pkg_config"] = "protobuf_full_package" # unofficial, but required to avoid side effects (libprotobuf component "steals" the default global pkg_config name)
lib_prefix = "lib" if self.settings.compiler == "Visual Studio" else ""
lib_suffix = "d" if self.settings.build_type == "Debug" else ""
self.cpp_info.components["libprotobuf"].names["cmake_find_package"] = "libprotobuf"
self.cpp_info.components["libprotobuf"].names["cmake_find_package_multi"] = "libprotobuf"
self.cpp_info.components["libprotobuf"].names["pkg_config"] = "protobuf"
self.cpp_info.components["libprotobuf"].libs = [lib_prefix + "protobuf" + lib_suffix]
if self.options.with_zlib:
self.cpp_info.components["libprotobuf"].requires = ["zlib::zlib"]
if self.settings.os == "Linux":
self.cpp_info.components["libprotobuf"].system_libs.append("pthread")
if self._is_clang_x86 or "arm" in str(self.settings.arch):
self.cpp_info.components["libprotobuf"].system_libs.append("atomic")
if self.settings.os == "Android":
self.cpp_info.components["libprotobuf"].system_libs.append("log")
if self.settings.os == "Windows":
if self.options.shared:
self.cpp_info.components["libprotobuf"].defines = ["PROTOBUF_USE_DLLS"]
self.cpp_info.components["libprotobuf"].builddirs.append(self._cmake_install_base_path)
self.cpp_info.components["libprotobuf"].build_modules = [
os.path.join(self._cmake_install_base_path, "protobuf-generate.cmake"),
os.path.join(self._cmake_install_base_path, "protobuf-module.cmake"),
os.path.join(self._cmake_install_base_path, "protobuf-options.cmake"),
]
self.cpp_info.components["libprotoc"].name = "libprotoc"
self.cpp_info.components["libprotoc"].libs = [lib_prefix + "protoc" + lib_suffix]
self.cpp_info.components["libprotoc"].requires = ["libprotobuf"]
bindir = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bindir))
self.env_info.PATH.append(bindir)
if self.options.lite:
self.cpp_info.components["libprotobuf-lite"].names["cmake_find_package"] = "libprotobuf-lite"
self.cpp_info.components["libprotobuf-lite"].names["cmake_find_package_multi"] = "libprotobuf-lite"
self.cpp_info.components["libprotobuf-lite"].names["pkg_config"] = "protobuf-lite"
self.cpp_info.components["libprotobuf-lite"].libs = [lib_prefix + "protobuf-lite" + lib_suffix]
if self.settings.os == "Linux":
self.cpp_info.components["libprotobuf-lite"].system_libs.append("pthread")
if self._is_clang_x86 or "arm" in str(self.settings.arch):
self.cpp_info.components["libprotobuf-lite"].system_libs.append("atomic")
if self.settings.os == "Windows":
if self.options.shared:
self.cpp_info.components["libprotobuf-lite"].defines = ["PROTOBUF_USE_DLLS"]
if self.settings.os == "Android":
self.cpp_info.components["libprotobuf-lite"].system_libs.append("log")
self.cpp_info.components["libprotobuf-lite"].builddirs.append(self._cmake_install_base_path)
self.cpp_info.components["libprotobuf-lite"].build_modules = [
os.path.join(self._cmake_install_base_path, "protobuf-generate.cmake"),
os.path.join(self._cmake_install_base_path, "protobuf-module.cmake"),
os.path.join(self._cmake_install_base_path, "protobuf-options.cmake"),
]