# CMakeFiles.txt for the MINC2 progs
#
# Andrew Janke - a.janke@gmail.com

INCLUDE_DIRECTORIES(Proglib)

LINK_LIBRARIES( ${LIBMINC_LIBRARIES} )
ADD_DEFINITIONS(-DHAVE_CONFIG_H)

IF(NOT MINC_TOOLS_EXTERNALLY_CONFIGURED)
  FIND_PACKAGE(BISON)
  FIND_PACKAGE(FLEX)
ENDIF(NOT MINC_TOOLS_EXTERNALLY_CONFIGURED)

# all the progs
ADD_EXECUTABLE(invert_raw_image mincview/invert_raw_image.c)
ADD_EXECUTABLE(mincaverage mincaverage/mincaverage.c)
TARGET_LINK_LIBRARIES(mincaverage m)

IF(BISON_FOUND AND FLEX_FOUND)
   IF(NOT FL_LIBRARY) # A HACK to fix missing FLEX library
   SET(FLEX_LIBRARIES "")
   ENDIF(NOT FL_LIBRARY)


  include_directories(${CMAKE_CURRENT_BINARY_DIR} minccalc)
  
  BISON_TARGET(gram ${CMAKE_CURRENT_SOURCE_DIR}/minccalc/gram.y ${CMAKE_CURRENT_BINARY_DIR}/gram.c COMPILE_FLAGS "--debug")
  FLEX_TARGET(lex ${CMAKE_CURRENT_SOURCE_DIR}/minccalc/lex.l ${CMAKE_CURRENT_BINARY_DIR}/lex.c  )
  #ADD_FLEX_BISON_DEPENDENCY(gram lex)

  ADD_EXECUTABLE(minccalc 
                  minccalc/minccalc.c
                  minccalc/eval.c
                  minccalc/ident.c
                  minccalc/node.c
                  minccalc/optim.c
                  minccalc/scalar.c
                  minccalc/sym.c
                  minccalc/vector.c
                  ${FLEX_lex_OUTPUTS}
                  ${BISON_gram_OUTPUTS}
                 )

  TARGET_LINK_LIBRARIES(minccalc ${FLEX_LIBRARIES} m)

  INSTALL( TARGETS minccalc  DESTINATION bin)

  include_directories(${CMAKE_CURRENT_BINARY_DIR} mincgen)
  BISON_TARGET(ncgentab ${CMAKE_CURRENT_SOURCE_DIR}/mincgen/ncgentab.y ${CMAKE_CURRENT_BINARY_DIR}/ncgentab.c)
  FLEX_TARGET(ncgenyy ${CMAKE_CURRENT_SOURCE_DIR}/mincgen/ncgenyy.l ${CMAKE_CURRENT_BINARY_DIR}/ncgenyy.c)

  ADD_EXECUTABLE(mincgen mincgen/main.c
                            ${BISON_ncgentab_OUTPUTS}
                            ${FLEX_ncgenyy_OUTPUTS}
                            mincgen/escapes.c
                            mincgen/genlib.c
                            mincgen/getfill.c
                            mincgen/init.c
                            mincgen/load.c)
  INSTALL( TARGETS mincgen    DESTINATION bin)

  TARGET_LINK_LIBRARIES(mincgen ${FLEX_LIBRARIES})

ENDIF(BISON_FOUND AND FLEX_FOUND)

ADD_EXECUTABLE(mincconcat mincconcat/mincconcat.c)
ADD_EXECUTABLE(mincconvert mincconvert/mincconvert.c)
ADD_EXECUTABLE(minccopy minccopy/minccopy.c)

ADD_EXECUTABLE(mincdump mincdump/mincdump.c
                            mincdump/vardata.c
                            mincdump/dumplib.c)
                            
ADD_EXECUTABLE(mincexample1 mincexample/mincexample1.c)
ADD_EXECUTABLE(mincexample2 mincexample/mincexample2.c)
ADD_EXECUTABLE(mincexpand mincexpand/mincexpand.c)
ADD_EXECUTABLE(mincextract mincextract/mincextract.c
                            Proglib/minc_endian.c)
ADD_EXECUTABLE(mincinfo mincinfo/mincinfo.c)
ADD_EXECUTABLE(minclookup minclookup/minclookup.c)
TARGET_LINK_LIBRARIES(minclookup m)

ADD_EXECUTABLE(mincmakescalar mincmakescalar/mincmakescalar.c)
TARGET_LINK_LIBRARIES(mincmakescalar m)

ADD_EXECUTABLE(mincmakevector mincmakevector/mincmakevector.c)
ADD_EXECUTABLE(mincmath mincmath/mincmath.c)
TARGET_LINK_LIBRARIES(mincmath m)

ADD_EXECUTABLE(minc_modify_header minc_modify_header/minc_modify_header.c)

ADD_EXECUTABLE(mincresample mincresample/mincresample.c
                               mincresample/resample_volumes.c
                               Proglib/convert_origin_to_start.c)
TARGET_LINK_LIBRARIES(mincresample ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)

ADD_EXECUTABLE(mincreshape mincreshape/mincreshape.c
                              mincreshape/copy_data.c)

ADD_EXECUTABLE(mincstats mincstats/mincstats.c)
TARGET_LINK_LIBRARIES(mincstats m)

ADD_EXECUTABLE(minctoraw minctoraw/minctoraw.c
                            Proglib/minc_endian.c)

ADD_EXECUTABLE(mincwindow mincwindow/mincwindow.c)


ADD_EXECUTABLE(mincmorph mincmorph/mincmorph.c
                         mincmorph/kernel_io.c
                         mincmorph/kernel_ops.c 
                         mincmorph/kernel_io.h 
                         mincmorph/kernel_ops.h )
TARGET_LINK_LIBRARIES(mincmorph ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)


ADD_EXECUTABLE(mincsample mincsample/mincsample.c
                mincsample/mt19937ar.c
                mincsample/mt19937ar.h )
TARGET_LINK_LIBRARIES(mincsample ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)

ADD_EXECUTABLE(rawtominc rawtominc/rawtominc.c
                            Proglib/convert_origin_to_start.c)
TARGET_LINK_LIBRARIES(rawtominc m)

ADD_EXECUTABLE(voxeltoworld coordinates/voxeltoworld.c)
TARGET_LINK_LIBRARIES(voxeltoworld ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)

ADD_EXECUTABLE(worldtovoxel coordinates/worldtovoxel.c)
TARGET_LINK_LIBRARIES(worldtovoxel ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)

ADD_EXECUTABLE(transformtags xfm/transformtags.c)
TARGET_LINK_LIBRARIES(transformtags ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)

ADD_EXECUTABLE(xfmconcat xfm/xfmconcat.c)
TARGET_LINK_LIBRARIES(xfmconcat ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)

ADD_EXECUTABLE(xfminvert xfm/xfminvert.c)
TARGET_LINK_LIBRARIES(xfminvert ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)

ADD_EXECUTABLE(mincblob mincblob/mincblob.c)
TARGET_LINK_LIBRARIES(mincblob ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)

ADD_EXECUTABLE(minccmp minccmp/minccmp.c)
TARGET_LINK_LIBRARIES(minccmp ${VOLUME_IO_LIBRARIES} ${LIBMINC_LIBRARIES} m)


SET(MINC_TOOLS
   invert_raw_image 
   mincaverage
   mincconcat
   mincconvert
   minccopy
   mincdump
#   mincexample1
#   mincexample2
   mincblob 
   minccmp
   mincexpand
   mincextract
   mincinfo
   minclookup
   mincmakescalar
   mincmakevector
   mincmath
   minc_modify_header
   mincresample
   mincreshape
   mincstats
   minctoraw
   mincwindow
   mincmorph
   mincsample
   rawtominc
   voxeltoworld
   worldtovoxel
   transformtags
   xfmconcat
   xfminvert

)

# install progs
INSTALL(TARGETS ${MINC_TOOLS}
   DESTINATION bin)

add_custom_target (minc_tools 
 DEPENDS
 ${MINC_TOOLS}
 )   
   
# perl and shell scripts
INSTALL(PROGRAMS
   mincdiff/mincdiff  
   mincedit/mincedit  
   mincheader/mincheader  
   mincview/mincview
   PERMISSIONS 
    OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ  WORLD_EXECUTE WORLD_READ
   DESTINATION bin )

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/minchistory/minchistory.in ${CMAKE_CURRENT_BINARY_DIR}/minchistory.out @ONLY)
add_custom_target(minchistory chmod +x ${CMAKE_CURRENT_BINARY_DIR}/minchistory.out 
 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/minchistory/minchistory.in)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/mincpik/mincpik.in ${CMAKE_CURRENT_BINARY_DIR}/mincpik.out @ONLY)
add_custom_target(mincpik chmod +x ${CMAKE_CURRENT_BINARY_DIR}/mincpik.out 
 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/mincpik/mincpik.in)


INSTALL(PROGRAMS
  ${CMAKE_CURRENT_BINARY_DIR}/minchistory.out
  PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ  WORLD_EXECUTE WORLD_READ
  RENAME minchistory
  DESTINATION bin )

INSTALL(PROGRAMS
  ${CMAKE_CURRENT_BINARY_DIR}/mincpik.out
  PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ  WORLD_EXECUTE WORLD_READ
  RENAME mincpik
  DESTINATION bin )


INSTALL_MAN_PAGES( ${CMAKE_INSTALL_PREFIX}/man
  coordinates/voxeltoworld.man1
  mincaverage/mincaverage.man1
  mincblob/mincblob.man1
  minccalc/minccalc.man1
  minccmp/minccmp.man1
  mincconcat/mincconcat.man1
  mincconvert/mincconvert.man1
  minccopy/minccopy.man1
  mincdiff/mincdiff.man1
  mincdump/mincdump.man1
  mincedit/mincedit.man1
  mincexpand/mincexpand.man1
  mincextract/mincextract.man1
  mincgen/mincgen.man1
  mincheader/mincheader.man1
  mincinfo/mincinfo.man1
  minclookup/minclookup.man1
  mincmakescalar/mincmakescalar.man1
  mincmakevector/mincmakevector.man1
  mincmath/mincmath.man1
  minc_modify_header/minc_modify_header.man1
  mincresample/mincresample.man1
  mincreshape/mincreshape.man1
  mincsample/mincsample.man1
  mincstats/mincstats.man1
  minctoraw/minctoraw.man1
  mincview/invert_raw_image.man1
  mincview/mincview.man1
  mincwindow/mincwindow.man1
  rawtominc/rawtominc.man1
  xfm/transformtags.man1
  xfm/xfm2def.man1
  xfm/xfmconcat.man1
  xfm/xfmflip.man1
  xfm/xfminvert.man1
)
