macro(append_suffix out suffix)
    unset(_tmp)
    foreach(arg ${ARGN})
        list( APPEND _tmp ${arg}_${suffix})
    endforeach()
    set(${out} ${_tmp})
endmacro()

macro(append_prefix out prefix)
    unset(_tmp)
    foreach(arg ${ARGN})
        list( APPEND _tmp ${prefix}_${arg})
    endforeach()
    set(${out} ${_tmp})
endmacro()

function(add_target_compile_definitions_platforms target)
    foreach(arg ${ARGN})
        foreach(id ${HWID})
            set_property(TARGET ${id}_${target} APPEND PROPERTY COMPILE_DEFINITIONS ${arg})
        endforeach()
    endforeach()
endfunction()

function(add_target_property_platforms target property)
    foreach(id ${HWID})
        set_property(TARGET ${id}_${target} APPEND PROPERTY ${property} ${ARGN})
    endforeach()
endfunction()

function(add_library_platforms lib )
    foreach(id ${HWID})
        add_library( ${id}_${lib} STATIC ${ARGN})
        SET_TARGET_PROPERTIES(${id}_${lib} PROPERTIES PREFIX "lib_")
        add_custom_target(${lib} DEPENDS ${id}_${lib})
    endforeach()
    set_platform_compile_properties(${lib})
    if (${lib} MATCHES "MBTLE")
        add_lint(${lib} ${ARGN})
    endif()
endfunction()

function(add_lint lib)
    foreach(id ${HWID})
        if(COMMAND add_pc_lint)
            add_pc_lint(${id}_${lib} ${ARGN})
            add_custom_target(${lib}_lint DEPENDS ${id}_${lib}_lint)
            unset(PC_LINT_USER_FLAGS)
        endif(COMMAND add_pc_lint)
    endforeach()
endfunction()

function(add_executable_platforms exec_name)
    foreach(id ${HWID})
        add_executable("${id}_${exec_name}" ${ARGN})
    endforeach()
endfunction()

function(target_link_libraries_platforms exec_name)
    foreach(id ${HWID})
        append_prefix(link_libs ${id} ${ARGN})
        target_link_libraries(${id}_${exec_name} ${link_libs})
    endforeach()
endfunction()

function(set_platform_compile_properties target)
    foreach(id ${HWID})
        if (${id} EQUAL "51")
            set_properties_nrf51(${id}_${target})
        endif()
        if (${id} EQUAL "52")
            set_properties_nrf52(${id}_${target})
        endif()
        if (${id} MATCHES "ut")
            set_properties_x86(${id}_${target})
        endif()
    endforeach()
endfunction()
