# Copyright 2020 The SwiftShader Authors. All Rights Reserved.
#
# 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.

set(ROOT_PROJECT_COMPILE_OPTIONS
    ${SWIFTSHADER_COMPILE_OPTIONS}
    ${WARNINGS_AS_ERRORS}
)

set(PIPELINE_SRC_FILES
    ComputeProgram.cpp
    ComputeProgram.hpp
    Constants.cpp
    Constants.hpp
    PixelProgram.cpp
    PixelProgram.hpp
    PixelRoutine.cpp
    PixelRoutine.hpp
    SamplerCore.cpp
    SamplerCore.hpp
    SetupRoutine.cpp
    SetupRoutine.hpp
    ShaderCore.cpp
    ShaderCore.hpp
    SpirvID.hpp
    SpirvShader.cpp
    SpirvShader.hpp
    SpirvShaderArithmetic.cpp
    SpirvShaderControlFlow.cpp
    SpirvShaderDebugger.cpp
    SpirvShaderEnumNames.cpp
    SpirvShaderGLSLstd450.cpp
    SpirvShaderGroup.cpp
    SpirvShaderImage.cpp
    SpirvShaderInstructions.cpp
    SpirvShaderMemory.cpp
    SpirvShaderSampling.cpp
    SpirvShaderSpec.cpp
    VertexProgram.cpp
    VertexProgram.hpp
    VertexRoutine.cpp
    VertexRoutine.hpp
)

add_library(vk_pipeline EXCLUDE_FROM_ALL
    ${PIPELINE_SRC_FILES}
)

# Add SPIRV-Tools dep
if (NOT TARGET SPIRV-Tools)
    message(FATAL_ERROR "Missing required target: SPIRV-Tools")
endif()

set_target_properties(core_tables PROPERTIES FOLDER "SPIRV-Tools build")
set_target_properties(enum_string_mapping PROPERTIES FOLDER "SPIRV-Tools build")
set_target_properties(extinst_tables PROPERTIES FOLDER "SPIRV-Tools build")
set_target_properties(spirv-tools-pkg-config PROPERTIES FOLDER "SPIRV-Tools build")
set_target_properties(spirv-tools-shared-pkg-config PROPERTIES FOLDER "SPIRV-Tools build")

# Copy the OpenCLDebugInfo100.h header that's generated by SPIRV-Tools
# out to a separate directory that can be added to the include path.
# Ideally, this header would just be pre-built and part of SPIRV-Headers.
# See: https://github.com/KhronosGroup/SPIRV-Headers/issues/137
set(SPIRV_TOOLS_EXT_INC_DIR ${CMAKE_CURRENT_BINARY_DIR}/spirv-tools-ext/include)
add_custom_command(
    OUTPUT "${SPIRV_TOOLS_EXT_INC_DIR}/spirv-tools/ext/OpenCLDebugInfo100.h"
    DEPENDS spirv-tools-header-OpenCLDebugInfo100
    COMMAND ${CMAKE_COMMAND} -E copy
        "${spirv-tools_BINARY_DIR}/OpenCLDebugInfo100.h"
        "${SPIRV_TOOLS_EXT_INC_DIR}/spirv-tools/ext/OpenCLDebugInfo100.h"
)
add_custom_target(spirv_tools_ext_includes
    DEPENDS "${SPIRV_TOOLS_EXT_INC_DIR}/spirv-tools/ext/OpenCLDebugInfo100.h")
set_target_properties(spirv_tools_ext_includes PROPERTIES FOLDER "SPIRV-Tools build")
add_dependencies(vk_pipeline spirv_tools_ext_includes)

set_target_properties(vk_pipeline PROPERTIES
    POSITION_INDEPENDENT_CODE 1
    FOLDER "SwiftShader VK"
    LINK_FLAGS "${SWIFTSHADER_LINK_FLAGS}"
)

target_include_directories(vk_pipeline
    PUBLIC
        ".."
        "${CMAKE_SOURCE_DIR}/include"
        "${SPIRV-Headers_SOURCE_DIR}/include"
        "${SPIRV_TOOLS_EXT_INC_DIR}"
)

target_compile_options(vk_pipeline
    PUBLIC
        ${ROOT_PROJECT_COMPILE_OPTIONS}
)

target_link_libraries(vk_pipeline
    PUBLIC
        vk_base
        vk_system
        marl
        SPIRV-Tools
        SPIRV-Tools-opt
)
