<commit_before>@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET CPU=""
SET CONFIGURATION=Release
SET PLATFORM=x86
SET solutionPath=ortc\windows\solutions\zsLib.Eventing.sln
SET msVS_Path=""
SET x86BuildCompilerOption=amd64_x86
SET x64BuildCompilerOption=amd64
SET armBuildCompilerOption=amd64_arm
SET currentBuildCompilerOption=amd64

SET compilerPath=%cd%\ortc\windows\solutions\Build\Output\!PLATFORM!\!CONFIGURATION!\zsLib.Eventing.Tool.Compiler\zsLib.Eventing.Tool.Compiler.exe
SET windowsKitPath="C:\Program Files (x86)\Windows Kits\10\bin\%PLATFORM%\"

SET eventsIncludePath=..\Internal\
SET eventsIntermediatePath=IntermediateTemp
SET eventsOutput=%cd%\ortc\windows\solutions\eventing\


SET startTime=0
SET endingTime=0

::log levels
SET logLevel=4											
SET error=0														
SET info=1														
SET warning=2													
SET debug=3														
SET trace=4	


:main
SET startTime=%time%

CALL:determineVisualStudioPath

CALL:setCompilerOption %PLATFORM%

::CALL:buildEventing

CALL:prepareEvent
GOTO:EOF

:determineVisualStudioPath

SET progfiles=%ProgramFiles%
IF NOT "%ProgramFiles(x86)%" == "" SET progfiles=%ProgramFiles(x86)%

REM Check if Visual Studio 2015 is installed
SET msVS_Path="%progfiles%\Microsoft Visual Studio 14.0"

IF NOT EXIST %msVS_Path% (
	REM Check if Visual Studio 2013 is installed
	SET msVS_Path="%progfiles%\Microsoft Visual Studio 12.0"
)

IF NOT EXIST %msVS_Path% CALL:error 1 "Visual Studio 2015 or 2013 is not installed"

CALL:print %trace% "Visual Studio path is %msVS_Path%"

GOTO:EOF

:setCompilerOption
CALL:print %trace% "Determining compiler options ..."
REG Query "HKLM\Hardware\Description\System\CentralProcessor\0" | FIND /i "x86" > NUL && SET CPU=x86 || SET CPU=x64

CALL:print %trace% "CPU arhitecture is %CPU%"

IF /I %CPU% == x86 (
	SET x86BuildCompilerOption=x86
	SET x64BuildCompilerOption=x86_amd64
	SET armBuildCompilerOption=x86_arm
)

IF /I %~1==x86 (
	SET currentBuildCompilerOption=%x86BuildCompilerOption%
) ELSE (
	IF /I %~1==ARM (
		SET currentBuildCompilerOption=%armBuildCompilerOption%
	) ELSE (
		SET currentBuildCompilerOption=%x64BuildCompilerOption%
	)
)

CALL:print %trace% "Selected compiler option is %currentBuildCompilerOption%"

GOTO:EOF


:buildEventing

IF EXIST %msVS_Path% (
	CALL %msVS_Path%\VC\vcvarsall.bat %currentBuildCompilerOption%
	IF ERRORLEVEL 1 CALL:error 1 "Could not setup compiler for  %PLATFORM%"
	
	MSBuild %solutionPath% /property:Configuration=%CONFIGURATION% /property:Platform=%PLATFORM% /t:Clean;Build /nodeReuse:False /m
	if ERRORLEVEL 1 CALL:error 1 "Building zsLib.Eventing.Tool.Compiler projects for %PLATFORM% has failed"
) ELSE (
	CALL:error 1 "Could not compile because proper version of Visual Studio is not found"
)
GOTO:EOF

:compileEvents
SET eventJsonPath=%1
SET eventPath=%~dp1
SET providerName=%~n1
SET intermediatePath=!eventPath!%eventsIntermediatePath%\
SET headersPath=!eventPath!%eventsIncludePath%!providerName!
SET outputPath=%eventsOutput%!providerName!

echo eventJsonPath=!eventJsonPath!
echo eventPath=!eventPath!
echo providerName=!providerName!
echo intermediatePath=!intermediatePath!
echo headersPath=!headersPath!
echo outputPath=!outputPath!
echo windowsKitPath=%windowsKitPath%
pause

CALL:createFolder !headersPath!
CALL:createFolder !intermediatePath!
CALL:createFolder !outputPath!

pause

PUSHD !eventPath!
CALL %compilerPath% -c !eventJsonPath! -o %eventsIncludePath%!providerName!\!providerName! > NUL

pause
echo Create manifest header file...

%windowsKitPath%mc.exe -um -r !intermediatePath! -h "!headersPath!" "!headersPath!\!providerName!_win_etw.man"

pause
echo Create resource file...
%windowsKitPath%rc.exe !intermediatePath!!providerName!_win_etw.rc

echo Create manifest resource dll...
pause

echo If compiling to managed code resource DLL use: csc.exe /out:!intermediatePath!!providerName!_win_etw.dll /target:library /win32res:!intermediatePath!!providerName!_win_etw.res

%msVS_Path%\VC\bin\link -dll -noentry /MACHINE:%PLATFORM% -out:!intermediatePath!!providerName!_win_etw.dll !intermediatePath!!providerName!_win_etw.res

echo Copy files to output directory...
pause
CALL:copyFiles !intermediatePath!!providerName!_win_etw.dll "!outputPath!\"
CALL:copyFiles "!headersPath!\!providerName!.jman" "!outputPath!\"
CALL:copyFiles "!headersPath!\!providerName!_win_etw.man" "!outputPath!\"
CALL:copyFiles "!headersPath!\!providerName!_win_etw.wprp" "!outputPath!\"
pause
icacls "!outputPath!\!providerName!_win_etw.dll" /grant Users:RX
popd

GOTO:EOF

:prepareEvent

for /r . %%g in (*.events.json) do CALL:compileEvents %%g

GOTO:EOF

:copyFiles
IF EXIST %1 (
	CALL:createFolder %2
	CALL:print %debug% "Copying %1 to %2"
	COPY %1 %2
	IF ERRORLEVEL 1 CALL:error 1 "Could not copy a %1"
) ELSE (
	CALL:error 1 "Could not copy a %1"
)
GOTO:EOF

:createFolder
IF NOT EXIST %1 (
	CALL:print %trace% "Creating folder %1"
	MKDIR %1
	IF ERRORLEVEL 1 CALL:error 1 "Could not make a directory %1"
)
GOTO:EOF

REM Print logger message. First argument is log level, and second one is the message
:print
<commit_msg>Added creating batch files for registering and uregistering eventing<commit_after>@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET CPU=""
SET CONFIGURATION=Release
SET PLATFORM=x86
SET solutionPath=ortc\windows\solutions\zsLib.Eventing.sln
SET msVS_Path=""
SET x86BuildCompilerOption=amd64_x86
SET x64BuildCompilerOption=amd64
SET armBuildCompilerOption=amd64_arm
SET currentBuildCompilerOption=amd64

SET compilerOutputPath=%cd%\ortc\windows\solutions\Build\Output\!PLATFORM!\!CONFIGURATION!\zsLib.Eventing.Tool.Compiler\zsLib.Eventing.Tool.Compiler.exe
SET compilerPath=%cd%\bin\zsLib.Eventing.Tool.Compiler.exe

SET windowsKitPath="C:\Program Files (x86)\Windows Kits\10\bin\%PLATFORM%\"

SET eventsIncludePath=..\Internal\
SET eventsIntermediatePath=IntermediateTemp
SET eventsOutput=%cd%\ortc\windows\solutions\eventing\


SET startTime=0
SET endingTime=0

::log levels
SET logLevel=4											
SET error=0														
SET info=1														
SET warning=2													
SET debug=3														
SET trace=4	


:main
SET startTime=%time%

CALL:determineVisualStudioPath

CALL:setCompilerOption %PLATFORM%

CALL:buildEventingToolCompiler

CALL:prepareEvents
GOTO:EOF

:determineVisualStudioPath

SET progfiles=%ProgramFiles%
IF NOT "%ProgramFiles(x86)%" == "" SET progfiles=%ProgramFiles(x86)%

REM Check if Visual Studio 2015 is installed
SET msVS_Path="%progfiles%\Microsoft Visual Studio 14.0"

IF NOT EXIST %msVS_Path% (
	REM Check if Visual Studio 2013 is installed
	SET msVS_Path="%progfiles%\Microsoft Visual Studio 12.0"
)

IF NOT EXIST %msVS_Path% CALL:error 1 "Visual Studio 2015 or 2013 is not installed"

CALL:print %trace% "Visual Studio path is %msVS_Path%"

GOTO:EOF

:setCompilerOption
CALL:print %trace% "Determining compiler options ..."
REG Query "HKLM\Hardware\Description\System\CentralProcessor\0" | FIND /i "x86" > NUL && SET CPU=x86 || SET CPU=x64

CALL:print %trace% "CPU arhitecture is %CPU%"

IF /I %CPU% == x86 (
	SET x86BuildCompilerOption=x86
	SET x64BuildCompilerOption=x86_amd64
	SET armBuildCompilerOption=x86_arm
)

IF /I %~1==x86 (
	SET currentBuildCompilerOption=%x86BuildCompilerOption%
) ELSE (
	IF /I %~1==ARM (
		SET currentBuildCompilerOption=%armBuildCompilerOption%
	) ELSE (
		SET currentBuildCompilerOption=%x64BuildCompilerOption%
	)
)

CALL:print %trace% "Selected compiler option is %currentBuildCompilerOption%"

GOTO:EOF


:buildEventingToolCompiler

IF EXIST %compilerPath% echo exists & GOTO:EOF

IF EXIST %msVS_Path% (
	CALL %msVS_Path%\VC\vcvarsall.bat %currentBuildCompilerOption%
	IF ERRORLEVEL 1 CALL:error 1 "Could not setup compiler for  %PLATFORM%"
	
	MSBuild %solutionPath% /property:Configuration=%CONFIGURATION% /property:Platform=%PLATFORM% /t:Clean;Build /nodeReuse:False /m
	if ERRORLEVEL 1 CALL:error 1 "Building zsLib.Eventing.Tool.Compiler projects for %PLATFORM% has failed"
) ELSE (
	CALL:error 1 "Could not compile because proper version of Visual Studio is not found"
)
CALL:copyFiles %compilerOutputPath% %cd%\bin\
GOTO:EOF

:compileEvent
SET eventJsonPath=%1
SET eventPath=%~dp1
SET providerName=%~n1
SET intermediatePath=!eventPath!%eventsIntermediatePath%\
SET headersPath=!eventPath!%eventsIncludePath%!providerName!
SET outputPath=%eventsOutput%!providerName!

echo eventJsonPath=!eventJsonPath!
echo eventPath=!eventPath!
echo providerName=!providerName!
echo intermediatePath=!intermediatePath!
echo headersPath=!headersPath!
echo outputPath=!outputPath!
echo windowsKitPath=%windowsKitPath%

CALL:createFolder !headersPath!
CALL:createFolder !intermediatePath!
CALL:createFolder !outputPath!


PUSHD !eventPath!
CALL %compilerPath% -c !eventJsonPath! -o %eventsIncludePath%!providerName!\!providerName! > NUL

echo Create manifest header file...

%windowsKitPath%mc.exe -um -r !intermediatePath! -h "!headersPath!" "!headersPath!\!providerName!_win_etw.man"

echo Create resource file...
%windowsKitPath%rc.exe !intermediatePath!!providerName!_win_etw.rc

echo Create manifest resource dll...


echo If compiling to managed code resource DLL use: csc.exe /out:!intermediatePath!!providerName!_win_etw.dll /target:library /win32res:!intermediatePath!!providerName!_win_etw.res

%msVS_Path%\VC\bin\link -dll -noentry /MACHINE:%PLATFORM% -out:!intermediatePath!!providerName!_win_etw.dll !intermediatePath!!providerName!_win_etw.res

echo Copy files to output directory...

CALL:copyFiles !intermediatePath!!providerName!_win_etw.dll "!outputPath!\"
CALL:copyFiles "!headersPath!\!providerName!.jman" "!outputPath!\"
CALL:copyFiles "!headersPath!\!providerName!_win_etw.man" "!outputPath!\"
CALL:copyFiles "!headersPath!\!providerName!_win_etw.wprp" "!outputPath!\"

icacls "!outputPath!\!providerName!_win_etw.dll" /grant Users:RX
popd

CALL:createRegistrationBatch !outputPath! !providerName!
CALL:createUnregistrationBatch !outputPath! !providerName!

GOTO:EOF

:createRegistrationBatch
SET outputFile=%1\register%2.bat

ECHO @ECHO OFF > !outputFile!
ECHO echo. >> !outputFile!
ECHO echo Registering manifest file and DLL for Windows Performance Recorder... >> !outputFile!
ECHO echo. >> !outputFile!
ECHO echo NOTE: Only run from command prompt as administrator >> !outputFile!
ECHO echo. >> !outputFile!

ECHO PUSHD %1 >> !outputFile!
ECHO CALL wevtutil.exe im !providerName!_win_etw.man /rf:"!outputPath!\!providerName!_win_etw.dll" /mf:"!outputPath!\!providerName!_win_etw.dll" >> !outputFile!
ECHO POPD >> !outputFile!
GOTO:EOF

:createUnregistrationBatch
SET outputFile=%1\unregister%2.bat

ECHO @ECHO OFF > !outputFile!
ECHO echo. >> !outputFile!
ECHO echo Unregistering manifest file and DLL for Windows Performance Recorder... >> !outputFile!
ECHO echo. >> !outputFile!
ECHO echo NOTE: Only run from command prompt as administrator >> !outputFile!
ECHO echo. >> !outputFile!

ECHO PUSHD %1 >> !outputFile!
ECHO CALL wevtutil.exe um !providerName!_win_etw.man >> !outputFile!
ECHO POPD >> !outputFile!
GOTO:EOF

:addAdminRights

ECHO net session ^>nul 2^>^&1 >> !outputFile!
ECHO if errorLevel 0 ( >> !outputFile!
ECHO 	echo Success: Administrative permissions confirmed. >> !outputFile!
ECHO ) else ( >> !outputFile!
ECHO 	echo Failure: Current permissions inadequate. >> !outputFile!
ECHO ) >> !outputFile!
	
GOTO:EOF

:prepareEvents

for /r . %%g in (*.events.json) do CALL:compileEvent %%g

GOTO:EOF

:copyFiles
IF EXIST %1 (
	CALL:createFolder %2
	CALL:print %debug% "Copying %1 to %2"
	COPY %1 %2
	IF ERRORLEVEL 1 CALL:error 1 "Could not copy a %1"
) ELSE (
	CALL:error 1 "Could not copy a %1"
)
GOTO:EOF

:createFolder
IF NOT EXIST %1 (
	CALL:print %trace% "Creating folder %1"
	MKDIR %1
	IF ERRORLEVEL 1 CALL:error 1 "Could not make a directory %1"
)
GOTO:EOF

REM Print logger message. First argument is log level, and second one is the message
:print

{'fraction_non_alphanumeric': 0.10988082157045051, 'fraction_numerical': 0.019524976755980052, 'mean_word_length': 5.997043169722058, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4359377', 'n_tokens_mistral': 4310, 'n_tokens_neox': 3889, 'n_words': 1016}