This commit is contained in:
parent
adfcd4b60c
commit
9b51680af5
8 changed files with 202 additions and 0 deletions
69
src/global/global.go
Normal file
69
src/global/global.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package global
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
// Global variables that are useful in all packages.
|
||||
var (
|
||||
Executable string
|
||||
Library string
|
||||
Root string
|
||||
WorkingDirectory string
|
||||
)
|
||||
|
||||
// init is the very first thing that's executed.
|
||||
// It disables the GC and initializes global variables.
|
||||
func init() {
|
||||
debug.SetGCPercent(-1)
|
||||
|
||||
var err error
|
||||
Executable, err = os.Executable()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Executable, err = filepath.EvalSymlinks(Executable)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
WorkingDirectory, err = os.Getwd()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Root = filepath.Dir(Executable)
|
||||
Library = filepath.Join(Root, "lib")
|
||||
stat, err := os.Stat(Library)
|
||||
|
||||
if err != nil || !stat.IsDir() {
|
||||
findLibrary()
|
||||
}
|
||||
}
|
||||
|
||||
// findLibrary tries to go up each directory from the working directory and check for the existence of a "lib" directory.
|
||||
// This is needed for tests to work correctly.
|
||||
func findLibrary() {
|
||||
dir := WorkingDirectory
|
||||
|
||||
for {
|
||||
Library = filepath.Join(dir, "lib")
|
||||
stat, err := os.Stat(Library)
|
||||
|
||||
if err == nil && stat.IsDir() {
|
||||
return
|
||||
}
|
||||
|
||||
if dir == "/" {
|
||||
panic("standard library not found")
|
||||
}
|
||||
|
||||
dir = filepath.Dir(dir)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue