Skip to content

Commit

Permalink
cmd/llgo-build: Add -test flag creating a test binary. For go-llvm#48.
Browse files Browse the repository at this point in the history
This is able to create a binary for some packages, although I'm
having issues actually running them. Will comment in go-llvm#48 with more
details.

This PR depends on having merged the others first.
  • Loading branch information
quarnster committed Oct 4, 2013
1 parent a41d38a commit ab4ba80
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
23 changes: 22 additions & 1 deletion cmd/llgo-build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ func getPackage(pkgpath string) (pkg *build.Package, err error) {
}
pkg.GoFiles[i] = path.Join(pkgdir, filename)
}
for i, filename := range pkg.TestGoFiles {
pkgdir := pkg.Dir
if overlayentries[filename] {
pkgdir = overlaypkg.Dir
}
pkg.TestGoFiles[i] = path.Join(pkgdir, filename)
}
for i, filename := range pkg.XTestGoFiles {
pkgdir := pkg.Dir
if overlayentries[filename] {
pkgdir = overlaypkg.Dir
}
pkg.XTestGoFiles[i] = path.Join(pkgdir, filename)
}
for i, filename := range pkg.CFiles {
pkgdir := pkg.Dir
if overlayentries[filename] {
Expand Down Expand Up @@ -191,7 +205,7 @@ func buildPackages(pkgpaths []string) error {
func buildPackage(pkg *build.Package, output string) error {
args := []string{"-c", "-triple", triple}
dir, file := path.Split(pkg.ImportPath)
if pkg.IsCommand() {
if pkg.IsCommand() || test {
if output == "" {
output = file
}
Expand All @@ -211,6 +225,9 @@ func buildPackage(pkg *build.Package, output string) error {
tempfile := path.Join(workdir, file+".bc")
args = append(args, "-o", tempfile)
args = append(args, pkg.GoFiles...)
if test {
args = append(args, pkg.TestGoFiles...)
}
cmd := exec.Command("llgo", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -271,6 +288,10 @@ func buildPackage(pkg *build.Package, output string) error {
if err != nil {
return err
}
} else if test {
if err = linktest(pkg, tempfile); err != nil {
return err
}
}
return moveFile(tempfile, output)
}
21 changes: 16 additions & 5 deletions cmd/llgo-build/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ func linkdeps(pkg *build.Package, output string) error {
deps["runtime"] = true
deps["unsafe"] = true

var mkdeps func(pkg *build.Package) error
mkdeps = func(pkg *build.Package) error {
for _, path := range pkg.Imports {
var mkdeps func(pkg *build.Package, imports []string) error
mkdeps = func(pkg *build.Package, imports []string) error {
for _, path := range imports {
if !deps[path] {
deps[path] = true
pkg, err := build.Import(path, "", 0)
if err != nil {
return err
}
if err = mkdeps(pkg); err != nil {
if err = mkdeps(pkg, pkg.Imports); err != nil {
return err
}
depslist = append(depslist, path)
Expand All @@ -37,10 +37,18 @@ func linkdeps(pkg *build.Package, output string) error {
return nil
}

err := mkdeps(pkg)
err := mkdeps(pkg, pkg.Imports)
if err != nil {
return err
}
if test {
if err = mkdeps(pkg, pkg.TestImports); err != nil {
return err
}
if err = mkdeps(pkg, pkg.XTestImports); err != nil {
return err
}
}

for i := 0; i < len(depslist)/2; i++ {
j := len(depslist) - i - 1
Expand All @@ -50,6 +58,9 @@ func linkdeps(pkg *build.Package, output string) error {
llvmlink := filepath.Join(llvmbindir, "llvm-link")
args := []string{"-o", output, output}
for _, path := range depslist {
if path == pkg.ImportPath {
continue
}
bcfile := filepath.Join(pkgroot, path+".bc")
if buildDeps {
if _, err := os.Stat(bcfile); err != nil {
Expand Down
14 changes: 13 additions & 1 deletion cmd/llgo-build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"flag"
"fmt"
llgobuild "github.com/axw/llgo/build"
"go/build"
"io/ioutil"
Expand All @@ -28,6 +29,7 @@ var (
emitllvm bool
buildctx *build.Context
workdir string
test bool
buildDeps bool = true
)

Expand All @@ -37,6 +39,7 @@ func init() {
flag.StringVar(&output, "o", "", "Output file")
flag.BoolVar(&emitllvm, "emit-llvm", false, "Emit LLVM bitcode instead of a native binary")
flag.BoolVar(&printcommands, "x", false, "Print the commands")
flag.BoolVar(&test, "test", test, "When specified, the created output binary will be similar to what's output of \"go test -c\"")
flag.BoolVar(&buildDeps, "build-deps", buildDeps, "Whether to also build dependency packages or not")
}

Expand Down Expand Up @@ -71,7 +74,16 @@ func main() {
if err != nil {
log.Fatal(err)
}
err = buildPackages(flag.Args())
args := flag.Args()
if test {
if len(args) > 1 {
err = fmt.Errorf("Multiple files/packages can not be specified when building a test binary")
} else {
err = buildPackageTests(args[0])

This comment has been minimized.

Copy link
@axw

axw Oct 4, 2013

I don't see the definition of buildPackageTests. Did you miss a file, or refactor something?

}
} else {
err = buildPackages(flag.Args())

This comment has been minimized.

Copy link
@axw

axw Oct 4, 2013

May as well replace flag.Args() with args here.

}
os.RemoveAll(workdir)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit ab4ba80

Please sign in to comment.