cmake_minimum_required(VERSION 3.20)
project(wuff)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)

# - - - YAML-CPP
# Pinned: the build has to be reproducible, and 0.9.0 is the first release whose
# cmake_minimum_required() is new enough for CMake 4 (0.8.0 declares 3.4 and fails there).
FetchContent_Declare(
    yaml-cpp
    GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
    GIT_TAG yaml-cpp-0.9.0
)
FetchContent_MakeAvailable(yaml-cpp)

# - - - Tree-sitter
# Tree-sitter does not use CMake, so MakeAvailable only populates the sources; they are
# added to the module by hand below.
FetchContent_Declare(
    tree_sitter
    GIT_REPOSITORY https://github.com/tree-sitter/tree-sitter.git
    GIT_TAG v0.20.9
)
FetchContent_MakeAvailable(tree_sitter)

set(TREE_SITTER_SRC ${tree_sitter_SOURCE_DIR}/lib/src/lib.c)
set(TREE_SITTER_INCLUDE_DIRS
    ${tree_sitter_SOURCE_DIR}/lib/include
    ${tree_sitter_SOURCE_DIR}/lib/src
    ${tree_sitter_SOURCE_DIR}/lib/src/unicode
)

# Tree-Sitter WooWoo
set(WOOWOO_PARSER_SRC tree-sitter/woowoo/parser.c)
set(WOOWOO_SCANNER_SRC tree-sitter/woowoo/scanner.cc)

# Tree-Sitter YAML
set(YAML_PARSER_SRC tree-sitter/yaml/parser.c)
set(YAML_SCANNER_SRC tree-sitter/yaml/scanner.cc)

# Tree-Sitter BibTeX
set(BIBTEX_PARSER_SRC tree-sitter/bibtex/parser.c)

# - - - Pybind11
FetchContent_Declare(
    pybind11
    GIT_REPOSITORY https://github.com/pybind/pybind11.git
    GIT_TAG v2.11.1
)
FetchContent_MakeAvailable(pybind11)

# - - - Build main module
pybind11_add_module(${PROJECT_NAME} NO_EXTRAS
    ${TREE_SITTER_SRC}
    ${WOOWOO_PARSER_SRC}
    ${WOOWOO_SCANNER_SRC}
    ${YAML_PARSER_SRC}
    ${YAML_SCANNER_SRC}
    ${BIBTEX_PARSER_SRC}
    Bindings.cpp
    WooWooAnalyzer.cpp
    project/WooWooDocument.cpp
    project/WooWooProject.cpp
    project/DialectedWooWooDocument.cpp
    project/MetaContext.cpp
    project/CommentLine.cpp
    project/UTF8toUTF16Mapping.cpp
    project/Woofile.cpp
    project/Bibliography.cpp
    dialect/DialectManager.cpp
    dialect/DocumentPart.cpp
    dialect/Dialect.cpp
    dialect/MetaBlock.cpp
    dialect/Field.cpp
    dialect/Shorthand.cpp
    dialect/Reference.cpp
    dialect/Environment.cpp
    dialect/Wobject.cpp
    components/Component.cpp
    components/Hoverer.cpp
    components/Highlighter.cpp
    components/Navigator.cpp
    components/Completer.cpp
    components/Linter.cpp
    components/Folder.cpp
    parser/Parser.cpp
    utils/utils.cpp
)
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${TREE_SITTER_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC yaml-cpp::yaml-cpp)

# The macOS deployment target is not passed with -mmacosx-version-min here: it has to match
# the value the wheel is tagged with, or delocate rejects the wheel as requiring a newer
# macOS than its tag promises. setup.py forwards MACOSX_DEPLOYMENT_TARGET as
# CMAKE_OSX_DEPLOYMENT_TARGET, with 10.15 as the floor because std::filesystem only exists
# in libc++ from macOS 10.15 on, so wheel builds, pip installs and manual cmake runs agree.

# - - - WooWooTest executable
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
    add_executable(WooWooTest
        ${TREE_SITTER_SRC}
        ${WOOWOO_PARSER_SRC}
        ${WOOWOO_SCANNER_SRC}
        ${YAML_PARSER_SRC}
        ${YAML_SCANNER_SRC}
        ${BIBTEX_PARSER_SRC}
        main.cpp
        WooWooAnalyzer.cpp
        project/WooWooDocument.cpp
        project/WooWooProject.cpp
        project/DialectedWooWooDocument.cpp
        project/MetaContext.cpp
        project/CommentLine.cpp
        project/UTF8toUTF16Mapping.cpp
        project/Woofile.cpp
        project/Bibliography.cpp
        dialect/DialectManager.cpp
        dialect/DocumentPart.cpp
        dialect/Dialect.cpp
        dialect/MetaBlock.cpp
        dialect/Field.cpp
        dialect/Shorthand.cpp
        dialect/Reference.cpp
        dialect/Environment.cpp
        dialect/Wobject.cpp
        components/Component.cpp
        components/Hoverer.cpp
        components/Highlighter.cpp
        components/Navigator.cpp
        components/Completer.cpp
        components/Linter.cpp
        components/Folder.cpp
        parser/Parser.cpp
        utils/utils.cpp
    )
    target_include_directories(WooWooTest SYSTEM PRIVATE ${TREE_SITTER_INCLUDE_DIRS})
    target_link_libraries(WooWooTest PUBLIC yaml-cpp::yaml-cpp pybind11::embed)

    if (MSVC)
        target_compile_options(WooWooTest PRIVATE /W4)
    else()
        target_compile_options(WooWooTest PRIVATE -Wall -Wextra -Wpedantic)
    endif()
endif()

