Build protoc for host architecture during cross-compilation

This commit is contained in:
Alexey Milovidov 2021-10-12 05:46:19 +03:00
parent 84e16f9c8e
commit 5ba876cad2
3 changed files with 52 additions and 5 deletions

View File

@ -187,12 +187,12 @@ function(protobuf_generate_grpc)
add_custom_command(
OUTPUT ${_generated_srcs}
COMMAND $<TARGET_FILE:protobuf::protoc>
COMMAND $<TARGET_FILE:protoc>
ARGS --${protobuf_generate_grpc_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_grpc_PROTOC_OUT_DIR}
--grpc_out ${_dll_export_decl}${protobuf_generate_grpc_PROTOC_OUT_DIR}
--plugin=protoc-gen-grpc=$<TARGET_FILE:${protobuf_generate_grpc_PLUGIN}>
${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
DEPENDS ${_abs_file} protobuf::protoc ${protobuf_generate_grpc_PLUGIN}
DEPENDS ${_abs_file} protoc ${protobuf_generate_grpc_PLUGIN}
COMMENT "Running ${protobuf_generate_grpc_LANGUAGE} protocol buffer compiler on ${_proto}"
VERBATIM)
endforeach()

View File

@ -10,8 +10,55 @@ else ()
set(protobuf_BUILD_SHARED_LIBS ON CACHE INTERNAL "" FORCE)
endif ()
if (CMAKE_CROSSCOMPILING)
# Will build 'protoc' for host arch instead of cross-compiling
set(protobuf_BUILD_PROTOC_BINARIES OFF CACHE INTERNAL "" FORCE)
endif ()
add_subdirectory("${protobuf_SOURCE_DIR}/cmake" "${protobuf_BINARY_DIR}")
# We don't want to stop compilation on warnings in protobuf's headers.
# The following line overrides the value assigned by the command target_include_directories() in libprotobuf.cmake
# The following line overrides the value assigned by the command target_include_directories() in libprotobuf.cmake
set_property(TARGET libprotobuf PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${protobuf_SOURCE_DIR}/src")
if (CMAKE_CROSSCOMPILING)
# Build 'protoc' for host arch
set (PROTOC_BUILD_DIR "${protobuf_BINARY_DIR}/build")
add_custom_command (
OUTPUT ${PROTOC_BUILD_DIR}
COMMAND mkdir -p ${PROTOC_BUILD_DIR})
add_custom_command (
OUTPUT "${PROTOC_BUILD_DIR}/CMakeCache.txt"
COMMAND ${CMAKE_COMMAND}
-G"${CMAKE_GENERATOR}"
-DCMAKE_MAKE_PROGRAM="${CMAKE_MAKE_PROGRAM}"
-DCMAKE_C_COMPILER="${CMAKE_C_COMPILER}"
-DCMAKE_CXX_COMPILER="${CMAKE_CXX_COMPILER}"
-Dprotobuf_BUILD_TESTS=0
-Dprotobuf_BUILD_CONFORMANCE=0
-Dprotobuf_BUILD_EXAMPLES=0
-Dprotobuf_BUILD_PROTOC_BINARIES=1
"${protobuf_SOURCE_DIR}/cmake"
DEPENDS "${PROTOC_BUILD_DIR}"
WORKING_DIRECTORY "${PROTOC_BUILD_DIR}"
COMMENT "Configuring 'protoc' for host architecture.")
add_custom_command (
OUTPUT "${PROTOC_BUILD_DIR}/protoc"
COMMAND ${CMAKE_COMMAND} --build "${PROTOC_BUILD_DIR}"
DEPENDS "${PROTOC_BUILD_DIR}/CMakeCache.txt"
COMMENT "Building 'protoc' for host architecture.")
#add_custom_target (protoc DEPENDS "${PROTOC_BUILD_DIR}/protoc")
#set_target_properties (protoc PROPERTIES TARGET_FILE "${PROTOC_BUILD_DIR}/protoc")
add_executable(protoc IMPORTED GLOBAL)
set_target_properties (protoc PROPERTIES IMPORTED_LOCATION "${PROTOC_BUILD_DIR}/protoc")
add_dependencies(protoc "${PROTOC_BUILD_DIR}/protoc")
#add_executable(protobuf::protoc ALIAS protoc)
endif ()

View File

@ -181,9 +181,9 @@ function(protobuf_generate)
add_custom_command(
OUTPUT ${_generated_srcs}
COMMAND $<TARGET_FILE:protobuf::protoc>
COMMAND $<TARGET_FILE:protoc>
ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
DEPENDS ${_abs_file} protobuf::protoc
DEPENDS ${_abs_file} protoc
COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
VERBATIM)
endforeach()