
################################################################################
##                                                                            ##
##  This file is part of NCrystal (see https://mctools.github.io/ncrystal/)   ##
##                                                                            ##
##  Copyright 2015-2025 NCrystal developers                                   ##
##                                                                            ##
##  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.25...3.31)

if ( NOT SKBUILD_PROJECT_NAME )
  message( FATAL_ERROR "This CMakeLists.txt is only for scikit-build-core" )
endif()

project( "${SKBUILD_PROJECT_NAME}" VERSION "${SKBUILD_PROJECT_VERSION}" )

set ( ncrystal_skbld_autogendir "${PROJECT_SOURCE_DIR}/skbld_autogen" )

if ( EXISTS "${ncrystal_skbld_autogendir}/is_sdist.txt"
     AND NOT "${SKBUILD_STATE}" STREQUAL "sdist" )
  message(STATUS "Running from sdist - will not clean autogenerated files")
else()
  if ( IS_DIRECTORY "${ncrystal_skbld_autogendir}" )
    message( STATUS "Cleaning out ${ncrystal_skbld_autogendir}" )
    file( REMOVE_RECURSE "${ncrystal_skbld_autogendir}" )
  endif()
  file( MAKE_DIRECTORY "${ncrystal_skbld_autogendir}" )
  if ( "${SKBUILD_STATE}" STREQUAL "sdist" )
    file(TOUCH "${ncrystal_skbld_autogendir}/is_sdist.txt")
  endif()
endif()

set( pymoddir "${ncrystal_skbld_autogendir}/_ncrystal_verify" )
if ( "${SKBUILD_STATE}" STREQUAL "sdist"
    OR NOT EXISTS "${pymoddir}/__init__.py" )
  find_package( Python 3.8 COMPONENTS Interpreter REQUIRED )
    execute_process(
      COMMAND "${Python_EXECUTABLE}" "-BI" "${PROJECT_SOURCE_DIR}/generate.py"
      RESULT_VARIABLE status
    )
    if( status AND NOT status EQUAL 0 )
      message(FATAL_ERROR "Failure while trying to invoke generate.py")
    endif()
endif()
