# building examples
#------------------
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
include_directories(${PROJECT_SOURCE_DIR}/include )
if (${CMAKE_VERSION} VERSION_GREATER "3.8")
  cmake_policy(SET CMP0069 NEW)
  include(CheckIPOSupported)
endif()


#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)
    list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
    list(APPEND CMAKE_PREFIX_PATH ${ROOT_DIR})
    find_package(ROOT REQUIRED COMPONENTS Core RIO Tree)
    if(ROOT_FOUND)
      message(STATUS "HepMC3 examples: ROOT_FOUND=${ROOT_FOUND}")
      message(STATUS "HepMC3 examples: ROOT_USE_FILE=${ROOT_USE_FILE}")
      message(STATUS "HepMC3 examples: ROOT_INCLUDE_DIRS=${ROOT_INCLUDE_DIRS}")
      message(STATUS "HepMC3 examples: ROOT_LIBRARIES=${ROOT_LIBRARIES}")
      message(STATUS "HepMC3 examples: ROOT_DEFINITIONS=${ROOT_DEFINITIONS}")
      message(STATUS "HepMC3 examples: ROOT_CXX_FLAGS=${ROOT_CXX_FLAGS}")
      message(STATUS "HepMC3 examples: ROOT_fortran_FLAGS=${ROOT_fortran_FLAGS}")
      string(REPLACE "ROOTUseFile.cmake" "modules/RootNewMacros.cmake" ROOT_NEWMACROS_FILE "${ROOT_USE_FILE}")
      if(EXISTS "${ROOT_NEWMACROS_FILE}")
        include(${ROOT_NEWMACROS_FILE})
      endif()
      add_definitions(-DHEPMC3_ROOTIO)
      if(DEFINED ROOT_CXX_STANDARD)
        set(HEPMC3_CXX_STANDARD ${ROOT_CXX_STANDARD})
      else()
        string(REPLACE " " ";" ROOT_CXX_FLAGS_LIST ${ROOT_CXX_FLAGS})
        foreach(fl ${ROOT_CXX_FLAGS_LIST})
          if ("${fl}" MATCHES   ".*\\+\\+14$" OR "${fl}" MATCHES   ".*\\+\\+1y$" )
            set(HEPMC3_CXX_STANDARD 14)
          endif()
          if ("${fl}" MATCHES   ".*\\+\\+17$" OR "${fl}" MATCHES   ".*\\+\\+1z$" )
            set(HEPMC3_CXX_STANDARD 17)
          endif()
          if ("${fl}" MATCHES   ".*\\+\\+20$" OR "${fl}" MATCHES   ".*\\+\\+2a$" )
            set(HEPMC3_CXX_STANDARD 20)
          endif()
        endforeach(fl ${ROOT_CXX_FLAGS_LIST})
      endif()
    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()
  if (HEPMC3_PROTOBUFIO_LIB)
      find_package(Protobuf 2.4 REQUIRED)
  endif()
  if (NOT HEPMC3_CXX_STANDARD)
    set(HEPMC3_CXX_STANDARD 11)
  endif()
  set(CMAKE_CXX_STANDARD ${HEPMC3_CXX_STANDARD})
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  set(CMAKE_CXX_EXTENSIONS OFF)
  set(${CMAKE_CXX_COMPILER_ID}_CXX_FLAGS_TO_CHECK "")
  set(MSVC_CXX_FLAGS_TO_CHECK "/bigobj;/D_CRT_SECURE_NO_WARNINGS;/wd4267;/wd4244;/wd4477")
  foreach(fl ${${CMAKE_CXX_COMPILER_ID}_CXX_FLAGS_TO_CHECK})
  CHECK_CXX_COMPILER_FLAG(${fl} CXX_COMPILER_SUPPORTS_${fl})
    if(CXX_COMPILER_SUPPORTS_${fl})
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${fl}")
    endif()
  endforeach()
endif()
#<-This is needed for standalone compilation

#Newest compillers want PIC
SET(OLD_CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE})
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
include(CheckLanguage)
check_language(Fortran)
SET(CMAKE_POSITION_INDEPENDENT_CODE ${OLD_CMAKE_POSITION_INDEPENDENT_CODE})
if (CMAKE_Fortran_COMPILER)
  enable_language(Fortran)
  include(CheckFortranCompilerFlag)
  get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)
  message(STATUS "HepMC3 examples: Fortran compiler found, Pythia6 example enabled. Fortran_COMPILER_NAME=${Fortran_COMPILER_NAME}  CMAKE_Fortran_COMPILER_ID=${CMAKE_Fortran_COMPILER_ID}")
  set(${CMAKE_Fortran_COMPILER_ID}_Fortran_FLAGS_TO_CHECK "")
  set( GNU_Fortran_FLAGS_TO_CHECK "-Wno-unused-label;-Wno-conversion;-Wno-unused-parameter;-Wno-maybe-uninitialized;-Wno-unused-dummy-argument;-Wno-integer-division;-Wno-compare-reals;-Wno-function-elimination;-Wno-tabs;-Wno-unused-variable;-Wno-do-subscript;-fno-automatic;-fno-backslash;-ffixed-line-length-132;-std=legacy")
  set( IntelLLVM_Fortran_FLAGS_TO_CHECK  "-noauto;-extend-source 132;-diag-disable 8291;-diag-disable 7784")
  set( Intel_Fortran_FLAGS_TO_CHECK "/noauto;/extend-source:132;-noauto;-extend-source 132;-diag-disable 8291;-diag-disable 7784")
  set( PGI_Fortran_FLAGS_TO_CHECK "-Mpreprocess;-Mextend;-Mnosave;-Mnofree;-noswitcherror")
  set( NVHPC_Fortran_FLAGS_TO_CHECK "-Mpreprocess;-Mextend;-Mnosave;-Mnofree;-noswitcherror")
  set( XL_Fortran_FLAGS_TO_CHECK "-qfixed=132;-qnosave;-qextname;-qsuppress=1500-030")
  message(STATUS "${CMAKE_Fortran_COMPILER_ID}_Fortran_FLAGS_TO_CHECK=${${CMAKE_Fortran_COMPILER_ID}_Fortran_FLAGS_TO_CHECK}")
  foreach(fl ${${CMAKE_Fortran_COMPILER_ID}_Fortran_FLAGS_TO_CHECK})
    CHECK_Fortran_COMPILER_FLAG(${fl} Fortran_COMPILER_SUPPORTS_${fl})
    if(Fortran_COMPILER_SUPPORTS_${fl})
      set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${fl}")
    endif()
  endforeach()
  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}")
  if (CMAKE_GENERATOR MATCHES "Visual Studio")
    message(STATUS "HepMC3: Visual Studio does not support targets with mixed code, please use e.g. NMake Files.")
  else()
    add_subdirectory(Pythia6Example)
  endif()
else()
  message(STATUS "HepMC3 examples: Fortran compiler not found, Pythia6 example disabled")
endif()


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

