add_executable(
    ${device.device}
    ${SOURCE_FILES}
)

if("${INCLUDE_DIRS}" STRGREATER "")
    target_include_directories(${device.device} PUBLIC "${INCLUDE_DIRS}")
endif()

set_target_properties(${device.device} PROPERTIES SUFFIX "" ENABLE_EXPORTS ON)

# link the executable with supporting libraries.
target_link_libraries(
    ${device.device}
    ${CODAL_DEPS}
)

# import toolchain bin generation command
if(${device.generate_bin})
    include(${TOOLCHAIN_FOLDER}/bin-generator.cmake)
endif()

# import toolchain hex generation command
if(${device.generate_hex})
    include(${TOOLCHAIN_FOLDER}/hex-generator.cmake)
endif()

# post process command hook, depends on the hex file generated by the build system.
if("${device.post_process.command}" STRGREATER "" OR "${device.post_process}" STRGREATER "")

    if("${device.post_process}" STRGREATER "")
        set(POST_PROCESS_COMMAND ${device.post_process})
    else()
        set(POST_PROCESS_COMMAND ${device.post_process.command})
    endif()

    set(POST_PROCESS_DEPENDS "${device.post_process.depends}")

    # replace specific strings in the command, this gives users flexibility, they don't have to manually specify the location of files
    string(REPLACE "<OUTPUT_HEX_LOCATION>" ${PROJECT_SOURCE_DIR}/${CODAL_APP_OUTPUT_DIR}/${device.device}.hex  CODAL_POSTPROCESS_COMMAND ${POST_PROCESS_COMMAND})
    string(REPLACE "<OUTPUT_HEX_DESTINATION>" ${PROJECT_SOURCE_DIR}/${CODAL_APP_OUTPUT_DIR} CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})
    string(REPLACE "<OUTPUT_HEX_NAME>" ${device.device} CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})

    string(REPLACE "<OUTPUT_BIN_LOCATION>" ${PROJECT_SOURCE_DIR}/${CODAL_APP_OUTPUT_DIR}/${device.device}.bin  CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})
    string(REPLACE "<OUTPUT_BIN_DESTINATION>" ${PROJECT_SOURCE_DIR}/${CODAL_APP_OUTPUT_DIR} CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})
    string(REPLACE "<OUTPUT_BIN_NAME>" ${device.device}.bin CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})

    string(REPLACE "<OUTPUT_ELF_LOCATION>" ${PROJECT_SOURCE_DIR}/build/${device.device} CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})
    string(REPLACE "<OUTPUT_ELF_DESTINATION>" ${PROJECT_SOURCE_DIR}/${CODAL_APP_OUTPUT_DIR} CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})
    string(REPLACE "<OUTPUT_ELF_NAME>" ${device.device} CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})

    string(REPLACE "<CODAL_APP_OUTPUT_DIR>" ${PROJECT_SOURCE_DIR}/${CODAL_APP_OUTPUT_DIR} CODAL_POSTPROCESS_COMMAND ${CODAL_POSTPROCESS_COMMAND})

    #convert to a command
    separate_arguments(FINAL_COMMAND UNIX_COMMAND ${CODAL_POSTPROCESS_COMMAND})

    # execute
    if(POST_PROCESS_DEPENDS STREQUAL "ELF")
        add_custom_command(
            TARGET ${device.device}
            COMMAND ${FINAL_COMMAND}
            DEPENDS  ${device.device}
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
            COMMENT "Executing post process command"
        )
    elseif(POST_PROCESS_DEPENDS STREQUAL "HEX")
        add_custom_command(
            TARGET ${device.device}_hex
            COMMAND ${FINAL_COMMAND}
            DEPENDS  ${device.device}
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
            COMMENT "Executing post process command"
        )
    else()
        #by default post process should depend on hex
        add_custom_command(
            TARGET ${device.device}_bin
            COMMAND ${FINAL_COMMAND}
            DEPENDS  ${device.device}
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
            COMMENT "Executing post process command"
        )
    endif()

endif()