
set (CMAKE_SYSTEM_NAME C51)
set (CMAKE_SYSTEM_PROCESSOR C51)

set(HINT_C51 "$ENV{KEILPATH}/BIN")
find_program(C51_EXE C51.EXE HINTS ${HINT_C51})
get_filename_component(KEILPATH ${C51_EXE} DIRECTORY)

find_package(PythonInterp)

file(TO_NATIVE_PATH "${KEILPATH}" KEILPATH_NATIVE)


message("Keil Compiler found at ${KEILPATH_NATIVE}")

set (CMAKE_C_COMPILER "${PROJECT_SOURCE_DIR}/utils/keil_wrap_c51.py")
set (CMAKE_ASM_COMPILER "${PROJECT_SOURCE_DIR}/utils/keil_wrap_c51.py")
set (CMAKE_AR "${PROJECT_SOURCE_DIR}/utils/keil_wrap_lib51.py")
set (CMAKE_LINKER "${PROJECT_SOURCE_DIR}/utils/keil_wrap_lx51.py")

set(CMAKE_C_COMPILER_TARGET c51)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_ASM_COMPILER_TARGET c51)
set(CMAKE_ASM_COMPILER_ID AX51)
set(CMAKE_ASM_COMPILER_WORKS 1)
SET(CMAKE_ASM_INFOMATION_LOADED 1)

# Suffixes of static libraries and object files created by us
SET(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lib")
SET(CMAKE_C_OUTPUT_EXTENSION ".obj")
SET(CMAKE_EXECUTABLE_SUFFIX_C ".aof")
SET(CMAKE_ASM_SOURCE_FILE_EXTENSIONS a51 )
SET(CMAKE_ASM_OUTPUT_EXTENSION ".obj")

set (CMAKE_C_FLAGS "-WL 2")

if( ${CMAKE_MAJOR_VERSION} EQUAL 3 AND ${CMAKE_MINOR_VERSION} GREATER 3 )
  set(CMAKE_C_COMPILE_OBJECT  "${PYTHON_EXECUTABLE} ${CMAKE_C_COMPILER} -E ${KEILPATH_NATIVE} <SOURCE> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT>")
  set(CMAKE_ASM_COMPILE_OBJECT  "${PYTHON_EXECUTABLE} ${CMAKE_ASM_COMPILER} -E ${KEILPATH_NATIVE} <SOURCE> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT>")
else()
  set(CMAKE_C_COMPILE_OBJECT  "${PYTHON_EXECUTABLE} ${CMAKE_C_COMPILER} -E ${KEILPATH_NATIVE} <SOURCE> <DEFINES> <FLAGS> -o <OBJECT>")
  set(CMAKE_ASM_COMPILE_OBJECT  "${PYTHON_EXECUTABLE} ${CMAKE_ASM_COMPILER} -E ${KEILPATH_NATIVE} <SOURCE> <DEFINES> <FLAGS> -o <OBJECT>")
endif()

set(CMAKE_C_LINK_EXECUTABLE "${PYTHON_EXECUTABLE} ${CMAKE_LINKER} -E ${KEILPATH_NATIVE}  <OBJECTS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES> -o <TARGET>")
set(CMAKE_C_CREATE_STATIC_LIBRARY "${PYTHON_EXECUTABLE} ${CMAKE_AR} -E ${KEILPATH_NATIVE} -o <TARGET> <OBJECTS> ")
