# - Config file for the atdna package
 
# Compute paths
get_filename_component(ATDNA_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
 
# Our library dependencies (contains definitions for IMPORTED targets)
if(NOT TARGET atdna AND NOT atdna_BINARY_DIR)
  include("${ATDNA_CMAKE_DIR}/atdnaTargets.cmake")
endif()

# Super handy macro for adding atdna target
function(atdna out)
  # Ninja wants invocations in root binary dir for DEPFILE application
  file(RELATIVE_PATH out_rel ${CMAKE_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/${out}")

  # Make input files source-relative
  set(ins "")
  set(ins_impdeps "")
  foreach(arg ${ARGN})
    list(APPEND ins ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
    list(APPEND ins_impdeps CXX)
    list(APPEND ins_impdeps ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
  endforeach()

  # Get local include directories for atdna
  get_property(incdirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
  set(inccli "")
  foreach(dir ${incdirs})
    list(APPEND inccli "-I${dir}")
  endforeach()

  # Get local defines for atdna
  get_property(cdefs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY COMPILE_DEFINITIONS)
  set(cdefcli "")
  foreach(def ${cdefs})
    list(APPEND cdefcli "-D${def}")
  endforeach()

  # MS extra
  unset(extraargs)
  if(MSVC)
    list(APPEND extraargs -fms-compatibility -fexceptions)
    if(MSVC_VERSION EQUAL 1800)
      list(APPEND extraargs -fms-compatibility-version=18.00)
    elseif(MSVC_VERSION EQUAL 1900)
      list(APPEND extraargs -fms-compatibility-version=19.00)
    elseif(MSVC_VERSION EQUAL 1910)
      list(APPEND extraargs -fms-compatibility-version=19.10)
    elseif(MSVC_VERSION EQUAL 1911)
      list(APPEND extraargs -fms-compatibility-version=19.11)
  endif()

  # OSX Extra
  elseif(APPLE)
    get_filename_component(COMPILER_DIR "${CMAKE_CXX_COMPILER}" DIRECTORY)
    if (NOT EXISTS "${CMAKE_OSX_SYSROOT}")
      message(FATAL_ERROR "CMAKE_OSX_SYSROOT not set")
    endif()
    file(GLOB SYSTEM_INCLUDE_DIR "${COMPILER_DIR}/../lib/clang/*/include")
    list(APPEND extraargs
         -isysroot ${CMAKE_OSX_SYSROOT}
         -stdlib++-isystem "${COMPILER_DIR}/../include/c++/v1"
         -isystem "${SYSTEM_INCLUDE_DIR}")
  endif()

  # Make target
  if(${CMAKE_GENERATOR} STREQUAL "Ninja")
    # Use Ninja's DEPFILE parser in cooperation with atdna
    add_custom_command(OUTPUT ${out} COMMAND $<TARGET_FILE:atdna>
                       ARGS ${extraargs} -o ${out_rel} -MD -MT ${out_rel} -MF ${out_rel}.d ${cdefcli} ${inccli}
                       "-I${ATHENA_INCLUDE_DIR}" ${ins}
                       DEPENDS atdna ${ins} IMPLICIT_DEPENDS ${ins_impdeps}
                       DEPFILE "${CMAKE_CURRENT_BINARY_DIR}/${out}.d"
                       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                       COMMENT "Generating DNA ${out_rel}")
  else()
    # Use CMake's built-in dependency scanner for makefile targets
    add_custom_command(OUTPUT ${out} COMMAND $<TARGET_FILE:atdna>
                       ARGS ${extraargs} -o ${out_rel} ${cdefcli} ${inccli}
                       "-I${ATHENA_INCLUDE_DIR}" ${ins}
                       DEPENDS atdna ${ins} IMPLICIT_DEPENDS ${ins_impdeps}
                       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                       COMMENT "Generating DNA ${out_rel}")
  endif()
endfunction()

