From f2159e7758ca1fb721882e1b5e8059ed1b0e9a52 Mon Sep 17 00:00:00 2001 From: em-eight Date: Sat, 16 Sep 2023 20:49:28 +0300 Subject: [PATCH] Try using non-legacy protobuf finding method on modern protobuf installations --- CMakeLists.txt | 61 ++++++++++++++++++++++++++++++++------------------ README.md | 40 --------------------------------- 2 files changed, 39 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f43539c..53de279 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,37 +46,54 @@ CPMAddPackage("gh:fmtlib/fmt#10.0.0") # could either specify a static or shared library. Depending on either, # we need to correctly set the link mode before calling find_package. # Newer versions of Protobuf depend on Abseil. -if(DEFINED Protobuf_LIBRARY) - if(Protobuf_LIBRARY MATCHES "\.a$") - set(Protobuf_USE_STATIC_LIBS ON) - else() - set(Protobuf_USE_STATIC_LIBS OFF) - endif() -else() - set(Protobuf_USE_STATIC_LIBS ON) # Use static libs by default +set(Protobuf_USE_STATIC_LIBS ON) + +find_package(protobuf CONFIG) +if(NOT protobuf_FOUND) # Legacy protobuf support + message(STATUS "Trying legacy method of finding protobuf") + set(protobuf_MODULE_COMPATIBLE ON) + include(FindProtobuf) + find_package(Protobuf REQUIRED) endif() - -include(FindProtobuf) -find_package(Protobuf REQUIRED) include_directories(${Protobuf_INCLUDE_DIRS}) # Generate Protobuf files # This is provided by FindProtobuf.cmake -file(GLOB PROTO_FILES "${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto") -protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES}) +set(PROTO_DIR "${CMAKE_CURRENT_SOURCE_DIR}/proto") +file(GLOB PROTO_FILES "${PROTO_DIR}/*.proto") +if(protobuf_MODULE_COMPATIBLE) + protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES}) +endif() # ppc2cpp core ################################################################# file(GLOB_RECURSE ppc2cpp_core_SRC "src/*.cpp") -add_library(ppc2cpp_core ${ppc2cpp_core_SRC} ${PROTO_SRCS} ${PROTO_HDRS} ) -target_include_directories(ppc2cpp_core PUBLIC - $ - $ # /include - ${YAML_CPP_SOURCE_DIR}/include - # protobuf_generate_cpp drops .pb.{h.c} files here. - ${CMAKE_CURRENT_BINARY_DIR} -) -target_link_libraries(ppc2cpp_core PUBLIC dolrel ppcdisasm elfio fmt ${Protobuf_LIBRARIES} yaml-cpp) + +if(protobuf_MODULE_COMPATIBLE) + add_library(ppc2cpp_core ${ppc2cpp_core_SRC} ${PROTO_SRCS} ${PROTO_HDRS} ) + target_include_directories(ppc2cpp_core PUBLIC + $ + $ # /include + ${YAML_CPP_SOURCE_DIR}/include + # protobuf_generate_cpp drops .pb.{h.c} files here. + ${CMAKE_CURRENT_BINARY_DIR} + ) + target_link_libraries(ppc2cpp_core PUBLIC dolrel ppcdisasm elfio fmt ${Protobuf_LIBRARIES} yaml-cpp) +else() + add_library(ppc2cpp_core ${ppc2cpp_core_SRC} ${PROTO_FILES}) + target_include_directories(ppc2cpp_core PUBLIC + $ + $ # /include + ${YAML_CPP_SOURCE_DIR}/include + # protobuf_generate_cpp drops .pb.{h.c} files here. + ${CMAKE_CURRENT_BINARY_DIR} + ) + target_link_libraries(ppc2cpp_core PUBLIC dolrel ppcdisasm elfio fmt protobuf::libprotobuf yaml-cpp) + protobuf_generate( + TARGET ppc2cpp_core + IMPORT_DIRS "${PROTO_DIR}" + ) +endif() # ppc2cpp CLI ################################################################# diff --git a/README.md b/README.md index e90a6e9..f85e139 100644 --- a/README.md +++ b/README.md @@ -22,43 +22,3 @@ Compare the equivalence between the version of a function in two ppc2cpp project Import symbol and relocation information from ppcdis ### ppc2pp compare (experimental) Compare two ppc2cpp programs. This takes a symbol and reference based approach, which allows symbols to be rearranged arbitrarily. This requires symbol name, type and size information, as well as relocations. - -## Troubleshooting - -### CMake issues - -This project uses CMake's builtin FindProtobuf module which is supposed -to locate your Protobuf library and compiler installation. Recent -versions of Protobuf broke the versioning scheme, resulting in a CMake -error (e.g. Protobuf 23.4 and CMake 3.26.4 won't work together). - -If you get an error like this ... - -``` -CMake Warning at /opt/homebrew/Cellar/cmake/3.26.4/share/cmake/Modules/FindProtobuf.cmake:524 (message): - Protobuf compiler version 23.4 doesn't match library version 4.23.4 -Call Stack (most recent call first): - CMakeLists.txt:33 (find_package) - - -CMake Error at /opt/homebrew/Cellar/cmake/3.26.4/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message): - Could NOT find Protobuf (missing: Protobuf_LIBRARIES) (found version - "4.23.4") -Call Stack (most recent call first): - /opt/homebrew/Cellar/cmake/3.26.4/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE) - /opt/homebrew/Cellar/cmake/3.26.4/share/cmake/Modules/FindProtobuf.cmake:650 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) - CMakeLists.txt:33 (find_package) -``` - -... work around this issue by defining CMake build variables manually -specifying protoc and Protobuf link flags. In the example below, we -use `pkg-config` to gather the correct link flags. - -``` -cmake -B build \ - -DProtobuf_PROTOC_EXECUTABLE="$(which protoc)" \ - -DProtobuf_LIBRARY="$(pkg-config --libs protobuf)" -``` - -If all of the above still doesn't work, sometimes it helps to delete -your CMake cache.