# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2026  Complement Alimentaire
#
# This file is part of simdpunk.
#
# simdpunk is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# simdpunk 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with simdpunk.  If not, see <https://www.gnu.org/licenses/>.

# simdpunk — header-only quantized-tensor library.

cmake_minimum_required(VERSION 3.16)
project(simdpunk LANGUAGES CXX)

add_library(simdpunk INTERFACE)
target_compile_features(simdpunk INTERFACE cxx_std_23)
target_include_directories(simdpunk INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)

if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  set(_SP_TOP ON)
else()
  set(_SP_TOP OFF)
endif()

option(SIMDPUNK_TESTS "Build + register simdpunk tests" ON)
option(SIMDPUNK_EXAMPLES "Build examples" ${_SP_TOP})

if(SIMDPUNK_TESTS)
  if(_SP_TOP)
    include(CTest)
  endif()
  find_package(Catch2 3 REQUIRED)

  include(CheckCXXCompilerFlag)
  check_cxx_compiler_flag(-mfma SIMDPUNK_HAVE_MFMA)
  set(SIMDPUNK_CANARY_ARCH_FLAGS "")
  if(SIMDPUNK_HAVE_MFMA)
    set(SIMDPUNK_CANARY_ARCH_FLAGS -mfma)   # implies __AVX__ + __FMA__ ⇒ the vector pair too
  endif()

  if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    list(APPEND SIMDPUNK_CANARY_ARCH_FLAGS --param=avoid-fma-max-bits=0 -fno-tree-vectorize)
  elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    list(APPEND SIMDPUNK_CANARY_ARCH_FLAGS -fno-vectorize -fno-slp-vectorize)
  endif()
endif()

#if(SIMDPUNK_EXAMPLES)
#  add_subdirectory(examples)
#endif()
