# This file was generated by running
#
# python cmake/scripts/generate-cmakefiles from dolfinx/cpp
#
cmake_minimum_required(VERSION 3.21)

set(PROJECT_NAME demo_hyperelasticity)
project(${PROJECT_NAME} LANGUAGES C CXX)

if(NOT TARGET dolfinx)
  find_package(DOLFINX REQUIRED)
endif()

include(CheckSymbolExists)
set(CMAKE_REQUIRED_INCLUDES ${PETSC_INCLUDE_DIRS})
check_symbol_exists(PETSC_USE_COMPLEX petscsystypes.h PETSC_SCALAR_COMPLEX)
if(PETSC_SCALAR_COMPLEX EQUAL 1)
  message(STATUS "** This demo does not support complex mode")
else()
  check_symbol_exists(PETSC_USE_REAL_DOUBLE petscsystypes.h PETSC_REAL_DOUBLE)
  if(PETSC_REAL_DOUBLE EQUAL 1)
    set(SCALAR_TYPE "--scalar_type=float64")
  else()
    set(SCALAR_TYPE "--scalar_type=float32")
  endif()

  # Add target to compile UFL files
  add_custom_command(
    OUTPUT hyperelasticity.c
    COMMAND ffcx ${CMAKE_CURRENT_SOURCE_DIR}/hyperelasticity.py ${SCALAR_TYPE}
    VERBATIM
    DEPENDS hyperelasticity.py
    COMMENT "Compile hyperelasticity.py using FFCx"
  )

  set(CMAKE_INCLUDE_CURRENT_DIR ON)

  add_executable(${PROJECT_NAME} main.cpp ${CMAKE_CURRENT_BINARY_DIR}/hyperelasticity.c)
  target_link_libraries(${PROJECT_NAME} dolfinx)

  # Set C++20 standard
  target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
  set(CMAKE_CXX_EXTENSIONS OFF)

  # Do not throw error for 'multi-line comments' (these are typical in rst which
  # includes LaTeX)
  include(CheckCXXCompilerFlag)
  check_cxx_compiler_flag("-Wno-comment" HAVE_NO_MULTLINE)
  set_source_files_properties(
    main.cpp
    PROPERTIES
      COMPILE_FLAGS
      "$<$<BOOL:${HAVE_NO_MULTLINE}>:-Wno-comment -Wall -Wextra -pedantic -Werror>"
  )

  # Test targets (used by DOLFINx testing system)
  set(TEST_PARAMETERS2 -np 2 "./${PROJECT_NAME}")
  set(TEST_PARAMETERS3 -np 3 "./${PROJECT_NAME}")
  add_test(NAME ${PROJECT_NAME}_mpi_2 COMMAND "mpirun" ${TEST_PARAMETERS2})
  add_test(NAME ${PROJECT_NAME}_mpi_3 COMMAND "mpirun" ${TEST_PARAMETERS3})
  add_test(NAME ${PROJECT_NAME}_serial COMMAND ${PROJECT_NAME})
endif()
