# Copyright © 2024 Intel Corporation
# SPDX-License-Identifier: Apache 2.0
# LEGAL NOTICE: Your use of this software and any required dependent software (the “Software Package”)
# is subject to the terms and conditions of the software license agreements for the Software Package,
# which may also include notices, disclaimers, or license terms for third party or open source software
# included in or with the Software Package, and your use indicates your acceptance of all such terms.
# Please refer to the “third-party-programs.txt” or other similarly-named text file included with the
# Software Package for additional details.

# src/schema/

# Define generated header paths
set(GENERATED_INCLUDE_DIR "${COST_MODEL_BINARY_DIR}/include")
set(VPUNN_GENERATED_HEADER "${GENERATED_INCLUDE_DIR}/vpunn_generated.h")
set(CYCLES_CACHE_GENERATED_HEADER "${GENERATED_INCLUDE_DIR}/cycles_cache_generated.h")

# Ensure output directory exists
file(MAKE_DIRECTORY ${GENERATED_INCLUDE_DIR})

add_custom_command(
    OUTPUT 
        ${VPUNN_GENERATED_HEADER}
        ${CYCLES_CACHE_GENERATED_HEADER}
    COMMAND 
        ${FLATC_COMMAND} --cpp -o ${GENERATED_INCLUDE_DIR} "vpunn.fbs" "cycles_cache.fbs"
    DEPENDS 
        vpunn.fbs 
        cycles_cache.fbs
        ${flatc_TARGET}
    WORKING_DIRECTORY 
        "${PROJECT_SOURCE_DIR}/src/schema"
    COMMENT 
        "Generating c++ schema files"
)

# C++ schema target
add_custom_target(vpunn_cpp_schema
    DEPENDS 
        ${VPUNN_GENERATED_HEADER}
        ${CYCLES_CACHE_GENERATED_HEADER}
)

# Define Python generated files (marker files for CMake dependency tracking)
# CMake needs to know WHAT FILES will be generated to determine when regeneration is needed.
# We don't need to list ALL generated files, just representative ones that CMake can check.
# If these files exist and are newer than the input .fbs files, CMake skips regeneration.
# Names of the files that will be generated can be found in either the output directory after running flatc --python, or in the .fbs schema files themselves.
set(VPUNN_PYTHON_GENERATED_INIT "${GENERATED_INCLUDE_DIR}/VPUNN_SCHEMA/__init__.py")        # Always generated by flatc --python (Python package marker)
set(CYCLES_CACHE_PYTHON_GENERATED "${GENERATED_INCLUDE_DIR}/VPUNN_SCHEMA/CyclesCache.py")   # Generated from cycles_cache.fbs schema

# This tells CMake exactly what files are created and what they depend on.
# CMake will only run this command when:
# 1. The OUTPUT files don't exist, OR
# 2. Any DEPENDS file is newer than the OUTPUT files
add_custom_command(
    OUTPUT                          # Files that will be CREATED by this command
        ${VPUNN_PYTHON_GENERATED_INIT}
        ${CYCLES_CACHE_PYTHON_GENERATED}
    COMMAND                         # The actual command to run
        ${FLATC_COMMAND} --python -o ${GENERATED_INCLUDE_DIR} 
        ${PROJECT_SOURCE_DIR}/src/schema/vpunn.fbs
        ${PROJECT_SOURCE_DIR}/src/schema/cycles_cache.fbs
    DEPENDS
        ${PROJECT_SOURCE_DIR}/src/schema/vpunn.fbs
        ${PROJECT_SOURCE_DIR}/src/schema/cycles_cache.fbs
        ${flatc_TARGET}                                    
    WORKING_DIRECTORY 
        "${PROJECT_SOURCE_DIR}/python"
    COMMENT 
        "Generating python schema files"
)

# Create a target that depends on the generated python files
# This is needed so the python schema generation gets included in the build process.
# The "ALL" keyword means this target will be built when you run "make" or "cmake --build ." building it along with is dependencies.
# Without this target, the add_custom_command would only run when something explicitly needs the OUTPUT files.
add_custom_target(vpunn_python_schema ALL
    DEPENDS 
        ${VPUNN_PYTHON_GENERATED_INIT}
        ${CYCLES_CACHE_PYTHON_GENERATED}
)
