
project(mathutils)

# avoid having empty buildtype
set(CMAKE_BUILD_TYPE_INIT Release)

set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS
  $<$<CONFIG:Debug>:DEBUG;_DEBUG>
  $<$<CONFIG:Release>:NDEBUG>
  $<$<CONFIG:MinSizeRel>:NDEBUG>
  $<$<CONFIG:RelWithDebInfo>:NDEBUG>
)

cmake_policy(SET CMP0003 NEW)
cmake_policy(SET CMP0005 NEW)

cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 20)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin CACHE INTERNAL "" FORCE )
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib CACHE INTERNAL "" FORCE )

# needed for python lib
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -funsigned-char")
endif()

if(MSVC)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /J")
endif()

include(TestBigEndian)
test_big_endian(_SYSTEM_BIG_ENDIAN)
if(_SYSTEM_BIG_ENDIAN)
  add_definitions(-D__BIG_ENDIAN__)
else()
  add_definitions(-D__LITTLE_ENDIAN__)
endif()
unset(_SYSTEM_BIG_ENDIAN)

# -----------------------------------------------------------------------------
# configure python

if(NOT DEFINED PYTHON_ROOT_DIR)
  execute_process(
    COMMAND python3 -c "import sysconfig; print(sysconfig.get_config_var('prefix'))"
    OUTPUT_VARIABLE _prefix
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  set(PYTHON_ROOT_DIR "${_prefix}" CACHE PATH INTERNAL)
  unset(_prefix)
endif()

if(NOT DEFINED PYTHON_SITE_PACKAGES)
  execute_process(
    COMMAND python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))"
    OUTPUT_VARIABLE _site_packages
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  set(PYTHON_SITE_PACKAGES "${_site_packages}" CACHE PATH INTERNAL)
  unset(_site_packages)
endif()

if(NOT DEFINED PYTHON_INCLUDE_DIR)
  execute_process(
    COMMAND python3 -c "import sysconfig; print(sysconfig.get_path('include'))"
    OUTPUT_VARIABLE _inc_dir
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  set(PYTHON_INCLUDE_DIR "${_inc_dir}" CACHE PATH INTERNAL)
  unset(_inc_dir)
endif()

# set standard cmake/python names, uncached
set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")


# -----------------------------------------------------------------------------
# stubs

# needed so we don't attempt to do clever back-trace
# (not included in stand-alone version).
add_definitions(-DWITH_ASSERT_ABORT)

set(SRC
  src/stubs/stubs.cc

  src/stubs/BLI_memarena.h
  src/stubs/MEM_guardedalloc.h
  src/stubs/MEM_sys_types.h
)

include_directories(
  src/stubs
)

add_library(stubs_lib ${SRC})

# -----------------------------------------------------------------------------
# Eigen3 (required by math_matrix.cc)

find_package(Eigen3 REQUIRED)

# -----------------------------------------------------------------------------
# blenlib

add_definitions(-DMATH_STANDALONE)

set(SRC_INLINE
  src/blenlib/intern/math_base_inline.cc
  src/blenlib/intern/math_base_safe_inline.cc
  src/blenlib/intern/math_bits_inline.cc
  src/blenlib/intern/math_color_blend_inline.cc
  src/blenlib/intern/math_color_inline.cc
  src/blenlib/intern/math_geom_inline.cc
  src/blenlib/intern/math_vector_inline.cc
)

set(SRC
  ${SRC_INLINE}
  src/blenlib/intern/BLI_assert.cc
  src/blenlib/intern/math_base.cc
  src/blenlib/intern/math_color.cc
  src/blenlib/intern/math_geom.cc
  src/blenlib/intern/math_matrix.cc
  src/blenlib/intern/math_matrix_c.cc
  src/blenlib/intern/math_rotation_c.cc
  src/blenlib/intern/task_range.cc
  src/blenlib/intern/math_vector.cc

  src/blenlib/BLI_assert.h
  src/blenlib/BLI_compiler_attrs.h
  src/blenlib/BLI_compiler_compat.h
  src/blenlib/BLI_compiler_typecheck.h
  src/blenlib/BLI_dynstr.h
  src/blenlib/BLI_math_base.h
  src/blenlib/BLI_allocator.hh
  src/blenlib/BLI_array.hh
  src/blenlib/BLI_build_config.h
  src/blenlib/BLI_function_ref.hh
  src/blenlib/BLI_hash_fwd.hh
  src/blenlib/BLI_index_range.hh
  src/blenlib/BLI_kdtree.hh
  src/blenlib/BLI_lazy_threading.hh
  src/blenlib/BLI_kdtree_types.hh
  src/blenlib/BLI_math_angle_types.hh
  src/blenlib/BLI_math_axis_angle_types.hh
  src/blenlib/BLI_math_base.hh
  src/blenlib/BLI_math_base_safe.h
  src/blenlib/BLI_math_basis_types.hh
  src/blenlib/BLI_math_bits.h
  src/blenlib/BLI_math_constants.h
  src/blenlib/BLI_memory_utils.hh
  src/blenlib/BLI_math_color.h
  src/blenlib/BLI_math_color.hh
  src/blenlib/BLI_math_color_blend.h
  src/blenlib/BLI_math_euler_types.hh
  src/blenlib/BLI_math_geom.h
  src/blenlib/BLI_math_inline.h
  src/blenlib/BLI_math_matrix.h
  src/blenlib/BLI_math_axis_angle.hh
  src/blenlib/BLI_math_euler.hh
  src/blenlib/BLI_math_matrix.hh
  src/blenlib/BLI_math_matrix_types.hh
  src/blenlib/BLI_math_quaternion_types.hh
  src/blenlib/BLI_math_quaternion.hh
  src/blenlib/BLI_math_rotation.h
  src/blenlib/BLI_math_rotation.hh
  src/blenlib/BLI_math_rotation_types.hh
  src/blenlib/BLI_math_solvers.h
  src/blenlib/BLI_math_statistics.h
  src/blenlib/BLI_math_vector.h
  src/blenlib/BLI_math_vector.hh
  src/blenlib/BLI_color_types.hh
  src/blenlib/BLI_math_vector_swizzle.hh
  src/blenlib/BLI_math_vector_types.hh
  src/blenlib/BLI_math_vector_unroll.hh
  src/blenlib/BLI_random_access_iterator_mixin.hh
  src/blenlib/BLI_simd.hh
  src/blenlib/BLI_span.hh
  src/blenlib/BLI_unroll.hh
  src/blenlib/BLI_utility_mixins.hh
  src/blenlib/BLI_strict_flags.h
  src/blenlib/BLI_task.hh
  src/blenlib/BLI_task_size_hints.hh
  src/blenlib/BLI_sys_types.h
  src/blenlib/BLI_system.h
  src/blenlib/BLI_utildefines.h
  src/blenlib/BLI_utildefines_variadic.h
  src/blenlib/BLI_vector.hh

  # DNA (deps)
  src/makesdna/DNA_vec_types.h
)
set_source_files_properties(${SRC_INLINE} PROPERTIES HEADER_FILE_ONLY 1)
unset(SRC_INLINE)

include_directories(
  src/blenlib
  src/makesdna
)

include_directories(
  SYSTEM
  ${PYTHON_INCLUDE_DIRS}
)

add_library(blenlib_lib ${SRC})
target_link_libraries(blenlib_lib Eigen3::Eigen)

# -----------------------------------------------------------------------------
# mathutils

set(SRC
  src/mathutils/mathutils.cc
  src/mathutils/mathutils_Color.cc
  src/mathutils/mathutils_Euler.cc
  src/mathutils/mathutils_Matrix.cc
  src/mathutils/mathutils_Quaternion.cc
  src/mathutils/mathutils_Vector.cc
  src/mathutils/mathutils_geometry.cc
  src/mathutils/mathutils_interpolate.cc
  src/mathutils/mathutils_kdtree.cc

  src/mathutils/mathutils.hh
  src/mathutils/mathutils_Color.hh
  src/mathutils/mathutils_Euler.hh
  src/mathutils/mathutils_Matrix.hh
  src/mathutils/mathutils_Quaternion.hh
  src/mathutils/mathutils_Vector.hh
  src/mathutils/mathutils_geometry.hh
  src/mathutils/mathutils_interpolate.hh
  src/mathutils/mathutils_kdtree.hh

  src/generic/py_capi_utils.cc
  src/generic/py_capi_utils.hh
  src/generic/python_compat.cc
  src/generic/python_compat.hh
  src/generic/python_utildefines.hh
)

include_directories(
  src/mathutils
  src/generic
)

# NOTE: The C-era poison macros for MEM_mallocN etc. are not compatible with C++
# (conflicting declarations). The stubs in stubs.cc provide assert-on-call instead.


# -----------------------------------------------------------------------------
# python module

add_library(mathutils_module SHARED ${SRC})

set_target_properties(
  mathutils_module
  PROPERTIES
  PREFIX ""
  OUTPUT_NAME mathutils
  LINKER_LANGUAGE CXX
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
  RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin  # only needed on windows
)

if(WIN32)
  # python modules use this
  set_target_properties(
    mathutils_module
    PROPERTIES
    SUFFIX ".pyd"
  )
endif()

# link
target_link_libraries(mathutils_module
  blenlib_lib
  stubs_lib
)

# -----------------------------------------------------------------------------
# testing

enable_testing()

add_test(
  NAME mathutils_tests
  COMMAND python3 ${CMAKE_SOURCE_DIR}/tests/python/bl_pyapi_mathutils.py -v
)

set_tests_properties(mathutils_tests PROPERTIES
  ENVIRONMENT "PYTHONPATH=${CMAKE_BINARY_DIR}/bin"
)

# -----------------------------------------------------------------------------
# installation

if(WIN32)
  install(
    TARGETS mathutils_module
    RUNTIME
    DESTINATION "${PYTHON_SITE_PACKAGES}"
  )
else()
  install(
    TARGETS mathutils_module
    LIBRARY
    DESTINATION "${PYTHON_SITE_PACKAGES}"
  )
endif()
