Wrong "protoc" binary when trying to cross-compile
Hello, i have issue compiling some proto files due to wrong pick of the protoc binary by CMake. I've got two projects, the first one is the main project which i want to cross-compile for arm64. The second project is a dependecy containining proto files and is responsible for building them. I've successfully installed protobuf conan package for both x86_64 and arm64 architectures, by specifying CMAKE_PROJECT_TOP_LEVEL_INCLUDES parameter and adding conanfile.txt, so as written in README. Both of my projects successfully resolves all the dependencies. But when it comes to generating C++ sources for protofiles, for some reason CMAKE chooses to use "protoc" which comes with android arm64 protobuf package, instead of using windows x84_64 version. I've got an idea for workaround, i can add project_b as an external project with different CMake command (at the moment it is being added as subdirectory), but i would like to keep this solution as a last resort and try to figure out a better way to do it. The way i am trying to generate proto files:
cmake_minimum_required(VERSION 3.10)
project(proto_cpp NONE)
find_package(Protobuf)
add_library(my_project_b)
protobuf_generate(
TARGET my_project_b
LANGUAGE cpp
PROTOS
FileFormat.proto
)
...
My conanfile.txt looks like that:
[requires]
openssl/3.3.5
protobuf/6.32.1
[tool_requires]
protobuf/6.32.1
[generators]
CMakeToolchain
CMakeDeps
[layout]
cmake_layout
Conan build profile:
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=15
os=Windows
Conan host profile:
include(default)
[settings]
os=Android
arch=armv8
os.api_level=27
compiler=clang
compiler.version=19
compiler.libcxx=c++_static
compiler.cppstd=20
[conf]
tools.android:ndk_path=C:\Users\MrF\AppData\Local\Android\Sdk\ndk\28.2.13676358
Build command:
%CMAKE% -S . -B %BUILD_DIR% ^
-DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK_HOME%/build/cmake/android.toolchain.cmake ^
-DANDROID_ABI=%ANDROID_ABI% ^
-DANDROID_PLATFORM=%ANDROID_PLATFORM% ^
-DBUILD_STATIC=%BUILD_STATIC% ^
-DCMAKE_BUILD_TYPE=%BUILD_TYPE% ^
-DCMAKE_MAKE_PROGRAM=%NINJA% ^
-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=cmake-conan/conan_provider.cmake ^
-DCONAN_HOST_PROFILE=android_conan_profile ^
-GNinja
The error CMake produces:
'C:\Users\MrF\.conan2\p\b\proto4217a8fc76618\p\bin\protoc' is not recognized as an internal or external command
Hi @MrF-31337
Thanks for the feedback.
I think this seems a limitation of the cmake-conan (or CMake in general) approach.
This use case should work better with the canonical flow of conan install + conanbuild.bat + cmake --preset (or in many cases the conanbuild.bat can be skipped, because the generated preset files already define environment variables.
The issue is that what CMake is launched first and driving the installation, even if Conan can install 2 different protobuf/version packages, CMake itself can only locate one of them (the host one, containing the Android libraries). But that package doesn't contain a valid protoc, it is necessary to find and use the package from the "build" context. But as CMake is already running, it is not possible to define the PATH environment variable or anything that makes that protoc.exe of the build package (tool-requires) usable.
We are working in the new CMakeConfigDeps generator that follows a different approach and might be able to inject some tool-requires executable targets (because it can also define IMPORTED EXECUTABLE targets), that might improve over this situation.
In the meantime, the recommendation for these cases with tool-requires is the canonical conan install + cmake --preset flow