diff --git a/thirdparty/build-protobuf.cmake b/thirdparty/build-protobuf.cmake index fc41247d19..93fbffd761 100644 --- a/thirdparty/build-protobuf.cmake +++ b/thirdparty/build-protobuf.cmake @@ -3,6 +3,8 @@ set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "My option" FORCE) if(UNIX) set(protobuf_BUILD_SHARED_LIBS ON CACHE BOOL "My option" FORCE) endif() +set(Protobuf_PROTOC_EXECUTABLE "" CACHE FILEPATH "Path to protoc executable") + add_subdirectory(thirdparty/protobuf/cmake) if (NOT TARGET protobuf::libprotobuf) @@ -18,20 +20,22 @@ if (NOT is_imported) ) endif() -if (NOT TARGET protoc AND CMAKE_CROSSCOMPILING) +if (CMAKE_CROSSCOMPILING) # In cross compile scenario, we don't build protoc, so we need # to find and import it so downstream usage will use the host compiler. # find_program will search the host paths if CMAKE_FIND_ROOT_PATH_MODE_PROGRAM # is configured correctly (see toolchain file) - find_program(protoc_path NAMES protoc REQUIRED) - add_executable(protoc IMPORTED) - set_target_properties(protoc PROPERTIES IMPORTED_LOCATION ${protoc_path}) - set_target_properties(protoc PROPERTIES IMPORTED_GLOBAL TRUE) + if (NOT Protobuf_PROTOC_EXECUTABLE) + find_program(Protobuf_PROTOC_EXECUTABLE NAMES protoc_host REQUIRED) + endif () + add_executable(protoc_host IMPORTED) + set_target_properties(protoc_host PROPERTIES IMPORTED_LOCATION ${Protobuf_PROTOC_EXECUTABLE}) + set_target_properties(protoc_host PROPERTIES IMPORTED_GLOBAL TRUE) - add_executable(protobuf::protoc ALIAS protoc) + add_executable(protobuf::protoc ALIAS protoc_host) - message(STATUS "protoc has been automatically imported from ${protoc_path} for crosscompiling.") + message(STATUS "protoc has been set to ${Protobuf_PROTOC_EXECUTABLE} for crosscompiling.") elseif(NOT TARGET protobuf::protoc) add_executable(protobuf::protoc ALIAS protoc) @@ -46,5 +50,4 @@ if (NOT is_imported) ) endif() -set(Protobuf_PROTOC_EXECUTABLE protoc) -set(Protobuf_VERSION 3.11.4) \ No newline at end of file +set(Protobuf_VERSION 3.11.4)