Skip to content

Commit

Permalink
Try using non-legacy protobuf finding method on modern protobuf insta…
Browse files Browse the repository at this point in the history
…llations
  • Loading branch information
em-eight committed Sep 16, 2023
1 parent 94bf4a7 commit f2159e7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 62 deletions.
61 changes: 39 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include> # <prefix>/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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include> # <prefix>/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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include> # <prefix>/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 #################################################################

Expand Down
40 changes: 0 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit f2159e7

Please sign in to comment.