add_afni_executable(1dSEM 1dSEM.c matrix.c sqrmat.c)
set_target_properties(1dSEM PROPERTIES PUBLIC_HEADER "matrix.h")
target_link_libraries(1dSEM AFNI::mri m)

foreach(
  target
  1dApar2mat
  1dAstrip
  1dBandpass
  1dBport
  1dCorrelate
  1dFlagMotion
  1dMarry
  1dTsort
  1dUpsample
  1dcat
  1ddot
  1dfft
  1dgenARMA11
  1dnorm
  1dsum
  1dsvd
  1dtranspose
  24swap
  2dcat
  2dImReg
  2perm
  2swap
  3dABoverlap
  3dAFNIto3D
  3dAFNItoANALYZE
  3dAFNItoNIFTI
  3dAFNItoNIML
  3dAFNItoRaw
  3dANALYZEtoAFNI
  3dAcost
  3dAnatNudge
  3dAnhist
  3dAttribute
  3dAutobox
  3dAutomask
  3dBrickStat
  3dCM
  3dClipLevel
  3dClustCount
  3dConformist
  3dConvolve
  3dCountSpikes
  3dDFT
  3dDTeig
  3dDTtoDWI
  3dDWItoDT
  3dDepthMap
  3dEmpty
  3dEntropy
  3dErrtsCormat
  3dExtractGroupInCorr
  3dExtrema
  3dFDR
  3dFFT
  3dFWHM
  3dFourier
  3dFriedman
  3dGetrow
  3dinfo
  3dIntracranial
  3dInvFMRI
  3dKruskalWallis
  3dLRflip
  3dLSS
  3dLocalBistat
  3dLocalSVD
  3dMannWhitney
  3dMean
  3dMedianFilter
  3dMultiThresh
  3dNormalityTest
  3dNotes
  3dOverlap
  3dPeriodogram
  3dPolyfit
  3dPval
  3dROIstats
  3dRank
  3dRankizer
  3dRetinoPhase
  3dRowFillin
  3dSetupGroupInCorr
  3dSharpen
  3dSpatNorm
  3dStatClust
  3dSynthesize
  3dTRfix
  3dTagalign
  3dTcat
  3dTcorrelate
  3dTfilter
  3dThreetoRGB
  3dTnorm
  3dToutcount
  3dToyProg
  3dTqual
  3dTshift
  3dTsmooth
  3dTsort
  3dTsplit4D
  3dTwotoComplex
  3dUndump
  3dUniformize
  3dUpsample
  3dWarp
  3dWarpDrive
  3dWavelets
  3dWilcoxon
  3dWinsor
  3dXYZcat
  3dZcat
  3dZcutup
  3dZeropad
  3dZregrid
  3daxialize
  3dbuc2fim
  3dbucket
  3dcopy
  3ddelay
  3ddot
  3ddup
  3dedgedog
  3dfim+
  3dnewid
  3dnoise
  3dnvals
  3dpc
  3drefit
  3drename
  3drotate
  3dsvm_linpredict
  3dTproject
  3dtoXdataset
  3dttest++
  3dvolreg
  4swap
  DTIStudioFibertoSegments
  FIRdesign
  GLTsymtest
  Ifile
  RSFgen
  abut
  apsearch
  bitvec
  byteorder
  cat_matvec
  cdf
  count
  dicom_hdr
  dicom_hinfo
  dicom_to_raw
  ent16
  epsim
  exx
  fdrval
  fftest
  float_scan
  from3d
  ftosh
  ge_header
  help_format
  ibinom
  im2niml
  images_equal
  imand
  imaver
  imcalc
  imcutup
  imdump
  immask
  imrotate
  imstack
  imstat
  imupsam
  mayo_analyze
  mritopgm
  mycat
  myget
  nicat
  niccc
  nidset
  nimel
  niml_feedme
  nimltest
  nsize
  plugout_drive
  plugout_ijk
  plugout_tt
  plugout_tta
  quotize
  rmz
  rotcom
  rtfeedme
  sfim
  siemens_vision
  sqwave
  stimband
  strblast
  test_powell
  testcox
  tfim
  uncomment
  uniq_images
  unu
  whereami
  whirlgif
)

  add_afni_executable(${target} ${target}.c)
  target_include_directories(${target} PRIVATE NIFTI::nifti2)
  target_link_libraries(${target} PRIVATE AFNI::mri NIFTI::nifti2 m)
endforeach(target)

# OMP targets with mri and either nifti2 or nifticdf
foreach(target
	3dAllineate
	3dAutoTcorrelate
	3dBandpass
	3dBlurInMask
	3dBlurToFWHM
	3dClustSim
	3dDegreeCentrality
	3dDespike
	3dFWHMx
	3dGroupInCorr
	3dLFCD
	3dLocalACF
	3dLocalHistog
	3dLocalPV
	3dLocalstat
	3dNwarpAdjust
	3dNwarpApply
	3dNwarpCalc
	3dNwarpCat
	3dNwarpFuncs
	3dNwarpXYZ
	3dRegAna
	3dREMLfit
	3dTcorr1D
	3dTcorrMap
	3dUnifize
	3dXClustSim
	afni_check_omp
	AlphaSim
)
  add_afni_executable(${target} ${target}.c)
  target_include_directories(${target} PRIVATE NIFTI::nifti2)
  target_link_libraries(${target}
  	PRIVATE
  		AFNI::mri
  		NIFTI::nifti2
  		NIFTI::nifticdf
  		$<TARGET_NAME_IF_EXISTS:OpenMP::OpenMP_C>
  		m
	)
	target_compile_definitions(${target}
	  PRIVATE
	  "$<IF:$<TARGET_EXISTS:OpenMP::OpenMP_C>,USE_OMP,>"
  )
endforeach(target)

# OMP targets with mri and either nifti2 or nifticdf and needs sparse_array.c
foreach(target 3dMSE 3dECM)
  add_afni_executable(${target} ${target}.c sparse_array.c)
  target_include_directories(${target} PRIVATE NIFTI::nifti2)
  target_link_libraries(${target}
  	PRIVATE
  		AFNI::mri
  		NIFTI::nifti2
  		NIFTI::nifticdf
  		$<TARGET_NAME_IF_EXISTS:OpenMP::OpenMP_C>
  		m
	)
	target_compile_definitions(${target}
	  PRIVATE
	  "$<IF:$<TARGET_EXISTS:OpenMP::OpenMP_C>,USE_OMP,>"
  )
endforeach(target)

add_afni_executable(3dQwarp 3dQwarp.c)
target_include_directories(3dQwarp PRIVATE NIFTI::nifti2)
target_link_libraries(3dQwarp PRIVATE AFNI::mri AFNI::3DEdge NIFTI::nifti2 $<TARGET_NAME_IF_EXISTS:OpenMP::OpenMP_C> m)
target_compile_definitions(3dQwarp
    PRIVATE
    "$<IF:$<TARGET_EXISTS:OpenMP::OpenMP_C>,USE_OMP,>"
)

foreach(i "" 2 3)
  set(target 3dANOVA${i})
  add_afni_executable(${target} ${target}.c 3dANOVA.lib)
  target_link_libraries(${target} PRIVATE AFNI::mri m)
endforeach(i)

add_afni_executable(3dmerge 3dmerge.c)
target_link_libraries(3dmerge PRIVATE AFNI::mri m)

foreach(
  target
  1dNLfit
  1deval
  1dmatcalc
  3dDetrend
  3dTstat
  3dcalc
  3dclust
  3dhistog
  3dmaskdump
  3dmatcalc
  3dproject
  ccalc
  waver
)
  add_afni_executable(${target} ${target}.c)
  target_compile_definitions(${target} PRIVATE "REPLACE_XT")
  target_link_libraries(${target} PRIVATE AFNI::mri m)
endforeach(target)

add_afni_executable(3dTfitter 3dTfitter.c thd_fitter.c thd_lasso.c)
add_afni_executable(
  3dfim "${CMAKE_CURRENT_BINARY_DIR}/configured_pcor/afni_pcor_float.c" 3dfim.c ts.c
)

add_afni_executable(fim2 $<TARGET_OBJECTS:pcor> fim2.c ts.c)

add_afni_executable(3danisosmooth 3danisosmooth.c DWIstructtensor.c)
target_link_libraries(3danisosmooth AFNI::mri m)

add_afni_executable(gwarp mri_gwarp.c)

add_afni_executable(3dttest 3dttest.c)
target_link_libraries(3dttest AFNI::mri m)

add_afni_executable(3dretroicor 3dretroicor.c retroicor.c)
target_link_libraries(3dretroicor PRIVATE AFNI::mri m)

foreach(target 3dfim 3dTfitter fim2 gwarp)
  target_link_libraries(${target} PRIVATE AFNI::mri NIFTI::nifti2 NIFTI::nifticdf m)
endforeach(target)

# link remaining executables from rickr
add_afni_executable(Dimon rickr/Dimon.c rickr/realtime.c)
add_afni_executable(Dimon1 rickr/Dimon1.c rickr/realtime1.c)
add_afni_executable(3dresample rickr/3dresample.c)
target_link_libraries(Dimon1 PRIVATE AFNI::mri NIFTI::niftiio)
target_link_libraries(Dimon PRIVATE AFNI::mri NIFTI::nifti2)


target_link_libraries(3dresample AFNI::mri AFNI::audio NIFTI::nifti2)



add_afni_executable(3dBrainSync 3dBrainSync.c)
target_link_libraries(3dBrainSync AFNI::mri)

add_afni_executable(3dMaskToASCII 3dMaskToASCII.c)
target_link_libraries(3dMaskToASCII AFNI::mri NIFTI::nifti2 NIFTI::nifticdf)

add_afni_executable(3dTto1D 3dTto1D.c)
target_link_libraries(3dTto1D AFNI::mri NIFTI::nifti2 NIFTI::nifticdf)

add_afni_executable(3dedge3 3dedge3.c)
target_link_libraries(3dedge3 AFNI::mri AFNI::3DEdge)

add_afni_executable(3dfractionize 3dfractionize.c)
target_link_libraries(3dfractionize AFNI::mri)

add_afni_executable(3dmaskSVD 3dmaskSVD.c)
target_link_libraries(3dmaskSVD AFNI::mri)

add_afni_executable(3dmask_tool 3dmask_tool.c)
target_link_libraries(3dmask_tool AFNI::mri)

add_afni_executable(3dmaskave 3dmaskave.c)
target_link_libraries(3dmaskave AFNI::mri)

add_afni_executable(3dmatmult 3dmatmult.c)
target_link_libraries(3dmatmult AFNI::mri)

add_afni_executable(
  afni_history
  afni_history.c
  afni_history_bpittman.c
  afni_history_christip.c
  afni_history_dglen.c
  afni_history_discoraj.c
  afni_history_gangc.c
  afni_history_laurenpd.c
  afni_history_ptaylor.c
  afni_history_rickr.c
  afni_history_rwcox.c
  afni_history_ziad.c
)

# optional external dep: cjpeg and djpeg are external dependencies. what needs them?

add_afni_executable(3dPVmap 3dPVmap.c)
set_target_properties(3dPVmap PROPERTIES PRIVATE_HEADER "mri_pvmap.c;despike_inc.c")
target_link_libraries(3dPVmap AFNI::mri NIFTI::nifti2)

add_afni_executable(3dGrayplot 3dGrayplot.c)
set_target_properties(3dGrayplot PROPERTIES PRIVATE_HEADER thd_dset_to_grayplot.c)
target_link_libraries(3dGrayplot AFNI::mri)

add_afni_executable(1dsound 1dsound.c)
set_target_properties(1dsound PROPERTIES PRIVATE_HEADER "cs_playsound.c;despike_inc.c")
target_link_libraries(
  1dsound PRIVATE AFNI::mri NIFTI::nifti2 NIFTI::nifticdf AFNI::audio
)


# AFNI models
foreach(mod PRF;PRF_6;PRF_6_BAD)
  set(target get_afni_model_${mod})
  add_afni_executable(${target} get_afni_model_${mod}.c)
  set_target_properties(
    ${target} PROPERTIES PRIVATE_HEADER model_conv_${mod}.c
  )
  target_link_libraries(${target} PRIVATE AFNI::mri)
endforeach()

# 3dTSgem won't be built
add_afni_executable(3dTSgen 3dTSgen.c)
set_target_properties( 3dTSgen PROPERTIES
PRIVATE_HEADER "NLfit.h;NLfit_model.h;NLfit.c;NLfit_model.c" )
target_link_libraries(3dTSgen AFNI::mri ${CMAKE_DL_LIBS})


if(COMP_GUI)
  add_afni_executable(Vecwarp Vecwarp.c)
  target_link_libraries(Vecwarp PRIVATE AFNI::mri)

   add_afni_executable(3dmaxima 3dmaxima.c maxima.c)
  target_link_libraries(3dmaxima PRIVATE AFNI::mrix)

  add_afni_executable(adwarp adwarp.c)
  target_link_libraries(adwarp PRIVATE AFNI::mrix)

  add_afni_executable(afni_vcheck afni_vcheck.c)
  target_link_libraries(afni_vcheck PRIVATE AFNI::mrix)

  add_afni_executable(3dExchange 3dExchange.c)
  target_link_libraries(3dExchange AFNI::mrix)

  add_afni_executable(3dDeconvolve $<TARGET_OBJECTS:parser> 3dDeconvolve.c)
  target_link_libraries(3dDeconvolve PRIVATE AFNI::coxplot AFNI::mrix f2c NIFTI::nifticdf m)

  add_afni_executable(3dDeconvolve_f $<TARGET_OBJECTS:parser> 3dDeconvolve.c)
  target_compile_definitions(3dDeconvolve_f PRIVATE "FLOATIZE")
  target_link_libraries(3dDeconvolve_f PRIVATE AFNI::coxplot AFNI::mrix f2c NIFTI::nifticdf m)

  add_afni_executable(FD2 $<TARGET_OBJECTS:pcorsh> csfft_AJ.c FD2.c mcw.c overfim.c ts.c)
  add_afni_executable(imreg $<TARGET_OBJECTS:pcor> imreg.c)
  add_afni_executable(1dplot 1dplot.c display.c)
  target_include_directories(1dplot PRIVATE AFNI::mri)
  target_link_libraries(
    1dplot PRIVATE AFNI::mrix AFNI::coxplot AFNI::eispack NIFTI::nifti2
  )

  add_afni_executable(1dgrayplot 1dgrayplot.c)
  target_link_libraries(
    1dgrayplot PRIVATE AFNI::mrix AFNI::coxplot
  )

  foreach(target FD2 imreg)
    target_link_libraries(
      ${target} PRIVATE AFNI::mrix  NIFTI::nifti2 NIFTI::nifticdf
    )
  endforeach(target)

  add_afni_executable(3dNLfim 3dNLfim.c)
  target_link_libraries(3dNLfim PRIVATE AFNI::mrix NIFTI::nifti2 ${CMAKE_DL_LIBS})

  add_afni_executable(to3d to3d.c)
  target_link_libraries(
    to3d PRIVATE AFNI::mrix
                 NIFTI::nifti2
  )


endif()
