2019-02-27 08:25:28 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package testcontext
|
|
|
|
|
|
|
|
import (
|
2019-10-28 16:54:55 +00:00
|
|
|
"os"
|
2019-02-27 08:25:28 +00:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
2019-06-13 16:09:05 +01:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-10-31 12:27:53 +00:00
|
|
|
"strconv"
|
2019-06-15 12:23:12 +01:00
|
|
|
"testing"
|
2019-10-31 12:27:53 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/version"
|
2019-02-27 08:25:28 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 12:40:05 +01:00
|
|
|
// CLibMath is the standard C math library (see `man math.h`).
|
|
|
|
var CLibMath = Include{Standard: true, Library: "m"}
|
|
|
|
|
|
|
|
// CompileCOptions stores options for compiling C source to an executable.
|
|
|
|
type CompileCOptions struct {
|
|
|
|
Dest string
|
|
|
|
Sources []string
|
|
|
|
Includes []Include
|
|
|
|
NoWarn bool
|
|
|
|
}
|
|
|
|
|
2019-02-27 08:25:28 +00:00
|
|
|
// Compile compiles the specified package and returns the executable name.
|
2019-10-31 12:27:53 +00:00
|
|
|
func (ctx *Context) Compile(pkg string, preArgs ...string) string {
|
2019-02-27 08:25:28 +00:00
|
|
|
ctx.test.Helper()
|
|
|
|
|
2019-10-28 16:54:55 +00:00
|
|
|
var binName string
|
|
|
|
if pkg == "" {
|
|
|
|
dir, _ := os.Getwd()
|
|
|
|
binName = path.Base(dir)
|
|
|
|
} else {
|
|
|
|
binName = path.Base(pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
exe := ctx.File("build", binName+".exe")
|
2019-02-27 08:25:28 +00:00
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
args := append([]string{"build"}, preArgs...)
|
2019-02-27 08:25:28 +00:00
|
|
|
if raceEnabled {
|
2019-09-19 05:46:39 +01:00
|
|
|
args = append(args, "-race")
|
2019-02-27 08:25:28 +00:00
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
if drpcEnabled {
|
|
|
|
args = append(args, "-tags=drpc")
|
|
|
|
}
|
|
|
|
args = append(args, "-o", exe, pkg)
|
|
|
|
|
|
|
|
cmd := exec.Command("go", args...)
|
2019-06-13 16:09:05 +01:00
|
|
|
ctx.test.Log("exec:", cmd.Args)
|
2019-02-27 08:25:28 +00:00
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
ctx.test.Error(string(out))
|
|
|
|
ctx.test.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return exe
|
|
|
|
}
|
2019-06-13 16:09:05 +01:00
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
// CompileWithVersion compiles the specified package with the version variables set
|
|
|
|
// to the passed version info values and returns the executable name.
|
|
|
|
func (ctx *Context) CompileWithVersion(pkg string, info version.Info) string {
|
|
|
|
ctx.test.Helper()
|
|
|
|
|
|
|
|
ldFlagsX := map[string]string{
|
|
|
|
"storj.io/storj/internal/version.buildTimestamp": strconv.Itoa(int(info.Timestamp.Unix())),
|
|
|
|
"storj.io/storj/internal/version.buildCommitHash": info.CommitHash,
|
|
|
|
"storj.io/storj/internal/version.buildVersion": info.Version.String(),
|
|
|
|
"storj.io/storj/internal/version.buildRelease": strconv.FormatBool(info.Release),
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.CompileWithLDFlagsX(pkg, ldFlagsX)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompileWithLDFlagsX compiles the specified package with the -ldflags flag set to
|
|
|
|
// "-s -w [-X <key>=<value>,...]" given the passed map and returns the executable name.
|
|
|
|
func (ctx *Context) CompileWithLDFlagsX(pkg string, ldFlagsX map[string]string) string {
|
|
|
|
ctx.test.Helper()
|
|
|
|
|
|
|
|
var ldFlags = "-s -w"
|
|
|
|
if ldFlagsX != nil {
|
|
|
|
for key, value := range ldFlagsX {
|
|
|
|
ldFlags += (" -X " + key + "=" + value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Compile(pkg, "-ldflags", ldFlags)
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:09:05 +01:00
|
|
|
// CompileShared compiles pkg as c-shared.
|
2019-06-15 12:23:12 +01:00
|
|
|
// TODO: support inclusion from other directories
|
|
|
|
// (cgo header paths are currently relative to package root)
|
|
|
|
func (ctx *Context) CompileShared(t *testing.T, name string, pkg string) Include {
|
|
|
|
t.Helper()
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
base := ctx.File("build", name)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
args := []string{"build", "-buildmode", "c-shared"}
|
|
|
|
if drpcEnabled {
|
|
|
|
args = append(args, "-tags=drpc")
|
|
|
|
}
|
|
|
|
args = append(args, "-o", base+".so", pkg)
|
|
|
|
|
2019-06-13 16:09:05 +01:00
|
|
|
// not using race detector for c-shared
|
2019-09-19 05:46:39 +01:00
|
|
|
cmd := exec.Command("go", args...)
|
2019-06-15 12:23:12 +01:00
|
|
|
t.Log("exec:", cmd.Args)
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-06-15 12:23:12 +01:00
|
|
|
t.Error(string(out))
|
|
|
|
t.Fatal(err)
|
2019-06-13 16:09:05 +01:00
|
|
|
}
|
2019-06-15 12:23:12 +01:00
|
|
|
t.Log(string(out))
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
return Include{Header: base + ".h", Library: base + ".so"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompileC compiles file as with gcc and adds the includes.
|
2019-07-30 12:40:05 +01:00
|
|
|
func (ctx *Context) CompileC(t *testing.T, opts CompileCOptions) string {
|
2019-06-15 12:23:12 +01:00
|
|
|
t.Helper()
|
2019-06-13 16:09:05 +01:00
|
|
|
|
2019-07-30 12:40:05 +01:00
|
|
|
exe := ctx.File("build", opts.Dest+".exe")
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
var args = []string{}
|
2019-07-30 12:40:05 +01:00
|
|
|
if !opts.NoWarn {
|
|
|
|
args = append(args, "-Wall")
|
|
|
|
}
|
|
|
|
args = append(args, "-ggdb")
|
2019-06-13 16:09:05 +01:00
|
|
|
args = append(args, "-o", exe)
|
2019-07-30 12:40:05 +01:00
|
|
|
for _, inc := range opts.Includes {
|
2019-06-13 16:09:05 +01:00
|
|
|
if inc.Header != "" {
|
|
|
|
args = append(args, "-I", filepath.Dir(inc.Header))
|
|
|
|
}
|
|
|
|
if inc.Library != "" {
|
2019-06-21 13:24:06 +01:00
|
|
|
if inc.Standard {
|
|
|
|
args = append(args,
|
|
|
|
"-l"+inc.Library,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
2019-06-13 16:09:05 +01:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
args = append(args,
|
|
|
|
"-L"+filepath.Dir(inc.Library),
|
|
|
|
"-l:"+filepath.Base(inc.Library),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
args = append(args, inc.Library)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 12:40:05 +01:00
|
|
|
args = append(args, opts.Sources...)
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
cmd := exec.Command("gcc", args...)
|
2019-06-15 12:23:12 +01:00
|
|
|
t.Log("exec:", cmd.Args)
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-06-15 12:23:12 +01:00
|
|
|
t.Error(string(out))
|
|
|
|
t.Fatal(err)
|
2019-06-13 16:09:05 +01:00
|
|
|
}
|
2019-06-15 12:23:12 +01:00
|
|
|
t.Log(string(out))
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
return exe
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include defines an includable library for gcc.
|
|
|
|
type Include struct {
|
2019-06-21 13:24:06 +01:00
|
|
|
Header string
|
|
|
|
Library string
|
|
|
|
Standard bool
|
2019-06-13 16:09:05 +01:00
|
|
|
}
|