Skip to content

Commit

Permalink
refactor: Rewrite CMake
Browse files Browse the repository at this point in the history
  • Loading branch information
XMuli committed Aug 23, 2022
1 parent ec80309 commit 1772461
Show file tree
Hide file tree
Showing 11 changed files with 2,561 additions and 2,714 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/LinuxDeb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
qt_ver: [5.15.2] # 参考: https://mirrors.cloud.tencent.com/qt/online/qtsdkrepository/linux_x64/desktop/qt5_5152
qt_target: [desktop]
qt_arch: [gcc_64]
arch: [amd64, arm64]
arch: [amd64] # arm64
os: [ubuntu-20.04]
env:
targetName: PicShot
Expand Down
216 changes: 66 additions & 150 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,53 @@ project(${PROJECT_NAME})
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
file(COPY config/config.ini DESTINATION ${CMAKE_BINARY_DIR}/bin)

# msvc multicore compilation and UNICODE
if(WIN32)
if(MSVC)
OPTION(USE_MP "use multiple" ON)
OPTION(ProjectConfig_Global_COMPILE_FLAGS_WITH_MP
"Set The Global Option COMPILE_FLAGS /MP to target." ON)
if(ProjectConfig_Global_COMPILE_FLAGS_WITH_MP OR USE_MP)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
endif()
set(VS_STARTUP_PROJECT ${PROJECT_NAME})

add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
# msvc multicore compilation
if(MSVC)
OPTION(USE_MP "use multiple" ON)
OPTION(ProjectConfig_Global_COMPILE_FLAGS_WITH_MP
"Set The Global Option COMPILE_FLAGS /MP to target." ON)
if(ProjectConfig_Global_COMPILE_FLAGS_WITH_MP OR USE_MP)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
endif()

set(VS_STARTUP_PROJECT ${PROJECT_NAME})
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
endif()

# main
set(SRCS_MAIN
set(SRC_MAIN
xglobal.h
main.cpp)

# core
set(SRCS_CORE
set(SRC_CORE
core/isingleton.h
core/xlog.h)

# platform
set(SRCS_PLATFORM
set(SRC_PLATFORM
platform/iwininfo.h
platform/iwininfo.cpp
platform/wininfo.h
platform/wininfo.cpp
platform/wininfo.cpp)

# 特定平台实现的文件列举在此,会自动剔除和分类归组。分类标准 *_win.* *_x11.*
platform/wininfo_win.h
platform/wininfo_win.cpp
platform/wininfo_x11.h
platform/wininfo_x11.cpp)

# 正则 http://blog.icodeten.com/cmake/2015/01/22/cmake-experience/
# 删除特定平台相关的 file
foreach(_rm_file ${SRCS_PLATFORM})
string(REGEX MATCH ".*/*_win.h|.*/*_win.cpp|.*/*_x11.h|.*/*_x11.cpp" _need_remove_source ${_rm_file})
if(_need_remove_source)
list(REMOVE_ITEM SRCS_PLATFORM ${_need_remove_source})
endif(_need_remove_source)
endforeach(_rm_file)
if(APPLE)
elseif(WIN32)
list(APPEND SRC_PLATFORM
platform/wininfo_win.h
platform/wininfo_win.cpp)
else()
list(APPEND SRC_PLATFORM
platform/wininfo_x11.h
platform/wininfo_x11.cpp)
endif()

##----------- Test begin -----------
#foreach(_rm_file ${SRCS_PLATFORM})
#----------- Test begin -----------
#foreach(_rm_file ${SRC_PLATFORM})
# message(${_rm_file})
#endforeach(_rm_file)
##----------- Test end -----------
#endforeach()
#----------- Test end -----------

# custom widget
set(SRCS_WIDGET
set(SRC_WIDGET
widget/xhorizontalline.h
widget/xhorizontalline.cpp
widget/xverticalline.h
Expand All @@ -81,8 +71,7 @@ set(SRCS_WIDGET
widget/xcombobox.h
widget/xcombobox.cpp)

# tool
set(SRCS_TOOL
set(SRC_TOOL
tool/base/colorparabar.h
tool/base/colorparabar.cpp
tool/base/managebar.h
Expand All @@ -100,17 +89,15 @@ set(SRCS_TOOL
tool/parameterbar.h
tool/parameterbar.cpp)

# tool
set(SRCS_PERFERENCE
set(SRC_PERFERENCE
preference/preference.h
preference/preference.cpp
preference/hotkeyswidget.h
preference/hotkeyswidget.cpp
preference/appellation.h
preference/appellation.cpp)

# screen
set(SRCS_WINSCREEN
set(SRC_WINSCREEN
screen/drawhelper.h
screen/drawhelper.cpp
screen/rectcalcu.h
Expand All @@ -120,19 +107,15 @@ set(SRCS_WINSCREEN
screen/tray.h
screen/tray.cpp)

# 测试模块
set(SRCS_EXAMPLE
set(SRC_EXAMPLE
example/exwidget.h
example/exwidget.cpp)


# 插件模块
set(SRCS_PLUGIN_INTERFACE
set(SRC_PLUGIN_INTERFACE
pluginsinterface/iplugininterface.h
pluginsinterface/iplugininterface.cpp)

# 资源模块
set(SRCS_RESOURCES
set(SRC_RESOURCES
Resources.qrc
logo.rc)

Expand Down Expand Up @@ -162,7 +145,7 @@ find_package(Qt5 COMPONENTS
REQUIRED)

qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES}) # .ts transition .qm
message("#2---->" ${QM_FILES})
# message("#2---->" ${QM_FILES})
#1. 自动批量生成 *.ts 文件
#2. 每次都能够更新 *ts 的增量
#3. 每次 清理 不会删除 .ts 文件
Expand Down Expand Up @@ -199,35 +182,34 @@ if (APPLE)
# set(MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION})

add_executable(${PROJECT_NAME} MACOSX_BUNDLE
${SRCS_MAIN}
${SRCS_PLATFORM}
${SRCS_CORE}
${SRCS_WIDGET}
${SRCS_TOOL}
${SRCS_PERFERENCE}
${SRCS_WINSCREEN}
${SRCS_EXAMPLE}
${SRCS_PLUGIN_INTERFACE}
${SRCS_RESOURCES}
${SRC_MAIN}
${SRC_PLATFORM}
${SRC_CORE}
${SRC_WIDGET}
${SRC_TOOL}
${SRC_PERFERENCE}
${SRC_WINSCREEN}
${SRC_EXAMPLE}
${SRC_PLUGIN_INTERFACE}
${SRC_RESOURCES}
#${QM_FILES}
)
else()
add_executable(${PROJECT_NAME} WIN32
${SRCS_MAIN}
${SRCS_PLATFORM}
${SRCS_CORE}
${SRCS_WIDGET}
${SRCS_TOOL}
${SRCS_PERFERENCE}
${SRCS_WINSCREEN}
${SRCS_EXAMPLE}
${SRCS_PLUGIN_INTERFACE}
${SRCS_RESOURCES}
${SRC_MAIN}
${SRC_PLATFORM}
${SRC_CORE}
${SRC_WIDGET}
${SRC_TOOL}
${SRC_PERFERENCE}
${SRC_WINSCREEN}
${SRC_EXAMPLE}
${SRC_PLUGIN_INTERFACE}
${SRC_RESOURCES}
#${QM_FILES}
)
endif()


IF (WIN32)
target_link_libraries(${PROJECT_NAME}
${QT5_LIBS_LINK}
Expand All @@ -243,93 +225,27 @@ ELSEIF (UNIX)
X11)
ENDIF ()

#target_link_libraries(${PROJECT_NAME} PRIVATE spdlog::spdlog_header_only)

# 文件归类, ref:
# https://stackoverflow.com/questions/33808087/cmake-how-to-create-visual-studio-filters
# https://blog.csdn.net/gzj2013/article/details/102619480
set(_src_root_path ${CMAKE_CURRENT_SOURCE_DIR}) # default root path curr path (CMakeList.txt)

set(_src_root_path ${CMAKE_CURRENT_SOURCE_DIR}) # default root path curr path (CMakeList.txt)
file(GLOB_RECURSE _source_list LIST_DIRECTORIES false
"${_src_root_path}/*.cpp"
"${_src_root_path}/*.h")

#message( "_source_list----------" ${_source_list})

source_group(TREE ${_src_root_path} FILES ${_source_list}) # [ source_group方法一, 会按照实际的目录结构进行组织, .h.cpp 放在一起]
source_group(TREE ${_src_root_path} FILES ${_source_list}) # will be organized according to the actual directory structure, .h.cpp is put together

# 特定平台的文件,进行过滤分组和添加得到编译中
foreach(_source IN ITEMS ${_source_list})
string(REGEX MATCH ".*/*_win.h|.*/*_win.cpp" _plat_win_source ${_source}) # 这个的匹配最后不能是 .c* 表示 .cpp
string(REGEX MATCH ".*/*_x11.h|.*/*_x11.cpp" _plat_x11_source ${_source})

# #----------- Test begin -----------
# message( "_source -->" ${_source})
# message( "_plat_win_source -->" ${_plat_win_source})
# message( "_plat_x11_source -->" ${_plat_x11_source})
# #----------- Test end -----------

get_filename_component(_source_path "${_source}" PATH)
file(RELATIVE_PATH _source_path_rel "${_src_root_path}" "${_source_path}")
string(REPLACE "/" "\\" _group_path "${_source_path_rel}")

if(_plat_win_source OR _plat_x11_source)
if(APPLE)
elseif(WIN32)
target_sources(${PROJECT_NAME} PRIVATE "${_plat_win_source}")
source_group("${_group_path}" FILES "${_plat_win_source}")
else()
target_sources(${PROJECT_NAME} PRIVATE "${_plat_x11_source}")
source_group("${_group_path}" FILES "${_plat_x11_source}")
endif()
else()
# source_group("${_group_path}" FILES "${_source}") # [source_group 方法二, 自行分组 .h 和最后的 .cpp 是分开放在两个大分组下]
endif(_plat_win_source OR _plat_x11_source)

endforeach()

# https://www.ljjyy.com/archives/2021/03/100653.html
# CPU是32位还是64位
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(_bit_arch "x64")
message(STATUS "Target is 64 bits")
else()
set(_bit_arch "x86")
message(STATUS "Target is 32 bits")
endif()

# 主机处理器架构
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i386")
message(STATUS "i386 architecture detected")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i686")
message(STATUS "i686 architecture detected")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
message(STATUS "x86_64 architecture detected")
else()
message(STATUS "host processor architecture is unknown")
endif()

## 此处配合 VMSVC 的 UTF8-BOM 插件,达到跨平台
#if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# message("---using Clang---")
#elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# message("---using GCC---")
#elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# message("---using Intel C++---")
#elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# message("---using Visual Studio C++---")
#endif()

# 定义一些变量,可以在 CPP 中使用
target_compile_definitions(${PROJECT_NAME} PUBLIC _PROJECT_NAME="${PROJECT_NAME}") # PicShot
target_compile_definitions(${PROJECT_NAME} PUBLIC _PROJECT_VERSION="${_version}") # 0.2 这个没打印出来,很奇怪,改用下面这个
target_compile_definitions(${PROJECT_NAME} PUBLIC _BIT_ARCH="${_bit_arch}") # x64/x86
# Define some variables that can be used in *.cpp
target_compile_definitions(${PROJECT_NAME} PUBLIC _PROJECT_NAME="${PROJECT_NAME}")
target_compile_definitions(${PROJECT_NAME} PUBLIC _PROJECT_VERSION="${_version}")
target_compile_definitions(${PROJECT_NAME} PUBLIC _BIT_ARCH=${CMAKE_SIZEOF_VOID_P}) # x64/x86
target_compile_definitions(${PROJECT_NAME} PUBLIC _COMPILER=${CMAKE_HOST_SYSTEM_PROCESSOR}) # i386/i686/x86_64/unknown
target_compile_definitions(${PROJECT_NAME} PUBLIC _COMPILER_ID="${CMAKE_CXX_COMPILER_ID}") # Clang/GCC/MSVC
if(WIN32)
target_compile_definitions(${PROJECT_NAME} PRIVATE
WIN32_LEAN_AND_MEAN # 仅仅包含常用的 API 的头文件,加快编译速度,效果明显
_CRT_SECURE_NO_WARNINGS # 允许使用 strcpy,scanf等不安全的函数
WIN32_LEAN_AND_MEAN # Header files containing only the common APIs
_CRT_SECURE_NO_WARNINGS # Unsafe functions such as strcpy, scanf, etc. are allowed
UNICODE
_UNICODE)
endif()
Expand Down
Loading

0 comments on commit 1772461

Please sign in to comment.