diff --git a/recipes/protobuf/all/conanfile.py b/recipes/protobuf/all/conanfile.py index eb5f365e1eb56..f696a27880684 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 3823d923c4684..81404c86104a8 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()