From f45bb1899bdb15efc8bf042a65b5e332f27b66d1 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Tue, 12 Dec 2023 21:55:54 +0100 Subject: [PATCH] (#20089) protobuf: improve discovery of protoc executable in build context * improve discovery of protoc executable in build context * addd more comments * fix test package --- recipes/protobuf/all/conanfile.py | 19 ++++++++++++++++--- .../protobuf/all/test_package/conanfile.py | 13 +++---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/recipes/protobuf/all/conanfile.py b/recipes/protobuf/all/conanfile.py index eb5f365e1eb561..f696a278806848 100644 --- a/recipes/protobuf/all/conanfile.py +++ b/recipes/protobuf/all/conanfile.py @@ -124,20 +124,33 @@ def _patch_sources(self): 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("""\ + protoc_target = textwrap.dedent(f"""\ if(NOT TARGET protobuf::protoc) + # Locate protoc executable + ## Workaround for legacy "cmake" generator in case of cross-build if(CMAKE_CROSSCOMPILING) - find_program(PROTOC_PROGRAM protoc PATHS ENV PATH NO_DEFAULT_PATH) + find_program(PROTOC_PROGRAM NAMES protoc PATHS ENV PATH NO_DEFAULT_PATH) endif() + ## And here this will work fine with "CMakeToolchain" (for native & cross-build) + ## and legacy "cmake" generator in case of native build + if(NOT PROTOC_PROGRAM) + find_program(PROTOC_PROGRAM NAMES protoc) + endif() + ## Last resort: we search in package folder directly if(NOT PROTOC_PROGRAM) set(PROTOC_PROGRAM \"${{CMAKE_CURRENT_LIST_DIR}}/{protoc_rel_path}\") endif() get_filename_component(PROTOC_PROGRAM \"${{PROTOC_PROGRAM}}\" ABSOLUTE) + + # Give opportunity to users to provide an external protoc executable + # (this is a feature of official FindProtobuf.cmake) set(Protobuf_PROTOC_EXECUTABLE ${{PROTOC_PROGRAM}} CACHE FILEPATH \"The protoc compiler\") + + # Create executable imported target protobuf::protoc add_executable(protobuf::protoc IMPORTED) set_property(TARGET protobuf::protoc PROPERTY IMPORTED_LOCATION ${{Protobuf_PROTOC_EXECUTABLE}}) endif() - """.format(protoc_rel_path=protoc_rel_path)) + """) replace_in_file(self, protobuf_config_cmake, "include(\"${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake\")", diff --git a/recipes/protobuf/all/test_package/conanfile.py b/recipes/protobuf/all/test_package/conanfile.py index 3823d923c46842..81404c86104a87 100644 --- a/recipes/protobuf/all/test_package/conanfile.py +++ b/recipes/protobuf/all/test_package/conanfile.py @@ -1,13 +1,12 @@ from conan import ConanFile -from conan.tools.build import can_run, cross_building +from conan.tools.build import can_run from conan.tools.cmake import cmake_layout, CMake, CMakeToolchain -from conan.tools.env import VirtualBuildEnv, VirtualRunEnv import os class TestPackageConan(ConanFile): settings = "os", "arch", "compiler", "build_type" - generators = "CMakeDeps" + generators = "CMakeDeps", "VirtualBuildEnv", "VirtualRunEnv" test_type = "explicit" def layout(self): @@ -17,15 +16,9 @@ def requirements(self): self.requires(self.tested_reference_str) def build_requirements(self): - if cross_building(self) and hasattr(self, "settings_build"): - self.tool_requires(self.tested_reference_str) + self.tool_requires(self.tested_reference_str) def generate(self): - VirtualRunEnv(self).generate() - if cross_building(self) and hasattr(self, "settings_build"): - VirtualBuildEnv(self).generate() - else: - VirtualRunEnv(self).generate(scope="build") tc = CMakeToolchain(self) tc.cache_variables["protobuf_LITE"] = self.dependencies[self.tested_reference_str].options.lite tc.generate()