# SPDX-FileCopyrightText: 2019 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later

# This suppresses the warning "This file includes at least one deprecated or antiquated
# header which may be removed without further notice at a future date", which is caused
# by the USD library including <ext/hash_set> on Linux. This has been reported at:
# https://github.com/PixarAnimationStudios/USD/issues/1057.
if(UNIX AND NOT APPLE)
  add_definitions(-D_GLIBCXX_PERMIT_BACKWARD_HASH)
endif()
if(WIN32)
  add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN -DBOOST_DEBUG_PYTHON)
endif()
add_definitions(-DBOOST_ALL_NO_LIB)

# Precompiled Linux libs are made with GCC, and USD uses some extensions
# which lead to an incompatible ABI for Clang. Using those extensions with
# Clang as well works around the issue.
if(UNIX AND NOT APPLE)
  if(CMAKE_C_COMPILER_ID MATCHES "Clang")
    if(EXISTS ${LIBDIR})
      add_definitions(-DARCH_HAS_GNU_STL_EXTENSIONS)
    endif()
  endif()
endif()

# USD headers use deprecated TBB headers, silence warning.
add_definitions(-DTBB_SUPPRESS_DEPRECATED_MESSAGES=1)

# Check if USD has the imaging headers available, if they are
# add a USD_HAS_IMAGING define so code can dynamically detect this.
# Cleanup of this variable is done at the end of the file since
# test code further down uses it to add imaging tests.
find_file(
  USD_IMAGING_HEADERS
  NAMES
    capsuleAdapter.h
  PATHS
    ${USD_INCLUDE_DIRS}
  PATH_SUFFIXES
    pxr/usdImaging/usdImaging/
  NO_DEFAULT_PATH
)

if(USD_IMAGING_HEADERS)
  add_definitions(-DUSD_HAS_IMAGING)
endif()

if(WIN32)
  # Some USD library headers trigger the "unreferenced formal parameter"
  # warning alert.
  # Silence them by restore warn C4100 back to w4
  remove_cc_flag("/w34100")
endif()

set(INC
  .
  ../common
  ../../blenkernel
  ../../blenloader
  ../../blentranslation
  ../../bmesh
  ../../depsgraph
  ../../editors/include
  ../../imbuf
  ../../makesrna
  ../../nodes
  ../../python/intern
  ../../windowmanager
  ../../../../intern/clog
  ../../../../intern/utfconv
  # RNA_prototypes.h
  ${CMAKE_BINARY_DIR}/source/blender/makesrna
)

set(INC_SYS
  ${USD_INCLUDE_DIRS}
  ${BOOST_INCLUDE_DIR}
  ${TBB_INCLUDE_DIR}
  ${PYTHON_INCLUDE_DIR}
)

set(SRC
  intern/usd_asset_utils.cc
  intern/usd_capi_export.cc
  intern/usd_capi_import.cc
  intern/usd_hierarchy_iterator.cc
  intern/usd_hook.cc
  intern/usd_writer_abstract.cc
  intern/usd_writer_camera.cc
  intern/usd_writer_curves.cc
  intern/usd_writer_hair.cc
  intern/usd_writer_light.cc
  intern/usd_writer_material.cc
  intern/usd_writer_mesh.cc
  intern/usd_writer_metaball.cc
  intern/usd_writer_transform.cc
  intern/usd_writer_volume.cc

  intern/usd_reader_camera.cc
  intern/usd_reader_curve.cc
  intern/usd_reader_geom.cc
  intern/usd_reader_light.cc
  intern/usd_reader_material.cc
  intern/usd_reader_mesh.cc
  intern/usd_reader_nurbs.cc
  intern/usd_reader_prim.cc
  intern/usd_reader_shape.cc
  intern/usd_reader_skeleton.cc
  intern/usd_reader_stage.cc
  intern/usd_reader_volume.cc
  intern/usd_reader_xform.cc
  intern/usd_skel_convert.cc

  usd.h
  usd.hh

  intern/usd_asset_utils.h
  intern/usd_exporter_context.h
  intern/usd_hash_types.h
  intern/usd_hierarchy_iterator.h
  intern/usd_hook.h
  intern/usd_writer_abstract.h
  intern/usd_writer_camera.h
  intern/usd_writer_curves.h
  intern/usd_writer_hair.h
  intern/usd_writer_light.h
  intern/usd_writer_material.h
  intern/usd_writer_mesh.h
  intern/usd_writer_metaball.h
  intern/usd_writer_transform.h
  intern/usd_writer_volume.h

  intern/usd_reader_camera.h
  intern/usd_reader_curve.h
  intern/usd_reader_geom.h
  intern/usd_reader_light.h
  intern/usd_reader_material.h
  intern/usd_reader_mesh.h
  intern/usd_reader_nurbs.h
  intern/usd_reader_prim.h
  intern/usd_reader_shape.h
  intern/usd_reader_skeleton.h
  intern/usd_reader_stage.h
  intern/usd_reader_volume.h
  intern/usd_reader_xform.h
  intern/usd_skel_convert.h
)

if(WITH_HYDRA)
  list(APPEND SRC
    hydra/camera.cc
    hydra/curves.cc
    hydra/hydra_scene_delegate.cc
    hydra/id.cc
    hydra/image.cc
    hydra/instancer.cc
    hydra/light.cc
    hydra/material.cc
    hydra/mesh.cc
    hydra/object.cc
    hydra/usd_scene_delegate.cc
    hydra/volume.cc
    hydra/volume_modifier.cc
    hydra/world.cc

    hydra/camera.h
    hydra/curves.h
    hydra/hydra_scene_delegate.h
    hydra/id.h
    hydra/image.h
    hydra/instancer.h
    hydra/light.h
    hydra/material.h
    hydra/mesh.h
    hydra/object.h
    hydra/usd_scene_delegate.h
    hydra/volume.h
    hydra/volume_modifier.h
    hydra/world.h
  )
endif()

set(LIB
  bf_blenkernel
  PRIVATE bf::blenlib
  PRIVATE bf::dna
  bf_imbuf
  PRIVATE bf::intern::guardedalloc
  bf_io_common
)

list(APPEND LIB
  ${BOOST_LIBRARIES}
  ${BOOST_PYTHON_LIBRARIES}
  ${PYTHON_LIBRARIES}
  ${USD_LIBRARIES}
  ${TBB_LIBRARIES}
)

if(WITH_OPENVDB)
  add_definitions(-DWITH_OPENVDB ${OPENVDB_DEFINITIONS})
  list(APPEND INC_SYS
    ${OPENVDB_INCLUDE_DIRS}
  )
  list(APPEND LIB
    ${OPENVDB_LIBRARIES}
  )
endif()

if(WITH_MATERIALX)
  add_definitions(-DWITH_MATERIALX)
  list(APPEND LIB MaterialXCore)
endif()

blender_add_lib(bf_io_usd "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")

# RNA_prototypes.h
add_dependencies(bf_io_usd bf_rna)

if(COMMAND target_precompile_headers)
  target_precompile_headers(bf_io_usd PRIVATE intern/usd_precomp.h)
endif()

if(WITH_GTESTS)
  set(TEST_SRC
    tests/usd_curves_test.cc
    tests/usd_export_test.cc
    tests/usd_stage_creation_test.cc
    tests/usd_usdz_export_test.cc
    intern/usd_writer_material.h
  )
  if(USD_IMAGING_HEADERS)
    list(APPEND TEST_SRC tests/usd_imaging_test.cc)
  endif()

  include_directories(intern)

  set(TEST_INC
  )
  set(TEST_LIB
  )
  include(GTestTesting)
  blender_add_test_lib(bf_io_usd_tests "${TEST_SRC}" "${INC};${TEST_INC}" "${INC_SYS}" "${LIB};${TEST_LIB}")
endif()

# In CMAKE version 3.21 and up, we can instead use the `NO_CACHE` option for
# `find_file` so we don't need to clear it from the cache here.
unset(USD_IMAGING_HEADERS CACHE)
