storj/internal/testcontext/compile.go
Jeff Wendling 098cbc9c67 all: use pkg/rpc instead of pkg/transport
all of the packages and tests work with both grpc and
drpc. we'll probably need to do some jenkins pipelines
to run the tests with drpc as well.

most of the changes are really due to a bit of cleanup
of the pkg/transport.Client api into an rpc.Dialer in
the spirit of a net.Dialer. now that we don't need
observers, we can pass around stateless configuration
to everything rather than stateful things that issue
observations. it also adds a DialAddressID for the
case where we don't have a pb.Node, but we do have an
address and want to assert some ID. this happened
pretty frequently, and now there's no more weird
contortions creating custom tls options, etc.

a lot of the other changes are being consistent/using
the abstractions in the rpc package to do rpc style
things like finding peer information, or checking
status codes.

Change-Id: Ief62875e21d80a21b3c56a5a37f45887679f9412
2019-09-25 15:37:06 -06:00

134 lines
2.8 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package testcontext
import (
"os/exec"
"path"
"path/filepath"
"runtime"
"testing"
)
// 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
}
// Compile compiles the specified package and returns the executable name.
func (ctx *Context) Compile(pkg string) string {
ctx.test.Helper()
exe := ctx.File("build", path.Base(pkg)+".exe")
args := []string{"build"}
if raceEnabled {
args = append(args, "-race")
}
if drpcEnabled {
args = append(args, "-tags=drpc")
}
args = append(args, "-o", exe, pkg)
cmd := exec.Command("go", args...)
ctx.test.Log("exec:", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
ctx.test.Error(string(out))
ctx.test.Fatal(err)
}
return exe
}
// CompileShared compiles pkg as c-shared.
// 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()
base := ctx.File("build", name)
args := []string{"build", "-buildmode", "c-shared"}
if drpcEnabled {
args = append(args, "-tags=drpc")
}
args = append(args, "-o", base+".so", pkg)
// not using race detector for c-shared
cmd := exec.Command("go", args...)
t.Log("exec:", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
t.Error(string(out))
t.Fatal(err)
}
t.Log(string(out))
return Include{Header: base + ".h", Library: base + ".so"}
}
// CompileC compiles file as with gcc and adds the includes.
func (ctx *Context) CompileC(t *testing.T, opts CompileCOptions) string {
t.Helper()
exe := ctx.File("build", opts.Dest+".exe")
var args = []string{}
if !opts.NoWarn {
args = append(args, "-Wall")
}
args = append(args, "-ggdb")
args = append(args, "-o", exe)
for _, inc := range opts.Includes {
if inc.Header != "" {
args = append(args, "-I", filepath.Dir(inc.Header))
}
if inc.Library != "" {
if inc.Standard {
args = append(args,
"-l"+inc.Library,
)
continue
}
if runtime.GOOS == "windows" {
args = append(args,
"-L"+filepath.Dir(inc.Library),
"-l:"+filepath.Base(inc.Library),
)
} else {
args = append(args, inc.Library)
}
}
}
args = append(args, opts.Sources...)
cmd := exec.Command("gcc", args...)
t.Log("exec:", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
t.Error(string(out))
t.Fatal(err)
}
t.Log(string(out))
return exe
}
// Include defines an includable library for gcc.
type Include struct {
Header string
Library string
Standard bool
}