(#20089) protobuf: improve discovery of protoc executable in build context

* improve discovery of protoc executable in build context

* addd more comments

* fix test package
This commit is contained in:
SpaceIm
2023-12-12 21:55:54 +01:00
committed by GitHub
parent 5dc0bc6018
commit 7b258139f1
2 changed files with 19 additions and 13 deletions

View File

@@ -124,20 +124,33 @@ class ProtobufConan(ConanFile):
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\")",

View File

@@ -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 @@ class TestPackageConan(ConanFile):
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()