message("\n${BoldGreen}Beginning configuration for subdirectory src${ColourReset}\n")

set(NONGUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/nongui)
set(GUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/gui)

########################################################
# Files
set(${TARGET}_nongui_SRCS
    ${NONGUI_DIR}/globals.cpp
    ${NONGUI_DIR}/PolChemDefCatParser.cpp
    ${NONGUI_DIR}/ConfigSetting.cpp
    ${NONGUI_DIR}/ConfigSettings.cpp
    ${NONGUI_DIR}/UserSpec.cpp
)

set(${TARGET}_gui_SRCS
    ${GUI_DIR}/AboutDlg.cpp
    ${GUI_DIR}/Application.cpp
    ${GUI_DIR}/ApplicationPreferencesWnd.cpp
    ${GUI_DIR}/ProgramWindow.cpp
    ${GUI_DIR}/SequenceEditorWnd.cpp
    ${GUI_DIR}/AbstractMainTaskWindow.cpp
    ${GUI_DIR}/AbstractPolChemDefDependentDlg.cpp
    ${GUI_DIR}/AbstractSeqEdWndDependentDlg.cpp
    ${GUI_DIR}/ChemEntVignette.cpp
    ${GUI_DIR}/MonomerSpec.cpp
    ${GUI_DIR}/ModifSpec.cpp
    ${GUI_DIR}/CrossLinkerSpec.cpp
    ${GUI_DIR}/CalculatorChemPadDlg.cpp
    ${GUI_DIR}/CalculatorChemPadGroupBox.cpp
    ${GUI_DIR}/CalculatorRecorderDlg.cpp
    ${GUI_DIR}/CalculatorWnd.cpp
    ${GUI_DIR}/ChemPadButton.cpp
    ${GUI_DIR}/CleavageDlg.cpp
    ${GUI_DIR}/CleaveOligomerTableView.cpp
    ${GUI_DIR}/CleaveOligomerTableViewMimeData.cpp
    ${GUI_DIR}/CleaveOligomerTableViewModel.cpp
    ${GUI_DIR}/CleaveOligomerTableViewSortProxyModel.cpp
    ${GUI_DIR}/CleavageAgentDefDlg.cpp
    ${GUI_DIR}/CompositionTreeView.cpp
    ${GUI_DIR}/CompositionTreeViewItem.cpp
    ${GUI_DIR}/CompositionTreeViewModel.cpp
    ${GUI_DIR}/CompositionTreeViewSortProxyModel.cpp
    ${GUI_DIR}/CompositionsDlg.cpp
    ${GUI_DIR}/CrossLinkerDefDlg.cpp
    ${GUI_DIR}/FileSystemHierarchyPreferencesWidget.cpp
    ${GUI_DIR}/FragmentationPathwayDefDlg.cpp
    ${GUI_DIR}/FragmentOligomerTableView.cpp
    ${GUI_DIR}/FragmentOligomerTableViewMimeData.cpp
    ${GUI_DIR}/FragmentOligomerTableViewModel.cpp
    ${GUI_DIR}/FragmentOligomerTableViewSortProxyModel.cpp
    ${GUI_DIR}/FragmentationDlg.cpp
    ${GUI_DIR}/IsotopeDefDlg.cpp
    ${GUI_DIR}/MassListSorterDlg.cpp
    ${GUI_DIR}/MassSearchDlg.cpp
    ${GUI_DIR}/MassSearchOligomerTableView.cpp
    ${GUI_DIR}/MassSearchOligomerTableViewMimeData.cpp
    ${GUI_DIR}/MassSearchOligomerTableViewModel.cpp
    ${GUI_DIR}/MassSearchOligomerTableViewSortProxyModel.cpp
    ${GUI_DIR}/ModifDefDlg.cpp
    ${GUI_DIR}/MonomerCodeEvaluator.cpp
    ${GUI_DIR}/MonomerCrossLinkDlg.cpp
    ${GUI_DIR}/MonomerDefDlg.cpp
    ${GUI_DIR}/MonomerModificationDlg.cpp
    ${GUI_DIR}/MzCalculationDlg.cpp
    ${GUI_DIR}/MzCalculationTreeView.cpp
    ${GUI_DIR}/MzCalculationTreeViewItem.cpp
    ${GUI_DIR}/MzCalculationTreeViewModel.cpp
    ${GUI_DIR}/MzCalculationTreeViewSortProxyModel.cpp
    ${GUI_DIR}/PkaPhPiDlg.cpp
    ${GUI_DIR}/PolChemDefRendering.cpp
    ${GUI_DIR}/PolChemDefWnd.cpp
    ${GUI_DIR}/PolymerModificationDlg.cpp
    ${GUI_DIR}/RegionSelection.cpp
    #
    ${GUI_DIR}/SeqToolsDlg.cpp
    ${GUI_DIR}/SequenceEditorFindDlg.cpp
    ${GUI_DIR}/SequenceEditorGraphicsView.cpp
    ${GUI_DIR}/SequenceEditorGraphicsViewKeySequenceHandling.cpp
    ${GUI_DIR}/SequenceEditorGraphicsViewKeyboardHandling.cpp
    ${GUI_DIR}/SequenceImportDlg.cpp
    ${GUI_DIR}/SequencePurificationDlg.cpp
    ${GUI_DIR}/SequenceSelection.cpp
)

set(${TARGET}_UIS
    ${GUI_DIR}/ui/AboutDlg.ui
    ${GUI_DIR}/ui/ApplicationPreferencesWnd.ui
    ${GUI_DIR}/ui/ApplicationPreferencesWndGeneralPageWidget.ui
    ${GUI_DIR}/ui/MaxThreadCountPreferencesWidget.ui
    ${GUI_DIR}/ui/FileSystemHierarchyPreferencesWidget.ui
    ${GUI_DIR}/ui/AbstractPolChemDefDependentDlg.ui
    ${GUI_DIR}/ui/CalculatorChemPadDlg.ui
    ${GUI_DIR}/ui/CalculatorRecorderDlg.ui
    ${GUI_DIR}/ui/CalculatorWnd.ui
    ${GUI_DIR}/ui/CleavageDlg.ui
    ${GUI_DIR}/ui/CleavageAgentDefDlg.ui
    ${GUI_DIR}/ui/CompositionsDlg.ui
    ${GUI_DIR}/ui/ConfigSettingsDlg.ui
    ${GUI_DIR}/ui/CrossLinkerDefDlg.ui
    ${GUI_DIR}/ui/DecimalPlacesOptionsDlg.ui
    ${GUI_DIR}/ui/DecimalPlacesPreferencesWidget.ui
    ${GUI_DIR}/ui/FragmentationPathwayDefDlg.ui
    ${GUI_DIR}/ui/FragmentationDlg.ui
    ${GUI_DIR}/ui/IsotopeDefDlg.ui
    ${GUI_DIR}/ui/JavaScriptingWnd.ui
    ${GUI_DIR}/ui/MassSearchDlg.ui
    ${GUI_DIR}/ui/ModifDefDlg.ui
    ${GUI_DIR}/ui/MonomerCrossLinkDlg.ui
    ${GUI_DIR}/ui/MonomerDefDlg.ui
    ${GUI_DIR}/ui/MonomerModificationDlg.ui
    ${GUI_DIR}/ui/MzCalculationDlg.ui
    ${GUI_DIR}/ui/MzLabFormulaBasedActionsDlg.ui
    ${GUI_DIR}/ui/MzLabInputOligomerTableViewDlg.ui
    ${GUI_DIR}/ui/MzLabMassBasedActionsDlg.ui
    ${GUI_DIR}/ui/MzLabMatchBasedActionsDlg.ui
    ${GUI_DIR}/ui/MzLabOutputOligomerTableViewDlg.ui
    ${GUI_DIR}/ui/MzLabWnd.ui
    ${GUI_DIR}/ui/NumeralsLocaleConverterDlg.ui
    ${GUI_DIR}/ui/PkaPhPiDlg.ui
    ${GUI_DIR}/ui/PolChemDefWnd.ui
    ${GUI_DIR}/ui/PolymerModificationDlg.ui
    ${GUI_DIR}/ui/SequenceEditorFindDlg.ui
    ${GUI_DIR}/ui/SequenceEditorWnd.ui
    ${GUI_DIR}/ui/SequenceImportDlg.ui
    ${GUI_DIR}/ui/SequencePurificationDlg.ui
)

set(CMAKE_AUTOUIC_SEARCH_PATHS ${GUI_DIR}/ui)

qt_standard_project_setup(REQUIRES 6.5)

qt6_add_resources(${TARGET}_qrc_CPP ../${TARGET}.qrc)

###############################################################
# Configuration of the binary to be built
###############################################################

message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")

include_directories(
    # The config file is binary directory-specific !!!
    # For the config.h file generated from config.h.cmake.in
    # For the ui_Xyyyyy.h files
    ${CMAKE_CURRENT_BINARY_DIR}
    # For all the source subdirectories
    ${NONGUI_DIR} ${GUI_DIR}
)

# Only now can we add the executable, because we have defined the sources of the
# object library above.

if(WIN32 OR _WIN32)

    qt_add_executable(
        ${TARGET}
        main.cpp
        ${${TARGET}_nongui_SRCS}
        ${${TARGET}_gui_SRCS}
        ${${TARGET}_UIS}
        ${${TARGET}_qrc_CPP}
        ${PLATFORM_SPECIFIC_SOURCES}
        #${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.rc
    )

    # # Required for Windows 10/11
    # Well, this is what I thought. In fact, the program would not run on Win11
    # not because the linker options below were not set, but because the Win11
    # system what a family edition. I know the software runs with the stuff below
    # on Win11 pro system right.
    # FURTHER, since I have added the linker options below the program does
    # not start anymore on Win11 pro machines with infamous error:
    # 0xc000007b. Removing the lines below make MassXpert3 run on Win11 pro
    # again.
    # target_link_options(${TARGET} PRIVATE
    #     # Set OS version to Windows 10
    #     "-Wl,--major-os-version=10"
    #     "-Wl,--minor-os-version=0"
    #     # Set subsystem version
    #     "-Wl,--major-subsystem-version=10"
    #     "-Wl,--minor-subsystem-version=0"
    #     # Ensure modern subsystem
    #     "-Wl,--subsystem,windows:10.0"
    # )

    # In Debug mode we want the console to be opened so that we can read the debug
    # messages. But in Release mode the console is not desirable (the snippet
    # below removes it).
    if(CMAKE_BUILD_TYPE STREQUAL "Release")
        set_property(TARGET ${TARGET} PROPERTY WIN32_EXECUTABLE true)
    endif()

elseif(UNIX)

    qt_add_executable(
        ${TARGET}
        main.cpp
        ${${TARGET}_nongui_SRCS}
        ${${TARGET}_gui_SRCS}
        ${${TARGET}_UIS}
        ${${TARGET}_qrc_CPP}
        ${PLATFORM_SPECIFIC_SOURCES}
    )

endif()

# Finally actually set the linking dependencies to the executable.
# Note that there are so many cross-dependencies that it is virtually unfeasible
# to actually link the binary without resorting to the linker dance steps below.

# Still, I discovered that the order below is good with respect of the symbols
# dependencies resolutions when linking

# The general idea is to first list the libraries that *use* symbols and later
# libraries that *provide* symbols. Which is why all the Qt libs are listed at
# the end and the private libs are listed first.
target_link_libraries(
    ${TARGET}
    PRIVATE -Wl,--no-as-needed
            XpertMass::Core
            XpertMass::Gui
            -Wl,--as-needed
            -Wl,--no-as-needed
            PappsoMSpp::Core
            -Wl,--as-needed
            -Wl,--no-as-needed
            PappsoMSpp::Gui
            -Wl,--as-needed
            -Wl,--no-as-needed
            IsoSpec++::IsoSpec++
            -Wl,--as-needed
            -Wl,--no-as-needed
            QScintilla::QScintilla
            -Wl,--as-needed
            Qt6::Widgets
            Qt6::Svg
            Qt6::SvgWidgets
            Qt6::PrintSupport
            Qt6::Xml
            Qt6::Network
            Qt6::Qml
            #
            # This is the object file obtained my compiling
            # the WindRes resource file using windres.exe
            # See corresponding MXE or WIN10UCRT64 toolchains.
            ${PLATFORM_SPECIFIC_LIB_FILES}
)

if(PROFILING)
    message(STATUS "Profiling is requested, adding linker -pg flag.")
    set(CMAKE_EXE_LINKER_FLAGS "-pg")
    target_link_options(${TARGET} PRIVATE -pg)
endif()

# These options allow for printing messages to the console from
# which the program is run (for example, in Linux, with wine MineXpert3).
if(WIN32)
    target_link_options(${TARGET} PRIVATE "-Wl,--subsystem,console")
endif()

# We want to install the binary arch-dependent target in
# specific situations. To have proper control, we define the arch
# component.
install(
    TARGETS ${TARGET}
    RUNTIME COMPONENT arch DESTINATION ${PROJECT_INSTALL_BIN_DIR}
)

message("")
message("\n${BoldGreen}Ending configuration for subdirectory src${ColourReset}\n")
message("")
