From 62d75c9391b89fb5ff67ae9438f876a6c79df700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Thu, 31 Mar 2022 12:49:37 +0200 Subject: [PATCH] Extend cmake config files with deps, version, etc. (#2916) This improves how the installed CutterConfig.cmake can be used for native plugin development: * Transitive dependencies are resolved automatically * Version file is included * Cutter_USER_PLUGINDIR is set to a path that plugins can use as a default install destination src/plugins/sample-cpp/CMakeLists.txt is an example for how to use it. Rizin was also updated to prevent an error with multiple `find_package()` calls. --- CMakeLists.txt | 8 +++--- cmake/BundledRizin.cmake | 3 ++- cmake/CutterConfig.cmake | 1 - cmake/CutterConfig.cmake.in | 22 +++++++++++++++ cmake/CutterInstallDirs.cmake | 2 +- rizin | 2 +- src/CMakeLists.txt | 27 ++++++++++++------- src/Main.cpp | 2 ++ src/core/MainWindow.h | 2 +- src/plugins/sample-cpp/CMakeLists.txt | 13 +++++++++ src/plugins/sample-cpp/CutterSamplePlugin.cpp | 16 +++++++---- src/plugins/sample-cpp/CutterSamplePlugin.h | 8 +++--- src/plugins/sample-cpp/CutterSamplePlugin.pro | 10 ------- src/widgets/ConsoleWidget.cpp | 1 + 14 files changed, 79 insertions(+), 38 deletions(-) delete mode 100644 cmake/CutterConfig.cmake create mode 100644 cmake/CutterConfig.cmake.in create mode 100644 src/plugins/sample-cpp/CMakeLists.txt delete mode 100644 src/plugins/sample-cpp/CutterSamplePlugin.pro diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c0f30b5..991368ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,16 +57,14 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) +set(QT_COMPONENTS Core Widgets Gui Svg Network) if (CUTTER_QT6) set(QT_PREFIX Qt6) + list(APPEND QT_COMPONENTS Core5Compat SvgWidgets OpenGLWidgets) else() set(QT_PREFIX Qt5) endif() - -find_package(${QT_PREFIX} REQUIRED COMPONENTS Core Widgets Gui Svg Network) -if (CUTTER_QT6) - find_package(${QT_PREFIX} REQUIRED COMPONENTS Core5Compat SvgWidgets OpenGLWidgets) -endif() +find_package(${QT_PREFIX} REQUIRED COMPONENTS ${QT_COMPONENTS}) if(CUTTER_ENABLE_PYTHON) find_package(PythonInterp REQUIRED) diff --git a/cmake/BundledRizin.cmake b/cmake/BundledRizin.cmake index 10f582b4..4dcf4301 100644 --- a/cmake/BundledRizin.cmake +++ b/cmake/BundledRizin.cmake @@ -57,7 +57,8 @@ target_link_libraries(Rizin INTERFACE ${RZ_LIBS}) target_include_directories(Rizin INTERFACE "$" - "$") + "$" + "$") install(TARGETS Rizin EXPORT CutterTargets) if (WIN32) diff --git a/cmake/CutterConfig.cmake b/cmake/CutterConfig.cmake deleted file mode 100644 index 926efecf..00000000 --- a/cmake/CutterConfig.cmake +++ /dev/null @@ -1 +0,0 @@ -include("${CMAKE_CURRENT_LIST_DIR}/CutterTargets.cmake") \ No newline at end of file diff --git a/cmake/CutterConfig.cmake.in b/cmake/CutterConfig.cmake.in new file mode 100644 index 00000000..cfab9542 --- /dev/null +++ b/cmake/CutterConfig.cmake.in @@ -0,0 +1,22 @@ +@PACKAGE_INIT@ + +set(Cutter_RIZIN_BUNDLED @CUTTER_USE_BUNDLED_RIZIN@) + +include(CMakeFindDependencyMacro) +find_dependency(@QT_PREFIX@ COMPONENTS @QT_COMPONENTS@) +find_dependency(Rizin COMPONENTS Core) + +# Make a best guess for a user location from where plugins can be loaded. +# This can be used in Cutter plugins like +# set(CUTTER_INSTALL_PLUGDIR "${Cutter_USER_PLUGINDIR}" CACHE STRING "Directory to install Cutter plugin into") +# see https://doc.qt.io/qt-5/qstandardpaths.html under AppDataLocation +if(APPLE) + set(Cutter_USER_PLUGINDIR "$ENV{HOME}/Library/Application Support/rizin/cutter/plugins/native") +elseif(WIN32) + file(TO_CMAKE_PATH "$ENV{APPDATA}" Cutter_USER_PLUGINDIR) + set(Cutter_USER_PLUGINDIR "${Cutter_USER_PLUGINDIR}/rizin/cutter/plugins/native") +else() + set(Cutter_USER_PLUGINDIR "$ENV{HOME}/.local/share/rizin/cutter/plugins/native") +endif() + +include("${CMAKE_CURRENT_LIST_DIR}/CutterTargets.cmake") diff --git a/cmake/CutterInstallDirs.cmake b/cmake/CutterInstallDirs.cmake index f7133abc..31873c69 100644 --- a/cmake/CutterInstallDirs.cmake +++ b/cmake/CutterInstallDirs.cmake @@ -20,4 +20,4 @@ else() include(GNUInstallDirs) set(CUTTER_INSTALL_DATADIR "${CMAKE_INSTALL_DATAROOTDIR}/${CUTTER_DIR_NAME}" CACHE PATH "Resource installation directory") endif() -set(ConfigPackageLocation "${CMAKE_INSTALL_LIBDIR}/Cutter" CACHE PATH "Cmake file install location") +set(CUTTER_INSTALL_CONFIGDIR "${CMAKE_INSTALL_LIBDIR}/cmake/Cutter" CACHE PATH "CMake file install location") diff --git a/rizin b/rizin index dd81b662..be48ddcd 160000 --- a/rizin +++ b/rizin @@ -1 +1 @@ -Subproject commit dd81b6629a45ec3d221042ed9729b50cd3c478c5 +Subproject commit be48ddcd9f0c2023d0e64750426c0476a13d8589 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4601cb96..4fb5d4d4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -558,17 +558,26 @@ install(TARGETS Cutter EXPORT CutterTargets RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" BUNDLE DESTINATION "." # needs to be tested - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT Devel -) + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT Devel) install(EXPORT CutterTargets NAMESPACE Cutter:: - DESTINATION "${ConfigPackageLocation}" + DESTINATION "${CUTTER_INSTALL_CONFIGDIR}" COMPONENT Devel) +include(CMakePackageConfigHelpers) +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/CutterConfig.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/CutterConfig.cmake" + INSTALL_DESTINATION "${CUTTER_INSTALL_CONFIGDIR}" + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/CutterConfigVersion.cmake" + VERSION "${CUTTER_VERSION_FULL}" + COMPATIBILITY AnyNewerVersion) install(FILES - ../cmake/CutterConfig.cmake - DESTINATION ${ConfigPackageLocation} - COMPONENT Devel -) + "${CMAKE_CURRENT_BINARY_DIR}/CutterConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/CutterConfigVersion.cmake" + DESTINATION ${CUTTER_INSTALL_CONFIGDIR} + COMPONENT Devel) foreach(_file ${HEADER_FILES}) # Can't use target PUBLIC_HEADER option for installing due to multiple directories get_filename_component(_header_dir "${_file}" DIRECTORY) @@ -576,8 +585,8 @@ foreach(_file ${HEADER_FILES}) endforeach() if(UNIX AND NOT APPLE) - install (FILES "img/cutter.svg" - DESTINATION "share/icons/hicolor/scalable/apps/") + install(FILES "img/cutter.svg" + DESTINATION "share/icons/hicolor/scalable/apps/") install(FILES "re.rizin.cutter.desktop" DESTINATION "share/applications" COMPONENT Devel) diff --git a/src/Main.cpp b/src/Main.cpp index b0cef4a0..8cb4347f 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -14,6 +14,8 @@ * @brief Attempt to connect to a parent console and configure outputs. */ #ifdef Q_OS_WIN +#include + static void connectToConsole() { BOOL attached = AttachConsole(ATTACH_PARENT_PROCESS); diff --git a/src/core/MainWindow.h b/src/core/MainWindow.h index dd878062..ce1bdceb 100644 --- a/src/core/MainWindow.h +++ b/src/core/MainWindow.h @@ -60,7 +60,7 @@ namespace Ui { class MainWindow; } -class MainWindow : public QMainWindow +class CUTTER_EXPORT MainWindow : public QMainWindow { Q_OBJECT diff --git a/src/plugins/sample-cpp/CMakeLists.txt b/src/plugins/sample-cpp/CMakeLists.txt new file mode 100644 index 00000000..93e140f5 --- /dev/null +++ b/src/plugins/sample-cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.12) +project(cutter-sample-plugin) + +find_package(Cutter REQUIRED) +set(CUTTER_INSTALL_PLUGDIR "${Cutter_USER_PLUGINDIR}" CACHE STRING "Directory to install Cutter plugin into") + +set(CMAKE_AUTOMOC ON) + +add_library(sample_plugin MODULE + CutterSamplePlugin.h + CutterSamplePlugin.cpp) +target_link_libraries(sample_plugin PRIVATE Cutter::Cutter) +install(TARGETS sample_plugin DESTINATION "${CUTTER_INSTALL_PLUGDIR}") diff --git a/src/plugins/sample-cpp/CutterSamplePlugin.cpp b/src/plugins/sample-cpp/CutterSamplePlugin.cpp index 98c95f70..bb2264b3 100644 --- a/src/plugins/sample-cpp/CutterSamplePlugin.cpp +++ b/src/plugins/sample-cpp/CutterSamplePlugin.cpp @@ -4,9 +4,10 @@ #include #include "CutterSamplePlugin.h" -#include "common/TempConfig.h" -#include "common/Configuration.h" -#include "MainWindow.h" + +#include +#include +#include void CutterSamplePlugin::setupPlugin() {} @@ -56,9 +57,14 @@ void CutterSamplePluginWidget::on_seekChanged(RVA addr) void CutterSamplePluginWidget::on_buttonClicked() { - QString fortune = Core()->cmd("fo").replace("\n", ""); + RzCoreLocked core(Core()); + char *fortune = rz_core_fortune_get_random(core); + if (!fortune) { + return; + } // cmdRaw can be used to execute single raw commands // this is especially good for user-controlled input - QString res = Core()->cmdRaw("?E " + fortune); + QString res = Core()->cmdRaw("?E " + QString::fromUtf8(fortune)); text->setText(res); + rz_mem_free(fortune); } diff --git a/src/plugins/sample-cpp/CutterSamplePlugin.h b/src/plugins/sample-cpp/CutterSamplePlugin.h index 45cbaedb..d0497a0c 100644 --- a/src/plugins/sample-cpp/CutterSamplePlugin.h +++ b/src/plugins/sample-cpp/CutterSamplePlugin.h @@ -1,9 +1,9 @@ #ifndef CUTTERSAMPLEPLUGIN_H #define CUTTERSAMPLEPLUGIN_H -#include -#include -#include "CutterPlugin.h" +#include + +#include class CutterSamplePlugin : public QObject, CutterPlugin { @@ -26,7 +26,7 @@ class CutterSamplePluginWidget : public CutterDockWidget Q_OBJECT public: - explicit CutterSamplePluginWidget(MainWindow *main, QAction *action); + explicit CutterSamplePluginWidget(MainWindow *main); private: QLabel *text; diff --git a/src/plugins/sample-cpp/CutterSamplePlugin.pro b/src/plugins/sample-cpp/CutterSamplePlugin.pro deleted file mode 100644 index c65aca87..00000000 --- a/src/plugins/sample-cpp/CutterSamplePlugin.pro +++ /dev/null @@ -1,10 +0,0 @@ -HEADERS += ../CutterSamplePlugin.h ../CutterPlugin.h -INCLUDEPATH += ../ ../../ ../../core ../../widgets -SOURCES += CutterSamplePlugin.cpp - -QMAKE_CXXFLAGS += $$system("pkg-config --cflags rz_core") - -TEMPLATE = lib -CONFIG += plugin -QT += widgets -TARGET = PluginSample diff --git a/src/widgets/ConsoleWidget.cpp b/src/widgets/ConsoleWidget.cpp index db378068..de0e6892 100644 --- a/src/widgets/ConsoleWidget.cpp +++ b/src/widgets/ConsoleWidget.cpp @@ -17,6 +17,7 @@ #include "WidgetShortcuts.h" #ifdef Q_OS_WIN +# include # include # define dup2 _dup2 # define dup _dup