###############################################################################
#  INTEL CONFIDENTIAL
# 
#  Copyright (c) 2020-2023 Intel Corporation All Rights Reserved
# 
#  This software and the related documents are Intel copyrighted materials,
#  and your use of them is governed by the express license under which they
#  were provided to you ("License"). Unless the License provides otherwise,
#  you may not use, modify, copy, publish, distribute, disclose or transmit
#  this software or the related documents without Intel's prior written
#  permission.
# 
#  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.
# 
#  This software and the related documents are provided as is,
#  with no express or implied warranties, other than those that are expressly
#  stated in the License.
# 
###############################################################################


cmake_minimum_required(VERSION 3.15)

# TODO: Specify the C++ standard [Placeholder - update version after we finalize]
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Only Debug and Release configurations are supported, make it so that only these
# two configurations are generated by CMake.
SET(CMAKE_CONFIGURATION_TYPES Debug Release CACHE INTERNAL "Only Debug and Release are supported for the IPF Extensible Framework" FORCE)

# CMake versions greater than 3.19 will complain about missing imported targets
# Ignore warnings about RelWithDebugInfo and MinSizeRel imported targets
cmake_policy(SET CMP0111 OLD)

if (WIN32)
set(OSLIBS)
SET(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
else()
set(OSLIBS)
endif (WIN32)

if (WIN32)
SET(CMAKE_CXX_FLAGS_DEBUG  "${CMAKE_CXX_FLAGS_DEBUG} /Zi /GS /sdl /Qspectre /guard:cf")
SET(CMAKE_CXX_FLAGS_RELEASE  "${CMAKE_CXX_FLAGS_RELEASE} /Zi /GS /sdl /Qspectre /guard:cf")
SET(CMAKE_C_FLAGS_DEBUG  "${CMAKE_CXX_FLAGS_DEBUG} /GS /sdl /Qspectre")
SET(CMAKE_C_FLAGS_RELEASE  "${CMAKE_CXX_FLAGS_RELEASE} /Zi /GS /sdl /Qspectre")
SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} /CETCOMPAT /NXCOMPAT /DYNAMICBASE /HIGHENTROPYVA /ALLOWISOLATION /guard:cf /debug:full")
# ALLOWISOLATION is incompatible with DLLs
SET(CMAKE_MODULE_LINKER_FLAGS  "${CMAKE_MODULE_LINKER_FLAGS} /CETCOMPAT /NXCOMPAT /DYNAMICBASE /HIGHENTROPYVA /guard:cf /debug:full") 
endif (WIN32)

# Optional Self-Signing Test Certificate. See README.md for Signing Requirements.
if (WIN32)
SET(SIGNTOOL_CERTIFICATE "../tools/ipftest.pfx")
SET(SIGNTOOL_COMMAND if exist ${SIGNTOOL_CERTIFICATE} (signtool.exe sign /f ${SIGNTOOL_CERTIFICATE} /fd sha512 $(OutDir)$(TargetName)$(TargetExt)) else (echo Signing skipped [No Test Certificate]: $(OutDir)$(TargetName)$(TargetExt)))
endif (WIN32)


project(Placeholder)

#################### Provider Section ####################

# Using Multi-threaded dynamically-linked (-MD) runtime libraries for Provider
# Import the ProviderApi static library
add_library(ProviderApi STATIC IMPORTED)
set_property(TARGET ProviderApi PROPERTY IMPORTED_LOCATION_DEBUG  "${CMAKE_SOURCE_DIR}/lib/Debug/ProviderApi.lib")
set_property(TARGET ProviderApi PROPERTY IMPORTED_LOCATION_RELEASE  "${CMAKE_SOURCE_DIR}/lib/Release/ProviderApi.lib")

# Build simple provider
add_library(SampleCameraProvider MODULE
              samples/provider/SimpleProvider/SampleCameraProvider.cpp
              samples/provider/SimpleProvider/SampleCameraProviderApi.cpp
            )
set_target_properties(SampleCameraProvider PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
target_link_libraries(SampleCameraProvider PUBLIC ProviderApi ${OSLIBS})
target_include_directories(SampleCameraProvider PUBLIC
                            "include"
                            "include/thirdparty")  
     					
add_custom_command(TARGET SampleCameraProvider PRE_BUILD
	COMMAND ${CMAKE_COMMAND} -E copy_if_different 
		"$<$<CONFIG:Debug,Release>:${CMAKE_SOURCE_DIR}/lib/$<CONFIG>/ProviderApi.pdb>"
		$<TARGET_FILE_DIR:SampleCameraProvider>)

if (WIN32)
add_custom_command(TARGET SampleCameraProvider POST_BUILD COMMAND ${SIGNTOOL_COMMAND})
endif (WIN32)

# Using Multi-threaded statically-linked (-MT) runtime libraries for Provider
# Import the ProviderApiStaticRT static library
add_library(ProviderApiStaticRT STATIC IMPORTED)
set_property(TARGET ProviderApiStaticRT PROPERTY IMPORTED_LOCATION_DEBUG  "${CMAKE_SOURCE_DIR}/lib/Debug/ProviderApiStaticRT.lib")
set_property(TARGET ProviderApiStaticRT PROPERTY IMPORTED_LOCATION_RELEASE  "${CMAKE_SOURCE_DIR}/lib/Release/ProviderApiStaticRT.lib")

# Build advanced provider
add_library(SampleBatteryProvider MODULE
              samples/provider/AdvancedProvider/SampleBatteryProvider.cpp
              samples/provider/AdvancedProvider/SampleBatteryProviderApi.cpp
            )
set_target_properties(SampleBatteryProvider PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded")
target_link_libraries(SampleBatteryProvider PUBLIC ProviderApiStaticRT ${OSLIBS})
target_include_directories(SampleBatteryProvider PUBLIC
                            "include"
                            "include/thirdparty")

add_custom_command(TARGET SampleBatteryProvider PRE_BUILD
	COMMAND ${CMAKE_COMMAND} -E copy_if_different 
	    "$<$<CONFIG:Debug,Release>:${CMAKE_SOURCE_DIR}/lib/$<CONFIG>/ProviderApiStaticRT.pdb>"
		$<TARGET_FILE_DIR:SampleBatteryProvider>)							

if (WIN32)
add_custom_command(TARGET SampleBatteryProvider POST_BUILD COMMAND ${SIGNTOOL_COMMAND})
endif (WIN32)

#################### Client Section ####################

# Import the ClientApiLib static library
add_library(ClientApiLib STATIC IMPORTED)
set_property(TARGET ClientApiLib PROPERTY IMPORTED_LOCATION_DEBUG "${CMAKE_SOURCE_DIR}/lib/Debug/ClientApiLib.lib")
set_property(TARGET ClientApiLib PROPERTY IMPORTED_LOCATION_RELEASE "${CMAKE_SOURCE_DIR}/lib/Release/ClientApiLib.lib")


### C++ Section
# The example C++ client application
add_executable(SampleClientAppCpp samples/client/cpp/SampleClientApp.cpp)
target_link_libraries(SampleClientAppCpp PUBLIC ClientApiLib ${OSLIBS})
target_include_directories(SampleClientAppCpp PUBLIC "include" "include/thirdparty")
	
### C Section
# The example C client application
add_executable(SampleClientAppC samples/client/c/SampleClientApp.c)
target_link_libraries(SampleClientAppC PUBLIC ClientApiLib ${OSLIBS})
target_include_directories(SampleClientAppC PUBLIC "include" "include/thirdparty")

### C# Section
#
#  Please see README.md for instructions to use the nuget package
#  to build the C# sample application with .NET 6.0
#

# Copy python libraries and samples to the build output folders
file(COPY ${CMAKE_SOURCE_DIR}/lib/Debug/ClientApiPy.pyd
     DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Debug)

file(COPY ${CMAKE_SOURCE_DIR}/samples/client/python/SampleClientApp.py
     DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Debug)

file(COPY ${CMAKE_SOURCE_DIR}/lib/Debug/ClientApiPy.pyd
     DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Release)

file(COPY ${CMAKE_SOURCE_DIR}/samples/client/python/SampleClientApp.py
     DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Release)

# Rename project
project(ipf-ef-sdk)

