Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created March 28, 2024 14:49
Show Gist options
  • Save t-mat/de819af37d3dd399147a75f9af1b034e to your computer and use it in GitHub Desktop.
Save t-mat/de819af37d3dd399147a75f9af1b034e to your computer and use it in GitHub Desktop.
MSVC batch file for lz4
@echo off
: Copy this file as lz4/build/visual_studio/lz4/build-vs2022.cmd
setlocal enabledelayedexpansion
: "%~dp0" = directory which contains this batch file.
cd /d "%~dp0"
: %esc% = escape code (\x1b)
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "esc=%%E"
: Find vswhere.exe
set "vswhere=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
echo vswhere="%vswhere%"
if not exist "%vswhere%" ( echo Failed to find vswhere.exe. && goto :ERROR )
: Find VBisual Studio via vswhere
:
: https://github.com/Microsoft/vswhere
: https://github.com/microsoft/vswhere/wiki/Find-VC#batch
: Find MSVC version [17.0 , 18.0)
"%vswhere%" -version [17.0,18.0) -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath > vs_dir.tmp
set /p VS_DIR=<vs_dir.tmp
echo VS_DIR="%VS_DIR%"
if "%VS_DIR%" == "" ( echo Failed to find Visual C++. && goto :ERROR )
echo call "%VS_DIR%\VC\Auxiliary\Build\vcvars64.bat"
call "%VS_DIR%\VC\Auxiliary\Build\vcvars64.bat" || goto :ERROR
echo rc.exe /I..\..\..\lib /fo lz4.res lz4.rc
rc.exe /I..\..\..\lib /fo lz4.res lz4.rc || goto :ERROR
set EXE_NAME=lz4.exe
set SRCS=^
..\..\..\lib\lz4.c ^
..\..\..\lib\lz4frame.c ^
..\..\..\lib\lz4hc.c ^
..\..\..\lib\xxhash.c ^
..\..\..\programs\bench.c ^
..\..\..\programs\lorem.c ^
..\..\..\programs\lz4cli.c ^
..\..\..\programs\lz4io.c ^
..\..\..\programs\threadpool.c ^
..\..\..\programs\timefn.c ^
..\..\..\programs\util.c
: /nologo : Don't show MSVC logo
: /utf-8 : Specify source code text encoding as UTF-8
: /Foobjs\ : Specify object directory "objs\"
: /I..\..\..\lib : Specify include path "..\..\..\lib"
set COMPILER_OPTIONS=^
/nologo ^
/utf-8 ^
/Foobjs\ ^
/O2 ^
/I ..\..\..\lib
: /emittoolversioninfo:no : Don't put PE "Rich" header
: /PDBALTPATH : Specify .pdb file name
: /out:"%EXE_NAME%" : Specify output executable name
set LINKER_OPTIONS=^
/emittoolversioninfo:no ^
/PDBALTPATH:"%EXE_NAME%.pdb" ^
/out:"%EXE_NAME%"
if not exist objs ( mkdir objs )
echo cl.exe %COMPILER_OPTIONS% %SRCS% /link %LINKER_OPTIONS% lz4.res
cl.exe %COMPILER_OPTIONS% %SRCS% /link %LINKER_OPTIONS% lz4.res || goto :ERROR
echo Build Status -%esc%[92m SUCCEEDED %esc%[0m
set /a errorno=0
goto :END
:ERROR
set /a errorno=1
echo Abort by error.
echo Build Status -%esc%[91m ERROR %esc%[0m
:END
popd
exit /B %errorno%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment