cmake_minimum_required(VERSION 3.5)

set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)

project(maskromtool VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 REQUIRED COMPONENTS Widgets LinguistTools Charts PrintSupport Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools Charts PrintSupport Core)

# We currently only support English, so we define this file but we do
# not link it.  That can, of course, be changed if/when we get around
# to translating the tool.
set(TS_FILES maskromtool_en_001.ts)

# The MACOSX_BUNDLE_ICON_FILE variable is added to the Info.plist
# generated by CMake. This variable contains the .icns file name,
# without the path.
set(MACOSX_BUNDLE_ICON_FILE icons.icns)
# And the following tells CMake where to find and install the file itself.
set(app_icon_macos "${CMAKE_CURRENT_SOURCE_DIR}/icons.icns")
set_source_files_properties(${app_icon_macos} PROPERTIES
       MACOSX_PACKAGE_LOCATION "Resources")

## And the same for Windows
set(app_icon_resource_windows "${CMAKE_CURRENT_SOURCE_DIR}/maskromtool.rc")


# Sources of the GatoROM library, but not its main.
set(GATOROM_SOURCES
    # Base Class
    gatorom.h gatorom.cpp
    # Printing
    gatoprinter.h gatoprinter.cpp
    # Solver and strategies.
    gatosolver.h gatosolver.cpp
    gatograderbytes.h gatograderbytes.cpp
    gatograderstring.h gatograderstring.cpp
    gatograderascii.h gatograderascii.cpp
    gatograderyara.h gatograderyara.cpp
    # Decoders that are new to GatoRom.
    gatodecoderinfo.h gatodecoderinfo.cpp  # Just info, no details.
    # gatodecoderarm6.h gatodecoderarm6.cpp  # MYK82 Clipper Chip Decoder.  Use cols-left instead.
    # gatodecodermsp430.h gatodecodermsp430.cpp # MSP430 ROM
    gatodecodertlcsfont.h gatodecodertlcsfont.cpp # TMP47C434N Font ROM
    gatodecoderz86x1.h gatodecoderz86x1.cpp # Zilog Z8 Z86x1
    gatodecodercolsdownlswap.h gatodecodercolsdownlswap.cpp # Used in NEC uCOM4 Micros
    # Decoder named after Zorrom strategies.
    gatodecodercolsdownr.h gatodecodercolsdownr.cpp   # Top-to-bottom then left to right, 8-bits.
    gatodecodercolsdownl.h gatodecodercolsdownl.cpp
    gatodecodercolsleft.h gatodecodercolsleft.cpp     # Left to right, then top to bottom, 8-bits.
    gatodecodercolsright.h gatodecodercolsright.cpp
    gatodecodersqueezelr.h gatodecodersqueezelr.cpp   # Even bits from left, odd bits from right.
)

set(MRT_SOURCES
# Handles CLI args.
        main.cpp
# Main classes.
        maskromtool.cpp maskromtool.h maskromtool.ui
        romview.h romview.cpp
        romsecond.h romsecond.cpp romsecond.ui
        romscene.h romscene.cpp
        romlineitem.h romlineitem.cpp
        rombititem.h rombititem.cpp
        rombitfix.h rombitfix.cpp
        romthresholddialog.h romthresholddialog.cpp romthresholddialog.ui
        romdecodedialog.h romdecodedialog.cpp romdecodedialog.ui
        aboutdialog.h aboutdialog.cpp aboutdialog.ui
        asciidialog.h asciidialog.cpp asciidialog.ui
        romhexdialog.h romhexdialog.cpp romhexdialog.ui
        romstringsdialog.h romstringsdialog.cpp romstringsdialog.ui
        romsolverdialog.h romsolverdialog.cpp romsolverdialog.ui
        romsolutionsdialog.h romsolutionsdialog.cpp romsolutionsdialog.ui
# Decoders
        romdecoder.h
        romdecodergato.h romdecodergato.cpp
        romdecoderascii.h romdecoderascii.cpp
        romdecoderjson.h romdecoderjson.cpp
        romdecodercsv.h romdecodercsv.cpp
#        romdecodermarc4.h romdecodermarc4.cpp   # Deprecated, needs to move to gatorom.
        romdecoderphotograph.h romdecoderphotograph.cpp
        romdecoderhistogram.h romdecoderhistogram.cpp
        romencoderdiff.h romencoderdiff.cpp
# Alignment stategies
        romaligner.h romaligner.cpp
        romalignerreliable.h romalignerreliable.cpp
        romalignertilting.h romalignertilting.cpp
        romaligndialog.h romaligndialog.cpp romaligndialog.ui
        romrule.h romrule.cpp
        romrulecount.h romrulecount.cpp
        romruleduplicate.h romruleduplicate.cpp
        romrulesanity.h romrulesanity.cpp
        romdecoderpython.h romdecoderpython.cpp
        romruleambiguous.h romruleambiguous.cpp
        romruledialog.h romruledialog.cpp romruledialog.ui
        romdisdialog.h romdisdialog.cpp romdisdialog.ui
# Sampler Strategies
        rombitsampler.h rombitsampler.cpp
        rombitsamplerwide.h rombitsamplerwide.cpp
        rombitsamplertall.h rombitsamplertall.cpp
# Additional libraries.
        ${GATOROM_SOURCES}
#        ${TS_FILES}
        ${app_icon_macos}
        ${app_icon_resource_windows}
)



qt_add_executable(maskromtool
    MANUAL_FINALIZATION
    ${MRT_SOURCES}
)
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
target_link_libraries(maskromtool PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Charts Qt${QT_VERSION_MAJOR}::PrintSupport)


set_target_properties(maskromtool PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER maskromtool.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)


# On Windows, we want a second CLI for the console.
if(WIN32)
    qt_add_executable(maskromtoolcli
        MANUAL_FINALIZATION
        ${MRT_SOURCES}
    )
    target_link_libraries(maskromtoolcli PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Charts Qt${QT_VERSION_MAJOR}::PrintSupport)
    qt_finalize_executable(maskromtoolcli)
    install(TARGETS maskromtoolcli DESTINATION bin)
endif()

qt_finalize_executable(maskromtool)
# Install targets in relation to ${CMAKE_INSTALL_PREFIX}/
install(TARGETS maskromtool DESTINATION bin)




add_executable(gatorom
  ${GATOROM_SOURCES}
  # CLI
  gatomain.cpp
)
target_link_libraries(gatorom Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::PrintSupport)

install(TARGETS gatorom
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
