#Cmake project file for uMat IC microstructure simulation software 
#By Robert Atwood
#$Id: CMakeLists.txt 1490 2010-07-29 22:56:53Z  $
PROJECT (uMatIC)
cmake_minimum_required(VERSION 2.6)

# Ensure that the source directory is referenced for #include files
include_directories(${uMatIC_SOURCE_DIR})
include_directories(${uMatIC_BINARY_DIR})
# Give the location of additional project files
# Functions for reading the external data files
add_subdirectory(./read_list)
link_directories(./read_list)
#add the default c flags

#Create the custom options. 
#Using UMAT prefix ensures that options are grouped together (alphabetically)
#and clarifies which options are custom for this project.

#If NEITHER of COMPRESS_BLOCKS or IMAGE_OUTPUT is selected then the compression 
#library zlib is not included. 
option(UMAT_COMPRESS_BLOCKS " Compile with block compression" OFF)
if(UMAT_COMPRESS_BLOCKS)
   set(COMPRESS_DEFS -DBL_COMPRESS)
endif(UMAT_COMPRESS_BLOCKS)

option(UMAT_IMAGE_OUTPUT " Compile with png image output" OFF)
if(UMAT_IMAGE_OUTPUT )
   set(IMAGE_DEFS -DIMAGEOUT -DPNG_OUTPUT)
endif(UMAT_IMAGE_OUTPUT )

option(UMAT_BUILD_READER " Compile ca_read utility" OFF)
option(UMAT_BUILD_MAIN " Compile the main program" ON)
option(UMAT_PROCAST_LIB " Compile shared library for Procast" OFF)
option(UMAT_PROCAST_STUB " Debugging stub subroutines for Pocast" OFF)
option(UMAT_DEBUG_MANYDUMPS " set the MANYDUMPS flag (for extra output)" OFF)
option(ELECTRIC_FENCE "use electric fence for debugging malloc" OFF)

if(UMAT_PROCAST_LIB)
   set(UMAT_BUILD_MAIN OFF)
   set(UMAT_BUILD_READER OFF)
set(PCFILES  user_rtns.c
           user_rtn_cell.c
           procast_userfuncs.c
           ca_feedback.c
   )
set(N_PRO_LCDEFS 
      -DPROCAST_HEATFLUX 
      -DPROCAST_CA
      -DPROCAST_3D
      -DUSER_2
      -DMYALLOC
#      -DALL_SIGS
#      -DVERBOSE_PROCAST 
#      -DDMALLOC 
#      -DNOFS_PROCAST  
#      -DQ_FEEDBACK_ROUTINE 
#      -DPROBE_CELL=1275 
   )
   if (UMAT_PROCAST_STUB)
       set(PRO_LCDEFS ${N_PRO_LCDEFS} -DPROCAST_STUBS)
   else (UMAT_PROCAST_STUB)
       set(PRO_LCDEFS ${N_PRO_LCDEFS})
   endif (UMAT_PROCAST_STUB)
endif(UMAT_PROCAST_LIB)

set(REG_LCDEFS
   -DLIST_ALL_NUC
   -DPRINT_AV_PROFILE
   -DGLOBAL_UND_GRO
   -DAV_DIFF_COEFF
   -DNOSOLID
   -DCELL_DIFF_ARR
   -DHISTO_NUC
   -DALWAYS_NUC
   -DRANSLEY
   -DTEST_LINE_INT
   -DWRITE_BLOCK
   -DINCONEL
   -DNLM_FORMULA
   -DCA_DOUBLE
   -DCLOCK
   -DALLSLICE
   -fPIC
)



#list of all the c files for this project
#this will need to use slightly more complicated syntax to define
#different projects e.g. ca_procast, sharing many of the same files.

set(MAINFILE ca_wrapper.c)
set(RMAINFILE ca_read_wrapper.c)


set(CFILES
   alloc_bb.c
   alloc_multicomp.c
   alloc_sb.c
   bigblock.c
   ca_histo.c
   calc_sb.c
   ca_solid.c
   castats.c
   curvature.c
   curvature_3D.c
   checkblock.c
   checkgas.c
   checks.c
   close_sb.c
   fcopymat.c
   fg_read.c
   fidap_interp_calc.c
   find_max.c
   find_nmols.c
   findroot.c
   freeblock.c
   gaussdev.c
   getxyz.c
   grain_functions.c
   handlers.c
   icopymat.c
   initcube.c
   initface.c
   init_sb_neigh.c
   init_nuc_mould.c
   mould_sources.c
   nbhd_def.c
   nuc_functions.c
   nuc_lookup.c
   open_sb.c
   output_ex.c
   output_img.c
   p_growth.c
   pnuc_function.c
   pore_multi.c
   pore_routines.c
   pore_setup_multi.c
   pr_lookup.c
   props.c
   rand_square.c
   rand_step.c
   rcs_id.c
   read_blocks.c
   read_cap_ca.c
   read_ca_procast.c
   read_ctrl.c
   readgeoplus.c
   readmat.c
   readphase.c
   read_sb.c
   recr_extra.c
   safeopen.c
   sb_boundary.c
   sb_ca_step.c
   sb_diffuse_alloy.c
   sb_diffuse_gas.c
   sb_diffuse_gas_poly.c
   sb_get_surface_cells.c
   sb_line_int.c
   sb_nuc.c
   sb_temp_calc.c
   setup_mould_src_function.c
   setup_temp_func.c
   solprops_reader.c
   solprops_writer.c
   step_output.c
   subblock.c
   SurCellRoutines.c
   thermo_trace_calc.c
   temp_calc_melt.c
   thermo_trace_init.c
   trans_interp_calc.c
   wfact_r_calc.c
   wfact_z_calc.c
   write_bb_values.c
   write_blocks.c
   write_block_values.c
   write_ctrl_values.c
   write_grain_histo.c
   write_header_ctrl.c
   write_matprop_values.c
   write_nprop_values.c
   write_POREprop_values.c
   write_pprop_values.c
   calc_solprops_poly.c
   sb_decentred_step.c
   sb_diffuse_alloy_decentred.c
   window_move.c
   sb_decentred_step_poly.c
   sb_diffuse_alloy_decentred_poly.c
   replacerand.c
   olderror.c
   ca_phy_calc.c
   plt_output.c
   vtk_output.c
   sb_max_uc.c
   ff_memoalloc.c
   ff_memoryall.c
   ff_initial.c
   ff_boundary.c
   ff_calc_temp.c
   ff_timestepcheck.c
   ff_vectortrans.c
   ff_newvelocity.c
   ff_ppe_iccgtest.c
   ff_imp_fgh.c
   ff_bc_cyc.c
   PPE_ICCG.c
   ff_ppesor.c
   ff_calc_velo.c
   phy_ca_memalloc.c
   phy_ca_tran.c  
   window_move_3d.c
   ext_readin.c
   ext_memalloc.c
)

#conditionally include the library files needed for ca_run, without the 'lib' prefix
# i.e. libz, libm, libpng, libgd and the library produced by the above subprojects

#Choose the system libraries needed depending upon the options
#We always need math library (but it may have a different name on some other systems)
if (UNIX)
set(SLIBS m )
endif()

#gather all the options into a single list variable
set(ALL_DEFS ${COMPRESS_DEFS} ${IMAGE_DEFS} ${REG_LCDEFS})
add_definitions(${ALL_DEFS})
# set the MANYDUMPS flag (for extra output)
if(UMAT_DEBUG_MANYDUMPS)
   add_definitions(-DMANYDUMPS)
endif(UMAT_DEBUG_MANYDUMPS)

if(ELECTRIC_FENCE)
   set(CMAKE_EXE_LINKER_FLAGS_DEBUG -lefence)
endif(ELECTRIC_FENCE)


   if(UMAT_COMPRESS_BLOCKS OR UMAT_IMAGE_OUTPUT)
#png depends upon zlib, gd depends upon png
   FIND_PACKAGE(ZLIB REQUIRED)
   IF (NOT ZLIB_FOUND)
     MESSAGE(FATAL " ZLib is Required for ZLib support in carun")
   ENDIF ()
	 set(CLIBS  ${ZLIB_LIBRARIES} )
	 include_directories(${ZLIB_INCLUDE_DIRS})
   endif(UMAT_COMPRESS_BLOCKS OR UMAT_IMAGE_OUTPUT)
   if(UMAT_IMAGE_OUTPUT)
      FIND_PACKAGE(PNG REQUIRED)
      IF (NOT PNG_FOUND)
        MESSAGE(FATAL "PNG is required when UMAT_IMAGE_OUTPUT is enabled")
      endif()
      set(IMLIBS ${PNG_LIBRRARIES})
  
  #    set(IMLIBS png gd)
   endif(UMAT_IMAGE_OUTPUT)
   set(SLIBS ${SLIBS} ${CLIBS} ${IMLIBS} )
#end of library setting section

   if (UMAT_PROCAST_LIB)
      add_library(ca_umat SHARED ${CFILES} ${PCFILES})   
      ADD_DEPENDENCIES(ca_umat includer)
      target_link_libraries(ca_umat ${SLIBS} readlist)   
      add_definitions(${PRO_LCDEFS})
   endif(UMAT_PROCAST_LIB)

#ensure that the generated readlist library is linked in the project
   if (UMAT_BUILD_MAIN)
      add_executable(ca_run ${MAINFILE} ${CFILES})
      ADD_DEPENDENCIES(ca_run includer)
      target_link_libraries(ca_run ${SLIBS} readlist ${ZLIB})
   endif(UMAT_BUILD_MAIN)

   if(UMAT_BUILD_READER)
       add_executable(ca_read ${RMAINFILE} ${CFILES})
       ADD_DEPENDENCIES(ca_read includer)
       target_link_libraries(ca_read ${SLIBS} readlist)
   endif(UMAT_BUILD_READER)


#create an include-file for incorporating the build details in the executable
#this should also somehow include a dependency for getcflags.inc ? 
#
set(INCFILE getcflags.h)

#file(WRITE ${INCFILE} "Cflags = strdup(\"Configured using ${CMAKE_COMMAND} and ${CMAKE_C_COMPILER}\\")
#   file(APPEND ${INCFILE} "\n\\n")
#this line should include the test for GNU C compiler and whether the command 'head' is present
#   exec_program("${CMAKE_C_COMPILER} --version | head -1" OUTPUT_VARIABLE CCOUT)
#   file(APPEND ${INCFILE} "${CCOUT}\\")
#   file(APPEND ${INCFILE} "\n\\nCFLAGS used:\\n${ALL_DEFS}\\")
#   file(APPEND ${INCFILE} "\n\\nConfigure Username:\\t$ENV{USER}\\")
#   file(APPEND ${INCFILE} "\n\\nConfigure Host:\\t$ENV{HOST}\\")
#this line should include the test whether the command 'svnversion' is present
#   exec_program("svnversion ${uMatIC_SOURCE_DIR}" OUTPUT_VARIABLE CCOUT)
#   file(APPEND ${INCFILE} "\n\\nSVN version:\\t${CCOUT}\\")
#this line should include a test for whether the command 'date' is present
#   exec_program("date" OUTPUT_VARIABLE CCOUT)
#   file(APPEND ${INCFILE} "\n\\nConfigure date:\\t${CCOUT}\\n\\")
#   file(APPEND ${INCFILE} "\n")

   ADD_CUSTOM_TARGET(includer
         COMMAND echo  '\#define GETCFLAGS \(\"\\' > ${INCFILE}
         COMMAND echo  'Build date: \\' >> ${INCFILE}
	 COMMAND echo `date` '\\' >> ${INCFILE}
         COMMAND echo  '\\nConfigure Username:\\t'$ENV{USER}'\\' >> ${INCFILE}
         COMMAND echo  '\\nConfigure Host:\\t'$ENV{HOSTNAME}'\\' >> ${INCFILE}
         COMMAND echo  '\\nCompiler Info:\\t' `${CMAKE_C_COMPILER} --version | head -1` '\\' >> ${INCFILE}
         COMMAND echo  '\\nConfigure CFLAGS:\\t'${ALL_DEFS}'\\' >> ${INCFILE}
         COMMAND echo  '\\nSVN Version:\\t' `svnversion ${uMatIC_SOURCE_DIR}` '\\'  >> ${INCFILE}
         COMMAND echo  '\\n\"\)' >> ${INCFILE}
         COMMAND echo  '\#define GETSVNINFO \(\"\\' >> ${INCFILE}
         COMMAND echo  '\\nsvn status:\\n' `cd ${uMatIC_SOURCE_DIR} && svn status -u | while read a\; do echo  $$a'\\n'\;done` '\\'  >> ${INCFILE}
         COMMAND echo  '\\nsvn info:\\n' `cd ${uMatIC_SOURCE_DIR} && svn info | while read a\; do echo $$a'\\n'\;done` '\\' >> ${INCFILE}
         COMMAND echo  '\\n\"\)' >> ${INCFILE}
# WORKING_DIRECTORY ${uMatIC_SOURCE_DIR}
    )
