From 3268ae342ab8b96c3e99c31d1e9f246950e07a89 Mon Sep 17 00:00:00 2001 From: plaristote Date: Mon, 2 Dec 2024 14:27:49 +0100 Subject: [PATCH] refactore cmake files, adding optional demos and tests to the build --- CMakeLists.txt | 30 ++++++++++++++++++++++++++++++ README.md | 20 +++++++++++++++----- demos/CMakeLists.txt | 8 ++++++++ demos/demo1/CMakeLists.txt | 5 +++++ demos/demo2/CMakeLists.txt | 10 ++++++++++ demos/demo3/CMakeLists.txt | 5 +++++ demos/demo4/CMakeLists.txt | 5 +++++ src/CMakeLists.txt | 30 ++++++++---------------------- test/CMakeLists.txt | 10 ++++++++++ 9 files changed, 96 insertions(+), 27 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 demos/CMakeLists.txt create mode 100644 demos/demo1/CMakeLists.txt create mode 100644 demos/demo2/CMakeLists.txt create mode 100644 demos/demo3/CMakeLists.txt create mode 100644 demos/demo4/CMakeLists.txt create mode 100644 test/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..015e5a1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.16) + +project(SmtpMime + VERSION 2.0 + LANGUAGES C CXX) + +option(BUILD_TESTS "builds tests" ON) +option(BUILD_DEMOS "builds demos" ON) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_AUTOUIC OFF) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC OFF) + +find_package(QT NAMES Qt6 COMPONENTS Core Network REQUIRED) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Network REQUIRED) + +set(LIBRARY_TARGET_NAME ${PROJECT_NAME}) + +message(USING Qt${QT_VERSION_MAJOR}) + +add_subdirectory(src) + +if (BUILD_TESTS) + add_subdirectory(test) +endif() +if (BUILD_DEMOS) + add_subdirectory(demos) +endif() diff --git a/README.md b/README.md index ea46a48..8a78bf9 100644 --- a/README.md +++ b/README.md @@ -107,20 +107,30 @@ For more examples see the [Wiki/Examples](https://github.com/bluetiger9/SmtpClie ## Building -### Building CMake +### Building with CMake -To build the project with CMake run the following comamnds: +To build the project with CMake run the following commands: ```sh -$ cmake . -DQt6_DIR="/qt/install/loccation/6.x.x/6.x.x/gcc_64/lib/cmake/Qt6" -DCMAKE_INSTALL_PREFIX="../release" +$ mkdir build +$ cd build +$ cmake .. -DCMAKE_PREFIX_PATH=/qt/install/location/6.x.x/6.x.x/gcc_64/lib/cmake/Qt6 -DCMAKE_INSTALL_PREFIX="../release" $ cmake --build ./ $ cmake --install ./ ``` +Or if you want to build without the demos and tests: +```sh +$ mkdir build +$ cd build +$ cmake .. -DBUILD_TESTS=OFF -DBUILD_DEMOS=OFF -DCMAKE_PREFIX_PATH=/qt/install/location/6.x.x/6.x.x/gcc_64/lib/cmake/Qt6 -DCMAKE_INSTALL_PREFIX="../release" +$ cmake --build ./ +``` + ### Building with QMake -To build the project with QMake run the following comamnds: +To build the project with QMake run the following commands: ```sh -$ /qt/install/loccation/6.x.x/6.x.x/gcc_64/bin/qmake . +$ /qt/install/location/6.x.x/6.x.x/gcc_64/bin/qmake . $ make ``` diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt new file mode 100644 index 0000000..f8c9d99 --- /dev/null +++ b/demos/CMakeLists.txt @@ -0,0 +1,8 @@ +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Gui Widgets REQUIRED) +set(CMAKE_AUTOUIC ON) +set(CMAKE_AUTORCC ON) + +add_subdirectory(demo1) +add_subdirectory(demo2) +add_subdirectory(demo3) +add_subdirectory(demo4) diff --git a/demos/demo1/CMakeLists.txt b/demos/demo1/CMakeLists.txt new file mode 100644 index 0000000..db89ab4 --- /dev/null +++ b/demos/demo1/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(demo1 demo1.cpp) + +target_link_libraries(demo1 PRIVATE + Qt${QT_VERSION_MAJOR}::Core + ${LIBRARY_TARGET_NAME}) diff --git a/demos/demo2/CMakeLists.txt b/demos/demo2/CMakeLists.txt new file mode 100644 index 0000000..3cc0820 --- /dev/null +++ b/demos/demo2/CMakeLists.txt @@ -0,0 +1,10 @@ +add_executable(demo2 + demo2.cpp + sendemail.cpp + sendemail.ui) + +target_link_libraries(demo2 PRIVATE + Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::Gui + Qt${QT_VERSION_MAJOR}::Widgets + ${LIBRARY_TARGET_NAME}) diff --git a/demos/demo3/CMakeLists.txt b/demos/demo3/CMakeLists.txt new file mode 100644 index 0000000..0e74310 --- /dev/null +++ b/demos/demo3/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(demo3 demo3.cpp) + +target_link_libraries(demo3 PRIVATE + Qt${QT_VERSION_MAJOR}::Core + ${LIBRARY_TARGET_NAME}) diff --git a/demos/demo4/CMakeLists.txt b/demos/demo4/CMakeLists.txt new file mode 100644 index 0000000..9ed1202 --- /dev/null +++ b/demos/demo4/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(demo4 demo4.cpp) + +target_link_libraries(demo4 PRIVATE + Qt${QT_VERSION_MAJOR}::Core + ${LIBRARY_TARGET_NAME}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 68ccda6..cfd6928 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,22 +1,6 @@ -cmake_minimum_required(VERSION 3.16) - -project(SmtpMime VERSION 2.0 LANGUAGES C CXX) - -set(CMAKE_AUTOUIC ON) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) - -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) -find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Network REQUIRED) - add_compile_options(-DSMTP_MIME_LIBRARY) -message(USING Qt${QT_VERSION_MAJOR}) - -qt_add_library(${PROJECT_NAME} SHARED +qt_add_library(${LIBRARY_TARGET_NAME} SHARED emailaddress.cpp mimeattachment.cpp mimebytearrayattachment.cpp @@ -56,14 +40,16 @@ qt_add_library(${PROJECT_NAME} SHARED mimebase64formatter.h mimecontentformatter.h ) -target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Core) -target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Network) + +target_link_libraries(${LIBRARY_TARGET_NAME} PRIVATE + Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::Network) set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) set(CMAKE_INSTALL_RPATH “${ORIGIN}”) -target_include_directories(${PROJECT_NAME} +target_include_directories(${LIBRARY_TARGET_NAME} PRIVATE # where the library itself will look for its internal headers ${CMAKE_CURRENT_SOURCE_DIR} @@ -98,14 +84,14 @@ set(public_headers mimebytearrayattachment.h ) # note that ${public_headers} has to be in quotes -set_target_properties(${PROJECT_NAME} PROPERTIES +set_target_properties(${LIBRARY_TARGET_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} PUBLIC_HEADER "${public_headers}" ) include(GNUInstallDirs) -install(TARGETS ${PROJECT_NAME} +install(TARGETS ${LIBRARY_TARGET_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/smtpmime ) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..a90880f --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,10 @@ +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Test REQUIRED) + +add_executable(test + main.cpp + connectiontest.cpp) + +target_link_libraries(test PRIVATE + Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::Test + ${LIBRARY_TARGET_NAME})