Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flatbuffers: refactoring #9292

Merged
merged 10 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions recipes/flatbuffers/all/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 3.4)
project(cmake_wrapper)

include(conanbuildinfo.cmake)
conan_basic_setup()
conan_basic_setup(KEEP_RPATHS)

if (WIN32 AND MSVC AND FLATBUFFERS_BUILD_SHAREDLIB)
if(MSVC AND FLATBUFFERS_BUILD_SHAREDLIB)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif(WIN32 AND MSVC AND FLATBUFFERS_BUILD_SHAREDLIB)
endif()

add_subdirectory("source_subfolder")
31 changes: 31 additions & 0 deletions recipes/flatbuffers/all/cmake/FlatcTargets.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if(NOT TARGET flatbuffers::flatc)
if(CMAKE_CROSSCOMPILING)
find_program(FLATBUFFERS_FLATC_EXECUTABLE
NAMES flatc
PATHS ENV PATH
NO_DEFAULT_PATH
)
else()
find_program(FLATBUFFERS_FLATC_EXECUTABLE
NAMES flatc
PATHS "${CMAKE_CURRENT_LIST_DIR}/../../bin/"
NO_DEFAULT_PATH
)
endif()
# TODO: In conan v2 with CMakeToolchain, can be replaced by:
# find_program(FLATBUFFERS_FLATC_EXECUTABLE NAMES flatc)
# # Nice enough to handle flatbuffers not in build_requires for native build
# if(NOT FLATBUFFERS_FLATC_EXECUTABLE AND NOT CMAKE_CROSSCOMPILING)
# find_program(FLATBUFFERS_FLATC_EXECUTABLE
# NAMES flatc
# PATHS "${CMAKE_CURRENT_LIST_DIR}/../../bin/"
# NO_DEFAULT_PATH
# )
# endif()

if(FLATBUFFERS_FLATC_EXECUTABLE)
get_filename_component(FLATBUFFERS_FLATC_EXECUTABLE "${FLATBUFFERS_FLATC_EXECUTABLE}" ABSOLUTE)
add_executable(flatbuffers::flatc IMPORTED)
set_property(TARGET flatbuffers::flatc PROPERTY IMPORTED_LOCATION ${FLATBUFFERS_FLATC_EXECUTABLE})
endif()
endif()
36 changes: 16 additions & 20 deletions recipes/flatbuffers/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
sources:
1.11.0:
url: "https://github.com/google/flatbuffers/archive/v1.11.0.tar.gz"
sha256: "3f4a286642094f45b1b77228656fbd7ea123964f19502f9ecfd29933fd23a50b"
1.12.0:
url: "https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz"
sha256: "62f2223fb9181d1d6338451375628975775f7522185266cd5296571ac152bc45"
2.0.0:
"2.0.5":
url: "https://github.com/google/flatbuffers/archive/refs/tags/v2.0.5.tar.gz"
sha256: "b01e97c988c429e164c5c7df9e87c80007ca87f593c0d73733ba536ddcbc8f98"
"2.0.0":
url: "https://github.com/google/flatbuffers/archive/v2.0.0.tar.gz"
sha256: "9ddb9031798f4f8754d00fca2f1a68ecf9d0f83dfac7239af1311e4fd9a565c4"
"1.12.0":
url: "https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz"
sha256: "62f2223fb9181d1d6338451375628975775f7522185266cd5296571ac152bc45"
"1.11.0":
url: "https://github.com/google/flatbuffers/archive/v1.11.0.tar.gz"
sha256: "3f4a286642094f45b1b77228656fbd7ea123964f19502f9ecfd29933fd23a50b"
patches:
1.11.0:
- patch_file: "patches/0001-buildflatbuffers-cmake.patch"
"2.0.5":
- patch_file: "patches/0002-apple-no-universal-build.patch"
base_path: "source_subfolder"
- patch_file: "patches/0003-no-flatc-execution-build-time.patch"
base_path: "source_subfolder"
- patch_file: "patches/1.11.0/fix-copy-ctor.patch"
"1.11.0":
- patch_file: "patches/0001-fix-copy-ctor.patch"
base_path: "source_subfolder"
# patch_type: portability
# patch_source: https://github.com/google/flatbuffers/pull/5650
# patch_description: Fix build with Clang
1.12.0:
- patch_file: "patches/0001-buildflatbuffers-cmake.patch"
base_path: "source_subfolder"
2.0.0:
- patch_file: "patches/0001-buildflatbuffers-cmake.patch"
base_path: "source_subfolder"
- patch_file: "patches/0002-versionflatbuffers-cmake.patch"
base_path: "source_subfolder"
- patch_file: "patches/0003-werrorflatbuffers-cmake.patch"
base_path: "source_subfolder"
223 changes: 131 additions & 92 deletions recipes/flatbuffers/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,143 +1,182 @@
import glob
import os
import shutil
from conans import ConanFile, CMake, tools
import os

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.43.0"


class FlatbuffersConan(ConanFile):
name = "flatbuffers"
license = "Apache-2.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "http://google.github.io/flatbuffers"
topics = ("conan", "flatbuffers", "serialization", "rpc", "json-parser")
topics = ("flatbuffers", "serialization", "rpc", "json-parser")
description = "Memory Efficient Serialization Library"
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False],
"fPIC": [True, False],
"header_only": [True, False],
"flatc": [True, False],
"flatbuffers": [True, False],
"options_from_context": [True, False]}
default_options = {"shared": False,
"fPIC": True, "header_only": False,
"flatc": True,
"flatbuffers": True,
"options_from_context": True}
exports_sources = ["CMakeLists.txt", "patches/**"]
generators = "cmake"

settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"header_only": [True, False],
"flatc": [True, False, "deprecated"],
"flatbuffers": [True, False, "deprecated"],
"options_from_context": [True, False, "deprecated"],
}
default_options = {
"shared": False,
"fPIC": True,
"header_only": False,
"flatc": "deprecated",
"flatbuffers": "deprecated",
"options_from_context": "deprecated",
}

generators = "cmake"
_cmake = None
_header_only = False

@property
def _source_subfolder(self):
return "source_subfolder"


@property
def _has_flatc(self):
# don't build flatc when it makes little sense or not supported
return self.settings.os not in ["Android", "iOS", "watchOS", "tvOS", "Neutrino"]

def export_sources(self):
self.copy("CMakeLists.txt")
self.copy(os.path.join("cmake", "FlatcTargets.cmake"))
for patch in self.conan_data.get("patches", {}).get(self.version, []):
self.copy(patch["patch_file"])

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def _patch_sources(self):
for patch in self.conan_data["patches"][self.version]:
tools.patch(**patch)

def configure(self):
# Detect if host or build context
if self.options.options_from_context:
settings_target = getattr(self, 'settings_target', None)
self.options.flatc = settings_target is not None
self.options.flatbuffers = settings_target is None
del self.options.options_from_context

if not self.options.flatbuffers:
del self.options.header_only
self._header_only = False
else:
self._header_only = self.options.header_only

if (self.options.shared and self.options.flatbuffers) or self._header_only or (not self.options.flatbuffers and self.options.flatc):
if self.options.shared or self.options.header_only:
del self.options.fPIC

if self._header_only and not self.options.flatc:
if self.options.header_only:
del self.options.shared
# deprecated options
for deprecated_option in ["flatc", "flatbuffers", "options_from_context"]:
if self.options.get_safe(deprecated_option) != "deprecated":
self.output.warn("{} option is deprecated, do not use".format(deprecated_option))

if self.settings.compiler.cppstd:
def validate(self):
if self.settings.compiler.get_safe("cppstd"):
tools.check_min_cppstd(self, 11)

def package_id(self):
if self._header_only and not self.options.flatc:
if self.options.header_only and not self._has_flatc:
self.info.header_only()
# deprecated options
del self.info.options.flatc
del self.info.options.flatbuffers
del self.info.options.options_from_context

def source(self):
tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)

def _patch_sources(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)

cmakelists = os.path.join(self._source_subfolder, "CMakeLists.txt")
# Prefer manual injection of current version in build(), otherwise it tries to call git
tools.replace_in_file(cmakelists, "include(CMake/Version.cmake)", "")
# No warnings as errors
tools.replace_in_file(cmakelists, "/WX", "")
tools.replace_in_file(cmakelists, "-Werror ", "")
# Install dll to bin folder
tools.replace_in_file(cmakelists,
"RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}",
"RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}")

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["FLATBUFFERS_BUILD_TESTS"] = False
self._cmake.definitions["FLATBUFFERS_BUILD_SHAREDLIB"] = self.options.flatbuffers and self.options.shared
self._cmake.definitions["FLATBUFFERS_BUILD_FLATLIB"] = self.options.flatbuffers and not self.options.shared
self._cmake.definitions["FLATBUFFERS_BUILD_FLATC"] = self.options.flatc
self._cmake.definitions["FLATBUFFERS_BUILD_FLATHASH"] = False
self._cmake.definitions["FLATBUFFERS_INSTALL"] = True
self._cmake.definitions["FLATBUFFERS_BUILD_FLATLIB"] = not self.options.header_only and not self.options.shared
self._cmake.definitions["FLATBUFFERS_BUILD_FLATC"] = self._has_flatc
self._cmake.definitions["FLATBUFFERS_STATIC_FLATC"] = False
self._cmake.definitions["FLATBUFFERS_BUILD_FLATHASH"] = False
self._cmake.definitions["FLATBUFFERS_BUILD_SHAREDLIB"] = not self.options.header_only and self.options.shared
# Honor conan profile
self._cmake.definitions["FLATBUFFERS_LIBCXX_WITH_CLANG"] = False
# Mimic upstream CMake/Version.cmake removed in _patch_sources()
version = tools.Version(self.version)
self._cmake.definitions["VERSION_MAJOR"] = version.major
self._cmake.definitions["VERSION_MINOR"] = version.minor
self._cmake.definitions["VERSION_PATCH"] = version.patch
# To install relocatable shared libs on Macos
self._cmake.definitions["CMAKE_POLICY_DEFAULT_CMP0042"] = "NEW"
# Fix iOS/tvOS/watchOS
if tools.is_apple_os(self.settings.os):
self._cmake.definitions["CMAKE_MACOSX_BUNDLE"] = False

self._cmake.configure()
return self._cmake

def build(self):
self._patch_sources()
if (self.options.flatbuffers and not self._header_only) or self.options.flatc:
cmake = self._configure_cmake()
cmake.build()
cmake = self._configure_cmake()
cmake.build()

def package(self):
self.copy(pattern="LICENSE.txt", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
self.copy(pattern="FlatcTargets.cmake",
dst=self._module_path,
src="cmake")
self.copy(pattern="BuildFlatBuffers.cmake",
dst=self._module_path,
src=os.path.join(self._source_subfolder, "CMake"))

# Run cmake if there is anything to build
if (self.options.flatbuffers and not self._header_only) or self.options.flatc:
cmake = self._configure_cmake()
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))

if self.options.flatbuffers:
self.copy(pattern="BuildFlatBuffers.cmake", dst="bin/cmake", src=os.path.join(self._source_subfolder, "CMake"))

if self._header_only:
header_dir = os.path.join(self._source_subfolder, "include", "flatbuffers")
dst_dir = os.path.join("include", "flatbuffers")
self.copy("*.h", dst=dst_dir, src=header_dir)

if not self._header_only:
if self.settings.os == "Windows" and self.options.shared:
tools.mkdir(os.path.join(self.package_folder, "bin"))
for dll_path in glob.glob(os.path.join(self.package_folder, "lib", "*.dll")):
shutil.move(dll_path, os.path.join(self.package_folder, "bin", os.path.basename(dll_path)))
elif self.options.flatc:
tools.rmdir(os.path.join(self.package_folder, "include"))
@property
def _module_path(self):
return os.path.join("lib", "cmake")

def package_info(self):
if self.options.flatbuffers:
self.cpp_info.filenames["cmake_find_package"] = "Flatbuffers"
self.cpp_info.filenames["cmake_find_package_multi"] = "Flatbuffers"
self.cpp_info.names["cmake_find_package"] = "flatbuffers"
self.cpp_info.names["cmake_find_package_multi"] = "flatbuffers"
self.cpp_info.names["pkg_config"] = "flatbuffers"
if not self._header_only:
cmake_target = "flatbuffers_shared" if self.options.shared else "flatbuffers"
self.cpp_info.components["libflatbuffers"].names["cmake_find_package"] = cmake_target
self.cpp_info.components["libflatbuffers"].names["cmake_find_package_multi"] = cmake_target
self.cpp_info.components["libflatbuffers"].libs = tools.collect_libs(self)
self.cpp_info.components["libflatbuffers"].builddirs.append("bin/cmake")
self.cpp_info.components["libflatbuffers"].build_modules.append("bin/cmake/BuildFlatBuffers.cmake")
if self.settings.os == "Linux":
self.cpp_info.components["libflatbuffers"].system_libs.append("m")
else:
self.cpp_info.builddirs.append("bin/cmake")
self.cpp_info.build_modules.append("bin/cmake/BuildFlatBuffers.cmake")
if self.options.flatc:
self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_module_file_name", "FlatBuffers")
self.cpp_info.set_property("cmake_file_name", "Flatbuffers")
cmake_target = "flatbuffers"
if not self.options.header_only and self.options.shared:
cmake_target += "_shared"
self.cpp_info.set_property("cmake_target_name", "flatbuffers::{}".format(cmake_target))
self.cpp_info.set_property("pkg_config_name", "flatbuffers")

# TODO: back to global scope in conan v2 once cmake_find_package* generators removed
if not self.options.header_only:
self.cpp_info.components["libflatbuffers"].libs = tools.collect_libs(self)
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.components["libflatbuffers"].system_libs.append("m")

# Provide flatbuffers::flatc target and CMake functions from BuildFlatBuffers.cmake
build_modules = [
os.path.join(self._module_path, "FlatcTargets.cmake"),
os.path.join(self._module_path, "BuildFlatBuffers.cmake"),
]
self.cpp_info.set_property("cmake_build_modules", build_modules)
if self._has_flatc:
bindir = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bindir))
self.env_info.PATH.append(bindir)

# TODO: to remove in conan v2 once cmake_find_package* generators removed
self.cpp_info.filenames["cmake_find_package"] = "FlatBuffers"
self.cpp_info.filenames["cmake_find_package_multi"] = "Flatbuffers"
self.cpp_info.names["cmake_find_package"] = "flatbuffers"
self.cpp_info.names["cmake_find_package_multi"] = "flatbuffers"
self.cpp_info.components["libflatbuffers"].names["cmake_find_package"] = cmake_target
self.cpp_info.components["libflatbuffers"].names["cmake_find_package_multi"] = cmake_target
self.cpp_info.components["libflatbuffers"].build_modules["cmake_find_package"] = build_modules
self.cpp_info.components["libflatbuffers"].build_modules["cmake_find_package_multi"] = build_modules
self.cpp_info.components["libflatbuffers"].set_property("cmake_file_name", "flatbuffers::{}".format(cmake_target))
self.cpp_info.components["libflatbuffers"].set_property("pkg_config_name", "flatbuffers")
13 changes: 0 additions & 13 deletions recipes/flatbuffers/all/patches/0001-buildflatbuffers-cmake.patch

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -282,7 +282,6 @@ elseif(CMAKE_COMPILER_IS_GNUCXX)

elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
if(APPLE)
- set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
Loading