@echo off
REM =====================================================================================
REM --------------------- Clion Run Configuration Template ------------------------------
REM -------------------------------------------------------------------------------------
REM Build a Sierra Chart ASCIL module with DEBUG (LLDB) libs in Clion.
REM ------------------------------------------------------------------
REM Required customization:
REM (1) Check the vcvarsall.bat folder location.
REM (2) Set DIRNAME and FILENAME env vars in the Clion dialog.
REM     DIRNAME=C:\SierraChart\SierraChartInstance_2;FILENAME=myStudyName
REM (3) Check the Sierra UDP IP addr and port (using 127.0.0.1, 22093)
REM     GLOBAL SETTINGS->SIERRA CHART SERVER SETTINGS
REM -------------------------------------------------------------------
REM The UDP command msgs goto Sierra Chart to unload/reload the dll.  Otherwise the link fails.
REM The call to VCVARSALL was copied from the output of a Sierra build in file
REM     .../ACS_Source/VisualCCompile.Bat
REM The working dir is set via pushd.  The working dir must be set.
REM =====================================================================================


pushd %DIRNAME%

set "c=         $client = New-Object System.Net.Sockets.UdpClient; "
set "c=%c%      $bytes = [System.Text.Encoding]::ASCII.GetBytes('RELEASE_DLL--%DIRNAME%\Data\%FILENAME%_64.dll'); "
set "c=%c%      $client.Send($bytes, $bytes.Length, '127.0.0.1', 22093); "
set "c=%c%      $client.Close();"
powershell -Command "%c%"


powershell -Command "Start-Sleep -Seconds 1"

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
cl /JMC /MP /analyze- /Zc:wchar_t /Z7 /Od /GS /W3 /RTC1 /Zc:inline ^
   /D "_WINDOWS" /D "_USRDLL" /D "_WINDLL" /Gd /Gy /GR- /GF /fp:precise ^
   /MTd /std:c++17 /LD /EHa /WX- /diagnostics:classic /nologo ^
   /Fo:"%DIRNAME%\ACS_Source\%FILENAME%.obj" ^
   "%DIRNAME%\ACS_Source\%FILENAME%.cpp" ^
   /link Gdi32.lib User32.lib ^
   /DLL /DYNAMICBASE /DEBUG /INCREMENTAL:NO /OPT:REF /MACHINE:X64 ^
   /OUT:"%DIRNAME%\Data\%FILENAME%_64.dll"


set "c=         $client = New-Object System.Net.Sockets.UdpClient; "
set "c=%c%      $bytes = [System.Text.Encoding]::ASCII.GetBytes('ALLOW_LOAD_ALL_DLLS'); "
set "c=%c%      $client.Send($bytes, $bytes.Length, '127.0.0.1', 22093); "
set "c=%c%      $client.Close();"
powershell -Command "%c%"


popd