# *****************************************************************************
# Copyright (c) 2025 AISS Group at Harbin Institute of Technology. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# *****************************************************************************

cmake_minimum_required(VERSION 3.22...3.30)
project(${SKBUILD_PROJECT_NAME} VERSION ${SKBUILD_PROJECT_VERSION} LANGUAGES CXX)

# CMake policies
if(POLICY CMP0135)
    cmake_policy(SET CMP0135 NEW)
endif()

# C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# FetchContent module (for fmt, spdlog, etc.)
include(FetchContent)

# ========== pybind11 (from pip, scikit-build-core auto-configures CMAKE_PREFIX_PATH) ==========
set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 CONFIG REQUIRED)

# ========== Python with NumPy ==========
find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)

# ========== CANN SDK (environment variable lookup) ==========
if(DEFINED ENV{ASCEND_TOOLKIT_HOME})
    set(ASCEND_CANN_PATH "$ENV{ASCEND_TOOLKIT_HOME}")
elseif(DEFINED ENV{ASCEND_HOME_PATH})
    set(ASCEND_CANN_PATH "$ENV{ASCEND_HOME_PATH}")
else()
    set(ASCEND_CANN_PATH "/usr/local/Ascend/ascend-toolkit/latest")
    message(WARNING "CANN SDK path not set. Using default: ${ASCEND_CANN_PATH}. "
                    "Set ASCEND_TOOLKIT_HOME or ASCEND_HOME_PATH environment variable.")
endif()
message(STATUS "CANN SDK: ${ASCEND_CANN_PATH}")

include_directories(${ASCEND_CANN_PATH}/include)
link_directories(${ASCEND_CANN_PATH}/lib64)

# NumPy include
include_directories(${Python_NumPy_INCLUDE_DIRS})

# ========== fmt (system package only) ==========
find_package(fmt REQUIRED PATHS /usr/lib64/cmake /usr/lib/cmake /usr/lib/aarch64-linux-gnu/cmake NO_DEFAULT_PATH)
message(STATUS "Using system fmt: ${fmt_VERSION}")

# ========== spdlog (system package only) ==========
find_package(spdlog REQUIRED PATHS /usr/lib64/cmake /usr/lib/cmake /usr/lib/aarch64-linux-gnu/cmake NO_DEFAULT_PATH)
message(STATUS "Using system spdlog: ${spdlog_VERSION}")
add_compile_definitions(SPDLOG_FMT_EXTERNAL)

# ========== Source directories ==========
include_directories(include)
add_subdirectory(csrc)
add_subdirectory(bindings/python)

if(SKBUILD)
    message(STATUS "Building with scikit-build-core")
endif()