# building examples
#------------------
cmake_minimum_required(VERSION 2.8.9 FATAL_ERROR)
include_directories(${PROJECT_SOURCE_DIR}/include )


#This is needed for standalone compilation
if (USE_INSTALLED_HEPMC3)
find_package(HepMC3 REQUIRED)
include_directories(${HEPMC3_INCLUDE_DIR})
include(GNUInstallDirs)
include(CheckCXXCompilerFlag)
if (HEPMC3_ROOTIO_LIB)
  set (ROOT_FIND_COMPONENTS Core RIO Tree)
  list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
  list(APPEND CMAKE_PREFIX_PATH ${ROOT_DIR})
  find_package(ROOT REQUIRED COMPONENTS Core RIO Tree)
  set(CMAKE_CXX_FLAGS_BEFORE_ROOT "${CMAKE_CXX_FLAGS}")
  set(CMAKE_C_FLAGS_BEFORE_ROOT "${CMAKE_C_FLAGS}")
  set(CMAKE_Fortran_FLAGS_BEFORE_ROOT "${CMAKE_Fortran_FLAGS}")
  include(${ROOT_USE_FILE})
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_BEFORE_ROOT}")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS_BEFORE_ROOT}")
  set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS_BEFORE_ROOT}")
  message(STATUS "ROOT_FOUND:     ${ROOT_FOUND}")
  message(STATUS "ROOT_USE_FILE ${ROOT_USE_FILE}")
  message(STATUS "ROOT includes:  ${ROOT_INCLUDE_DIRS}")
  message(STATUS "ROOT libraries: ${ROOT_LIBRARIES}")
  message(STATUS "ROOT definitions: ${ROOT_DEFINITIONS}")
  if(ROOT_FOUND)
    include_directories(${ROOT_INCLUDE_DIRS})
    add_definitions(-DHEPMC3_ROOTIO)
    foreach(fl ${ROOT_CXX_FLAGS})
    CHECK_CXX_COMPILER_FLAG(${fl} COMPILER_SUPPORTS_${fl})
    if(COMPILER_SUPPORTS_${fl})
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${fl}")
    endif()
    endforeach(fl ${ROOT_CXX_FLAGS})
    set(HEPMC3_ROOTIO_LIBRARY "-lHepMC3rootIO" )
  else()
    message(STATUS "HepMC3 warning: ROOT needed for ROOTIO but not found")
    message(STATUS "HepMC3 warning: Skipping HepMC ROOT IO libraries")
    set(HEPMC3_ENABLE_ROOTIO OFF)
  endif()
endif()

#This is a workaround for ROOT+Cmake problem.
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "PGI")
set(CMAKE_CXX_EXTENSIONS ON)
endif()
endif()
if (MSVC)
#This is a workaround for VC memory problem and false positive warnings.
set(MSVC_CXX_FLAGS_TO_CHECK "/bigobj" "/D _CRT_SECURE_NO_WARNINGS" "/wd4267" "/wd4244" "/wd4477")
foreach(fl ${MSVC_CXX_FLAGS_TO_CHECK})
CHECK_CXX_COMPILER_FLAG(${fl} COMPILER_SUPPORTS_${fl})
if(COMPILER_SUPPORTS_${fl})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${fl}")
endif()
endforeach(fl ${MSVC_CXX_FLAGS_TO_CHECK})
endif()
endif()
#<-This is needed for standalone compilation 





include(CheckLanguage)
check_language(Fortran)
if (CMAKE_Fortran_COMPILER)
enable_language(Fortran)
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)
message(STATUS "HepMC3 examples: Fortran compiler found, Pythia6 example enabled. ${Fortran_COMPILER_NAME}")
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS} -fno-automatic -fno-backslash -ffixed-line-length-132")
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS} -fno-automatic -fno-backslash -extend-source 132")
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "PGI")
set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS}  ")
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "SunPro")
set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS}  ")
elseif()
message(STATUS "HepMC3 examples: Don't know how to set Fortran FLAGS for  ${Fortran_COMPILER_NAME} with ID ${Fortran_COMPILER_ID}")
endif ()
if(CMAKE_SYSTEM_NAME MATCHES Darwin)
  set(CMAKE_SHARED_LIBRARY_CREATE_Fortran_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_Fortran_FLAGS} -flat_namespace -single_module -undefined dynamic_lookup")
else()
endif()
message(STATUS "HepMC3 examples: CMAKE_Fortran_FLAGS ${CMAKE_Fortran_FLAGS}")
add_subdirectory(Pythia6Example)
else()
message(STATUS "HepMC3 examples: Fortran compiler not found, Pythia6 example disabled")
endif()


add_subdirectory(BasicExamples)
add_subdirectory(LHEFExample)
add_subdirectory(ConvertExample)
add_subdirectory(Pythia8Example)

if(ROOT_FOUND)
  add_subdirectory(RootIOExample)
  add_subdirectory(RootIOExample2)
  add_subdirectory(RootIOExample3)
  add_subdirectory(ViewerExample)
else()
  message(STATUS "HepMC3 examples: ROOT package not found, rootIO_examples disabled.")
endif()

