Allow microfactory to be used as a package

In addition to running with `go run` and creating a microfactory binary,
allow microfactory to be used as a package from other go tools as well.

To allow other packages to use this, it needs to be in a non-main
package, but `go run` requires a main package. So microfactory.bash runs
a sed script before running microfactory with `go run`.

This could also be solved by using a relative import, but neither
blueprint nor microfactory currently support that.

Change-Id: I084163b14720102b3fb93a3c9d44b5d0225ff2c8
This commit is contained in:
Dan Willemsen 2017-08-08 13:14:40 -07:00
parent f00c03da53
commit ff092863b3
5 changed files with 42 additions and 5 deletions

View file

@ -176,6 +176,13 @@ bootstrap_go_binary{
blueprint_go_binary { blueprint_go_binary {
name: "microfactory", name: "microfactory",
deps: ["blueprint-microfactory"],
srcs: ["microfactory/main/main.go"],
}
bootstrap_go_package {
name: "blueprint-microfactory",
pkgPath: "github.com/google/blueprint/microfactory",
srcs: ["microfactory/microfactory.go"], srcs: ["microfactory/microfactory.go"],
testSrcs: ["microfactory/microfactory_test.go"], testSrcs: ["microfactory/microfactory_test.go"],
} }

23
microfactory/main/main.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"github.com/google/blueprint/microfactory"
)
func main() {
microfactory.Main()
}

View file

@ -47,7 +47,12 @@ function build_go
local mf_cmd local mf_cmd
if [ $from_src -eq 1 ]; then if [ $from_src -eq 1 ]; then
mf_cmd="${GOROOT}/bin/go run ${mf_src}/microfactory.go" # `go run` requires a single main package, so create one
local gen_src_dir="${BUILDDIR}/.microfactory_$(uname)_intermediates/src"
mkdir -p "${gen_src_dir}"
sed "s/^package microfactory/package main/" "${mf_src}/microfactory.go" >"${gen_src_dir}/microfactory.go"
mf_cmd="${GOROOT}/bin/go run ${gen_src_dir}/microfactory.go"
else else
mf_cmd="${mf_bin}" mf_cmd="${mf_bin}"
fi fi

View file

@ -39,7 +39,7 @@
// Combined with a shell script like microfactory.bash that uses `go run` to // Combined with a shell script like microfactory.bash that uses `go run` to
// run Microfactory for the first time, go programs can be quickly bootstrapped // run Microfactory for the first time, go programs can be quickly bootstrapped
// entirely from source (and a standard go distribution). // entirely from source (and a standard go distribution).
package main package microfactory
import ( import (
"bytes" "bytes"
@ -430,7 +430,7 @@ func (p *GoPackage) Link(out string) error {
// and if does, it will launch a new copy and return true. Otherwise it will return // and if does, it will launch a new copy and return true. Otherwise it will return
// false to continue executing. // false to continue executing.
func rebuildMicrofactory(mybin string, pkgMap *pkgPathMapping) bool { func rebuildMicrofactory(mybin string, pkgMap *pkgPathMapping) bool {
mysrc, ok, err := pkgMap.Path("github.com/google/blueprint/microfactory") mysrc, ok, err := pkgMap.Path("github.com/google/blueprint/microfactory/main")
if err != nil { if err != nil {
fmt.Println(os.Stderr, "Error finding microfactory source:", err) fmt.Println(os.Stderr, "Error finding microfactory source:", err)
os.Exit(1) os.Exit(1)
@ -501,7 +501,9 @@ func un(f func()) {
f() f()
} }
func main() { // microfactory.bash will make a copy of this file renamed into the main package for use with `go run`
func main() { Main() }
func Main() {
var output, mybin, trimPath string var output, mybin, trimPath string
var pkgMap pkgPathMapping var pkgMap pkgPathMapping

View file

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package main package microfactory
import ( import (
"flag" "flag"