# Author:
#   Sven Czarnian <devel@svcz.de>
# License:
#   Closed Source
# Brief:
#   Defines the protobuf functions

# Brief:
#   Proto-files are compiled into C++ files
# Parameters:
#   PROTO_FILES      - The proto-files with the message description
#   SOURCE_FILES     - Contains the filenames and paths of the generated files
FUNCTION(ProtobufCompile PROTO_FILES SOURCE_FILES)
    SET(GENERATED_FILES "")

    FOREACH (PROTO ${PROTO_FILES})
        # get the relevant information to configure the protoc-run
        GET_FILENAME_COMPONENT(FILENAME ${PROTO} NAME_WLE)
        GET_FILENAME_COMPONENT(DIRECTORY ${PROTO} DIRECTORY)

        # define the output files
        SET(CPP_FILE ${CMAKE_CURRENT_BINARY_DIR}/protobuf/${FILENAME}.pb.cc)
        SET(HPP_FILE ${CMAKE_CURRENT_BINARY_DIR}/protobuf/${FILENAME}.pb.h)

        # create the protoc-directory
        FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/protobuf)

        # define the protoc-command
        ADD_CUSTOM_COMMAND(
            OUTPUT ${CPP_FILE} ${HPP_FILE}
            DEPENDS protobuf
            COMMAND ${CMAKE_SOURCE_DIR}/external/bin/protoc.exe
            ARGS -I=${DIRECTORY} --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/protobuf ${PROTO}
            WORKING_DIRECTORY "${CMAKE_INSTALL_PREFIX}/bin"
            COMMENT "Creating C++-sources for ${PROTO}"
        )

        # disable warnings
        IF (MSVC)
            SET_SOURCE_FILES_PROPERTIES(${CPP_FILE} PROPERTIES COMPILE_FLAGS "/wd4127 /wd5054 /wd4125 /wd4267")
            SET_SOURCE_FILES_PROPERTIES(${HPP_FILE} PROPERTIES COMPILE_FLAGS "/wd4127 /wd5054 /wd4125 /wd4267")
        ENDIF ()

        # add the generated files
        LIST(APPEND GENERATED_FILES ${CPP_FILE})
        LIST(APPEND GENERATED_FILES ${HPP_FILE})
    ENDFOREACH ()

    # set the output variables
    SET(${SOURCE_FILES} ${GENERATED_FILES} PARENT_SCOPE)
ENDFUNCTION()
