# INTEL CONFIDENTIAL
# Copyright (C) Intel Corporation All Rights Reserved.
#
# The source code contained or described herein and all documents related to
# the source code ("Material") are owned by Intel Corporation or its suppliers
# or licensors. Title to the Material remains with Intel Corporation or its
# suppliers and licensors. The Material contains trade secrets and proprietary
# and confidential information of Intel or its suppliers and licensors. The
# Material is protected by worldwide copyright and trade secret laws and
# treaty provisions. No part of the Material may be used, copied, reproduced,
# modified, published, uploaded, posted, transmitted, distributed, or disclosed
# in any way without Intel's prior express written permission.
#
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be
# express and approved by Intel in writing.
#

cmake_minimum_required(VERSION 3.7.2)
# Some tests pause so user can see them.  Don't pause if a robot is running the tests
OPTION(DEFINE_ROBOT "Automation build" OFF) # Disabled by default
IF(DEFINE_ROBOT)
       ADD_DEFINITIONS(-DROBOT)
ENDIF(DEFINE_ROBOT)
enable_testing()

# We only want Debug and Release configurations to be available for now.
# This is because we don't set the custom link flags for the other configurations yet
set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE TYPE INTERNAL FORCE )

project (OpenIPC CXX)

#Definition of all defs which may be included
set(INCLUDE_DIR "CMakeIncludes")
set(SRC "sources")

# Use this macro to include all OS&Architecture specific parts of code
MACRO(include_cmake CMakeFile)
    include("${CMAKE_CURRENT_SOURCE_DIR}/${INCLUDE_DIR}/${CMakeFile}.cmake" OPTIONAL)
    include("${CMAKE_CURRENT_BINARY_DIR}/${INCLUDE_DIR}/${CMakeFile}.cmake" OPTIONAL)
ENDMACRO(include_cmake)

SET(MAIN_ROOT       "${CMAKE_CURRENT_SOURCE_DIR}")
SET(OPENIPC_ROOT    "${MAIN_ROOT}/OpenIPC")
SET(VALIDATION_ROOT "${MAIN_ROOT}/Validation")
SET(THIRDPARTY_ROOT "${OPENIPC_ROOT}/ThirdParty")
SET(CRYPTO_ROOT     "${THIRDPARTY_ROOT}/openssl")
SET(IPC_ROOT        "${MAIN_ROOT}/IPC")

IF(DEFINED PACKAGING_DIR)
    SET(DPL_OPENIPC_ROOT "${PACKAGING_DIR}")
ELSE()
    SET(DPL_OPENIPC_ROOT ".")
ENDIF()

SET(DPL_BIN_ROOT "${DPL_OPENIPC_ROOT}/Bin")
SET(DPL_PYTHON_MODULES_ROOT "${DPL_OPENIPC_ROOT}/PythonModules")

IF(DEFINED TESTS_DEPLOY_BASE)
    SET(DPL_TESTS_ROOT "${TESTS_DEPLOY_BASE}")
ELSE()
    SET(DPL_TESTS_ROOT "${DPL_BIN_ROOT}")
ENDIF()

set(DPL_PROBEPLUGINSDK_ROOT "${DPL_OPENIPC_ROOT}/ProbePluginSDK")

set(DPL_PROBEPLUGINSDK_LIBS "${DPL_PROBEPLUGINSDK_ROOT}/Libraries/Bin")
set(DPL_PROBEPLUGINSDK_SOURCE "${DPL_PROBEPLUGINSDK_ROOT}/Source")
set(DPL_PROBEPLUGINSDK_INCLUDE "${DPL_PROBEPLUGINSDK_SOURCE}/Public_Include")

set(DPL_RUNCONTROLSDK_ROOT "${DPL_OPENIPC_ROOT}/RunControlSDK")

set(DPL_RUNCONTROLSDK_LIBS "${DPL_RUNCONTROLSDK_ROOT}/Libraries/Bin")
set(DPL_RUNCONTROLSDK_RCTL "${DPL_RUNCONTROLSDK_ROOT}/Source/PluginSDK/RunControl")
set(DPL_RUNCONTROLSDK_INCLUDE "${DPL_RUNCONTROLSDK_ROOT}/Source/Public_Include")

if(NOT BUILD_RECIPE)
    file(GLOB ALL_BUILD_RECIPES "${INCLUDE_DIR}/recipes/*.cmake")
    list(LENGTH ALL_BUILD_RECIPES NUM_BUILD_RECIPES)

    if(NUM_BUILD_RECIPES EQUAL 1)
        # There is only one build recipe. Use it
        list(GET ALL_BUILD_RECIPES 0 BUILD_RECIPE_FULL_PATH)
        get_filename_component(BUILD_RECIPE "${BUILD_RECIPE_FULL_PATH}" NAME_WE)
    elseif(NUM_BUILD_RECIPES EQUAL 0)
        # There are no build recipes. Oops
        message(FATAL_ERROR "No build recipes exist. At least one build recipe should be defined uner \"CMakeIncludes/recipes\"")
    else()
        # There is more than one build recipe, and the user hasn't specified which one to use
        message(FATAL_ERROR "BUILD_RECIPE has not been set. This is required to be set to the name of one of the files under \"CMakeIncludes/recipes\"")
    endif()
endif()

if(NOT EXISTS "${MAIN_ROOT}/${INCLUDE_DIR}/recipes/${BUILD_RECIPE}.cmake")
    message(FATAL_ERROR "The specified build recipe (\"${BUILD_RECIPE}\") cannot be found under \"CMakeIncludes/recipes\"")
endif()

message(STATUS "INFO: The selected build recipe is ${BUILD_RECIPE}")

set(PROJECT_CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/Deploy")
set(CMAKE_INSTALL_PREFIX "${PROJECT_CMAKE_INSTALL_PREFIX}" CACHE INTERNAL "prefix override")

set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "_CMakePredefinedTargets")
include_cmake("globals")
include_cmake("helper_functions")

# output folders for lib-archives, lib-stubs, and binaries
#
# note that this is not the "Deployment" area, it is simply where the compiler/linker will output binaries
#
set(OPENIPC_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/build/${BUILD_RECIPE}")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OPENIPC_OUTPUT_DIRECTORY}/static_libs)                     # .a files on linux, .lib files for static libs on Windows
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG            ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE          ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO   ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_MINSIZEREL       ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OPENIPC_OUTPUT_DIRECTORY}/Bin)                    # .so files on Linux, .lib DLL stubs on Windows (for shared libs)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG            ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE          ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO   ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL       ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OPENIPC_OUTPUT_DIRECTORY}/Bin)                     # .exe/dll files on Windows, executables on Linux
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG            ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE          ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO   ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL       ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})


#########################################################################################
#
#  _____           _
# |  __ \         | |
# | |__) |_ _  ___| | ____ _  __ _  ___
# |  ___/ _` |/ __| |/ / _` |/ _` |/ _ \
# | |  | (_| | (__|   < (_| | (_| |  __/
# |_|   \__,_|\___|_|\_\__,_|\__, |\___|
#                             __/ |
#                            |___/
#
# Set options for CPack, which will create an archive or installable package using the info already encoded into the
# CMake build scripts.
# Note that this section only sets global options, each target must specify its own packaging requirements in its
# own lists file.  Search for "install(" to find those directives!

set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)

set(CPACK_PACKAGE_NAME "OpenIPC ${BUILD_RECIPE}")
set(CPACK_PACKAGE_VENDOR "Intel Corporation")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "OpenIPC")
set(CPACK_PACKAGE_VERSION_MAJOR "1")
set(CPACK_PACKAGE_VERSION_MINOR "0")
set(CPACK_PACKAGE_VERSION_PATCH "0")

# names of *ALL* components
#
set(CPACK_COMPONENTS_ALL
    openipc             # OpenIPC standalone component
    ipcsdk              # IPC API SDK
    probepluginsdk      # ProbePluginSDK
    runcontrolsdk       # RunControlSDK
    tests               # Unit tests
    debug_symbols       # Debug symbol files
    )

#########################################################################################

# This is the part of the product data that is common/generated
# This stuff is consumed by CMakeIncludes/resource_template.rc to set the file properties
# for all binary files
set(OPENIPC_COPYRIGHT_STRING "Copyright (C) Intel Corporation. All rights reserved.")
set(OPENIPC_PRODUCT_NAME "OpenIPC Debug Library")

#########################################################################################
set(SOURCE_RELEASE_DIRECTORY "${OPENIPC_OUTPUT_DIRECTORY}/SourceRelease")

ShipSources(FILES "CMakeLists.txt"
                  ".editorconfig"
                  "CMakeIncludes/globals.cmake"
                  "CMakeIncludes/helper_functions.cmake"
                  "CMakeIncludes/resource_template.rc"
                  "OpenIPC_Source_Users_Guide.txt") # Include the main CMake files in the source release
ShipSources(FILES "${OPENIPC_ROOT}/Source/Public_Include/Foundation/Types.h"
                  "${OPENIPC_ROOT}/Source/Public_Include/Foundation/Version.h") # Include the common headers
ShipSources(FILES "${OPENIPC_ROOT}/OpenIPC_documentation.html"
                  "${OPENIPC_ROOT}/OpenIPC_Users_Guide.html") # Include the basic documentation pages

include_cmake("recipes/${BUILD_RECIPE}")
ShipSources(FILES "${MAIN_ROOT}/CMakeIncludes/recipes/${BUILD_RECIPE}.cmake")

DefineRecipeConfigurationKnobs()
DefineRecipeCompilerFlags()

include("CMakeIncludes/PreRecipeSteps.cmake" OPTIONAL)

DefineRecipeIngredients()
ShipLicenseFile("${CONFIG_LICENSE}")
DefineSourceReleaseProject("${SOURCE_RELEASE_DIRECTORY}")

include("CMakeIncludes/PostRecipeSteps.cmake" OPTIONAL)

INCLUDE(CPack) # must be after setting the CPack variables above
include(CTest)
