protobuf using pkgconfig, how to generate files into a target build tree?

Hi, due to project requirement only pkgconfig third party packages can be used, and cmake find_package(Protobuf) and protobuf_generate_cpp() does not work when using pkgconfig-target defined protobuf.

How do i make a target with protoc generated header files and export a link to its build tree generated files? And how do i make the generator run before depent target and only run when .proto file is changed?

I have below design idea that i need help with.

add_library(proto_def)

add_custom_target(proto_def-out ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/out
COMMENT “Creates protobuf sources: ${CMAKE_BINARY_DIR}/out”
#WORKING_DIRECTORY “${_BINARY_DIR}”
COMMAND protoc --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/out .proto
)

add_custom_command(OUTPUT
COMMAND protoc --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/out
COMMENT “Generating proto headers.”
VERBATIM
)

TARGET_SOURCES(proto_def PUBLIC
“Some gen expression pointing to build tree target proto gen out files”
)

add_dependencies(proto_def-out proto_def)
target_link_libraries(proto_def-out proto-pkgconfig)

This is the parent and works.

add_library(proto-pkgconfig)

set_target_properties (proto-pkgconfig PROPERTIES
        EXPORT_COMPILE_COMMANDS ON
        DEBUG_POSTFIX _debug
)

target_compile_features(proto-pkgconfig PRIVATE cxx_std_23)

target_sources(proto-pkgconfig
        PUBLIC
        proto.cpp
        PUBLIC
        FILE_SET HEADERS
)

find_package(PkgConfig REQUIRED)
pkg_check_modules(${PROJECT_NAME}-protoPkgconfig REQUIRED IMPORTED_TARGET protobuf)
target_link_libraries(proto-pkgconfig PRIVATE PkgConfig::${PROJECT_NAME}-protoPkgconfig)

add_subdirectory(proto_def)