# Copyright (C) 2023 Uniontech Technology Co., Ltd.
#
# Author:     zhijie geng <gengzhijie@uniontech.com>
#
# Maintainer: zhijie geng <gengzhiejie@uniontech.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Qt对cmake版本的最小要求
cmake_minimum_required(VERSION 3.13.0)

# set(TARGET_PATH "${CMAKE_INSTALL_PREFIX}/lib")
set(BIN_NAME usm)
set(SESSION_INCLUDE ${CMAKE_SOURCE_DIR}/include/usm)

# 有些项目会动态生成头文件,项目中需要引入它,因此需要将output目录也include进来
# 等同于INCLUDE_DIRECTORY(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Qt5 对C++版本推荐至少11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Qt特有的编译器需要打开,默认是关闭的
set(CMAKE_AUTOMOC ON) # Meta-Object Compiler
set(CMAKE_AUTORCC ON) # Resource Compiler
set(CMAKE_AUTOUIC ON) # User Interface Compiler

# 寻找Qt的库
# Qt5 COMPONENTS Widgets：寻找Qt库中的Widget模块
# REQUIRED： 意味着找不到报错并不会继续下去
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(X11)

# 当前模块的头文件肯定要include进来
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${SESSION_INCLUDE})

# 当前模块下的cpp跟CMakeList.txt在同级目录
# aux_source_directory(./ SRC_LIST)
set(SESSION_LIB_SRCS
    session.cpp
    client.cpp
)

set(SESSION_LIB_HEADERS
    client.h
    ${SESSION_INCLUDE}/session.h
)

set(LIBS
    Qt5::Core
    X11
    Xext
    SM
    ICE
)

add_library(${BIN_NAME} SHARED
    ${SESSION_LIB_SRCS}
    ${SESSION_LIB_HEADERS}
)

if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
    string(REPLACE "-fsanitize=address" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()

# 链接所有其他模块到当前模块
target_link_libraries(${BIN_NAME} PRIVATE ${LIBS})

set_target_properties(${BIN_NAME} PROPERTIES
    VERSION ${CMAKE_PROJECT_VERSION}
    SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR}
)

target_compile_definitions(${BIN_NAME} PRIVATE VERSION="${CMAKE_PROJECT_VERSION}")

install(FILES ${SESSION_LIB_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${BIN_NAME})
install(TARGETS ${BIN_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})