include_guard(GLOBAL)

include(CheckOBJCXXCompilerFlag OPTIONAL)
include(CheckOBJCCompilerFlag OPTIONAL)
include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)
include(CMakePushCheckState)

# TODO: This needs to be cleaned up to allow checking when the arguments have
# changed as well, otherwise the cache needs to be re-edited
function (ixm_check_flag flag)
  set(valid OBJCXX OBJC CXX C)
  parse(${ARGN} @ARGS=1 LANGUAGE)
  string(MAKE_C_IDENTIFIER CAN_USE_${flag} var)
  cmake_push_check_state()
  if (NOT LANGUAGE IN_LIST valid)
    log(FATAL "LANGUAGE must be one of ${valid}")
  endif()
  if (LANGUAGE STREQUAL "OBJCXX")
    check_objcxx_compiler_flag(${flag} ${var})
  elseif (LANGUAGE STREQUAL "OBJC")
    check_objc_compiler_flag(${flag} ${var})
  elseif (LANGUAGE STREQUAL "CXX")
    check_cxx_compiler_flag(${flag} ${var})
  elseif(LANGUAGE STREQUAL "C")
    check_c_compiler_flag(${flag} ${var})
  endif()
  cmake_pop_check_state()
endfunction()
