# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
# All rights reserved.
#
# SPDX-License-Identifier: MIT
#
# Licensed under the MIT License

# set required cmake version
cmake_minimum_required(VERSION 3.24...4.2)

project(
  mqt-core
  LANGUAGES C CXX
  DESCRIPTION "MQT Core - The Backbone of the Munich Quantum Toolkit")

# Add path for custom modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(AddMQTPythonBinding)
include(StandardProjectSettings)
include(PreventInSourceBuilds)
include(PackageAddTest)
include(Cache)
include(AddMQTCoreLibrary)

option(BUILD_MQT_CORE_BINDINGS "Build the MQT Core Python bindings" OFF)
if(BUILD_MQT_CORE_BINDINGS)
  # ensure that the BINDINGS option is set
  set(BINDINGS
      ON
      CACHE INTERNAL "Enable settings related to Python bindings")
  # Some common settings for finding Python
  set(Python_FIND_VIRTUALENV
      FIRST
      CACHE STRING "Give precedence to virtualenvs when searching for Python")
  set(Python_FIND_FRAMEWORK
      LAST
      CACHE STRING "Prefer Brew/Conda to Apple framework Python")
  set(Python_ARTIFACTS_INTERACTIVE
      ON
      CACHE BOOL "Prevent multiple searches for Python and instead cache the results.")

  if(DISABLE_GIL)
    message(STATUS "Disabling Python GIL")
    add_compile_definitions(Py_GIL_DISABLED)
  endif()

  # top-level call to find Python
  find_package(Python 3.10 REQUIRED COMPONENTS Interpreter Development.Module
                                               ${SKBUILD_SABI_COMPONENT})
endif()

# check if this is the master project or used via add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
  set(MQT_CORE_MASTER_PROJECT ON)
else()
  set(MQT_CORE_MASTER_PROJECT OFF)
endif()

option(MQT_CORE_INSTALL "Generate installation instructions for MQT Core"
       ${MQT_CORE_MASTER_PROJECT})
option(BUILD_MQT_CORE_TESTS "Build tests for the MQT Core project" ${MQT_CORE_MASTER_PROJECT})
option(BUILD_MQT_CORE_SHARED_LIBS "Build MQT Core libraries as shared libraries"
       ${BUILD_SHARED_LIBS})

# try to determine the project version
include(GetVersion)
get_mqt_core_version()

project(
  mqt-core
  LANGUAGES C CXX
  VERSION ${MQT_CORE_VERSION}
  DESCRIPTION "MQT Core - The Backbone of the Munich Quantum Toolkit")

include(cmake/ExternalDependencies.cmake)

# set the include directory for the build tree
set(MQT_CORE_INCLUDE_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include/mqt-core")

if(MQT_CORE_INSTALL)
  if(APPLE)
    set(BASEPOINT @loader_path)
  else()
    set(BASEPOINT $ORIGIN)
  endif()
  set(CMAKE_INSTALL_RPATH ${BASEPOINT} ${BASEPOINT}/${CMAKE_INSTALL_LIBDIR}
                          ${BASEPOINT}/../${CMAKE_INSTALL_LIBDIR})
  set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
endif()

set(MQT_CORE_TARGET_NAME "mqt-core")

option(BUILD_MQT_CORE_MLIR "Build the MLIR submodule of the MQT Core project" OFF)
if(APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  set(BUILD_MQT_CORE_MLIR
      OFF
      CACHE BOOL
            "Disable MLIR build on macOS with GCC to avoid ABI issues between STL and libstdc++"
            FORCE)
  message(
    WARNING "Disabling MLIR build on macOS with GCC to avoid ABI issues between STL and libstdc++")
endif()
if(APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
  if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "17.0.0")
    set(BUILD_MQT_CORE_MLIR
        OFF
        CACHE
          BOOL
          "Disable MLIR build on macOS with Apple Clang < 17 due to missing complete C++20 support"
          FORCE)
    message(
      WARNING
        "Disabling MLIR build on macOS with Apple Clang < 17 due to missing complete C++20 support")
  endif()
endif()
if(BUILD_MQT_CORE_MLIR)
  include(SetupMLIR)
endif()
cmake_dependent_option(BUILD_MQT_CORE_QIR_RUNNER "Build the QIR runner of the MQT Core project" ON
                       "BUILD_MQT_CORE_MLIR" OFF)

# add main library code
add_subdirectory(src)

# add bindings code if enabled
if(BUILD_MQT_CORE_BINDINGS)
  add_subdirectory(bindings)
endif()

# add test code
if(BUILD_MQT_CORE_TESTS)
  enable_testing()
  include(GoogleTest)
  add_subdirectory(test)
endif()

option(BUILD_MQT_CORE_BENCHMARKS "Also build benchmarks for the MQT Core project" OFF)
if(BUILD_MQT_CORE_BENCHMARKS)
  add_subdirectory(eval)
endif()

if(BUILD_MQT_CORE_MLIR)
  add_subdirectory(mlir)

  # copy generated MLIR documentation
  add_custom_command(
    TARGET mlir-doc
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_BINARY_DIR}/docs/
            ${CMAKE_CURRENT_SOURCE_DIR}/docs/mlir/
    COMMAND ${CMAKE_COMMAND} -D DOCS_DIR:PATH=${CMAKE_CURRENT_SOURCE_DIR}/docs/mlir -P
            ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CleanMLIRDocs.cmake
    COMMENT "Copying and cleaning up generated MLIR documentation"
    VERBATIM)

endif()

if(MQT_CORE_MASTER_PROJECT)
  if(NOT TARGET mqt-core-uninstall)
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
                   ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake IMMEDIATE @ONLY)
    add_custom_target(mqt-core-uninstall COMMAND ${CMAKE_COMMAND} -P
                                                 ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
  endif()
else()
  set(mqt-core_FOUND
      TRUE
      CACHE INTERNAL "True if mqt-core is found on the system")
endif()
