cmake_minimum_required(VERSION 2.8.11)

if(POLICY CMP0025)
  #Clang and AppleClang are now unique compiler ids
  cmake_policy(SET CMP0025 NEW)
endif()


if (APPLE)
  if(NOT CMAKE_OSX_DEPLOYMENT_TARGET)
    message(WARNING "Ensure that CMAKE_OSX_SYSROOT, CMAKE_OSX_DEPLOYMENT_TARGET are set correctly")
    set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "By default, build for 64-bit Leopard")
  endif()
endif()

project(CMBSuperBuild)
set (SuperBuild_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/CMake")
set (SuperBuild_PROJECTS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Projects")
set (SuperBuild_BINARY_DIR ${CMBSuperBuild_BINARY_DIR})

option(BUILD_SHARED_LIBS "Build shared libraries" ON)

set(ep_list_separator "-+-")

set (platform)
if (APPLE)
  set (platform "apple")
elseif (UNIX)
  set (platform "unix")
elseif (WIN32)
  set (platform "win32")
endif()

#-----------------------------------------------------------------------------
# Cross compilation
set (CROSS_BUILD_STAGE "HOST" CACHE STRING
  "Cross compilation stage: either HOST(ie no crosscompile)|TOOLS|CROSS")
set_property(CACHE CROSS_BUILD_STAGE PROPERTY STRINGS
   "HOST" "TOOLS" "CROSS")

#-----------------------------------------------------------------------------
# Setup CMAKE_MODULE_PATH so that platform specific configurations are processed
# before the generic ones.
string(TOLOWER "${CROSS_BUILD_STAGE}" lstage)
set (CMAKE_MODULE_PATH
  "${SuperBuild_PROJECTS_DIR}/${platform}/${lstage}"
  "${SuperBuild_PROJECTS_DIR}/${platform}"
  "${SuperBuild_PROJECTS_DIR}/common"
  "${SuperBuild_PROJECTS_DIR}"
  "${SuperBuild_CMAKE_DIR}"
  "${CMAKE_CURRENT_SOURCE_DIR}"
  ${CMAKE_MODULE_PATH})

#-----------------------------------------------------------------------------
if (NOT CROSS_BUILD_STAGE STREQUAL "HOST")
  include(CrossCompilationMacros)

  #ask user to say what machine they are compiling onto
  #so we can get the right environment settings
  query_target_machine()

  if (CROSS_BUILD_STAGE STREQUAL "CROSS")
    #configure the platform dependent settings
    #64bit_build, static_only, mpi search path
    do_cross_platform_settings()

    #find location of hosttools or host build to get compilation tools from
    find_hosttools()
  endif()
endif()

include(CMakeDependentOption)

#-----------------------------------------------------------------------------
# Collect information about the build platform.
include(CheckTypeSize)

if (NOT CROSS_BUILD_STAGE STREQUAL "CROSS")
  check_type_size(void* VOID_PTR_SIZE BUILTIN_TYPES_ONLY)
  if (VOID_PTR_SIZE EQUAL 8)
    set (64bit_build TRUE)
  else ()
    set (64bit_build FALSE)
  endif()
endif()

# Set suffix to be used for generating archives. This ensures that the package
# files have decent names that we can directly upload to the website.
set (package_suffix_default "${CMAKE_SYSTEM_NAME}")
if (64bit_build)
  set (package_suffix_default "${package_suffix_default}-64bit")
else ()
  set (package_suffix_default "${package_suffix_default}-32bit")
endif()

# PACKAGE_SUFFIX: A string that can be set to the suffix you want to use for
# all the generated packages.
set(PACKAGE_SUFFIX "${package_suffix_default}"
  CACHE STRING "String to use as a suffix for generated packages")
mark_as_advanced(PACKAGE_SUFFIX)

include(paraview_version)

option(TRUST_SVN_CERTIFICATES_AUTOMATICALLY
  "Trust SVN certificates automatically" ON)
mark_as_advanced(TRUST_SVN_CERTIFICATES_AUTOMATICALLY)

#-----------------------------------------------------------------------------
# Setup some standard variables that control various locations and flags.
if (__BUILDBOT_INSTALL_LOCATION)
  set (install_location "${__BUILDBOT_INSTALL_LOCATION}")
else ()
  set (install_location "${CMAKE_CURRENT_BINARY_DIR}/install")
endif ()
set (download_location "${CMAKE_CURRENT_BINARY_DIR}/downloads"
     CACHE PATH "Location where source tar-balls are (to be) downloaded.")
mark_as_advanced(download_location)

if (WIN32)
  set (ldflags)
  set (cppflags)
  set (cxxflags)
  set (ld_library_path)
else ()
  # FIXME: currently, if any of these are empty, we have build issues on Unix.
  set (ldflags "$ENV{LDFLAGS} -L${install_location}/lib")
  if (NOT CROSS_BUILD_STAGE STREQUAL "CROSS")
    set (cppflags "$ENV{CPPFLAGS} -fPIC")
    set (cxxflags "$ENV{CXXFLAGS} -fPIC")
    set (cflags "$ENV{CFLAGS} -fPIC")
  endif()
  set (ld_library_path "${install_location}/lib:$ENV{LD_LIBRARY_PATH}")
endif()

if (UNIX AND NOT APPLE)
  # I am not exactly sure about the cause of these issues and how to cleanly
  # overcome them, however, on some linuxes when Cmake is built shared, we end
  # if we set LD_LIBRARY_PATH to the install/lib dir, we end up with errors
  # due to conflicts with system libz. So for now, I am making this an option.
  # By default we will let the superbuild scripts set LD_LIBRARY_PATH. However
  # if users get errors like: libz.so.1: no version information available, then
  # users should turn this flag off.
  option(PASS_LD_LIBRARY_PATH_FOR_BUILDS
         "Pass LD_LIBRARY_PATH to build scripts."
         ON)
  mark_as_advanced(PASS_LD_LIBRARY_PATH_FOR_BUILDS)
endif()

if (APPLE)
  set (cflags "${cflags} -arch x86_64 -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET} --sysroot=${CMAKE_OSX_SYSROOT} ")
  set (cppflags "${cppflags} -arch x86_64 -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET} --sysroot=${CMAKE_OSX_SYSROOT} ")
  set (cxxflags "${cxxflags} -arch x86_64 -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET} --sysroot=${CMAKE_OSX_SYSROOT} ")
endif()
set (prefix_path "${install_location}")

# merge in default/user-specified CMake flags.
set (ldflags "${CMAKE_SHARED_LINKER_FLAGS} ${ldflags}")
set (cppflags "${CMAKE_CXX_FLAGS} ${cppflags}")
set (cxxflags "${CMAKE_CXX_FLAGS} ${cxxflags}")
set (cflags "${CMAKE_C_FLAGS} ${cflags}")

string (STRIP "${ldflags}" ldflags)
string (STRIP "${cppflags}" cppflags)
string (STRIP "${cxxflags}" cxxflags)
string (STRIP "${cflags}"  cflags)

file(WRITE "${CMAKE_BINARY_DIR}/env.cmake" "#AUTOMATICALLY GENERATED FILE. This is used the set environment variables at configure time for cmake projects\n")
if(UNIX AND NOT APPLE AND PASS_LD_LIBRARY_PATH_FOR_BUILDS)
  file(APPEND "${CMAKE_BINARY_DIR}/env.cmake" "set( ENV{LD_LIBRARY_PATH} \"${ld_library_path}\")\n")
endif()

#-----------------------------------------------------------------------------
## Top-level options. These must be limited to a commonly used subset with
## default values being used for building ParaView binaries.
#option(PV_COMMAND_LINE_TOOLS_ONLY
#  "Turn this ON to build command lines tools alone." OFF)

if (WIN32)
  if ( (NOT "${CMAKE_GENERATOR}" MATCHES "^NMake.*$") OR
       (NOT "${CMAKE_GENERATOR}" MATCHES "^Visual Studio.*$"))
  # not use VS environment. We need to be pointed to nmake and devenv paths
  # since they are needed when building tools (qt, python, etc.)
    find_program(DEVENV_PATH devenv)
    find_program(NMAKE_PATH nmake)
    mark_as_advanced(DEVENV_PATH NMAKE_PATH)
  endif()
endif()
#-----------------------------------------------------------------------------
include (CMBModules)

include (versions)

#------------------------------------------------------------------------------
# enable testing support.
configure_file(
  "${SuperBuild_CMAKE_DIR}/CTestCustom.cmake.in"
  "${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake" @ONLY)
include(CTest)
#------------------------------------------------------------------------------

#-----------------------------------------------------------------------------
set (CM_PROJECTS_ALL)

option(USE_NONFREE_COMPONENTS "Enable the non-free parts of the superbuild" OFF)
mark_as_advanced(USE_NONFREE_COMPONENTS)

#must before any including of projects
option(USE_OPENGL2_RENDERING "Use OpenGL2 rendering" ON)
SetupCMBSuperBuild()

# Include all optional projects. These are the ones user can pick and choose.
# Some of these allow using system libraries too.
if (CROSS_BUILD_STAGE STREQUAL "CROSS")
  #include(crossnumpy)
  include(python)
  include(mesa)
  include(paraview)
elseif(CROSS_BUILD_STAGE STREQUAL "TOOLS")
  include(boost)
  include(png)
  include(zlib)
  include(python)
  include(paraview)
  include(portfwd)
else()
  include(boost)
  include(cgns)
  include(freetype)
  include(hdf5)
  include(libxml2)
  include(manta)
  include(mpi)
  include(png)
  include(python)
  include(qt)
  include(silo)
  include(szip)
  include(visitbridge)
  include(zlib)
endif()

if (UNIX AND NOT APPLE)
    include(fontconfig)
    include(mesa)
    include(osmesa)
endif()

if (UNIX)
    # Supported on Linux/Mac
    include(ffmpeg)
  endif()

# REMEMBER to tell VTK/ParaView to use VTK_SYSTEM_* for the
# libraries we've built here.
include(paraview)

#CMB dependencies
include(gdal)
include(kml)
include(vxl)
include(zeroMQ)
include(remus)
include(shiboken)
include(smtk)

#dependencies for moab
include(ftgl)
include(netcdf)
include(oce)
include(cgm)
include(moab)

include(cmb)
include(cmbworkflows)

if(USE_NONFREE_COMPONENTS)
  include(triangle)
endif()

# paraview.sdk is a module that is used install the ParaView SDK.
# Simply enabling this module will result in the ParaView SDK to be
# installed by "make install". Note that this is not always supported.
include(paraviewsdk OPTIONAL)

set(CMB_SUPERBUILD_MODE "CMB"
  CACHE STRING "What to use the superbuild for")
set_property(CACHE CMB_SUPERBUILD_MODE
  PROPERTY
    STRINGS "CMB;SMTK;DEVELOPER")
mark_as_advanced(
  ENABLE_cmb
  ENABLE_cmb_BUILD_MODE
  ENABLE_smtk
  ENABLE_smtk_BUILD_MODE)

if (__BUILDBOT_INSTALL_LOCATION)
  # Buildbot is installing things; let it do its thing; let it be.
elseif (CMB_SUPERBUILD_MODE STREQUAL "CMB")
  set_property(CACHE ENABLE_cmb
    PROPERTY
      VALUE ON)
  set_property(CACHE ENABLE_cmbworkflows
    PROPERTY
      VALUE ON)
  set_property(CACHE ENABLE_cmb_BUILD_MODE
    PROPERTY
      VALUE SuperBuild)
  set_property(CACHE ENABLE_smtk
    PROPERTY
      # XXX(binaries): CMB packaging on OS X is strange and confuses the SMTK
      # inside of CMB. To fix this, use an external SMTK.
      VALUE ON)
      #VALUE OFF)
elseif (CMB_SUPERBUILD_MODE STREQUAL "SMTK")
  set_property(CACHE ENABLE_smtk
    PROPERTY
      VALUE ON)
  set_property(CACHE ENABLE_smtk_BUILD_MODE
    PROPERTY
      VALUE SuperBuild)
  set_property(CACHE ENABLE_cmb
    PROPERTY
      VALUE OFF)
elseif (CMB_SUPERBUILD_MODE STREQUAL "DEVELOPER")
  set_property(CACHE ENABLE_cmb
    PROPERTY
      VALUE ON)
  set_property(CACHE ENABLE_smtk
    PROPERTY
      VALUE OFF)
  set_property(CACHE ENABLE_cmb_BUILD_MODE
    PROPERTY
      VALUE Developer)
  set_property(CACHE ENABLE_smtk_BUILD_MODE
    PROPERTY
      VALUE Developer)
elseif (CMB_SUPERBUILD_MODE STREQUAL "DEVELOPMENT")
  # If this is set, let the options through unmodified.
else ()
  message(FATAL_ERROR "Unrecognized superbuild mode: ${CMB_SUPERBUILD_MODE}")
endif ()

#call cmb process dependencies to get the ability to do a build
#for developers.
cmb_process_dependencies()

SET (CMAKE_INSTALL_COMPONENT runtime)
if (cmb_ENABLED)
  set(test_build_verbose)
  option(TEST_PACT_VERBOSE
         "Set the building package test to verbose."
         OFF)
  if(TEST_PACT_VERBOSE)
    set(test_build_verbose "-V")
  endif()

  if (WIN32)
    set(generators
      NSIS
      ZIP)
  elseif (APPLE)
    set(generators
      DragNDrop)
  else ()
    set(generators
      TGZ)
  endif ()

  foreach (generator IN LISTS generators)
    cmb_superbuild_add_bundle(modelbuilder "${generator}"
      LABELS  "CMB"
      TIMEOUT 1200)
    cmb_superbuild_add_bundle(hydrology "${generator}"
      LABELS  "CMB"
      TIMEOUT 1200)
  endforeach ()
endif()


#-------------------------------------------------------------------------
# Some sanity checks.
# Sanity check: Cannot has osmesa and qt enabled at the same time.
if (osmesa_ENABLED AND qt_ENABLED)
  message(SEND_ERROR "Cannot enable OSMesa and QT at the same time.")
endif()
# Sanity check: Cannot has osmesa and mesa at the same time.
if (osmesa_ENABLED AND mesa_ENABLED)
  message(SEND_ERROR "Cannot enable OSMesa and Mesa at the same time.")
endif()
#-------------------------------------------------------------------------


option(NIGHTLY_BINARY_TESTING "Run Nightly-Binary CMB Testing" OFF)
if (NIGHTLY_BINARY_TESTING)
  add_subdirectory(tests)
endif()

set(extra_ctest_dirs)

cmake_dependent_option(CMB_RUN_TESTS "Run CMB tests from the superbuild" ON
  cmb_ENABLED OFF)
if (TARGET cmb AND CMB_RUN_TESTS)
  set(cmb_binary_dir "<BINARY_DIR>")
  _ep_replace_location_tags(cmb cmb_binary_dir)
  list(APPEND extra_ctest_dirs "${cmb_binary_dir}")
endif ()

cmake_dependent_option(SMTK_RUN_TESTS "Run SMTK tests from the superbuild" ON
  smtk_ENABLED OFF)
if (TARGET smtk AND SMTK_RUN_TESTS)
  set(smtk_binary_dir "<BINARY_DIR>")
  _ep_replace_location_tags(smtk smtk_binary_dir)
  list(APPEND extra_ctest_dirs "${smtk_binary_dir}")
endif ()

if (extra_ctest_dirs)
  configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/CMake/cmb_testing_trampoline.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmb_testing_trampoline.cmake"
    @ONLY)
  set_property(
    DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
    PROPERTY
      TEST_INCLUDE_FILE "${CMAKE_CURRENT_BINARY_DIR}/cmb_testing_trampoline.cmake")
endif ()
