# aicad-fea: the FEA solver program of aicad (MFEM + hypre + METIS, Netgen meshing on Netgen's own OpenCascade).
# Built by the superbuild (superbuild/, pinned dependencies); can also be configured against an existing MFEM /
# Netgen / OpenCascade installation with MFEM_DIR, Netgen_DIR, OpenCASCADE_DIR and HYPRE_ROOT (or CMAKE_PREFIX_PATH:
# a conda-forge env, Homebrew, Spack; the app's discovery writes that command, aicad/fea/discovery.py). Linux and
# macOS (Apple clang, arm64); the app never ships the binary, users build it (#32, #98).
cmake_minimum_required(VERSION 3.21)
project(aicad_fea VERSION 1.0.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

set(AICAD_FEA_VARIANT "custom" CACHE STRING "Build name reported by --capabilities")
set(AICAD_FEA_DEPS_VERSIONS "" CACHE STRING "name=version,... of the pinned dependencies (superbuild)")
set(AICAD_FEA_DEPS "" CACHE PATH "Dependency prefix of the superbuild (libraries bundled from here)")
option(AICAD_FEA_GPL "This build links GPL components (local builds only, never redistributed)" OFF)
set(AICAD_FEA_SUPERBUILD_DIR "" CACHE PATH "Build directory of the superbuild that builds this (build-info.json)")
option(AICAD_FEA_BUNDLE_LIBS "Install the shared Netgen / OpenCascade libraries next to the program" OFF)
if(AICAD_FEA_BUNDLE_LIBS)
  set(_aicad_fea_link_rpath_default OFF)
else()
  set(_aicad_fea_link_rpath_default ON)
endif()

find_package(MFEM CONFIG QUIET)
if(NOT MFEM_FOUND)
  # an MFEM installed by its makefile (conda-forge's) has no CMake package, only share/mfem/config.mk
  find_file(MFEM_CONFIG_MK config.mk HINTS ${MFEM_DIR} ${MFEM_DIR}/share/mfem PATH_SUFFIXES share/mfem
    NO_CACHE REQUIRED)
  file(STRINGS ${MFEM_CONFIG_MK} _mfem_mk REGEX "^MFEM_(USE_[A-Z0-9_]+|VERSION_STRING|PREFIX)[ ]*=")
  foreach(_line IN LISTS _mfem_mk)
    string(REGEX MATCH "^(MFEM_[A-Z0-9_]+)[ ]*=[ ]*(.*)$" _ ${_line})
    string(STRIP "${CMAKE_MATCH_2}" _value)
    if(_value STREQUAL "YES")
      set(${CMAKE_MATCH_1} ON)
    elseif(_value STREQUAL "NO")
      set(${CMAKE_MATCH_1} OFF)
    else()
      set(${CMAKE_MATCH_1} "${_value}")
    endif()
  endforeach()
  find_path(MFEM_INCLUDE_DIRS mfem.hpp HINTS ${MFEM_PREFIX}/include REQUIRED)
  find_library(MFEM_LIBRARY mfem HINTS ${MFEM_PREFIX}/lib REQUIRED)
  set(MFEM_LIBRARIES ${MFEM_LIBRARY})
  if(MFEM_USE_METIS)
    find_library(MFEM_METIS_LIBRARY metis HINTS ${METIS_DIR}/lib ${MFEM_PREFIX}/lib REQUIRED)
    list(APPEND MFEM_LIBRARIES ${MFEM_METIS_LIBRARY})
  endif()
  if(MFEM_USE_MPI)
    find_package(MPI REQUIRED COMPONENTS CXX)
    list(APPEND MFEM_LIBRARIES MPI::MPI_CXX)
  endif()
  message(STATUS "aicad-fea: MFEM ${MFEM_VERSION_STRING} from ${MFEM_CONFIG_MK} (MPI: ${MFEM_USE_MPI})")
endif()
find_package(Netgen REQUIRED CONFIG)
find_package(OpenCASCADE REQUIRED CONFIG)
find_path(HYPRE_INCLUDE_DIR HYPRE.h HINTS ${HYPRE_ROOT}/include ${AICAD_FEA_DEPS}/include PATH_SUFFIXES hypre REQUIRED)
find_library(HYPRE_LIBRARY HYPRE HINTS ${HYPRE_ROOT}/lib ${AICAD_FEA_DEPS}/lib REQUIRED)
# an MFEM with SuiteSparse (e.g. Homebrew's) links GPL parts: fine for a build that stays on this machine, and said so
if(MFEM_USE_SUITESPARSE)
  set(AICAD_FEA_GPL ON)
endif()
# Eigen (MPL-2, headers only): the sparse direct solver (#32; src/direct.cpp). The superbuild always provides it;
# without it the build has no direct solver. METIS (in MFEM's link) orders it when its header is found too.
find_path(EIGEN3_INCLUDE_DIR Eigen/SparseCholesky HINTS ${AICAD_FEA_DEPS}/include/eigen3 PATH_SUFFIXES eigen3)
find_path(METIS_INCLUDE_DIR metis.h HINTS ${METIS_DIR}/include ${AICAD_FEA_DEPS}/include)

set(SOURCES
  src/main.cpp
  src/json.cpp
  src/events.cpp
  src/study.cpp
  src/anisotropy.cpp
  src/study_solver.cpp
  src/study_loads.cpp
  src/study_topology.cpp
  src/geometry.cpp
  src/geometry_check.cpp
  src/mesher.cpp
  src/fem.cpp
  src/mpi_util.cpp
  src/bc_prep.cpp
  src/mfem_mesh.cpp
  src/integrators.cpp
  src/linear_choice.cpp
  src/free_motion.cpp
  src/postprocess.cpp
  src/fem_tables.cpp
  src/pass_mesh.cpp
  src/pass_distribute.cpp
  src/pass_thermal.cpp
  src/pass_heat_transient.cpp
  src/pass_cases.cpp
  src/pass_write.cpp
  src/elastic_setup.cpp
  src/elastic_couplings.cpp
  src/elastic_modal.cpp
  src/static_setup.cpp
  src/static_linear.cpp
  src/newton.cpp
  src/newton_tangent.cpp
  src/newton_tangent_mpi.cpp
  src/newton_algebra.cpp
  src/tangent_check.cpp
  src/static_increment.cpp
  src/static_reactions.cpp
  src/static_control.cpp
  src/static_transient.cpp
  src/static_run.cpp
  src/welds.cpp
  src/static_wear.cpp
  src/static_follower.cpp
  src/follower.cpp
  src/bolts.cpp
  src/connectors.cpp
  src/contact.cpp
  src/contact_search.cpp
  src/contact_results.cpp
  src/contact_wear.cpp
  src/wear_geometry.cpp
  src/nonlinear.cpp
  src/plasticity.cpp
  src/plasticity_thermal.cpp
  src/finite_strain.cpp
  src/plasticity_results.cpp
  src/dynamics.cpp
  src/mirror.cpp
  src/estimator.cpp
  src/linsolve.cpp
  src/direct.cpp
  src/results.cpp
  src/aero_load.cpp
  src/selftest.cpp
  src/selftest_tangent.cpp
  src/selftest_heat.cpp
  src/topo_map.cpp
  src/topo_regions.cpp
  src/topo_density.cpp
  src/topo_physics.cpp
  src/topo_sens.cpp
  src/topo_eigen.cpp
  src/topo_eval.cpp
  src/topo_run.cpp
  src/topo_restart.cpp
  src/topo_void.cpp
  src/mma.cpp
  src/topo_write.cpp
  src/topo_preview.cpp
  src/study_shape.cpp
  src/mesh_data.cpp
  src/shape_terms.cpp
  src/shape_stress.cpp
  src/shape_sens.cpp
  src/shape_check.cpp
  src/topo_check.cpp)

# build identity (#114): git commit, dirty flag and date of this build, refreshed on every build (not only at
# configure time) into build_id.cpp and build-info.json; --capabilities reports them, the app compares them with
# its source tree (aicad/fea/solvers.py, "older than source"); build-info.json also says where the build tree is,
# so the app can offer to rebuild exactly this build
set(AICAD_FEA_BUILD_ID_CPP ${CMAKE_CURRENT_BINARY_DIR}/build_id.cpp)
set(AICAD_FEA_BUILD_INFO ${CMAKE_CURRENT_BINARY_DIR}/build-info.json)
add_custom_target(aicad_fea_build_id ALL
  COMMAND ${CMAKE_COMMAND}
    -DSRC=${CMAKE_CURRENT_LIST_DIR}
    -DOUT_CPP=${AICAD_FEA_BUILD_ID_CPP}
    -DOUT_JSON=${AICAD_FEA_BUILD_INFO}
    -DJSON_IN=${CMAKE_CURRENT_LIST_DIR}/build-info.json.in
    -DPROJECT_VERSION=${PROJECT_VERSION}
    -DAICAD_FEA_VARIANT=${AICAD_FEA_VARIANT}
    -DAICAD_FEA_GPL=${AICAD_FEA_GPL}
    "-DAICAD_FEA_DEPS_VERSIONS=${AICAD_FEA_DEPS_VERSIONS}"
    -DBUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}
    -DSUPERBUILD_DIR=${AICAD_FEA_SUPERBUILD_DIR}
    -DINSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
    -P ${CMAKE_CURRENT_LIST_DIR}/cmake/build_id.cmake
  BYPRODUCTS ${AICAD_FEA_BUILD_ID_CPP} ${AICAD_FEA_BUILD_INFO}
  COMMENT "aicad-fea build identity"
  VERBATIM)
add_executable(aicad-fea ${SOURCES} ${AICAD_FEA_BUILD_ID_CPP})
add_dependencies(aicad-fea aicad_fea_build_id)
target_include_directories(aicad-fea PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)

target_include_directories(aicad-fea PRIVATE ${MFEM_INCLUDE_DIRS} ${HYPRE_INCLUDE_DIR} ${NETGEN_INCLUDE_DIRS}
  ${OpenCASCADE_INCLUDE_DIR})
target_compile_definitions(aicad-fea PRIVATE
  OCCGEOMETRY HAVE_DLFCN_H
  AICAD_FEA_VERSION="${PROJECT_VERSION}"
  AICAD_FEA_VARIANT="${AICAD_FEA_VARIANT}"
  AICAD_FEA_DEPS_VERSIONS="${AICAD_FEA_DEPS_VERSIONS}"
  AICAD_FEA_OCCT_VERSION="${OpenCASCADE_VERSION}"
  AICAD_FEA_NETGEN_VERSION="${NETGEN_VERSION}"
  $<$<BOOL:${AICAD_FEA_GPL}>:AICAD_FEA_GPL=1>)
target_link_libraries(aicad-fea PRIVATE
  ${MFEM_LIBRARIES} ${HYPRE_LIBRARY}
  nglib ngcore
  TKernel TKMath TKG2d TKG3d TKGeomBase TKBRep TKGeomAlgo TKTopAlgo TKPrim TKBO TKBool TKOffset TKShHealing TKMesh)
# a Netgen built with Python (conda-forge's) leaves the Python symbols of ngcore to the process: link libpython
if(NETGEN_USE_PYTHON AND NETGEN_PYTHON_LIBRARIES)
  target_link_libraries(aicad-fea PRIVATE ${NETGEN_PYTHON_LIBRARIES})
endif()

if(EIGEN3_INCLUDE_DIR)
  target_include_directories(aicad-fea PRIVATE ${EIGEN3_INCLUDE_DIR})
  target_compile_definitions(aicad-fea PRIVATE AICAD_FEA_EIGEN=1)
  if(METIS_INCLUDE_DIR AND MFEM_USE_METIS)
    target_include_directories(aicad-fea PRIVATE ${METIS_INCLUDE_DIR})
    target_compile_definitions(aicad-fea PRIVATE AICAD_FEA_EIGEN_METIS=1)
  endif()
  message(STATUS "aicad-fea: direct solver from Eigen in ${EIGEN3_INCLUDE_DIR}")
else()
  message(WARNING "aicad-fea: Eigen not found (EIGEN3_INCLUDE_DIR): no direct solver in this build")
endif()

# STRUMPACK (BSD-3-Clause, with OpenBLAS: BSD-3-Clause): the multifrontal factorization of the direct solver
# (#32; serial / OpenMP builds). The superbuild provides it; without it Eigen's simplicial LDL^T factors.
option(AICAD_FEA_WITH_STRUMPACK "Use STRUMPACK for the direct solver when found" ON)
if(AICAD_FEA_WITH_STRUMPACK AND EIGEN3_INCLUDE_DIR AND NOT MFEM_USE_MPI)
  find_package(STRUMPACK CONFIG QUIET HINTS ${AICAD_FEA_DEPS})
  if(STRUMPACK_FOUND)
    target_link_libraries(aicad-fea PRIVATE STRUMPACK::strumpack)
    # (its Fortran LAPACK extensions: libgfortran, linked statically where the toolchain has it)
    include(CheckLanguage)
    check_language(Fortran)
    if(CMAKE_Fortran_COMPILER)
      enable_language(Fortran)
      if(APPLE)
        # Apple clang links: no -static-libgfortran, and libgfortran lives in the Fortran compiler's own directories
        # (Homebrew gcc); the build stays on this machine, so the dylib is referenced where it is
        target_link_directories(aicad-fea PRIVATE ${CMAKE_Fortran_IMPLICIT_LINK_DIRECTORIES})
        target_link_libraries(aicad-fea PRIVATE gfortran)
      else()
        target_link_libraries(aicad-fea PRIVATE $<IF:$<BOOL:${AICAD_FEA_STATIC_GFORTRAN}>,-static-libgfortran,> gfortran)
      endif()
    endif()
    target_compile_definitions(aicad-fea PRIVATE AICAD_FEA_STRUMPACK=1)
    # the supernodal Cholesky factorization of the SPD stiffness (#211) on the same OpenBLAS; STRUMPACK's LU
    # factors what it cannot (a matrix that is not positive definite)
    target_sources(aicad-fea PRIVATE src/cholesky.cpp src/cholesky_numeric.cpp)
    message(STATUS "aicad-fea: direct solver factorization by our supernodal Cholesky, STRUMPACK ${STRUMPACK_VERSION} "
      "for indefinite matrices")
  endif()
endif()

find_package(Threads REQUIRED)
target_link_libraries(aicad-fea PRIVATE Threads::Threads)
if(MFEM_USE_OPENMP OR AICAD_FEA_OPENMP)
  find_package(OpenMP REQUIRED)
  target_link_libraries(aicad-fea PRIVATE OpenMP::OpenMP_CXX)
endif()

# aicad-cfd (#217, docs/cfd.md): the CFD driver. Geometry, Netgen meshing with prism layers, SU2's files and the
# results are ours; SU2_CFD (LGPL-2.1) runs as a separate, unmodified program next to it (the superbuild builds it
# and passes AICAD_CFD_SU2; otherwise aicad-cfd finds one in AICAD_CFD_SU2 or on the PATH at run time).
option(AICAD_FEA_CFD "Also build aicad-cfd, the CFD driver (runs SU2_CFD)" ON)
set(AICAD_CFD_SU2 "" CACHE FILEPATH "SU2_CFD to install next to aicad-cfd (the superbuild's)")
option(AICAD_CFD_SU2_MPI "That SU2_CFD is an MPI build (aicad-cfd starts it through mpiexec)" OFF)
option(AICAD_CFD_SU2_OPENMP "That SU2_CFD is an OpenMP build (threads)" OFF)
set(AICAD_FEA_PROGRAMS aicad-fea)
if(AICAD_FEA_CFD)
  add_executable(aicad-cfd
    src/cfd/cfd_main.cpp
    src/cfd/cfd_study.cpp
    src/cfd/domain.cpp
    src/cfd/fluid_mesh.cpp
    src/cfd/su2_io.cpp
    src/cfd/su2_run.cpp
    src/cfd/cfd_run.cpp
    src/cfd/cfd_post.cpp
    src/cfd/cfd_sample.cpp
    src/cfd/cfd_selftest.cpp
    src/json.cpp
    src/events.cpp
    src/results.cpp
    ${AICAD_FEA_BUILD_ID_CPP})
  add_dependencies(aicad-cfd aicad_fea_build_id)
  target_include_directories(aicad-cfd PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src ${NETGEN_INCLUDE_DIRS}
    ${OpenCASCADE_INCLUDE_DIR})
  target_compile_definitions(aicad-cfd PRIVATE
    OCCGEOMETRY HAVE_DLFCN_H
    AICAD_FEA_VERSION="${PROJECT_VERSION}"
    AICAD_FEA_VARIANT="${AICAD_FEA_VARIANT}"
    AICAD_FEA_NETGEN_VERSION="${NETGEN_VERSION}"
    $<$<BOOL:${AICAD_CFD_SU2_MPI}>:AICAD_CFD_SU2_MPI=1>
    $<$<BOOL:${AICAD_CFD_SU2_OPENMP}>:AICAD_CFD_SU2_OPENMP=1>
    # (the build tree's fallback; an install has SU2_CFD next to aicad-cfd)
    $<$<BOOL:${AICAD_CFD_SU2}>:AICAD_CFD_SU2_BUILD_PATH="${AICAD_CFD_SU2}">)
  target_link_libraries(aicad-cfd PRIVATE nglib ngcore
    TKernel TKMath TKG2d TKG3d TKGeomBase TKBRep TKGeomAlgo TKTopAlgo TKPrim TKBO TKBool TKShHealing TKMesh
    Threads::Threads)
  if(NETGEN_USE_PYTHON AND NETGEN_PYTHON_LIBRARIES)
    target_link_libraries(aicad-cfd PRIVATE ${NETGEN_PYTHON_LIBRARIES})
  endif()
  list(APPEND AICAD_FEA_PROGRAMS aicad-cfd)
  if(AICAD_CFD_SU2)
    message(STATUS "aicad-cfd: SU2_CFD from ${AICAD_CFD_SU2}")
  else()
    message(STATUS "aicad-cfd: no SU2_CFD given (AICAD_CFD_SU2): found at run time in AICAD_CFD_SU2 / PATH")
  endif()
endif()

# relocatable install: the program finds lib/ next to bin/ (@loader_path is macOS's $ORIGIN); a build against a
# system / conda / Homebrew prefix also keeps that prefix's lib directory (its libraries are not copied)
if(APPLE)
  set(AICAD_FEA_ORIGIN "@loader_path")
else()
  set(AICAD_FEA_ORIGIN "$ORIGIN")
endif()
set_target_properties(${AICAD_FEA_PROGRAMS} PROPERTIES INSTALL_RPATH "${AICAD_FEA_ORIGIN}/../lib" BUILD_WITH_INSTALL_RPATH OFF)
# libraries that stay where they are installed (a build against conda / Homebrew / Spack, or the superbuild with
# AICAD_FEA_MFEM_PREFIX) are found through the link path in the RPATH; a plain superbuild install keeps only lib/
option(AICAD_FEA_LINK_RPATH "Keep the link directories in the installed RPATH" ${_aicad_fea_link_rpath_default})
if(AICAD_FEA_LINK_RPATH)
  set_target_properties(${AICAD_FEA_PROGRAMS} PROPERTIES INSTALL_RPATH_USE_LINK_PATH ON)
endif()
# an MPI wrapper's own -rpath (conda's mpicxx) would come before lib/ and load that prefix's Netgen / OpenCascade
# instead of the bundled ones: drop it (the MPI library directory follows lib/ through the link path above)
if(TARGET MPI::MPI_CXX AND AICAD_FEA_LINK_RPATH AND AICAD_FEA_BUNDLE_LIBS)
  get_target_property(_mpi_link MPI::MPI_CXX INTERFACE_LINK_OPTIONS)
  if(_mpi_link)
    string(REGEX REPLACE "-Wl(,|\\$<COMMA>)-rpath( -Wl(,|\\$<COMMA>)|,|\\$<COMMA>)[^ >;]*" "" _mpi_link "${_mpi_link}")
    set_target_properties(MPI::MPI_CXX PROPERTIES INTERFACE_LINK_OPTIONS "${_mpi_link}")
  endif()
endif()

include(GNUInstallDirs)
# "^<dir>/" matching <dir> regardless of case and slash direction (Windows paths)
function(aicad_fea_dir_regex out dir)
  string(REPLACE "\\" "/" p "${dir}")
  string(REGEX REPLACE "([][+.*()^$?|\\])" "\\\\\\1" p "${p}")
  set(r "")
  string(LENGTH "${p}" n)
  if(n GREATER 0)
    math(EXPR last "${n} - 1")
    foreach(i RANGE ${last})
      string(SUBSTRING "${p}" ${i} 1 c)
      string(TOLOWER "${c}" lo)
      string(TOUPPER "${c}" up)
      if(c STREQUAL "/")
        string(APPEND r "[/\\\\]")
      elseif(lo STREQUAL up)
        string(APPEND r "${c}")
      else()
        string(APPEND r "[${lo}${up}]")
      endif()
    endforeach()
  endif()
  set(${out} "^${r}[/\\\\]" PARENT_SCOPE)
endfunction()

if(AICAD_FEA_BUNDLE_LIBS AND WIN32)
  # Windows (#99): no rpaths, the DLLs the program loads go next to it in bin/: those of the dependency prefix
  # (superbuild: Netgen, OpenCascade), of CMAKE_PREFIX_PATH / vcpkg (conda's Library, a vcpkg triplet) and, for
  # MinGW, the compiler runtime (libgcc / libstdc++ / winpthreads / libgomp / libgfortran). System DLLs
  # (api-ms-win-*, System32) stay out.
  set(AICAD_FEA_DLL_DIRS)
  foreach(prefix IN LISTS AICAD_FEA_DEPS CMAKE_PREFIX_PATH)
    if(prefix)
      list(APPEND AICAD_FEA_DLL_DIRS "${prefix}/bin" "${prefix}/lib")
    endif()
  endforeach()
  if(DEFINED _VCPKG_INSTALLED_DIR AND DEFINED VCPKG_TARGET_TRIPLET)
    list(APPEND AICAD_FEA_DLL_DIRS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin")
  endif()
  if(MINGW)
    get_filename_component(AICAD_FEA_CXX_BIN "${CMAKE_CXX_COMPILER}" DIRECTORY)
    list(APPEND AICAD_FEA_DLL_DIRS "${AICAD_FEA_CXX_BIN}")
  endif()
  set(AICAD_FEA_DLL_KEEP)
  set(AICAD_FEA_DLL_DIRS_CM)
  foreach(dir IN LISTS AICAD_FEA_DLL_DIRS)
    file(TO_CMAKE_PATH "${dir}" dir_cm)
    list(APPEND AICAD_FEA_DLL_DIRS_CM "${dir_cm}")
    aicad_fea_dir_regex(re "${dir_cm}")
    list(APPEND AICAD_FEA_DLL_KEEP "${re}")
  endforeach()
  install(TARGETS ${AICAD_FEA_PROGRAMS} RUNTIME_DEPENDENCY_SET aicad_fea_deps RUNTIME DESTINATION bin)
  install(RUNTIME_DEPENDENCY_SET aicad_fea_deps
    DIRECTORIES ${AICAD_FEA_DLL_DIRS_CM}
    PRE_EXCLUDE_REGEXES "^api-ms-" "^ext-ms-" "^(kernel32|user32|advapi32|ws2_32|msvcrt|ucrtbase|shell32|ole32|bcrypt|ntdll|psapi|gdi32)\\.dll$"
    POST_INCLUDE_REGEXES ${AICAD_FEA_DLL_KEEP}
    POST_EXCLUDE_REGEXES ".*"
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION bin)
elseif(AICAD_FEA_BUNDLE_LIBS)
  # the shared libraries the program loads from the dependency prefix (Netgen, OpenCascade), next to it in lib/
  install(TARGETS ${AICAD_FEA_PROGRAMS} RUNTIME_DEPENDENCY_SET aicad_fea_deps RUNTIME DESTINATION bin)
  install(RUNTIME_DEPENDENCY_SET aicad_fea_deps
    DIRECTORIES ${AICAD_FEA_DEPS}/lib
    PRE_EXCLUDE_REGEXES "^lib(c|m|dl|rt|pthread|gcc_s|stdc\\+\\+|z)\\.so" "^ld-linux" "^/usr/lib/" "^/System/"
    POST_INCLUDE_REGEXES "^${AICAD_FEA_DEPS}/"
    POST_EXCLUDE_REGEXES ".*"
    LIBRARY DESTINATION lib)
else()
  install(TARGETS ${AICAD_FEA_PROGRAMS} RUNTIME DESTINATION bin)
endif()

# SU2_CFD next to aicad-cfd (an unmodified separate program, LGPL-2.1; its licence from the superbuild)
if(AICAD_FEA_CFD AND AICAD_CFD_SU2)
  install(PROGRAMS ${AICAD_CFD_SU2} DESTINATION bin)
endif()

# build info and licences of everything in the install (docs/licensing.md "FEA solver")
install(FILES ${AICAD_FEA_BUILD_INFO} DESTINATION share/aicad-fea)
install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/licenses/ DESTINATION share/aicad-fea/licenses OPTIONAL)
