Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cmake packaging #11

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,46 @@ cmake_minimum_required(VERSION 3.14)
# set the project name and version
project(QSsh VERSION 1.0 LANGUAGES CXX)

# use GNU installation dirs
include(GNUInstallDirs)

# use package configuration helpers
include(CMakePackageConfigHelpers)

# cached variables
set(QSSH_BUILD_EXAMPLES True CACHE BOOL "Build the QSsh examples.")
set(BUILD_SHARED_LIBS False CACHE BOOL "Build QSsh using shared libraries.")
set(BOTAN_INCLUDE_DIR "" CACHE PATH "Path of the Botan include directory. Set this if user specified BOTAN_LIBRARY location is used.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to use find package?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last time I checked Botan-2 does not provide .cmake files for finding package. So if I build Botan-2 manually I need a way to point QSsh to the version I have built.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can provide your own Findbotan.cmake (example https://stackoverflow.com/a/72323666). botan is a thirdparty library so it should not be in main cmakelists

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Could be done in a separate MR though, this one is already changing a bit.

set(BOTAN_LIBRARY "" CACHE FILEPATH "Path of the Botan library. Leave blank if you want to use the automated package search.")

# specify the C++ standard
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

if(MSVC)
# user specified Botan library?
# note in this case user must take care of installation of library/header files if required
if(NOT "${BOTAN_LIBRARY}" STREQUAL "")
add_library(Botan2 UNKNOWN IMPORTED)
if(WIN32)
set_property(TARGET Botan2 PROPERTY IMPORTED_IMPLIB "${BOTAN_LIBRARY}")
set_property(TARGET Botan2 PROPERTY IMPORTED_LOCATION "${BOTAN_LIBRARY}") # the location should be the .dll, but we don't care about its location. However CMake requires IMPORTED_LOCATED to be set to 'something'
else()
set_property(TARGET Botan2 PROPERTY IMPORTED_LOCATION "${BOTAN_LIBRARY}")
endif()
target_include_directories(Botan2 INTERFACE "${BOTAN_INCLUDE_DIR}")
set_property(GLOBAL PROPERTY BOTAN_LIB Botan2)
else()
# use package search to find the Botan module
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4005")
set(BOTAN_LIB ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/botan.lib)
else(MSVC)
else()
find_package(PkgConfig REQUIRED)
pkg_search_module(Botan REQUIRED IMPORTED_TARGET GLOBAL botan-2)

set_property(GLOBAL PROPERTY BOTAN_LIB PkgConfig::Botan)
endif(MSVC)
endif()
endif()


find_package(QT NAMES Qt5 Qt6 COMPONENTS Core Quick REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Widgets Network REQUIRED)
Expand All @@ -31,17 +58,30 @@ if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

# the main library
add_subdirectory(src)
add_subdirectory(examples)

# INSTALL RULES
install(EXPORT QSsh-targets DESTINATION lib)
# the examples
if(QSSH_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

# install rules
install(EXPORT QSsh-targets
FILE QSshTargets.cmake
NAMESPACE QSsh::
DESTINATION share/QSsh/cmake
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/QSsh/cmake
)

# create package configuration files
configure_package_config_file(QSshConfig.cmake.in
"${CMAKE_INSTALL_DATAROOTDIR}/QSsh/cmake/QSshConfig.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/QSsh/cmake
)
write_basic_package_version_file("${CMAKE_INSTALL_DATAROOTDIR}/QSsh/cmake/QSshConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY ExactVersion)

install(FILES "${CMAKE_SOURCE_DIR}/cmakeFindModules/QSshConfig.cmake"
DESTINATION share/QSsh/cmake)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_DATAROOTDIR}/QSsh/cmake/QSshConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_DATAROOTDIR}/QSsh/cmake/QSshConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/QSsh/cmake)
8 changes: 8 additions & 0 deletions QSshConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(QSsh_VERSION @PROJECT_VERSION@)

# see https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html for more details
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/QSshTargets.cmake")

check_required_components(QSsh)
71 changes: 0 additions & 71 deletions cmakeFindModules/QSshConfig.cmake

This file was deleted.

13 changes: 3 additions & 10 deletions examples/sftp/sftptest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "sftptest.h"

#include <cstdlib>
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
Expand Down Expand Up @@ -151,11 +152,7 @@ void SftpTest::handleChannelInitialized()
if (success) {
int content[1024/sizeof(int)];
for (size_t j = 0; j < sizeof content / sizeof content[0]; ++j)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
content[j] = QRandomGenerator::system()->generate();
#else
content[j] = qrand();
#endif
content[j] = rand();
file->write(reinterpret_cast<char *>(content), sizeof content);
file->close();
}
Expand Down Expand Up @@ -302,11 +299,7 @@ void SftpTest::handleSftpJobFinished(SftpJobId job, const SftpError errorType, c
for (quint64 block = 0; block < blockCount; ++block) {
int content[blockSize/sizeof(int)];
for (size_t j = 0; j < sizeof content / sizeof content[0]; ++j) {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
content[j] = QRandomGenerator::system()->generate();
#else
content[j] = qrand();
#endif
content[j] = rand();
}
m_localBigFile->write(reinterpret_cast<char *>(content),
sizeof content);
Expand Down
2 changes: 1 addition & 1 deletion examples/ssh-shell/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_executable(ssh-hell
add_executable(ssh-shell
argumentscollector.cpp main.cpp shell.cpp)

target_link_libraries(ssh-shell PRIVATE QSsh Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network ${BOTAN_LIB})
Expand Down
7 changes: 5 additions & 2 deletions examples/ssh-shell/argumentscollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ SshConnectionParameters ArgumentsCollector::collect(bool &success) const

for (pos = 1; pos < m_arguments.count() - 1; ++pos) {
QString host;
QString user;
if (checkAndSetStringArg(pos, host, "-h") || checkAndSetStringArg(pos, user, "-u")) {
if (checkAndSetStringArg(pos, host, "-h")) {
parameters.setHost(host);
continue;
}
QString user;
if (checkAndSetStringArg(pos, user, "-u")) {
parameters.setUserName(user);
continue;
}
Expand Down
13 changes: 9 additions & 4 deletions src/libs/qssh/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
add_library(QSsh
set(LIBTYPE STATIC)
if(BUILD_SHARED_LIBS)
set(LIBTYPE SHARED)
endif()

add_library(QSsh ${LIBTYPE}
sshsendfacility.cpp
sshremoteprocess.cpp
sshpacketparser.cpp
Expand Down Expand Up @@ -35,7 +40,7 @@ add_library(QSsh
qssh.qrc)

get_property(BOTAN_LIB GLOBAL PROPERTY BOTAN_LIB)
target_link_libraries( QSsh Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Widgets ${BOTAN_LIB})
target_link_libraries(QSsh Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Widgets ${BOTAN_LIB})

# state that anybody linking to us needs to include the current source dir
target_include_directories(QSsh
Expand All @@ -49,8 +54,8 @@ target_include_directories(QSsh
#INSTALL RULES
install(
DIRECTORY .
DESTINATION include
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/qssh
COMPONENT headers
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h"
)
install(TARGETS QSsh DESTINATION lib EXPORT QSsh-targets)
install(TARGETS QSsh DESTINATION ${CMAKE_INSTALL_LIBDIR} EXPORT QSsh-targets)
8 changes: 5 additions & 3 deletions src/libs/qssh/sftpchannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ SftpJobId SftpChannel::downloadFile(const QString &remoteFilePath, QSharedPointe
}

SftpJobId SftpChannel::uploadDir(const QString &localDirPath,
const QString &remoteParentDirPath)
const QString &remoteParentDirPath, bool appendDirPath)
{
if (state() != Initialized)
return SftpInvalidJob;
Expand All @@ -248,8 +248,10 @@ SftpJobId SftpChannel::uploadDir(const QString &localDirPath,
return SftpInvalidJob;
const Internal::SftpUploadDir::Ptr uploadDirOp(
new Internal::SftpUploadDir(++d->m_nextJobId));
const QString remoteDirPath
= remoteParentDirPath + u'/' + localDir.dirName();
QString remoteDirPath = remoteParentDirPath;
if (appendDirPath) {
remoteDirPath += u'/' + localDir.dirName();
}
const Internal::SftpMakeDir::Ptr mkdirOp(
new Internal::SftpMakeDir(++d->m_nextJobId, remoteDirPath, uploadDirOp));
uploadDirOp->mkdirsInProgress.insert(mkdirOp,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/qssh/sftpchannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ class QSSH_EXPORT SftpChannel : public QObject
/*!
* \brief Uploads a local directory (recursively) with files to the remote host
* \param localDirPath The path to an existing local directory
* \param remoteParentDirPath The remote path to upload it to, the name of the local directory will be appended to this
* \param remoteParentDirPath The remote path to upload it to, the name of the local directory will be appended to this if appendDirPath is set
* \return A unique ID identifying this job
*/
SftpJobId uploadDir(const QString &localDirPath,
const QString &remoteParentDirPath);
const QString &remoteParentDirPath, bool appendDirPath = true);

/*!
* \brief Downloads a remote directory (recursively) to a local path
Expand Down