1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""Conan recipe package for Google FlatBuffers
5"""
6import os
7import shutil
8from conans import ConanFile, CMake, tools
9
10
11class FlatbuffersConan(ConanFile):
12    name = "flatbuffers"
13    license = "Apache-2.0"
14    url = "https://github.com/google/flatbuffers"
15    homepage = "http://google.github.io/flatbuffers/"
16    author = "Wouter van Oortmerssen"
17    topics = ("conan", "flatbuffers", "serialization", "rpc", "json-parser")
18    description = "Memory Efficient Serialization Library"
19    settings = "os", "compiler", "build_type", "arch"
20    options = {"shared": [True, False], "fPIC": [True, False]}
21    default_options = {"shared": False, "fPIC": True}
22    generators = "cmake"
23    exports = "LICENSE.txt"
24    exports_sources = ["CMake/*", "include/*", "src/*", "grpc/*", "CMakeLists.txt", "conan/CMakeLists.txt"]
25
26    def source(self):
27        """Wrap the original CMake file to call conan_basic_setup
28        """
29        shutil.move("CMakeLists.txt", "CMakeListsOriginal.txt")
30        shutil.move(os.path.join("conan", "CMakeLists.txt"), "CMakeLists.txt")
31
32    def config_options(self):
33        """Remove fPIC option on Windows platform
34        """
35        if self.settings.os == "Windows":
36            self.options.remove("fPIC")
37
38    def configure_cmake(self):
39        """Create CMake instance and execute configure step
40        """
41        cmake = CMake(self)
42        cmake.definitions["FLATBUFFERS_BUILD_TESTS"] = False
43        cmake.definitions["FLATBUFFERS_BUILD_SHAREDLIB"] = self.options.shared
44        cmake.definitions["FLATBUFFERS_BUILD_FLATLIB"] = not self.options.shared
45        cmake.configure()
46        return cmake
47
48    def build(self):
49        """Configure, build and install FlatBuffers using CMake.
50        """
51        cmake = self.configure_cmake()
52        cmake.build()
53
54    def package(self):
55        """Copy Flatbuffers' artifacts to package folder
56        """
57        cmake = self.configure_cmake()
58        cmake.install()
59        self.copy(pattern="LICENSE.txt", dst="licenses")
60        self.copy(pattern="FindFlatBuffers.cmake", dst=os.path.join("lib", "cmake", "flatbuffers"), src="CMake")
61        self.copy(pattern="flathash*", dst="bin", src="bin")
62        self.copy(pattern="flatc*", dst="bin", src="bin")
63        if self.settings.os == "Windows" and self.options.shared:
64            if self.settings.compiler == "Visual Studio":
65                shutil.move(os.path.join(self.package_folder, "lib", "%s.dll" % self.name),
66                            os.path.join(self.package_folder, "bin", "%s.dll" % self.name))
67            elif self.settings.compiler == "gcc":
68                shutil.move(os.path.join(self.package_folder, "lib", "lib%s.dll" % self.name),
69                            os.path.join(self.package_folder, "bin", "lib%s.dll" % self.name))
70
71    def package_info(self):
72        """Collect built libraries names and solve flatc path.
73        """
74        self.cpp_info.libs = tools.collect_libs(self)
75        self.user_info.flatc = os.path.join(self.package_folder, "bin", "flatc")
76