Merge pull request #109 from ceh/make-bat

make.bat: add Makefile-like functionality for Windows
This commit is contained in:
Armon Dadgar 2015-04-30 09:49:47 -07:00
commit f8aad6fd69
2 changed files with 216 additions and 0 deletions

107
make.bat Normal file
View file

@ -0,0 +1,107 @@
@echo off
setlocal
set _EXITCODE=0
REM If no target is provided, default to test.
if [%1]==[] goto test
set _TARGETS=bin,dev,generate,test,testacc,testrace,vet
REM Run target.
for %%a in (%_TARGETS%) do (if x%1==x%%a goto %%a)
goto usage
REM bin generates the releaseable binaries for Vault
:bin
call :generate
call .\scripts\windows\build.bat %CD%
goto :eof
REM dev creates binaries for testing Vault locally. These are put
REM into ./bin/ as well as %GOPATH%/bin
:dev
call :generate
call .\scripts\windows\build.bat %CD% VAULT_DEV
goto :eof
REM generate runs `go generate` to build the dynamically generated
REM source files.
:generate
go generate ./...
goto :eof
REM test runs the unit tests and vets the code.
:test
call :testsetup
godep go test %_TEST% %TESTARGS% -timeout=30s -parallel=4
call :setMaxExitCode %ERRORLEVEL%
echo.
goto vet
REM testacc runs acceptance tests.
:testacc
call :testsetup
if x%_TEST% == x./... goto testacc_fail
if x%_TEST% == x.\... goto testacc_fail
set TF_ACC=1
godep go test %_TEST% -v %TESTARGS% -timeout 45m
exit /b %ERRORLEVEL%
:testacc_fail
echo ERROR: Set %%TEST%% to a specific package.
exit /b 1
REM testrace runs the race checker.
:testrace
call :testsetup
godep go test -race %_TEST% %TESTARGS%
exit /b %ERRORLEVEL%
REM testsetup calls `go generate` and defines the variables TF_ACC
REM and _TEST. TF_ACC is always cleared. _TEST defaults to the value
REM of the TEST environment variable, provided that TEST is defined,
REM otherwise _TEST it is set to "./...".
:testsetup
call :generate
set TF_ACC=
set _TEST=./...
if defined TEST set _TEST=%TEST%
goto :eof
REM vet runs the Go source code static analysis tool `vet` to find
REM any common errors.
:vet
set _VETARGS=-asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr
if defined VETARGS set _VETARGS=%VETARGS%
go tool vet 2>nul
if %ERRORLEVEL% equ 3 go get golang.org/x/tools/cmd/vet
set _vetExitCode=0
set _VAULT_PKG_DIRS=%TEMP%\vault-pkg-dirs.txt
go list -f {{.Dir}} ./... >%_VAULT_PKG_DIRS%
REM Skip the first row, which is the main vault package (.*github.com/hashicorp/vault$)
for /f "delims= skip=1" %%d in (%_VAULT_PKG_DIRS%) do (
go tool vet %_VETARGS% %%d
if ERRORLEVEL 1 set _vetExitCode=1
call :setMaxExitCode %_vetExitCode%
)
del /f %_VAULT_PKG_DIRS% 2>NUL
if %_vetExitCode% equ 0 exit /b %_EXITCODE%
echo.
echo Vet found suspicious constructs. Please check the reported constructs
echo and fix them if necessary before submitting the code for reviewal.
exit /b %_EXITCODE%
:setMaxExitCode
if %1 gtr %_EXITCODE% set _EXITCODE=%1
goto :eof
:usage
echo usage: make [target]
echo.
echo target is in {%_TARGETS%}.
echo target defaults to test if none is provided.
exit /b 2
goto :eof

109
scripts/windows/build.bat Normal file
View file

@ -0,0 +1,109 @@
@echo off
setlocal
set _EXITCODE=0
set _DEV_BUILD=0
if not exist %1 exit /b 1
if x%2 == xVAULT_DEV set _DEV_BUILD=1
cd %1
md bin 2>nul
:: Get the git commit
set _GIT_COMMIT_FILE=%TEMP%\vault-git_commit.txt
set _GIT_DIRTY_FILE=%TEMP%\vault-git_dirty.txt
set _NUL_CMP_FILE=%TEMP%\vault-nul_cmp.txt
type nul >%_NUL_CMP_FILE%
git rev-parse HEAD >%_GIT_COMMIT_FILE%
set /p _GIT_COMMIT=<%_GIT_COMMIT_FILE%
del /f "%_GIT_COMMIT_FILE%" 2>nul
set _GIT_DIRTY=
git status --porcelain >%_GIT_DIRTY_FILE%
fc %_GIT_DIRTY_FILE% %_NUL_CMP_FILE% >nul
if errorlevel 1 set _GIT_DIRTY=+CHANGES
del /f "%_GIT_DIRTY_FILE%" 2>nul
del /f "%_NUL_CMP_FILE%" 2>nul
REM Determine the arch/os combos we're building for
set _XC_ARCH=386 amd64 arm
set _XC_OS=linux darwin windows freebsd openbsd
REM Install dependencies
echo ==^> Installing dependencies...
go get ./...
REM Clean up the old binaries and packages.
echo ==^> Cleaning old builds...
rd /s /q bin pkg 2>nul
md bin 2>nul
REM If its dev mode, only build for ourself
if not %_DEV_BUILD% equ 1 goto build
set _ORIGINAL_GOPATH=%GOPATH%
set _GODEP_PATH_FILE=%TEMP%\vault-godep-path.txt
godep path >%_GODEP_PATH_FILE%
set /p GOPATH=<%_GODEP_PATH_FILE%
del /f "%_GODEP_PATH_FILE%" 2>nul
set GOPATH=%GOPATH%;%_ORIGINAL_GOPATH%
:devbuild
echo ==^> Preparing for development build...
set _GO_ENV_TMP_FILE=%TEMP%\vault-go-env.txt
go env GOARCH >%_GO_ENV_TMP_FILE%
set /p _XC_ARCH=<%_GO_ENV_TMP_FILE%
del /f "%_GO_ENV_TMP_FILE%" 2>nul
go env GOOS >%_GO_ENV_TMP_FILE%
set /p _XC_OS=<%_GO_ENV_TMP_FILE%
del /f "%_GO_ENV_TMP_FILE%" 2>nul
:build
REM Build!
echo ==^> Building...
gox^
-os="%_XC_OS%"^
-arch="%_XC_ARCH%"^
-ldflags "-X github.com/hashicorp/vault/cli.GitCommit %_GIT_COMMIT%%_GIT_DIRTY%"^
-output "pkg/{{.OS}}_{{.Arch}}/vault"^
.
if %ERRORLEVEL% equ 1 set %_EXITCODE%=1
set GOPATH=%_ORIGINAL_GOPATH%
if %_EXITCODE% equ 1 exit /b %_EXITCODE%
set _GO_ENV_TMP_FILE=%TEMP%\vault-go-env.txt
go env GOPATH >%_GO_ENV_TMP_FILE%
set /p _GOPATH=<%_GO_ENV_TMP_FILE%
del /f "%_GO_ENV_TMP_FILE%" 2>nul
go env GOARCH >%_GO_ENV_TMP_FILE%
set /p _GOARCH=<%_GO_ENV_TMP_FILE%
del /f "%_GO_ENV_TMP_FILE%" 2>nul
go env GOOS >%_GO_ENV_TMP_FILE%
set /p _GOOS=<%_GO_ENV_TMP_FILE%
del /f "%_GO_ENV_TMP_FILE%" 2>nul
REM Copy our OS/Arch to the bin/ directory
set _DEV_PLATFORM=pkg\%_GOOS%_%_GOARCH%
for /r %%f in (%_DEV_PLATFORM%) do (
copy /b /y %%f bin\ >nul
copy /b /y %%f %_GOPATH%\bin\ >nul
)
REM TODO(ceh): package dist
REM Done!
echo.
echo ==^> Results:
echo.
for %%A in ("bin\*") do echo %%~fA
exit /b %_EXITCODE%