Skip to content

Instantly share code, notes, and snippets.

@felixfbecker
Last active February 22, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixfbecker/cce58b198a209abae49780271ae854bd to your computer and use it in GitHub Desktop.
Save felixfbecker/cce58b198a209abae49780271ae854bd to your computer and use it in GitHub Desktop.
#!/usr/bin/env pwsh
$ErrorActionPreference = 'Stop'
$repositoryRoot = $PWD
# Builds the PCRE extension to sqlite3.
function Build-Libsqlite3Pcre {
[OutputType([string])]
param()
pkg-config --cflags sqlite3 libpcre
if ($LASTEXITCODE -ne 0) {
throw ("Missing sqlite dependencies.`n" + (if ($IsMacOS) {
"Install them by running ``brew install pkg-config sqlite pcre FiloSottile/musl-cross/musl-cross``"
} elseif ($IsLinux) {
"Install them by running ``apt-get install libpcre3-dev libsqlite3-dev pkg-config musl-tools``"
} else {
"See the local development documentation: https://github.com/sourcegraph/sourcegraph/blob/master/doc/dev/local_development.md#step-2-install-dependencies"
}))
}
$libsqlite3PcrePath = if ($IsMacOS) {
"$repositoryRoot/libsqlite3-pcre.dylib"
} elseif ($IsLinux) {
"$repositoryRoot/libsqlite3-pcre.so"
} else {
throw "Unsupported platform"
}
if (Test-Path $libsqlite3PcrePath) {
return
}
$sqlite3PcreRepositoryDirectory = (mktemp -d)
Remove-Item -Recurse -Force $sqlite3PcreRepositoryDirectory
Write-Information "Building $libsqlite3PcrePath..."
curl -fsSL 'https://codeload.github.com/ralight/sqlite3-pcre/tar.gz/c98da412b431edb4db22d3245c99e6c198d49f7a' | tar -C $sqlite3PcreRepositoryDirectory -xvf - --strip 1
Push-Location $sqlite3PcreRepositoryDirectory
$libsqlite3PcrePath = if ($IsMacOS) {
"$repositoryRoot/libsqlite3-pcre.dylib"
} elseif ($IsLinux) {
"$repositoryRoot/libsqlite3-pcre.so"
} else {
throw "Unsupported platform"
}
Pop-Location
Write-Information "Building $libsqlite3PcrePath... done"
return $libsqlite3PcrePath
}
# Builds the symbols executable.
function Build-Executable {
param(
[Parameter(Mandatory)]
[string] $OutputPath,
[ValidateSet('dev', 'dist')]
[string] $BuildType = 'dev'
)
$symbolsPackage = "github.com/sourcegraph/sourcegraph/cmd/symbols"
switch ($BuildType) {
'dev' {
$gcFlags = "all=-N -l"
$tags = "dev delve"
}
'dist' {
$gcFlags = ""
$tags = "dist"
}
}
Write-Information "Building the $OutputPath executable..."
go build -buildmode exe -gcflags $gcFlags -tags $tags -o $OutputPath $symbolsPackage
Write-Information "Building the $OutputPath executable... done"
}
# Builds and runs the symbols executable.
function Invoke-Executable {
param(
[ValidateSet('dev', 'dist')]
$BuildType = 'dev'
)
$symbolsExecutablePath = "$repositoryRoot/.bin/symbols"
$libsqlite3PcrePath = Build-Libsqlite3Pcre
Build-Executable -OutputPath $symbolsExecutablePath -BuildType $BuildType
Build-CtagsDockerImage
$env:LIBSQLITE3_PCRE = $libsqlite3PcrePath
if (-not $env:CTAGS_COMMAND) {
$env:CTAGS_COMMAND = 'cmd/symbols/universal-ctags-dev'
}
if (-not $env:CTAGS_PROCESSES) {
$env:CTAGS_PROCESSES = 1
}
&$symbolsExecutablePath
}
# Builds the symbols Docker image.
function Build-SymbolsDockerImage {
param(
$DockerImageName = "dev-symbols",
[ValidateSet('dev', 'dist')]
$BuildType = 'dev'
)
if ($IsMacOS) {
$muslGcc = "x86_64-linux-musl-gcc"
if (-not (Get-Command $muslGcc)) {
throw "Couldn't find musl C compiler $muslGcc. Run `brew install FiloSottile/musl-cross/musl-cross`."
}
} elseif ($IsLinux) {
$muslGcc = "musl-gcc"
if (-not (Get-Command $muslGcc)) {
throw "Couldn't find musl C compiler $muslGcc. Install the musl-tools package (e.g. on Ubuntu, run `apt-get install musl-tools`)."
}
} else {
throw "Unknown platform $OSTYPE"
}
$symbolsDockerBuildContext = (mktemp -d)
Remove-Item -Recurse -Force $symbolsDockerBuildContext
$env:CC = $muslGcc
$env:GO111MODULE = 'on'
$env:GOARCH = 'amd64'
$env:GOOS = 'linux'
$env:CGO_ENABLED = 1 # to build the sqlite3 library
Build-Executable -SymbolsExecutablePath "$symbolsDockerBuildContext/symbols"
Build-CtagsDockerImage
Copy-Item -Recurse cmd/symbols/.ctags.d $symbolsDockerBuildContext
Write-Information "Building the $SymbolsImage Docker image..."
docker build --quiet -f cmd/symbols/Dockerfile -t $SymbolsImage $symbolsDockerBuildContext
Write-Information "Building the $SymbolsImage Docker image... done"
}
# Builds the ctags docker image, used by universal-ctags-dev and the symbols Docker image.
function Build-CtagsDockerImage($DockerImageName = 'ctags') {
$ctagsDockerBuildContext = (mktemp -d)
Remove-Item -Recurse -Force $ctagsDockerBuildContext
Copy-Item -Recurse cmd/symbols/.ctags.d $ctagsDockerBuildContext
Write-Information "Building the $DockerImageName Docker image..."
docker build --quiet -f cmd/symbols/internal/pkg/ctags/Dockerfile -t $DockerImageName $ctagsDockerBuildContext
Write-Information "Building the $DockerImageName Docker image... done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment