#
# @file CMakeLists.txt
#
# @author Tobias Triffterer
#
# Rutherford Experiment Lab Course Online
# Copyright © 2021 Ruhr-Universität Bochum, Institut für Experimentalphysik I
# https://www.ep1.ruhr-uni-bochum.de/
#
# 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
# (at your option) 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/>.
#

cmake_minimum_required(VERSION 3.13)
project(fp311online-client)

if("${CMAKE_BUILD_TYPE}" STREQUAL "")
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build" FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

set(FP311ONLINE_CLIENT_KNOWN_BUILD_TYPES Debug Release MinSizeRel RelWithDebInfo)
list(FIND FP311ONLINE_CLIENT_KNOWN_BUILD_TYPES ${CMAKE_BUILD_TYPE} FP311ONLINE_CLIENT_BUILD_TYPE_VALID)
if (${FP311ONLINE_CLIENT_BUILD_TYPE_VALID} EQUAL -1)
  message(WARNING "The build type \"${CMAKE_BUILD_TYPE}\" for simulact is not recognized. Valid values are \"Debug\", \"Release\", \"MinSizeRel\", and \"RelWithDebInfo\".\nSee https://cmake.org/cmake/help/v3.13/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type for more information.\nThe project can be built, but if you have not taken the necessary steps outlined in the CMake documentation to create a custom build type, the behaviour is undefined.")
endif (${FP311ONLINE_CLIENT_BUILD_TYPE_VALID} EQUAL -1)

if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif(NOT CMAKE_CXX_STANDARD)

# Instruct CMake to create a compile_commands.json file for clang-tidy
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Find Qt
find_package(Qt5 COMPONENTS Core Gui Widgets DBus LinguistTools WebSockets LinguistTools CONFIG REQUIRED)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Instruct CMake to run uic automatically when needed.
set(CMAKE_AUTOUIC ON)
# Instruct CMake to run qrc automatically when needed.
set(CMAKE_AUTORCC ON)

# Generate version information
add_custom_target(
    fp311online_client_versioning
    ALL
    DEPENDS "src/version.fp311online-client.cpp.in"
    BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/version.fp311online-client.cpp"
    COMMENT "Generating git-based version information for fp311online-client..."
    COMMAND ${CMAKE_COMMAND} -DSOURCE_DIR=${CMAKE_SOURCE_DIR} -DCURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} -DCURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR} -P "${CMAKE_CURRENT_SOURCE_DIR}/versioning.cmake"
)

set(FP311ONLINE_CLIENT_COMPILE_FLAGS -Wall -Wextra -Wconversion -Weffc++ -Wshadow -Wwrite-strings -Wredundant-decls -Wcast-align -Wmissing-include-dirs -Wmissing-declarations -Wswitch-enum -Wswitch-default -Winvalid-pch -Wformat=2 -Wmissing-format-attribute -pedantic -pthread -fexceptions -fstack-protector-strong -Wstack-protector $<$<CONFIG:Release>:-D_FORTIFY_SOURCE=2> $<$<CONFIG:RelWithDebInfo>:-D_FORTIFY_SOURCE=2> $<$<CONFIG:MinSizeRel>:-D_FORTIFY_SOURCE=2>)
set(FP311ONLINE_CLIENT_LINK_FLAGS -rdynamic -pthread -Wl,-z,relro,-z,now -pie)

set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_AUTOUIC_SEARCH_PATHS} ${PROJECT_SOURCE_DIR}/ui)

set(fp311online_client_SRCS
  src/legalstuff.cpp
  src/logingui.cpp
  src/clientgui.cpp
  src/main.cpp
  "${CMAKE_CURRENT_BINARY_DIR}/version.fp311online-client.cpp"
  res/resources.qrc
)

# Include libfp311online
add_subdirectory(submodules/libfp311online)

#Compile Translations
add_subdirectory(l10n)

add_executable(fp311online-client
 ${fp311online_client_SRCS}
 ${TRANSLATIONS_QRC_FILE}
)

target_compile_features(fp311online-client PUBLIC cxx_std_${CMAKE_CXX_STANDARD})
target_compile_options(fp311online-client PRIVATE $<$<CXX_COMPILER_ID:GNU>:${FP311ONLINE_CLIENT_COMPILE_FLAGS}>)
target_compile_options(fp311online-client PRIVATE $<$<CXX_COMPILER_ID:Clang>:${FP311ONLINE_CLIENT_COMPILE_FLAGS}>)
target_link_options(fp311online-client PRIVATE $<$<CXX_COMPILER_ID:GNU>:${FP311ONLINE_CLIENT_LINK_FLAGS}>)
target_link_options(fp311online-client PRIVATE $<$<CXX_COMPILER_ID:Clang>:${FP311ONLINE_CLIENT_LINK_FLAGS}>)
target_include_directories(fp311online-client PUBLIC ${CMAKE_CURRENT_BINARY_DIR} src/)

target_link_libraries(fp311online-client Qt5::Core Qt5::Gui Qt5::Widgets Qt5::WebSockets)
target_link_libraries(fp311online-client fp311online)

add_dependencies(fp311online-client fp311online_client_versioning fp311online_client_translations)

set_property(TARGET fp311online-client PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET fp311online-client PROPERTY CXX_VISIBILITY_PRESET hidden)
set_property(TARGET fp311online-client PROPERTY VISIBILITY_INLINES_HIDDEN ON)

option(BUILD_FOR_FLATPAK "Install wrapper script for Flatpak to set environment variables for ROOT's Cling." OFF)

include(GNUInstallDirs)

if (${BUILD_FOR_FLATPAK})
  set_target_properties(fp311online-client PROPERTIES OUTPUT_NAME "fp311online-client.bin")
  install(PROGRAMS "${PROJECT_SOURCE_DIR}/res/flatpak-wrapper.sh" DESTINATION "${CMAKE_INSTALL_BINDIR}" RENAME "fp311online-client")
  install(PROGRAMS "${PROJECT_SOURCE_DIR}/res/cling-dummy-g++.sh" DESTINATION "${CMAKE_INSTALL_BINDIR}" RENAME "x86_64-unknown-linux-gnu-g++")
endif (${BUILD_FOR_FLATPAK})

install(TARGETS fp311online-client RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES "${PROJECT_SOURCE_DIR}/res/de.ruhr_uni_bochum.ep1.tobias.fp311online.client.metainfo.xml" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/metainfo/")
install(FILES "${PROJECT_SOURCE_DIR}/res/de.ruhr_uni_bochum.ep1.tobias.fp311online.client.desktop" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications/")
install(FILES "${PROJECT_SOURCE_DIR}/res/de.ruhr_uni_bochum.ep1.tobias.fp311online.client.png" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/128x128/apps")
