# Gingoduino - Music theory engine for embedded systems.
#
# Coexists with Arduino IDE / PlatformIO (which see library.properties /
# library.json) and ESP-IDF (which sees idf_component.yml). This CMake
# build is for standalone consumers, FetchContent users, and system
# installs.
#
# Consumer usage:
#
#   if(NOT TARGET gingoduino)
#     find_package(gingoduino QUIET CONFIG)
#     if(NOT gingoduino_FOUND)
#       include(FetchContent)
#       FetchContent_Declare(gingoduino
#         GIT_REPOSITORY https://github.com/sauloverissimo/gingoduino.git
#         GIT_TAG        v0.6.0)
#       FetchContent_MakeAvailable(gingoduino)
#     endif()
#   endif()
#   target_link_libraries(my_target PRIVATE gingoduino::gingoduino)

cmake_minimum_required(VERSION 3.14)

# ESP-IDF Component Manager path. When IDF resolves this directory as
# a component (clone under managed_components/gingoduino + idf_component.yml
# at the root), it sets ESP_PLATFORM before processing this file. We
# route to idf_component_register and skip the rest, leaving the
# native CMake build untouched for every other consumer.
if(ESP_PLATFORM)
    idf_component_register(
        SRC_DIRS "src"
        INCLUDE_DIRS "src"
    )
    return()
endif()

project(gingoduino VERSION 0.6.0
        DESCRIPTION "Music theory engine for embedded systems"
        LANGUAGES CXX)

include(GNUInstallDirs)

set(GINGODUINO_SOURCES
    src/GingoChord.cpp
    src/GingoChordComparison.cpp
    src/GingoDuration.cpp
    src/GingoEvent.cpp
    src/GingoField.cpp
    src/GingoFretboard.cpp
    src/GingoInterval.cpp
    src/GingoMIDI1.cpp
    src/GingoMonitor.cpp
    src/GingoNote.cpp
    src/GingoProgression.cpp
    src/GingoScale.cpp
    src/GingoSequence.cpp
    src/GingoTempo.cpp
    src/GingoTimeSig.cpp
    src/GingoTree.cpp
)

add_library(gingoduino STATIC ${GINGODUINO_SOURCES})
add_library(gingoduino::gingoduino ALIAS gingoduino)

target_include_directories(gingoduino PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_features(gingoduino PUBLIC cxx_std_11)

# Warning parity with the Arduino build. PRIVATE: only when building
# gingoduino itself; downstream consumers pick their own warning policy.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
    target_compile_options(gingoduino PRIVATE -Wall -Wextra)
endif()

option(GINGODUINO_BUILD_TESTS "Build native test executable" OFF)
if(GINGODUINO_BUILD_TESTS)
    add_executable(gingoduino_test extras/tests/test_native.cpp)
    target_include_directories(gingoduino_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
        target_compile_options(gingoduino_test PRIVATE -Wall -Wextra)
    endif()
    enable_testing()
    add_test(NAME native COMMAND gingoduino_test)
endif()

# find_package(gingoduino CONFIG) support for downstream package managers.
# Skipped when consumed via add_subdirectory or FetchContent (where install
# would pollute the parent's install rules unless the parent opts in).
if(PROJECT_IS_TOP_LEVEL)
    include(CMakePackageConfigHelpers)
    write_basic_package_version_file(
        "${CMAKE_CURRENT_BINARY_DIR}/gingoduino-config-version.cmake"
        VERSION ${PROJECT_VERSION}
        COMPATIBILITY SameMajorVersion
    )

    install(TARGETS gingoduino EXPORT gingoduino-targets
            ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
    install(DIRECTORY src/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
            FILES_MATCHING PATTERN "*.h")
    install(EXPORT gingoduino-targets
            NAMESPACE gingoduino::
            FILE gingoduino-config.cmake
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/gingoduino)
    install(FILES "${CMAKE_CURRENT_BINARY_DIR}/gingoduino-config-version.cmake"
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/gingoduino)
endif()
