OPTION( BUILD_SWV2_OSX_APP "Build ShapeWorksView2 as an application." OFF )


###########################################
# Flags for Microsoft Compiler
###########################################
IF (WIN32 AND MSVC)
  ADD_DEFINITIONS(-D_WIN32_WINNT=0x0501 -DNTDDI_VERSION=0x05010000)
#  ADD_DEFINITIONS(-D_WIN32_WINNT=0x0501)
  ADD_DEFINITIONS(-DPSAPI_VERSION=1)
  # Disable Visual C++ Secure Warnings
  ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
  ADD_DEFINITIONS(-D_BIND_TO_CURRENT_VCLIBS_VERSION=1)
  ADD_DEFINITIONS(-D_BIND_TO_CURRENT_CRT_VERSION=1)
  SET(CMAKE_CXX_FLAGS "/Oi ${CMAKE_CXX_FLAGS} /MP4")
ENDIF (WIN32 AND MSVC)


#Match everything when analyzing dependencies
INCLUDE_REGULAR_EXPRESSION("^.*$")


SET(CMAKE_INCLUDE_CURRENT_DIR ON)

MESSAGE(STATUS "** QT_QMAKE_EXECUTABLE: ${QT_QMAKE_EXECUTABLE}")

# Instructs the MSVC toolset to use the precompiled header PRECOMPILED_HEADER
# for each source file given in the collection named by SOURCES.
function(enable_precompiled_headers PRECOMPILED_HEADER SOURCES)
  if(MSVC)
    set(files ${${SOURCES}})

    get_filename_component(pch_basename ${PRECOMPILED_HEADER} NAME_WE)
    set(pch_header ${CMAKE_CURRENT_SOURCE_DIR}/${PRECOMPILED_HEADER})
    set(pch_file ${CMAKE_CURRENT_BINARY_DIR}/${pch_basename}.cc)
    set_source_files_properties(${pch_file}  PROPERTIES COMPILE_FLAGS "/Yc\"${pch_header}\"")

    # Generate precompiled header translation unit
    IF(NOT EXISTS "${pch_file}")
      FILE(WRITE ${pch_file} "// Precompiled header unity generated by CMake\n")
      FILE(APPEND ${pch_file} "#include <${pch_header}>\n")
    ENDIF(NOT EXISTS "${pch_file}")

    # Update properties of source files to use the precompiled header.
    # Additionally, force the inclusion of the precompiled header at beginning of each source file.
    foreach(source_file ${files} )
      get_filename_component(extension ${source_file} EXT)
      if ("${extension}" STREQUAL ".cc")
        #message ("setting property on ${source_file}")
        set_source_files_properties(
          ${source_file}
          PROPERTIES COMPILE_FLAGS
          "/Yu\"${pch_header}\" /FI\"${pch_header}\""
          )
      endif("${extension}" STREQUAL ".cc")
    endforeach(source_file)

    # Finally, update the source file collection to contain the precompiled header translation unit
    set(${SOURCES} ${pch_file} ${${SOURCES}} PARENT_SCOPE)
  endif(MSVC)
endfunction(enable_precompiled_headers)



FILE(GLOB SHAPEWORKSVIEW_HDRS "*.h")
FILE(GLOB SHAPEWORKSVIEW_SRCS "*.cc" "*.cxx")
FILE(GLOB SHAPEWORKSVIEW_UIS  "*.ui")

SET( SHAPEWORKSVIEW_RCS
     ShapeWorksView2.qrc
)


# Turn on precompiled headers
enable_precompiled_headers(PrefixHeader.h SHAPEWORKSVIEW_SRCS)

# this command will generate rules that will run rcc on all files from SHAPEWORKSVIEW_RCS
# in result SHAPEWORKSVIEW_RC_SRCS variable will contain paths to files produced by rcc
QT4_ADD_RESOURCES( SHAPEWORKSVIEW_RC_SRCS ${SHAPEWORKSVIEW_RCS} )

# this will run uic on .ui files:
QT4_WRAP_UI( SHAPEWORKSVIEW_UI_HDRS ${SHAPEWORKSVIEW_UIS} )

# and finally this will run moc:
QT4_WRAP_CPP( SHAPEWORKSVIEW_MOC_SRCS ${SHAPEWORKSVIEW_HDRS} )

# we need this to be able to include headers produced by uic in our code
# (CMAKE_BINARY_DIR holds a path to the build directory)
INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR} )
#INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} )


# For Apple set the icns file containing icons
IF(APPLE)
  # set how it shows up in the Info.plist file
  SET(MACOSX_BUNDLE_ICON_FILE ShapeWorksView2.icns) 
  # set where in the bundle to put the icns file
  SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/ShapeWorksView2.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
  # include the icns file in the target
  SET(SHAPEWORKSVIEW_SRCS ${SHAPEWORKSVIEW_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/ShapeWorksView2.icns)
ENDIF(APPLE)

IF(WIN32)
  SET(SHAPEWORKSVIEW_SRCS ${SHAPEWORKSVIEW_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/ShapeWorksView2.rc)
ENDIF(WIN32)

if(MSVC)
  SOURCE_GROUP(moc_sources FILES ${SHAPEWORKSVIEW_MOC_SRCS})
endif(MSVC)


# here we instruct CMake to build SHAPEWORKSVIEW executable from all of the source files

IF ( BUILD_SWV2_OSX_APP )
  ADD_EXECUTABLE( ShapeWorksView2 MACOSX_BUNDLE WIN32 
    ${SHAPEWORKSVIEW_SRCS}
    ${SHAPEWORKSVIEW_HDRS}
    ${SHAPEWORKSVIEW_MOC_SRCS} 
    ${SHAPEWORKSVIEW_RC_SRCS} 
    ${SHAPEWORKSVIEW_UI_HDRS} 
    ${SHAPEWORKSVIEW_MOC_HDRS}
  )
ELSE ( BUILD_SWV2_OSX_APP )
  ADD_EXECUTABLE( ShapeWorksView2 WIN32
    ${SHAPEWORKSVIEW_SRCS}
    ${SHAPEWORKSVIEW_HDRS}
    ${SHAPEWORKSVIEW_MOC_SRCS} 
    ${SHAPEWORKSVIEW_RC_SRCS} 
    ${SHAPEWORKSVIEW_UI_HDRS} 
    ${SHAPEWORKSVIEW_MOC_HDRS}
  )
ENDIF ( BUILD_SWV2_OSX_APP )


TARGET_LINK_LIBRARIES( ShapeWorksView2
  ${QT_LIBRARIES} 
  ${ITK_LIBRARIES}
  QVTK
  ITKParticleSystem tinyxml
  vtkCommon vtkGraphics vtkImaging vtkRendering vtkWidgets 
  )



# INSTALLATION AND PACKAGING
SET(plugin_dest_dir bin)
SET(qtconf_dest_dir bin)
SET(APPS "\${CMAKE_INSTALL_PREFIX}/bin/ShapeWorksView2")
IF(APPLE)
  SET(plugin_dest_dir ShapeWorksView2.app/Contents/MacOS)
  SET(qtconf_dest_dir ShapeWorksView2.app/Contents/Resources)
  SET(contents_dest_dir ShapeWorksView2.app/Contents)
  SET(APPS "\${CMAKE_INSTALL_PREFIX}/ShapeWorksView2.app")
ENDIF(APPLE)
IF(WIN32)
  SET(APPS "\${CMAKE_INSTALL_PREFIX}/bin/ShapeWorksView2.exe")
ENDIF(WIN32)



#--------------------------------------------------------------------------------
# Install the SHAPEWORKSVIEW application, on Apple, the bundle is at the root of the
# install tree, and on other platforms it'll go into the bin directory.
INSTALL(TARGETS ShapeWorksView2
    BUNDLE DESTINATION . COMPONENT Runtime
    RUNTIME DESTINATION bin COMPONENT Runtime
    )

#--------------------------------------------------------------------------------
# Install needed Qt plugins by copying directories from the qt installation
# One can cull what gets copied by using 'REGEX "..." EXCLUDE'
INSTALL(DIRECTORY "${QT_PLUGINS_DIR}/imageformats" DESTINATION ${plugin_dest_dir}/plugins COMPONENT Runtime)

#--------------------------------------------------------------------------------
# Install needed Qt .nib files and others
IF(APPLE)
INSTALL(DIRECTORY "${QT_QTGUI_LIBRARY_RELEASE}/Versions/Current/Resources" DESTINATION ${contents_dest_dir} COMPONENT Runtime)
ENDIF(APPLE)



#--------------------------------------------------------------------------------
# install a qt.conf file
# this inserts some cmake code into the install script to write the file
INSTALL(CODE "
    file(WRITE \"\${CMAKE_INSTALL_PREFIX}/${qtconf_dest_dir}/qt.conf\" \"\")
    " COMPONENT Runtime)

