# If not stated otherwise in this file or this component's LICENSE file the
# following copyright and licenses apply:

# Copyright 2020 Sky UK

# Licensed 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.

# Build and run the libocispec generator tool to create header files
# Will also build parsers for any schemas in the bundle/schemas directory

# TODO:: Do this in a more "bitbake-like" way instead of with submodules

# Where the libocispec submodule lives
set(LIBOCISPEC_DIR
    ${CMAKE_SOURCE_DIR}/libocispec
)

# Where we store any custom Dobby specific schemas
set(SCHEMAS_DIR
    ${CMAKE_SOURCE_DIR}/bundle/runtime-schemas
)

message("Updating libocispec submodule")
# Make sure the git submodule contains the nested submodules properly
execute_process(
    COMMAND git submodule update --init --recursive
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

message("Cleaning libocispec")
# Clean the libocispec directory to remove old generated code
execute_process(
    COMMAND git clean -dfx -e "*.py"
    WORKING_DIRECTORY ${LIBOCISPEC_DIR}/src
)

# Get a list of all the JSON customs schema files and copy them into libocispec
file(GLOB CUSTOM_SCHEMA_FILES "${SCHEMAS_DIR}/*.json" CONFIGURE_DEPENDS)
file(COPY ${CUSTOM_SCHEMA_FILES} DESTINATION ${LIBOCISPEC_DIR}/runtime-spec/schema/)

# Get the add_plugin_tables.py script and copy it into libocispec
file(COPY "${SCHEMAS_DIR}/add_plugin_tables.py" DESTINATION ${LIBOCISPEC_DIR}/src/)


# Where the libocispec generated C files will live
set(LIBOCISPEC_GENERATED_DIR
    ${LIBOCISPEC_DIR}/src
)

message("Configuring libocispec")
# Actually build libocispec with our custom schemas added
# Run autotools to make sure everything is ready (and create config.h)
execute_process(
    WORKING_DIRECTORY ${LIBOCISPEC_DIR}
    COMMAND ${LIBOCISPEC_DIR}/autogen.sh "--host=x86_64-pc-linux-gnu"
)

# Change the names of the output source files to reduce the length of variable and function names
execute_process(
    WORKING_DIRECTORY ${LIBOCISPEC_DIR}
    COMMAND mkdir -p ./schemas/rt
    COMMAND mkdir -p ./schemas/img
    COMMAND cp -r ./runtime-spec/schema/. ./schemas/rt/
    COMMAND cp -r ./image-spec/schema/. ./schemas/img/
)

# Now run the generator to make our code
execute_process(
    WORKING_DIRECTORY ${LIBOCISPEC_DIR}
    COMMAND python3 ./src/generate.py --gen-common --gen-ref --root=./schemas --out=./src ./schemas/rt ./schemas/img
)

# DobbyConfig needs to be able to see a list of plugins' names and pointers to their structs
# Add plugin tables to Dobby's schema
execute_process(
    WORKING_DIRECTORY ${LIBOCISPEC_DIR}/src/
    COMMAND python3 add_plugin_tables.py
)

# Get the generated C files
file(GLOB_RECURSE LIBOCISPEC_GENERATED_FILES
    ${LIBOCISPEC_GENERATED_DIR}/*.c)
list(REMOVE_ITEM LIBOCISPEC_GENERATED_FILES "${LIBOCISPEC_GENERATED_DIR}/validate.c")

# Create a new library called libocispec from the generated files
add_library(libocispec
    ${LIBOCISPEC_GENERATED_FILES}
)

target_include_directories(libocispec
    PUBLIC
    ${LIBOCISPEC_GENERATED_DIR}

    PRIVATE
    ${LIBOCISPEC_DIR} # Need to add this for the config.h generated by autotools
)

# Libocispec needs yajl to work
target_link_libraries(libocispec
    yajl
)

set_target_properties(libocispec
    PROPERTIES POSITION_INDEPENDENT_CODE ON
)