From 9dd3b2f2f3de796ccbe6df86e5149e3573fa1e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Mon, 22 Jul 2019 21:18:14 +0200 Subject: [PATCH] Use bundled radare2 as ExternalProject in cmake (#1674) --- .travis.yml | 32 +++++++++-------- scripts/Brewfile | 1 + src/CMakeLists.txt | 29 ++++++--------- src/cmake/BundledRadare2.cmake | 38 ++++++++++++++++++++ src/cmake/FindRadare2.cmake | 64 +++++++++++++++++++++++----------- 5 files changed, 111 insertions(+), 53 deletions(-) create mode 100644 src/cmake/BundledRadare2.cmake diff --git a/.travis.yml b/.travis.yml index c602bc4a..e6e044cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,28 +14,29 @@ matrix: - name: Linux QMake + Deploy os: linux env: BUILD_SYSTEM=qmake DEPLOY=1 + - name: Linux CMake os: linux env: BUILD_SYSTEM=cmake + before_install: + - sudo apt-get update && sudo apt-get install ninja-build # because the apt addon is broken on bionic + - pyenv global 3.7.1 + - pip install meson + - name: macOS QMake + Deploy os: osx osx_image: xcode10.1 env: BUILD_SYSTEM=qmake DEPLOY=1 + - name: macOS CMake os: osx osx_image: xcode10.1 env: BUILD_SYSTEM=cmake + - name: Documentation + Deploy os: linux cache: ~ - addons: - apt: - update: true - packages: - - doxygen - - python3-sphinx - - python3-breathe - before_install: ~ + before_install: sudo apt-get update && sudo apt-get install doxygen python3-sphinx python3-breathe python3-sphinx-rtd-theme # because the apt addon is broken on bionic install: ~ before_script: ~ after_success: ~ @@ -59,7 +60,7 @@ matrix: sudo: required -dist: xenial +dist: bionic addons: homebrew: @@ -79,12 +80,13 @@ install: fi before_script: - - git submodule init ; git submodule update - cd radare2 - - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then - INSTALL_TARGET=install sys/install.sh; - else - LDFLAGS=-headerpad_max_install_names INSTALL_TARGET=install sys/install.sh; + - if [[ "$BUILD_SYSTEM" == "qmake" ]]; then + if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + INSTALL_TARGET=install sys/install.sh; + else + LDFLAGS=-headerpad_max_install_names INSTALL_TARGET=install sys/install.sh; + fi fi - cd .. @@ -111,6 +113,7 @@ script: -DCUTTER_ENABLE_PYTHON=ON -DCUTTER_ENABLE_PYTHON_BINDINGS=ON -DCUTTER_ENABLE_CRASH_REPORTS=ON + -DCUTTER_USE_BUNDLED_RADARE2=ON ../src && make -j4; fi @@ -134,6 +137,7 @@ script: -DCUTTER_ENABLE_PYTHON=ON -DCUTTER_ENABLE_PYTHON_BINDINGS=ON -DCUTTER_ENABLE_CRASH_REPORTS=ON + -DCUTTER_USE_BUNDLED_RADARE2=ON -DBREAKPAD_FRAMEWORK_DIR="$BREAKPAD_FRAMEWORK_DIR" ../src && make -j4; diff --git a/scripts/Brewfile b/scripts/Brewfile index da9b4cc9..dc80bdbc 100644 --- a/scripts/Brewfile +++ b/scripts/Brewfile @@ -3,3 +3,4 @@ brew "ccache" brew "openssl" brew "xz" brew "llvm" +brew "meson" \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f863b1cf..06ed5e86 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,6 +11,7 @@ include(Utils) set(CUTTER_PYTHON_MIN 3.5) +option(CUTTER_USE_BUNDLED_RADARE2 "Use radare2 from src/radare2 submodule instead of searching for it on the system" OFF) option(CUTTER_ENABLE_PYTHON "Enable Python integration. Requires Python >= ${CUTTER_PYTHON_MIN}." OFF) option(CUTTER_ENABLE_PYTHON_BINDINGS "Enable generating Python bindings with Shiboken2. Unused if CUTTER_ENABLE_PYTHON=OFF." OFF) option(CUTTER_ENABLE_CRASH_REPORTS "Enable crash report system. Unused if CUTTER_ENABLE_CRASH_REPORTS=OFF" OFF) @@ -49,26 +50,14 @@ set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Svg Network) - -if(WIN32) - # use radare2 libraries from submodule on windows - set(CUTTER_WIN32_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../cutter_win32") - list(APPEND CMAKE_PREFIX_PATH "${CUTTER_WIN32_DIR}") - - if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64 bit - list(APPEND CMAKE_LIBRARY_PATH "${CUTTER_WIN32_DIR}/radare2/lib64") - else() - list(APPEND CMAKE_LIBRARY_PATH "${CUTTER_WIN32_DIR}/radare2/lib32") - endif() - - set(RADARE2_INCLUDE_DIRS "${CUTTER_WIN32_DIR}/radare2/include/libr" "${CUTTER_WIN32_DIR}/include") +if(CUTTER_USE_BUNDLED_RADARE2) + include(BundledRadare2) + set(RADARE2_TARGET Radare2) +else() + find_package(Radare2 REQUIRED) + set(RADARE2_TARGET Radare2::libr) endif() - -find_package(Radare2 REQUIRED) -include_directories(${RADARE2_INCLUDE_DIRS}) -link_directories(${RADARE2_LIBRARY_DIRS}) - if(CUTTER_ENABLE_PYTHON) find_package(PythonLibs ${CUTTER_PYTHON_MIN} REQUIRED) @@ -118,6 +107,7 @@ endif() message(STATUS "") message(STATUS "Building Cutter version ${CUTTER_VERSION_FULL}") message(STATUS "Options:") +message(STATUS "- Bundled radare2: ${CUTTER_USE_BUNDLED_RADARE2}") message(STATUS "- Python: ${CUTTER_ENABLE_PYTHON}") message(STATUS "- Python Bindings: ${CUTTER_ENABLE_PYTHON_BINDINGS}") message(STATUS "- Crash Handling: ${CUTTER_ENABLE_CRASH_REPORTS}") @@ -182,7 +172,7 @@ if(CUTTER_ENABLE_CRASH_REPORTS) endif() target_link_libraries(Cutter Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Svg Qt5::Network) -target_link_libraries(Cutter ${RADARE2_LIBRARIES}) +target_link_libraries(Cutter ${RADARE2_TARGET}) if(CUTTER_ENABLE_PYTHON) target_link_libraries(Cutter ${PYTHON_LIBRARIES}) if(CUTTER_ENABLE_PYTHON_BINDINGS) @@ -196,6 +186,7 @@ if(CUTTER_ENABLE_PYTHON) list(APPEND BINDINGS_INCLUDE_DIRS "${_qt5Core_install_prefix}/include/QtWidgets") endif() list(APPEND BINDINGS_INCLUDE_DIRS ${Qt5Core_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS}) + list(APPEND BINDINGS_INCLUDE_DIRS ${Radare2_INCLUDE_DIRS}) list(APPEND BINDINGS_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}") string(REPLACE ";" ":" BINDINGS_INCLUDE_DIRS "${BINDINGS_INCLUDE_DIRS}") diff --git a/src/cmake/BundledRadare2.cmake b/src/cmake/BundledRadare2.cmake new file mode 100644 index 00000000..a272bec5 --- /dev/null +++ b/src/cmake/BundledRadare2.cmake @@ -0,0 +1,38 @@ + +include(ExternalProject) + +set(RADARE2_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../radare2") +set(RADARE2_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/Radare2-prefix") +set(MESON_OPTIONS "--prefix=${RADARE2_INSTALL_DIR}" --libdir=lib) + +find_program(MESON meson) +if(NOT MESON) + message(FATAL_ERROR "Failed to find meson, which is required to build bundled radare2") +endif() + +find_program(NINJA ninja) +if(NOT NINJA) + message(FATAL_ERROR "Failed to find ninja, which is required to build bundled radare2") +endif() + +ExternalProject_Add(Radare2-Bundled + SOURCE_DIR "${RADARE2_SOURCE_DIR}" + INSTALL_DIR "${RADARE2_INSTALL_DIR}" + CONFIGURE_COMMAND "${MESON}" "${RADARE2_SOURCE_DIR}" ${MESON_OPTIONS} && "${MESON}" configure ${MESON_OPTIONS} + BUILD_COMMAND "${NINJA}" + INSTALL_COMMAND "${NINJA}" install) + +set(Radare2_INCLUDE_DIRS "${RADARE2_INSTALL_DIR}/include/libr") + +add_library(Radare2 INTERFACE) +add_dependencies(Radare2 Radare2-Bundled) +if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(Radare2 INTERFACE "${RADARE2_INSTALL_DIR}/lib") +else() + link_directories("${RADARE2_INSTALL_DIR}/lib") +endif() +target_link_libraries(Radare2 INTERFACE + r_core r_config r_cons r_io r_util r_flag r_asm r_debug + r_hash r_bin r_lang r_io r_anal r_parse r_bp r_egg r_reg + r_search r_syscall r_socket r_fs r_magic r_crypto) +target_include_directories(Radare2 INTERFACE "${Radare2_INCLUDE_DIRS}") diff --git a/src/cmake/FindRadare2.cmake b/src/cmake/FindRadare2.cmake index db9a0789..e543ef9b 100644 --- a/src/cmake/FindRadare2.cmake +++ b/src/cmake/FindRadare2.cmake @@ -1,22 +1,29 @@ # - Find Radare2 (libr) # -# RADARE2_FOUND - True if libr has been found. -# RADARE2_INCLUDE_DIRS - libr include directory -# RADARE2_LIBRARIES - List of libraries when using libr. -# RADARE2_LIBRARY_DIRS - libr library directories +# This module provides the following imported targets, if found: +# +# Radare2::libr +# +# This will define the following variables: +# (but don't use them if you don't know what you are doing, use Radare2::libr) +# +# Radare2_FOUND - True if libr has been found. +# Radare2_INCLUDE_DIRS - libr include directory +# Radare2_LIBRARIES - List of libraries when using libr. +# Radare2_LIBRARY_DIRS - libr library directories # # If libr was found using find_library and not pkg-config, the following variables will also be set: -# RADARE2_LIBRARY_ - Path to library r_ +# Radare2_LIBRARY_ - Path to library r_ if(WIN32) - find_path(RADARE2_INCLUDE_DIRS + find_path(Radare2_INCLUDE_DIRS NAMES r_core.h r_bin.h r_util.h HINTS "$ENV{HOME}/bin/prefix/radare2/include/libr" /usr/local/include/libr /usr/include/libr) - set(RADARE2_LIBRARY_NAMES + set(Radare2_LIBRARY_NAMES core config cons @@ -41,35 +48,52 @@ if(WIN32) magic crypto) - set(RADARE2_LIBRARIES "") - set(RADARE2_LIBRARIES_VARS "") - foreach(libname ${RADARE2_LIBRARY_NAMES}) - find_library(RADARE2_LIBRARY_${libname} + set(Radare2_LIBRARIES "") + set(Radare2_LIBRARIES_VARS "") + foreach(libname ${Radare2_LIBRARY_NAMES}) + find_library(Radare2_LIBRARY_${libname} r_${libname} HINTS "$ENV{HOME}/bin/prefix/radare2/lib" /usr/local/lib /usr/lib) - list(APPEND RADARE2_LIBRARIES ${RADARE2_LIBRARY_${libname}}) - list(APPEND RADARE2_LIBRARIES_VARS "RADARE2_LIBRARY_${libname}") + list(APPEND Radare2_LIBRARIES ${Radare2_LIBRARY_${libname}}) + list(APPEND Radare2_LIBRARIES_VARS "Radare2_LIBRARY_${libname}") endforeach() - set(RADARE2_LIBRARY_DIRS "") + set(Radare2_LIBRARY_DIRS "") + + add_library(Radare2::libr UNKNOWN IMPORTED) + set_target_properties(Radare2::libr PROPERTIES + IMPORTED_LOCATION "${Radare2_CORE_LIBRARY}" + IMPORTED_LINK_INTERFACE_LIBRARIES "${Radare2_LIBRARIES}" + INTERFACE_LINK_DIRECTORIES "${Radare2_LIBRARY_DIRS}" + INTERFACE_INCLUDE_DIRECTORIES "${Radare2_INCLUDE_DIRS}") + set(Radare2_TARGET Radare2::libr) else() # support sys/user.sh install - set(RADARE2_CMAKE_PREFIX_PATH_TEMP ${CMAKE_PREFIX_PATH}) + set(Radare2_CMAKE_PREFIX_PATH_TEMP ${CMAKE_PREFIX_PATH}) list(APPEND CMAKE_PREFIX_PATH "$ENV{HOME}/bin/prefix/radare2") find_package(PkgConfig REQUIRED) - pkg_search_module(RADARE2 REQUIRED r_core) + pkg_search_module(Radare2 IMPORTED_TARGET REQUIRED r_core) # reset CMAKE_PREFIX_PATH - set(CMAKE_PREFIX_PATH ${RADARE2_CMAKE_PREFIX_PATH_TEMP}) - mark_as_advanced(RADARE2_CMAKE_PREFIX_PATH_TEMP) + set(CMAKE_PREFIX_PATH ${Radare2_CMAKE_PREFIX_PATH_TEMP}) + mark_as_advanced(Radare2_CMAKE_PREFIX_PATH_TEMP) + + if(TARGET PkgConfig::Radare2) + set_target_properties(PkgConfig::Radare2 PROPERTIES IMPORTED_GLOBAL ON) + add_library(Radare2::libr ALIAS PkgConfig::Radare2) + set(Radare2_TARGET Radare2::libr) + else() + set(Radare2_TARGET Radare2_TARGET-NOTFOUND) + endif() + endif() include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(RADARE2 REQUIRED_VARS RADARE2_LIBRARIES RADARE2_INCLUDE_DIRS) +find_package_handle_standard_args(Radare2 REQUIRED_VARS Radare2_TARGET Radare2_LIBRARIES Radare2_INCLUDE_DIRS) -mark_as_advanced(RADARE2_LIBRARIES_VARS) +mark_as_advanced(Radare2_LIBRARIES_VARS Radare2_CORE_LIBRARY Radare2_TARGET)