|
| 1 | +from conans import ConanFile, CMake, tools |
| 2 | +from conans.errors import ConanInvalidConfiguration |
| 3 | +import shutil |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +class 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 | + "enable_gtest_tests": [True, False] |
| 25 | + } |
| 26 | + default_options = {"shared": False, "fPIC": True, "enable_lto": False, "enable_exceptions": True, "enable_gtest_tests": False} |
| 27 | + |
| 28 | + _build_subfolder = "." |
| 29 | + |
| 30 | + def source(self): |
| 31 | + # Wrap the original CMake file to call conan_basic_setup |
| 32 | + shutil.move("CMakeLists.txt", "CMakeListsOriginal.txt") |
| 33 | + shutil.move(os.path.join("conan", "CMakeLists.txt"), "CMakeLists.txt") |
| 34 | + |
| 35 | + def config_options(self): |
| 36 | + if self.settings.os == "Windows": |
| 37 | + if self.settings.compiler == "Visual Studio" and float(self.settings.compiler.version.value) <= 12: |
| 38 | + raise ConanInvalidConfiguration("{} {} does not support Visual Studio <= 12".format(self.name, self.version)) |
| 39 | + del self.options.fPIC |
| 40 | + |
| 41 | + if tools.get_env("CONAN_RUN_TESTS") != "True": |
| 42 | + self.output.info("Environment variable CONAN_RUN_TEST is not defined.") |
| 43 | + self.output.info("Tests are disabled and the option enable_gtest_tests isn't available.") |
| 44 | + del self.options.enable_gtest_tests |
| 45 | + |
| 46 | + def configure(self): |
| 47 | + if self.settings.os == "Windows" and self.options.shared: |
| 48 | + raise ConanInvalidConfiguration("Windows shared builds are not supported right now, see issue #639") |
| 49 | + |
| 50 | + def _configure_cmake(self): |
| 51 | + cmake = CMake(self) |
| 52 | + |
| 53 | + if tools.get_env("CONAN_RUN_TESTS"): |
| 54 | + cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "ON" |
| 55 | + cmake.definitions["BENCHMARK_ENABLE_GTEST_TESTS"] = "ON" if self.options.enable_gtest_tests else "OFF" |
| 56 | + else: |
| 57 | + cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "OFF" |
| 58 | + cmake.definitions["BENCHMARK_ENABLE_GTEST_TESTS"] = "OFF" |
| 59 | + |
| 60 | + cmake.definitions["BENCHMARK_ENABLE_LTO"] = "ON" if self.options.enable_lto else "OFF" |
| 61 | + cmake.definitions["BENCHMARK_ENABLE_EXCEPTIONS"] = "ON" if self.options.enable_exceptions else "OFF" |
| 62 | + |
| 63 | + # See https://github.com/google/benchmark/pull/638 for Windows 32 build explanation |
| 64 | + if self.settings.os != "Windows": |
| 65 | + cmake.definitions["BENCHMARK_BUILD_32_BITS"] = "ON" if "64" not in str(self.settings.arch) else "OFF" |
| 66 | + cmake.definitions["BENCHMARK_USE_LIBCXX"] = "ON" if (str(self.settings.compiler.libcxx) == "libc++") else "OFF" |
| 67 | + else: |
| 68 | + cmake.definitions["BENCHMARK_USE_LIBCXX"] = "OFF" |
| 69 | + |
| 70 | + cmake.configure(build_folder=self._build_subfolder) |
| 71 | + return cmake |
| 72 | + |
| 73 | + def build_requirements(self): |
| 74 | + if tools.get_env("CONAN_RUN_TESTS") and self.options.enable_gtest_tests: |
| 75 | + self.build_requires("gtest/1.8.0@bincrafters/stable") |
| 76 | + |
| 77 | + def build(self): |
| 78 | + cmake = self._configure_cmake() |
| 79 | + cmake.build() |
| 80 | + |
| 81 | + def package(self): |
| 82 | + cmake = self._configure_cmake() |
| 83 | + cmake.install() |
| 84 | + |
| 85 | + self.copy(pattern="LICENSE", dst="licenses") |
| 86 | + |
| 87 | + def package_info(self): |
| 88 | + self.cpp_info.libs = tools.collect_libs(self) |
| 89 | + if self.settings.os == "Linux": |
| 90 | + self.cpp_info.libs.extend(["pthread", "rt"]) |
| 91 | + elif self.settings.os == "Windows": |
| 92 | + self.cpp_info.libs.append("shlwapi") |
| 93 | + elif self.settings.os == "SunOS": |
| 94 | + self.cpp_info.libs.append("kstat") |
| 95 | + |
| 96 | + def package_id(self): |
| 97 | + # Development options don't change the binary output |
| 98 | + # from a user point of view; overwrite id information |
| 99 | + # so that the package_id is the same |
| 100 | + # if only the development options are different |
| 101 | + del self.info.options.enable_gtest_tests |
0 commit comments