From 001d981d4ce6bb5d1cd2dcef80e49b8c88d9cc66 Mon Sep 17 00:00:00 2001 From: zhangkun Date: Tue, 14 May 2024 00:36:14 +0800 Subject: [PATCH] feat: support arch --- CMakeLists.txt | 15 ++++++++++++-- example/CMakeLists.txt | 40 ++++++++------------------------------ src/CMakeLists.txt | 44 +++++++++++++++++++++++------------------- 3 files changed, 45 insertions(+), 54 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93cabb0..251c036 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,19 @@ cmake_minimum_required(VERSION 3.16) project(MFloat VERSION 0.1 LANGUAGES CXX) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") + set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -add_subdirectory(example) -add_subdirectory(src) \ No newline at end of file +set(CMAKE_INSTALL_PREFIX /usr) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra") +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed") + +add_subdirectory(src) +add_subdirectory(example) \ No newline at end of file diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 8c4c6c3..0833914 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,50 +1,26 @@ cmake_minimum_required(VERSION 3.20) -project(MFloat_Example VERSION 0.1 LANGUAGES CXX) +set(BIN_NAME MFloat-Example-bin) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Quick) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Quick) -# 无法成功编译资源文件,暂时换回老方法 -#qt_standard_project_setup() - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - set(resource_files example.qrc ) -set(QT_SDK_DIR "${Qt${QT_VERSION_MAJOR}_DIR}") -cmake_path(GET QT_SDK_DIR PARENT_PATH QT_SDK_DIR) -cmake_path(GET QT_SDK_DIR PARENT_PATH QT_SDK_DIR) -cmake_path(GET QT_SDK_DIR PARENT_PATH QT_SDK_DIR) - -set(QML_IMPORT_PATH qrc:/qt/qml) - -qt_add_executable(${PROJECT_NAME} +qt_add_executable(${BIN_NAME} main.cpp + ${resource_files} ) -qt_add_qml_module(${PROJECT_NAME} - URI "MFloat_Example" - VERSION 1.0 - QML_FILES Main.qml - RESOURCES ${resource_files} - RESOURCE_PREFIX "/" +find_library(MY_LIBRARY + NAMES mylibrary + PATHS ${MY_LIBRARY_PATH} ) -set_target_properties(${PROJECT_NAME} PROPERTIES - MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com - MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} - MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} - MACOSX_BUNDLE TRUE - WIN32_EXECUTABLE TRUE -) - -target_link_libraries(${PROJECT_NAME} PRIVATE +target_link_libraries(${BIN_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick mfloatui -) +) \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e4abd4a..3e8afdd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,13 +2,20 @@ cmake_minimum_required(VERSION 3.20) project(mfloatui VERSION 1.0) -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Quick Qml) +find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core Quick Qml) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Quick Qml) -qt_standard_project_setup() +set(PLUGIN_OUTPUT_DIR ${PROJECT_BINARY_DIR}/plugins/) +set(LIB_NAME mfloatui) +set(LIB_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}" CACHE STRING "Library install path") +set(QML_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/qt${QT_VERSION_MAJOR}/qml" CACHE STRING "Qml plugin install directory") + +# 遍历所有头文件 +file(GLOB_RECURSE HEAD_PATHS *.h) +foreach(filepath ${HEAD_PATHS}) + string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath}) + list(APPEND head_files ${filename}) +endforeach(filepath) # 遍历所有源文件 file(GLOB_RECURSE SRC_PATHS *.cpp *.hpp) @@ -38,31 +45,28 @@ foreach(filepath IN LISTS qml_files res_files) set_source_files_properties(${filepath} PROPERTIES QT_RESOURCE_ALIAS ${filename}) endforeach() -set(QT_SDK_DIR "${Qt${QT_VERSION_MAJOR}_DIR}") -cmake_path(GET QT_SDK_DIR PARENT_PATH QT_SDK_DIR) -cmake_path(GET QT_SDK_DIR PARENT_PATH QT_SDK_DIR) -cmake_path(GET QT_SDK_DIR PARENT_PATH QT_SDK_DIR) - -# 设置qml模块输出目录 -set(MFLOAT_QML_PLUGIN_DIRECTORY ${QT_SDK_DIR}/qml/MFloat) - -qt_add_library(${PROJECT_NAME} SHARED) -qt_add_qml_module(${PROJECT_NAME} +qt_add_qml_module( ${LIB_NAME} PLUGIN_TARGET "mfloatuipugin" - OUTPUT_DIRECTORY ${MFLOAT_QML_PLUGIN_DIRECTORY} VERSION 1.0 URI "MFloat" + SHARED TYPEINFO "plugins.qmltypes" - SOURCES ${src_files} + SOURCES ${src_files} ${head_files} QML_FILES ${qml_files} RESOURCES ${res_files} RESOURCE_PREFIX "/qt/qml" + OUTPUT_DIRECTORY "${PLUGIN_OUTPUT_DIR}/MFloat" ) -target_link_libraries(${PROJECT_NAME} PUBLIC +target_link_libraries(${LIB_NAME} PUBLIC Qt${QT_VERSION_MAJOR}::CorePrivate Qt${QT_VERSION_MAJOR}::QuickPrivate Qt${QT_VERSION_MAJOR}::QmlPrivate ) -#安装 -install(DIRECTORY ${MFLOAT_QML_PLUGIN_DIRECTORY} DESTINATION ${CMAKE_INSTALL_PREFIX}/imports) + +# Install library +install(TARGETS ${LIB_NAME} DESTINATION "${LIB_INSTALL_DIR}") + +# Install plugin +install(TARGETS ${PLUGIN_NAME} DESTINATION "${QML_INSTALL_DIR}") +install(DIRECTORY "${PLUGIN_OUTPUT_DIR}/MFloat" DESTINATION "${QML_INSTALL_DIR}")