Skip to content

Commit

Permalink
Allow using system protobuf for shared lib builds
Browse files Browse the repository at this point in the history
Also moves the `protos` build into /protos rather than building it via
`../` inside src.
  • Loading branch information
jagerman committed Oct 4, 2023
1 parent 8a5d71c commit c3753db
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
76 changes: 53 additions & 23 deletions proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,64 @@ function(check_target target)
endif()
endfunction()

include(FetchContent)

FetchContent_Declare(
protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
GIT_TAG v3.21.12 # apparently this must be a tag (not hash) for git_shallow to work?
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
if (BUILD_SHARED_LIBS AND NOT BUILD_STATIC_DEPS)
find_package(Protobuf 3.21)
endif()

if (NOT Protobuf_FOUND)

# System protobuf not found, or we are building our own deps:
include(FetchContent)

FetchContent_Declare(
protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
GIT_TAG v3.21.12 # apparently this must be a tag (not hash) for git_shallow to work?
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)

set(protobuf_VERBOSE ON CACHE BOOL "" FORCE)
set(protobuf_INSTALL ON CACHE BOOL "" FORCE)
set(protobuf_WITH_ZLIB OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_LIBPROTOC OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(protobuf_ABSL_PROVIDER "module" CACHE STRING "" FORCE)
set(protobuf_BUILD_PROTOC_BINARIES OFF CACHE BOOL "")
set(protobuf_BUILD_PROTOBUF_BINARIES ON CACHE BOOL "" FORCE)

set(protobuf_VERBOSE ON CACHE BOOL "" FORCE)
set(protobuf_INSTALL ON CACHE BOOL "" FORCE)
set(protobuf_WITH_ZLIB OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_LIBPROTOC OFF CACHE BOOL "" FORCE)
set(protobuf_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(protobuf_ABSL_PROVIDER "module" CACHE STRING "" FORCE)
set(protobuf_BUILD_PROTOC_BINARIES OFF CACHE BOOL "")
set(protobuf_BUILD_PROTOBUF_BINARIES ON CACHE BOOL "" FORCE)
message(STATUS "Pulling protobuf repository...")

message(STATUS "Pulling protobuf repository...")
FetchContent_MakeAvailable(protobuf)

FetchContent_MakeAvailable(protobuf)
check_target(protobuf::libprotobuf-lite)

check_target(protobuf::libprotobuf-lite)
libsession_static_bundle(protobuf::libprotobuf-lite)

libsession_static_bundle(protobuf::libprotobuf-lite)
endif()

add_library(protos
SessionProtos.pb.cc
WebSocketResources.pb.cc)
target_include_directories(protos PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(protos PUBLIC protobuf::libprotobuf-lite)
set_target_properties(
protos PROPERTIES
OUTPUT_NAME session-protos
SOVERSION ${LIBSESSION_LIBVERSION})

libsession_static_bundle(protos)

add_library(libsession::protos ALIAS protos)
export(
TARGETS protos
NAMESPACE libsession::
FILE libsessionTargets.cmake
)
list(APPEND libsession_export_targets protos)
set(libsession_export_targets "${libsession_export_targets}" PARENT_SCOPE)


add_custom_target(regen-protobuf
Expand Down
18 changes: 4 additions & 14 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,7 @@ add_libsession_util_library(onionreq
onionreq/parser.cpp
)

add_libsession_util_library(protos
../proto/SessionProtos.pb.cc
../proto/WebSocketResources.pb.cc
)



target_link_libraries(protos
PUBLIC
protobuf::libprotobuf-lite
)
target_include_directories(protos PUBLIC ../proto)

target_link_libraries(crypto
PUBLIC
Expand All @@ -101,7 +90,7 @@ target_link_libraries(config
PUBLIC
crypto
common
protos
libsession::protos
PRIVATE
libsodium::sodium-internal
libzstd::static
Expand Down Expand Up @@ -163,7 +152,8 @@ endforeach()
export(
TARGETS ${export_targets} common version
NAMESPACE libsession::
FILE libsessionTargets.cmake
APPEND FILE libsessionTargets.cmake
)

set(libsession_export_targets "${export_targets}" PARENT_SCOPE)
list(APPEND libsession_export_targets ${export_targets})
set(libsession_export_targets "${libsession_export_targets}" PARENT_SCOPE)
1 change: 0 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ add_executable(testAll
target_link_libraries(testAll PRIVATE
libsession::config
libsession::onionreq
libsession::protos
libsodium::sodium-internal
Catch2::Catch2WithMain)

Expand Down

0 comments on commit c3753db

Please sign in to comment.