# building examples
#------------------
cmake_minimum_required(VERSION 3.1.0 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)
    if(ROOT_FOUND)
      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}")
      if (ROOT_CXX_FLAGS_BEFORE_ROOT)
        message(STATUS "Manualy set ROOT flags ${ROOT_CXX_FLAGS_BEFORE_ROOT}")
        set( ROOT_CXX_FLAGS "${ROOT_CXX_FLAGS_BEFORE_ROOT}")
      endif()
      if (${CMAKE_CXX_COMPILER_ID} MATCHES "SunPro")
        add_definitions(-DANSICPP)
      endif()
      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}")
      include_directories(${ROOT_INCLUDE_DIRS})
      add_definitions(-DHEPMC3_ROOTIO)
      string(REPLACE " " ";" ROOT_CXX_FLAGS_LIST ${ROOT_CXX_FLAGS})
      foreach(fl ${ROOT_CXX_FLAGS_LIST})
        CHECK_CXX_COMPILER_FLAG(${fl} COMPILER_SUPPORTS_${fl})
        if(COMPILER_SUPPORTS_${fl})
          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()
          if ("${fl}" MATCHES   ".*\\+\\+23$" OR "${fl}" MATCHES   ".*\\+\\+2b$" )
            set(HEPMC3_CXX_STANDARD 23)
          endif()
          set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${fl}")
        endif()
      endforeach(fl ${ROOT_CXX_FLAGS_LIST})
      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()
  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)
  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()
else()
  set(HEPMC3_PROTOBUFIO_LIB HepMC3protobufIO)
  set(HEPMC3_ROOTIO_LIB HepMC3rootIO)
  set(HEPMC3_SEARCH_LIB HepMC3search)
  set(HEPMC3_LIB HepMC3)
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)
  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}")
  if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
    set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS} -fno-automatic -fno-backslash -ffixed-line-length-132 -std=legacy")
  elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
    if (WIN32)
      set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS} /noauto /extend-source:132")
    else()
      set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS} -noauto -extend-source 132")
    endif()
  elseif (${CMAKE_CXX_COMPILER_ID} MATCHES "PGI" OR ${CMAKE_CXX_COMPILER_ID} MATCHES "NVHPC")
    set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS} -Mpreprocess -Mextend -Mnosave -Mnofree")
  elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "SunPro")
    set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS}  ")
  elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "XL")
    set( CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS} -qfixed=256  -qnosave -qextname")
  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}")
  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(BasicExamples)
add_subdirectory(LHEFExample)
add_subdirectory(ConvertExample)
add_subdirectory(Pythia8Example)
if (HEPMC3_ENABLE_SEARCH)
   add_subdirectory(SearchExample)
endif()
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()

