set(CONFIG_GENERATOR_DIR ${CMAKE_CURRENT_LIST_DIR})

function(target_add_colleen_config)
    set(target_name ${ARGV0})
    list(REMOVE_AT ARGV 0)
    set(configs ${ARGV})
    set(COLLEEN_CONFIG_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/colleen-autogenerated)

    execute_process(COMMAND ${CONFIG_GENERATOR_DIR}/run-config-generator.sh ${configs} --output_dir ${COLLEEN_CONFIG_OUTPUT_DIR}
            RESULT_VARIABLE CONFIG_GENERATOR_OUTPUT)
    if (NOT ${CONFIG_GENERATOR_OUTPUT} STREQUAL "0")
        message(SEND_ERROR "colleen config generator exited with error code: ${CONFIG_GENERATOR_OUTPUT}")
    endif ()

    set(COLLEEN_CONFIG_INCLUDE_DIR ${COLLEEN_CONFIG_OUTPUT_DIR}/include)
    set(COLLEEN_CONFIG_SOURCE_DIR ${COLLEEN_CONFIG_OUTPUT_DIR}/source)
    set(CONFIG_LIBRARY "colleen_config_${COLLEEN_CONFIG_OUTPUT_DIR}")
    string(REPLACE "/" "_" CONFIG_LIBRARY ${CONFIG_LIBRARY})

    add_library(${CONFIG_LIBRARY})
    file(GLOB_RECURSE COLLEEN_CONFIG_SOURCES ${COLLEEN_CONFIG_SOURCE_DIR}/*.cpp)
    target_sources(${CONFIG_LIBRARY}
            PRIVATE
            ${COLLEEN_CONFIG_SOURCES})
    target_include_directories(${CONFIG_LIBRARY} PUBLIC ${COLLEEN_CONFIG_INCLUDE_DIR})
    target_link_libraries(${CONFIG_LIBRARY}
            PUBLIC
            colleen)

    target_link_libraries(${target_name} PUBLIC ${CONFIG_LIBRARY})
endfunction(target_add_colleen_config)