Skip to content

Commit

Permalink
(conan-io#20089) protobuf: improve discovery of protoc executable in …
Browse files Browse the repository at this point in the history
…build context

* improve discovery of protoc executable in build context

* addd more comments

* fix test package
  • Loading branch information
SpaceIm authored and Tony Perrie committed Dec 15, 2023
1 parent 249a272 commit f45bb18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
19 changes: 16 additions & 3 deletions recipes/protobuf/all/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\")",
Expand Down
13 changes: 3 additions & 10 deletions recipes/protobuf/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
Expand Down

0 comments on commit f45bb18

Please sign in to comment.