function(m2lang_tablegen)
  # Syntax:
  # m2lang_tablegen output-file [tablegen-arg ...] SOURCE source-file
  # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
  #
  # Generates a custom command for invoking tblgen as
  #
  # tblgen source-file -o=output-file tablegen-arg ...
  #
  # and, if cmake-target-name is provided, creates a custom target for
  # executing the custom command depending on output-file. It is
  # possible to list more files to depend after DEPENDS.

  cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN})

  if( NOT CTG_SOURCE )
    message(FATAL_ERROR "SOURCE source-file required by m2lang_tablegen")
  endif()

  set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
  tablegen(M2LANG ${CTG_UNPARSED_ARGUMENTS})

  if(CTG_TARGET)
    add_public_tablegen_target(${CTG_TARGET})
    set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "M2lang tablegenning")
    set_property(GLOBAL APPEND PROPERTY M2LANG_TABLEGEN_TARGETS ${CTG_TARGET})
  endif()
endfunction(m2lang_tablegen)

macro(set_m2lang_windows_version_resource_properties name)
  if(DEFINED windows_resource_file)
    set_windows_version_resource_properties(${name} ${windows_resource_file}
      VERSION_MAJOR ${M2LANG_VERSION_MAJOR}
      VERSION_MINOR ${M2LANG_VERSION_MINOR}
      VERSION_PATCHLEVEL ${M2LANG_VERSION_PATCHLEVEL}
      VERSION_STRING "${M2LANG_VERSION} (${BACKEND_PACKAGE_STRING})"
      PRODUCT_NAME "m2lang")
  endif()
endmacro()

macro(add_m2lang_subdirectory name)
  add_llvm_subdirectory(M2LANG TOOL ${name})
endmacro()

macro(add_m2lang_library name)
  cmake_parse_arguments(ARG
    "SHARED"
    ""
    "ADDITIONAL_HEADERS"
    ${ARGN})
  set(srcs)
  if(MSVC_IDE OR XCODE)
    # Add public headers
    file(RELATIVE_PATH lib_path
      ${M2LANG_SOURCE_DIR}/lib/
      ${CMAKE_CURRENT_SOURCE_DIR}
    )
    if(NOT lib_path MATCHES "^[.][.]")
      file( GLOB_RECURSE headers
        ${M2LANG_SOURCE_DIR}/include/m2lang/${lib_path}/*.h
        ${M2LANG_SOURCE_DIR}/include/m2lang/${lib_path}/*.def
      )
      set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)

      file( GLOB_RECURSE tds
        ${M2LANG_SOURCE_DIR}/include/m2lang/${lib_path}/*.td
      )
      source_group("TableGen descriptions" FILES ${tds})
      set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)

      if(headers OR tds)
        set(srcs ${headers} ${tds})
      endif()
    endif()
  endif(MSVC_IDE OR XCODE)
  if(srcs OR ARG_ADDITIONAL_HEADERS)
    set(srcs
      ADDITIONAL_HEADERS
      ${srcs}
      ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
      )
  endif()
  if(ARG_SHARED)
    set(LIBTYPE SHARED)
  else()
    # llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
    # so we need to handle it here.
    if(BUILD_SHARED_LIBS)
      set(LIBTYPE SHARED OBJECT)
    else()
      set(LIBTYPE STATIC OBJECT)
    endif()
    set_property(GLOBAL APPEND PROPERTY M2LANG_STATIC_LIBS ${name})
  endif()
  llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})

  if(TARGET ${name})
    target_link_libraries(${name} INTERFACE ${LLVM_COMMON_LIBS})

    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libm2lang")

      if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
          NOT LLVM_DISTRIBUTION_COMPONENTS)
        set(export_to_m2langtargets EXPORT M2langTargets)
        set_property(GLOBAL PROPERTY M2LANG_HAS_EXPORTS True)
      endif()

      install(TARGETS ${name}
        COMPONENT ${name}
        ${export_to_m2langtargets}
        LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
        ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
        RUNTIME DESTINATION bin)

      if (NOT LLVM_ENABLE_IDE)
        add_llvm_install_targets(install-${name}
                                 DEPENDS ${name}
                                 COMPONENT ${name})
      endif()

      set_property(GLOBAL APPEND PROPERTY M2LANG_LIBS ${name})
    endif()
    set_property(GLOBAL APPEND PROPERTY M2LANG_EXPORTS ${name})
  else()
    # Add empty "phony" target
    add_custom_target(${name})
  endif()

  set_target_properties(${name} PROPERTIES FOLDER "M2lang libraries")
  set_m2lang_windows_version_resource_properties(${name})
endmacro(add_m2lang_library)

macro(add_m2lang_executable name)
  add_llvm_executable( ${name} ${ARGN} )
  set_target_properties(${name} PROPERTIES FOLDER "M2lang executables")
  set_m2lang_windows_version_resource_properties(${name})
endmacro(add_m2lang_executable)

macro(add_m2lang_tool name)
  if (NOT M2LANG_BUILD_TOOLS)
    set(EXCLUDE_FROM_ALL ON)
  endif()

  add_m2lang_executable(${name} ${ARGN})
  #add_dependencies(${name} m2lang-headers)

  if (M2LANG_BUILD_TOOLS)
    if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
        NOT LLVM_DISTRIBUTION_COMPONENTS)
      set(export_to_m2langtargets EXPORT M2langTargets)
      set_property(GLOBAL PROPERTY M2LANG_HAS_EXPORTS True)
    endif()

    install(TARGETS ${name}
      ${export_to_m2langtargets}
      RUNTIME DESTINATION bin
      COMPONENT ${name})

    if(NOT LLVM_ENABLE_IDE)
      add_llvm_install_targets(install-${name}
                               DEPENDS ${name}
                               COMPONENT ${name})
    endif()
    set_property(GLOBAL APPEND PROPERTY M2LANG_EXPORTS ${name})
  endif()
endmacro()

macro(add_m2lang_symlink name dest)
  add_llvm_tool_symlink(${name} ${dest} ALWAYS_GENERATE)
  # Always generate install targets
  llvm_install_symlink(${name} ${dest} ALWAYS_GENERATE)
endmacro()

function(m2lang_target_link_libraries target type)
  if (M2LANG_LINK_M2LANG_DYLIB)
    target_link_libraries(${target} ${type} m2lang-m2)
  else()
    target_link_libraries(${target} ${type} ${ARGN})
  endif()
endfunction()
