Skip to content

Commit

Permalink
Build: protoc in cross compile case
Browse files Browse the repository at this point in the history
  • Loading branch information
KerstinKeller authored Nov 2, 2023
1 parent aaf57c1 commit 1248a4d
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions thirdparty/build-protobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -46,5 +50,4 @@ if (NOT is_imported)
)
endif()

set(Protobuf_PROTOC_EXECUTABLE protoc)
set(Protobuf_VERSION 3.11.4)
set(Protobuf_VERSION 3.11.4)

0 comments on commit 1248a4d

Please sign in to comment.