# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# All these functions are needed in order to make the `tizona` application
# work properly.
#
# .. macro:: breakpoint()
#
#   call CMakeDebugger breakpoint and serialize some things to
#   ${CMAKE_BINARY_DIR}/CMakeDebugger.
#
#   What is serialized?
#   - All the defined variables (sorted by name) and their values.
#
#
# .. macro:: _serialize_variables()
#
#   :param DEBUGGER_FILE: file where the information will be serialized.
#
#   serialize the variables to the given file. Each variable will be serialized
#   as (one per line): <VARIABLE_NAME>=<VARIABLE_VALUE>.
#
#
# .. macro:: _serialize_targets()
#
#   :param DEBUGGER_FILE: file where the information will be serialized.
#
#   serialize all the defined targets to the given file. Each target will be
#   serialized as (one per line): <VARIABLE_NAME>=<VARIABLE_VALUE>.
#

include(CMakeParseArguments)

function(breakpoint)
    set(CMAKE_DEBUGGER_FILE "${CMAKE_BINARY_DIR}/CMakeDebugger")

    # Ensure the file is created
    file(WRITE ${CMAKE_DEBUGGER_FILE}
        "##############################################\n"
        "THIS FILE IS AUTOGENERATED. DO NOT MODIFY IT\n"
        "##############################################\n"
    )

    _serialize_variables(CMAKE_DEBUGGER_FILE ${CMAKE_DEBUGGER_FILE})
    _serialize_targets(CMAKE_DEBUGGER_FILE ${CMAKE_DEBUGGER_FILE})

    execute_process(
        COMMAND
            tizona --binary-dir "${CMAKE_BINARY_DIR}"
    )

endfunction()


function(_serialize_variables)
    cmake_parse_arguments(_SERIALIZE "" "CMAKE_DEBUGGER_FILE" "" ${ARGN})

    file(APPEND ${_SERIALIZE_CMAKE_DEBUGGER_FILE}
        "\n# Serialized variables\n"
    )

    # These are all the defined variables at this point
    get_cmake_property(defined_variables VARIABLES)

    # Sort alphabetically
    list(SORT defined_variables)

    # Serialize to the file
    foreach (defined_variable ${defined_variables})
        file(APPEND ${_SERIALIZE_CMAKE_DEBUGGER_FILE}
            "${defined_variable}=${${defined_variable}}\n"
        )
    endforeach()

    file(APPEND ${_SERIALIZE_CMAKE_DEBUGGER_FILE}
        "# End serialized variables\n"
    )

endfunction()


function(_serialize_targets)
    cmake_parse_arguments(_SERIALIZE "" "CMAKE_DEBUGGER_FILE" "" ${ARGN})

    file(APPEND ${_SERIALIZE_CMAKE_DEBUGGER_FILE}
        "\n# Serialized targets\n"
    )

    # Get all the supportec CMake variable
    execute_process(
        COMMAND
            ${CMAKE_COMMAND} --help-property-list
        OUTPUT_VARIABLE
        CMAKE_PROPERTY_LIST
    )

    string(REGEX REPLACE
        ";"
        "\\\\;"
        CMAKE_PROPERTY_LIST
        "${CMAKE_PROPERTY_LIST}"
    )
    string(REGEX REPLACE
        "\n"
        ";"
        CMAKE_PROPERTY_LIST
        "${CMAKE_PROPERTY_LIST}"
    )

    # Get all the targets
    get_property(all_defined_targets
        DIRECTORY ${CMAKE_SOURCE_DIR}
        PROPERTY BUILDSYSTEM_TARGETS
    )

    foreach (defined_target ${all_defined_targets})
        # Write the header for the property
        file(APPEND ${_SERIALIZE_CMAKE_DEBUGGER_FILE}
            "++++ ${defined_target}\n"
        )

        foreach (prop ${CMAKE_PROPERTY_LIST})
            string(REPLACE
                "<CONFIG>" "${CMAKE_BUILD_TYPE}" prop ${prop})

            # Ignore these properties
            if(prop STREQUAL "LOCATION" OR
                prop MATCHES "^LOCATION_" OR
                prop MATCHES "_LOCATION$")
                continue()
            endif()

            # Get the value of the property for this target
            get_property(propval TARGET ${defined_target} PROPERTY ${prop} SET)
            if (propval)
                get_target_property(propval ${defined_target} ${prop})

                file(APPEND ${_SERIALIZE_CMAKE_DEBUGGER_FILE}
                    "${defined_target} ${prop} = ${propval}\n"
                )

            endif()
        endforeach(prop)

        # Print the tail
        file(APPEND ${_SERIALIZE_CMAKE_DEBUGGER_FILE}
            "---- ${defined_target}\n"
        )
    endforeach()

    file(APPEND ${_SERIALIZE_CMAKE_DEBUGGER_FILE}
        "# End serialized targets\n"
    )

endfunction()
