cmake_minimum_required(VERSION 3.8) # Due to CSharpUtilities

project(CSharpViewer LANGUAGES CSharp)
include(CSharpUtilities)

file(GLOB CS_FILES Properties/*.cs *.cs *.resx)


set(PROJECT_NAME "CSharpViewer")
add_executable(${PROJECT_NAME}  app.config  ${CS_FILES} )



# This project can be built stand alone or as a subproject.
#
# - Set needed variable if we're building standalone.
#
if ( NOT DEFINED AGX_BUILD_ARCHITECTURE )
  set( AGX_BUILD_ARCHITECTURE "$ENV{AGX_BUILD_ARCHITECTURE}")
endif()

if ( NOT DEFINED AGX_DIR )
  if ( DEFINED AGX_ROOT_DIRECTORY )
    set(AGX_DIR "${AGX_ROOT_DIRECTORY}") # Called as subproject
  else()
    set(AGX_DIR "$ENV{AGX_DIR}")
  endif()
endif()

#
# Handle x86/x64 so we match how the AGX library is built:
#
if( "${AGX_BUILD_ARCHITECTURE}" STREQUAL "x86")
      string(REPLACE  "x64" "x86"  CMAKE_CSharp_FLAGS ${CMAKE_CSharp_FLAGS} )
else()
      string(REPLACE  "x86" "x64"  CMAKE_CSharp_FLAGS ${CMAKE_CSharp_FLAGS} )
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CSharp)
target_link_libraries(${PROJECT_NAME} agxDotNet)

set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${AGX_SWIG_BINARY_DIR})

csharp_set_windows_forms_properties( ViewControl.cs  "ViewControl.Designer.cs" )
csharp_set_windows_forms_properties( ViewControl.cs  "ViewControl.resx" )

set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
set_property(TARGET ${PROJECT_NAME} PROPERTY WIN32_EXECUTABLE TRUE)

# CMake doesn't recognize the agxDotNet target as a C# target, so just using target_link_libraries
# is not enough. If/when CMake supports creating C# targets for non-Windows platforms we should
# be able to remove this reference. See https://gitlab.kitware.com/cmake/cmake/-/issues/16865
if (MSVC)
  set(AGXDOTNET_DLL_FILE ${AGX_SWIG_BINARY_DIR}/$(Configuration)/agxDotNet.dll)
  set(AGXMATH_DLL_FILE ${AGX_SWIG_BINARY_DIR}/$(Configuration)/agxMathDotNet.dll)
else()
  set(AGXDOTNET_DLL_FILE ${AGX_SWIG_BINARY_DIR}/agxDotNet.dll)
  set(AGXMATH_DLL_FILE ${AGX_SWIG_BINARY_DIR}/agxMathDotNet.dll)
endif()

set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DOTNET_REFERENCES
    "Microsoft.CSharp"
    "System"
    "System.Core"
    "System.Management"
    "System.Xml.Linq"
    "System.Data.DataSetExtensions"
    "System.Data"
    "System.Xml"
    "System.Windows.Forms"
    "System.Drawing"
    "${AGXDOTNET_DLL_FILE}"
    "${AGXMATH_DLL_FILE}"
)
