1from conans import ConanFile, CMake, tools
2from conans.errors import ConanInvalidConfiguration
3import shutil
4import os
5
6
7class GoogleBenchmarkConan(ConanFile):
8    name = "benchmark"
9    description = "A microbenchmark support library."
10    topics = ("conan", "benchmark", "google", "microbenchmark")
11    url = "https://github.com/google/benchmark"
12    homepage = "https://github.com/google/benchmark"
13    author = "Google Inc."
14    license = "Apache-2.0"
15    exports_sources = ["*"]
16    generators = "cmake"
17
18    settings = "arch", "build_type", "compiler", "os"
19    options = {
20        "shared": [True, False],
21        "fPIC": [True, False],
22        "enable_lto": [True, False],
23        "enable_exceptions": [True, False]
24    }
25    default_options = {"shared": False, "fPIC": True, "enable_lto": False, "enable_exceptions": True}
26
27    _build_subfolder = "."
28
29    def source(self):
30        # Wrap the original CMake file to call conan_basic_setup
31        shutil.move("CMakeLists.txt", "CMakeListsOriginal.txt")
32        shutil.move(os.path.join("conan", "CMakeLists.txt"), "CMakeLists.txt")
33
34    def config_options(self):
35        if self.settings.os == "Windows":
36            if self.settings.compiler == "Visual Studio" and float(self.settings.compiler.version.value) <= 12:
37                raise ConanInvalidConfiguration("{} {} does not support Visual Studio <= 12".format(self.name, self.version))
38            del self.options.fPIC
39
40    def configure(self):
41        if self.settings.os == "Windows" and self.options.shared:
42            raise ConanInvalidConfiguration("Windows shared builds are not supported right now, see issue #639")
43
44    def _configure_cmake(self):
45        cmake = CMake(self)
46
47        cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "OFF"
48        cmake.definitions["BENCHMARK_ENABLE_GTEST_TESTS"] = "OFF"
49        cmake.definitions["BENCHMARK_ENABLE_LTO"] = "ON" if self.options.enable_lto else "OFF"
50        cmake.definitions["BENCHMARK_ENABLE_EXCEPTIONS"] = "ON" if self.options.enable_exceptions else "OFF"
51
52        # See https://github.com/google/benchmark/pull/638 for Windows 32 build explanation
53        if self.settings.os != "Windows":
54            cmake.definitions["BENCHMARK_BUILD_32_BITS"] = "ON" if "64" not in str(self.settings.arch) else "OFF"
55            cmake.definitions["BENCHMARK_USE_LIBCXX"] = "ON" if (str(self.settings.compiler.libcxx) == "libc++") else "OFF"
56        else:
57            cmake.definitions["BENCHMARK_USE_LIBCXX"] = "OFF"
58
59        cmake.configure(build_folder=self._build_subfolder)
60        return cmake
61
62    def build(self):
63        cmake = self._configure_cmake()
64        cmake.build()
65
66    def package(self):
67        cmake = self._configure_cmake()
68        cmake.install()
69
70        self.copy(pattern="LICENSE", dst="licenses")
71
72    def package_info(self):
73        self.cpp_info.libs = tools.collect_libs(self)
74        if self.settings.os == "Linux":
75            self.cpp_info.libs.extend(["pthread", "rt"])
76        elif self.settings.os == "Windows":
77            self.cpp_info.libs.append("shlwapi")
78        elif self.settings.os == "SunOS":
79            self.cpp_info.libs.append("kstat")
80