CMakeLists.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. cmake_minimum_required(VERSION 3.21)
  2. project(DupFind VERSION 0.3.0 LANGUAGES CXX)
  3. set(CMAKE_CXX_STANDARD 20)
  4. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  5. option(USE_NATIVE_OPTIMIZATION "Optimize for the local machine" OFF)
  6. # WIndows用アイコン
  7. set(app_icon "${CMAKE_CURRENT_SOURCE_DIR}/icons/DupFind.ico")
  8. # 最適化と popcnt 命令出力のためのフラグ設定
  9. if(MSVC)
  10. add_compile_options(/arch:AVX2)
  11. set(X_VCPKG_APPLOCAL_DEPS_INSTALL ON)
  12. elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  13. add_compile_options(-mpopcnt -msse4.2)
  14. if(USE_NATIVE_OPTIMIZATION)
  15. add_compile_options(-march=native)
  16. endif()
  17. endif()
  18. # Qt 6
  19. find_package(Qt6 COMPONENTS Widgets Concurrent Linguist Network REQUIRED)
  20. set(CMAKE_AUTOMOC ON)
  21. set(CMAKE_AUTOUIC ON)
  22. set(CMAKE_AUTORCC ON)
  23. # OpenCVの前にProtobufを自力で見つけさせる
  24. find_package(Protobuf REQUIRED)
  25. # OpenCV
  26. find_package(OpenCV REQUIRED)
  27. # SQLite3
  28. find_package(SQLite3 REQUIRED)
  29. include_directories(include)
  30. file(GLOB_RECURSE SOURCES src/*.cpp)
  31. file(GLOB_RECURSE HEADERS include/*.hpp)
  32. add_library(DupFindCore STATIC ${SOURCES} ${HEADERS})
  33. target_link_libraries(DupFindCore
  34. PUBLIC
  35. Qt6::Widgets
  36. Qt6::Concurrent
  37. Qt6::Network
  38. ${OpenCV_LIBS}
  39. SQLite::SQLite3
  40. )
  41. # Filter out mains from the core library sources if possible, but CMake GLOB can't filter easily inline.
  42. # Better approach: remove them explicitly.
  43. get_target_property(CORE_SOURCES DupFindCore SOURCES)
  44. list(REMOVE_ITEM CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
  45. list(REMOVE_ITEM CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/CmdMain.cpp")
  46. set_target_properties(DupFindCore PROPERTIES SOURCES "${CORE_SOURCES}")
  47. # GUI target
  48. #add_executable(${PROJECT_NAME} src/main.cpp)
  49. qt_add_executable(${PROJECT_NAME}
  50. src/main.cpp
  51. ${app_icon}
  52. resources.qrc
  53. )
  54. target_link_libraries(${PROJECT_NAME} PRIVATE DupFindCore)
  55. set_target_properties(${PROJECT_NAME} PROPERTIES
  56. WIN32_EXECUTABLE ON
  57. MACOSX_BUNDLE ON
  58. )
  59. # Translations
  60. qt_add_translations(${PROJECT_NAME}
  61. TS_FILES
  62. translations/dupfind_en.ts
  63. translations/dupfind_ja.ts
  64. translations/dupfind_zh_CN.ts
  65. translations/dupfind_zh_TW.ts
  66. translations/dupfind_es.ts
  67. translations/dupfind_de.ts
  68. translations/dupfind_ru.ts
  69. translations/dupfind_pt.ts
  70. translations/dupfind_pl.ts
  71. translations/dupfind_ko.ts
  72. RESOURCE i18n
  73. )
  74. # CLI target
  75. add_executable(DupFindCmd src/CmdMain.cpp)
  76. target_link_libraries(DupFindCmd PRIVATE DupFindCore)
  77. # -----------------------------------------------------------------------------
  78. # Installation
  79. # -----------------------------------------------------------------------------
  80. include(GNUInstallDirs)
  81. install(TARGETS ${PROJECT_NAME} DupFindCmd
  82. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  83. )
  84. # Desktop file and Icon (only for Linux)
  85. if(NOT WIN32)
  86. install(FILES debian/dupfind.desktop DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications)
  87. install(FILES Deploy/Assets/Square150x150Logo.png
  88. DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pixmaps
  89. RENAME dupfind.png)
  90. endif()
  91. # -----------------------------------------------------------------------------
  92. # Deployment Configuration (Windows)
  93. # -----------------------------------------------------------------------------
  94. if(WIN32)
  95. # UCRTライブラリを自動的に含める
  96. set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
  97. include(InstallRequiredSystemLibraries)
  98. # vc_redist.x64.exeを外す
  99. if(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS)
  100. list(FILTER CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS EXCLUDE REGEX "\\.exe$")
  101. endif()
  102. # Qt6::qmake 経由で bin ディレクトリを特定し windeployqt を探す
  103. get_target_property(_qmake_exe Qt6::qmake IMPORTED_LOCATION)
  104. get_filename_component(_qt_bin_dir "${_qmake_exe}" DIRECTORY)
  105. find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
  106. set(DEPLOY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Deploy/DupFind")
  107. # Appx Manifest Generation
  108. configure_file(
  109. "${CMAKE_CURRENT_SOURCE_DIR}/Deploy/AppxManifest.xml.in"
  110. "${CMAKE_CURRENT_BINARY_DIR}/AppxManifest.xml"
  111. @ONLY
  112. )
  113. # Path to makeappx.exe (Windows SDK Build Tools)
  114. set(MAKEAPPX_EXECUTABLE "C:/Program Files (x86)/Microsoft Visual Studio/Shared/NuGetPackages/microsoft.windows.sdk.buildtools/10.0.26100.1742/bin/10.0.26100.0/x64/makeappx.exe")
  115. # `cmake --build build --target deploy` で起動する専用ターゲット
  116. add_custom_target(deploy
  117. # 1. 展開用ディレクトリの作成
  118. COMMAND ${CMAKE_COMMAND} -E make_directory "${DEPLOY_DIR}"
  119. # 2. 実行ファイル本体のコピー
  120. COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:${PROJECT_NAME}>" "${DEPLOY_DIR}/"
  121. COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:DupFindCmd>" "${DEPLOY_DIR}/"
  122. # 3. リソースディレクトリのコピーは不要になったため削除
  123. # 4. vcpkgがビルドディレクトリに出力した サードパーティDLL (OpenCV, SQLiteなど) の収集
  124. COMMAND powershell -NoProfile -Command "Copy-Item -Path '$<TARGET_FILE_DIR:${PROJECT_NAME}>/*.dll' -Destination '${DEPLOY_DIR}/' -Force -ErrorAction SilentlyContinue"
  125. # UCRTライブラリのコピー
  126. COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} "${DEPLOY_DIR}/"
  127. # 5. windeployqt による Qt DLL (--compiler-runtime) の自動収集
  128. COMMAND "${WINDEPLOYQT_EXECUTABLE}" --dir "${DEPLOY_DIR}" "${DEPLOY_DIR}/$<TARGET_FILE_NAME:${PROJECT_NAME}>"
  129. # 6. vc_redist.x64.exe の削除 (Appxには不要)
  130. COMMAND ${CMAKE_COMMAND} -E rm -f "${DEPLOY_DIR}/vc_redist.x64.exe"
  131. # 7. Appx用リソースとManifestのコピー
  132. COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/AppxManifest.xml" "${DEPLOY_DIR}/"
  133. COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/Deploy/Assets" "${DEPLOY_DIR}/Assets"
  134. # 8. Appxパッケージの作成
  135. COMMAND "${MAKEAPPX_EXECUTABLE}" pack /d "${DEPLOY_DIR}" /p "${CMAKE_CURRENT_SOURCE_DIR}/Deploy/DupFind.appx" /l /o
  136. DEPENDS ${PROJECT_NAME} DupFindCmd
  137. COMMENT "Deploying and creating Appx package in ${CMAKE_CURRENT_SOURCE_DIR}/Deploy/DupFind.appx..."
  138. )
  139. endif()