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.
This commit is contained in:
Florian Märkl 2022-03-31 12:49:37 +02:00 committed by Anton Kochkov
parent 6629d579c6
commit ae2ad5d50c
13 changed files with 78 additions and 37 deletions

View File

@ -57,16 +57,14 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON) set(CMAKE_AUTORCC ON)
set(QT_COMPONENTS Core Widgets Gui Svg Network)
if (CUTTER_QT6) if (CUTTER_QT6)
set(QT_PREFIX Qt6) set(QT_PREFIX Qt6)
list(APPEND QT_COMPONENTS Core5Compat SvgWidgets OpenGLWidgets)
else() else()
set(QT_PREFIX Qt5) set(QT_PREFIX Qt5)
endif() endif()
find_package(${QT_PREFIX} REQUIRED COMPONENTS ${QT_COMPONENTS})
find_package(${QT_PREFIX} REQUIRED COMPONENTS Core Widgets Gui Svg Network)
if (CUTTER_QT6)
find_package(${QT_PREFIX} REQUIRED COMPONENTS Core5Compat SvgWidgets OpenGLWidgets)
endif()
if(CUTTER_ENABLE_PYTHON) if(CUTTER_ENABLE_PYTHON)
find_package(PythonInterp REQUIRED) find_package(PythonInterp REQUIRED)

View File

@ -57,7 +57,8 @@ target_link_libraries(Rizin INTERFACE
${RZ_LIBS}) ${RZ_LIBS})
target_include_directories(Rizin INTERFACE target_include_directories(Rizin INTERFACE
"$<BUILD_INTERFACE:${Rizin_INCLUDE_DIRS}>" "$<BUILD_INTERFACE:${Rizin_INCLUDE_DIRS}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/librz>") "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/librz>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/librz/sdb>")
install(TARGETS Rizin EXPORT CutterTargets) install(TARGETS Rizin EXPORT CutterTargets)
if (WIN32) if (WIN32)

View File

@ -1 +0,0 @@
include("${CMAKE_CURRENT_LIST_DIR}/CutterTargets.cmake")

View File

@ -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")

View File

@ -20,4 +20,4 @@ else()
include(GNUInstallDirs) include(GNUInstallDirs)
set(CUTTER_INSTALL_DATADIR "${CMAKE_INSTALL_DATAROOTDIR}/${CUTTER_DIR_NAME}" CACHE PATH "Resource installation directory") set(CUTTER_INSTALL_DATADIR "${CMAKE_INSTALL_DATAROOTDIR}/${CUTTER_DIR_NAME}" CACHE PATH "Resource installation directory")
endif() 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")

View File

@ -558,17 +558,26 @@ install(TARGETS Cutter
EXPORT CutterTargets EXPORT CutterTargets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
BUNDLE DESTINATION "." # needs to be tested BUNDLE DESTINATION "." # needs to be tested
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT Devel ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT Devel)
)
install(EXPORT CutterTargets install(EXPORT CutterTargets
NAMESPACE Cutter:: NAMESPACE Cutter::
DESTINATION "${ConfigPackageLocation}" DESTINATION "${CUTTER_INSTALL_CONFIGDIR}"
COMPONENT Devel) 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 install(FILES
../cmake/CutterConfig.cmake "${CMAKE_CURRENT_BINARY_DIR}/CutterConfig.cmake"
DESTINATION ${ConfigPackageLocation} "${CMAKE_CURRENT_BINARY_DIR}/CutterConfigVersion.cmake"
COMPONENT Devel DESTINATION ${CUTTER_INSTALL_CONFIGDIR}
) COMPONENT Devel)
foreach(_file ${HEADER_FILES}) foreach(_file ${HEADER_FILES})
# Can't use target PUBLIC_HEADER option for installing due to multiple directories # Can't use target PUBLIC_HEADER option for installing due to multiple directories
get_filename_component(_header_dir "${_file}" DIRECTORY) get_filename_component(_header_dir "${_file}" DIRECTORY)
@ -576,7 +585,7 @@ foreach(_file ${HEADER_FILES})
endforeach() endforeach()
if(UNIX AND NOT APPLE) if(UNIX AND NOT APPLE)
install (FILES "img/cutter.svg" install(FILES "img/cutter.svg"
DESTINATION "share/icons/hicolor/scalable/apps/") DESTINATION "share/icons/hicolor/scalable/apps/")
install(FILES "re.rizin.cutter.desktop" install(FILES "re.rizin.cutter.desktop"
DESTINATION "share/applications" DESTINATION "share/applications"

View File

@ -14,6 +14,8 @@
* @brief Attempt to connect to a parent console and configure outputs. * @brief Attempt to connect to a parent console and configure outputs.
*/ */
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h>
static void connectToConsole() static void connectToConsole()
{ {
BOOL attached = AttachConsole(ATTACH_PARENT_PROCESS); BOOL attached = AttachConsole(ATTACH_PARENT_PROCESS);

View File

@ -60,7 +60,7 @@ namespace Ui {
class MainWindow; class MainWindow;
} }
class MainWindow : public QMainWindow class CUTTER_EXPORT MainWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT

View File

@ -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}")

View File

@ -4,9 +4,10 @@
#include <QAction> #include <QAction>
#include "CutterSamplePlugin.h" #include "CutterSamplePlugin.h"
#include "common/TempConfig.h"
#include "common/Configuration.h" #include <common/TempConfig.h>
#include "MainWindow.h" #include <common/Configuration.h>
#include <MainWindow.h>
void CutterSamplePlugin::setupPlugin() {} void CutterSamplePlugin::setupPlugin() {}
@ -56,9 +57,14 @@ void CutterSamplePluginWidget::on_seekChanged(RVA addr)
void CutterSamplePluginWidget::on_buttonClicked() 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 // cmdRaw can be used to execute single raw commands
// this is especially good for user-controlled input // 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); text->setText(res);
rz_mem_free(fortune);
} }

View File

@ -1,9 +1,9 @@
#ifndef CUTTERSAMPLEPLUGIN_H #ifndef CUTTERSAMPLEPLUGIN_H
#define CUTTERSAMPLEPLUGIN_H #define CUTTERSAMPLEPLUGIN_H
#include <QObject> #include <CutterPlugin.h>
#include <QtPlugin>
#include "CutterPlugin.h" #include <QLabel>
class CutterSamplePlugin : public QObject, CutterPlugin class CutterSamplePlugin : public QObject, CutterPlugin
{ {
@ -26,7 +26,7 @@ class CutterSamplePluginWidget : public CutterDockWidget
Q_OBJECT Q_OBJECT
public: public:
explicit CutterSamplePluginWidget(MainWindow *main, QAction *action); explicit CutterSamplePluginWidget(MainWindow *main);
private: private:
QLabel *text; QLabel *text;

View File

@ -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

View File

@ -17,6 +17,7 @@
#include "WidgetShortcuts.h" #include "WidgetShortcuts.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
# include <windows.h>
# include <io.h> # include <io.h>
# define dup2 _dup2 # define dup2 _dup2
# define dup _dup # define dup _dup