Browse Source

BUILD: uniform style, change go build step

BUILD: Make pass shellcheck. Mostly just some quotes and exporting.

util: shuffle go files into a GOPATH-style tree.
Makes for a single-command go build step, and uses incremental builds.
Offsets the go1.5 build slowdowns a teeny tiny bit. Also hit everyting
with a gofmt stick.

harvey/cmd/build: make the build tool repect CC/AR/LD etc. instead of
unconditionally calling one binary name.

Change-Id: Icf57a2c4c0158ba1af8eafcdcd9c20f72a97dd40
Signed-off-by: Hank Donnay <hdonnay@gmail.com>
Hank Donnay 8 years ago
parent
commit
3d332d9b34

+ 51 - 60
BUILD

@@ -21,22 +21,23 @@
 #
 
 #Search for config:
-_BUILD_DIR=`dirname $0`
+_BUILD_DIR="$(dirname "$0")"
 export _BUILD_DIR=$(cd "$_BUILD_DIR"; pwd)
-. ${_BUILD_DIR}/BUILD.conf
+. "${_BUILD_DIR}/BUILD.conf"
+export HARVEY="$_BUILD_DIR"
 
 compile_kernel()
 {
-	export HARVEY="$_BUILD_DIR"
 	cd "$KRL_DIR"
-	$HARVEY/util/build $KERNEL_CONF.json
+	build "$KERNEL_CONF.json"
 	cd "$PATH_ORI" > /dev/null
 }
 
 build_kernel()
 {
 	compile_kernel
-	if [ $? -ne 0 ]; then
+	if [ $? -ne 0 ]
+	then
 		echo "SOMETHING WENT WRONG!"
 	else
 		echo "KERNEL COMPILED OK"
@@ -45,22 +46,14 @@ build_kernel()
 
 
 build_go_utils()
-{
-	cd "${UTIL_DIR}"
-	GOPATH="$UTIL_DIR/third_party" go get
-	GOPATH="$UTIL_DIR/third_party" go install github.com/rminnich/go9p/ufs
-	cp $UTIL_DIR/third_party/bin/* $UTIL_DIR/
-	for i in `ls *.go`
-	do
-		GOPATH="$UTIL_DIR/third_party" go build $i
-		if [ $? -ne 0 ]
-		then
-			printf "\nERROR compiling $i \n"
-			exit 1
-		fi
-	done
-	cd - > /dev/null
-}
+{ (
+	# this is run in a subshell, so environment modifications are local only
+	set -e
+	export GOBIN="${UTIL_DIR}"
+	export GOPATH="${UTIL_DIR}/third_party:${UTIL_DIR}"
+	go get -d all # should really vendor these bits
+	go install github.com/rminnich/go9p/ufs harvey/cmd/...
+) }
 
 check_utils()
 {
@@ -74,66 +67,66 @@ check_utils()
 
 build_libs()
 {
-	export HARVEY="$_BUILD_DIR"
-	rm -rf $HARVEY/amd64/lib/*
+	rm -rf "$HARVEY/amd64/lib/"*
 	cd "$SRC_DIR"
-	$HARVEY/util/build libs.json
+	build libs.json
 	cd "$PATH_ORI" > /dev/null
 }
 
 
 build_klibs()
 {
-	export HARVEY="$_BUILD_DIR"
 	cd "$SRC_DIR"
-	$HARVEY/util/build klibs.json
+	build klibs.json
 	cd "$PATH_ORI" > /dev/null
 
 }
 
 build_cmds()
 {
-	export HARVEY="$_BUILD_DIR"
-	rm -rf $HARVEY/amd64/bin/*
+	rm -rf "$HARVEY/amd64/bin/"*
 	cd "$CMD_DIR"
-	$HARVEY/util/build cmds.json
-	$HARVEY/util/build kcmds.json
+	build cmds.json
+	build kcmds.json
 	cd "$PATH_ORI" > /dev/null
 }
 
 build_regress()
 {
-	export HARVEY="$_BUILD_DIR"
-	rm -rf $HARVEY/amd64/bin/regress
+	rm -rf "$HARVEY/amd64/bin/regress"
 	cd "$SRC_DIR"/regress
-	$HARVEY/util/build regress.json
+	build regress.json
 	cd "$PATH_ORI" > /dev/null
 }
 
 show_help()
 {
-	printf "\n\nBUILD script for Harvey\n\n"
-	printf "OPTIONS:\n"
-	printf "  all        \tBuild all components\n"
-	printf "  cleanall     \tClean all components\n"
-	printf "  libs       \tBuild the libraries\n"
-	printf "  libs <libname>\tBuild the library <libname>\n"
-	printf "  cleanlibs\tClean the libraries\n"
-	printf "  klibs       \tBuild the Klibraries\n"
-	printf "  klibs <libname>\tBuild the Klibrary <libname>\n"
-	printf "  cleanklibs\tClean the Klibraries\n"
-	printf "  utils     \tBuild go utils\n"
-	printf "  cmd       \tBuild all cmds \n"
-	printf "  cmd <cmdname>\tBuild cmd named <cmdname>\n"
-	printf "  regress	Build all regression tests\n"
-	printf "  cleancmd   \tClean the cmds\n"
-	printf "  kernel     \tBuild kernel\n"
-	printf "  cleankernel\tClean kernel\n"
-	printf "\nFLAGS:\n"
-	printf "  -g        \tCompile with debugs flags\n"
-	printf "  -t <test> \tCompile <test> app and package it with the kernel"
-	printf "\n"
-
+cat <<end
+
+BUILD script for Harvey
+
+OPTIONS:
+  all                Build all components
+  cleanall           Clean all components
+  libs               Build the libraries
+  libs <libname>     Build the library <libname>
+  cleanlibs          Clean the libraries
+  klibs              Build the Klibraries
+  klibs <libname>    Build the Klibrary <libname>
+  cleanklibs         Clean the Klibraries
+  utils              Build go utils
+  cmd                Build all cmds
+  cmd <cmdname>      Build cmd named <cmdname>
+  regress            Build all regression tests
+  cleancmd           Clean the cmds
+  kernel             Build kernel
+  cleankernel        Clean kernel
+
+FLAGS:
+  -g                 Compile with debugs flags
+  -t <test>          Compile <test> app and package it with the kernel
+
+end
 }
 
 ### MAIN SCRIPT ####
@@ -143,17 +136,17 @@ then
 	exit 1
 else
 	# We need our binary dir
-	mkdir -p $BIN_DIR
+	mkdir -p "$BIN_DIR"
 
 	#BUILD_DEBUG=
 	#Until we have a stable kernel, debug mode is the default.
-	BUILD_DEBUG="$CFLAGS_DEBUG"
+	export BUILD_DEBUG="$CFLAGS_DEBUG"
 	TEST_APP=
 	while [ -n "$1" ]
 	do
 		case "$1" in
 			"-g")
-					BUILD_DEBUG="$CFLAGS_DEBUG"
+					export BUILD_DEBUG="$CFLAGS_DEBUG"
 					;;
 			"-t"*)
 					#is -tSomething?
@@ -211,5 +204,3 @@ else
 		shift
 	done
 fi
-
-

+ 1 - 1
BUILD.conf

@@ -33,4 +33,4 @@ EXTKERNDATE="-DKERNDATE=${DATE}"
 KERNEL_CONF="k8cpu"
 
 ### PATH ###
-PATH=$PATH:$UTIL_DIR
+PATH="$UTIL_DIR:$PATH"

+ 5 - 0
util/.gitignore

@@ -3,3 +3,8 @@
 /kpdump
 /removemach
 /mksys
+/build
+/jsonpretty
+/op2
+/telnet
+/ufs

+ 50 - 12
util/build.go → util/src/harvey/cmd/build/build.go

@@ -1,7 +1,18 @@
 // Build builds code as directed by json files.
 // We slurp in the JSON, and recursively process includes.
-// At the end, we issue a single gcc command for all the files.
+// At the end, we issue a single cc command for all the files.
 // Compilers are fast.
+//
+// ENVIRONMENT
+//
+// Needed: HARVEY, ARCH
+//
+// Currently only "amd64" is a valid ARCH. HARVEY should point to a harvey root.
+//
+// Optional: CC, AR, LD, RANLIB, STRIP, TOOLPREFIX
+//
+// These all control how the needed tools are found.
+//
 package main
 
 import (
@@ -15,6 +26,7 @@ import (
 	"os/exec"
 	"path"
 	"path/filepath"
+	"strings"
 	"text/template"
 )
 
@@ -60,9 +72,18 @@ type build struct {
 }
 
 var (
-	cwd        string
-	harvey     string
-	toolprefix string
+	cwd    string
+	harvey string
+
+	// findTools looks at all env vars and absolutizes these paths
+	// also respects TOOLPREFIX
+	tools = map[string]string{
+		"cc":     "gcc",
+		"ar":     "ar",
+		"ld":     "ld",
+		"ranlib": "ranlib",
+		"strip":  "strip",
+	}
 )
 
 func fail(err error) {
@@ -150,7 +171,7 @@ func compile(b *build) {
 	if len(b.SourceFilesCmd) > 0 {
 		for _, i := range b.SourceFilesCmd {
 			argscmd := append(args, []string{i}...)
-			cmd := exec.Command(toolprefix+"gcc", argscmd...)
+			cmd := exec.Command(tools["cc"], argscmd...)
 			cmd.Env = append(os.Environ(), b.Env...)
 			cmd.Stdin = os.Stdin
 			cmd.Stderr = os.Stderr
@@ -164,7 +185,7 @@ func compile(b *build) {
 		}
 	} else {
 		args = append(args, b.SourceFiles...)
-		cmd := exec.Command(toolprefix+"gcc", args...)
+		cmd := exec.Command(tools["cc"], args...)
 		cmd.Env = append(os.Environ(), b.Env...)
 
 		cmd.Stdin = os.Stdin
@@ -195,7 +216,7 @@ func link(b *build) {
 			args = append(args, b.Oflags...)
 			args = append(args, adjust([]string{"-L", "/amd64/lib"})...)
 			args = append(args, b.Libs...)
-			cmd := exec.Command(toolprefix+"ld", args...)
+			cmd := exec.Command(tools["ld"], args...)
 			cmd.Env = append(os.Environ(), b.Env...)
 
 			cmd.Stdin = os.Stdin
@@ -213,7 +234,7 @@ func link(b *build) {
 		args = append(args, b.Oflags...)
 		args = append(args, adjust([]string{"-L", "/amd64/lib"})...)
 		args = append(args, b.Libs...)
-		cmd := exec.Command(toolprefix+"ld", args...)
+		cmd := exec.Command(tools["ld"], args...)
 		cmd.Env = append(os.Environ(), b.Env...)
 
 		cmd.Stdin = os.Stdin
@@ -292,7 +313,7 @@ func install(b *build) {
 			n = n + ".o"
 			args = append(args, n)
 		}
-		cmd := exec.Command(toolprefix+"ar", args...)
+		cmd := exec.Command(tools["ar"], args...)
 
 		cmd.Stdin = os.Stdin
 		cmd.Stderr = os.Stderr
@@ -304,7 +325,7 @@ func install(b *build) {
 			log.Fatalf("%v\n", err)
 		}
 
-		cmd = exec.Command(toolprefix+"ranlib", libpath)
+		cmd = exec.Command(tools["ranlib"], libpath)
 		err = cmd.Run()
 		if err != nil {
 			log.Fatalf("%v\n", err)
@@ -351,7 +372,7 @@ func data2c(name string, path string) (string, error) {
 			log.Fatalf("%v\n", err)
 		}
 		args := []string{"-o", tmpf.Name(), path}
-		cmd := exec.Command(toolprefix+"strip", args...)
+		cmd := exec.Command(tools["strip"], args...)
 		cmd.Env = nil
 		cmd.Stdin = os.Stdin
 		cmd.Stderr = os.Stderr
@@ -571,7 +592,9 @@ func main() {
 	cwd, err = os.Getwd()
 	fail(err)
 	harvey = os.Getenv("HARVEY")
-	toolprefix = os.Getenv("TOOLPREFIX")
+	if err := findTools(os.Getenv("TOOLPREFIX")); err != nil {
+		fail(err)
+	}
 	if harvey == "" {
 		log.Printf("You need to set the HARVEY environment variable")
 		badsetup = true
@@ -589,3 +612,18 @@ func main() {
 	fail(err)
 	project(file)
 }
+
+func findTools(toolprefix string) (err error) {
+	for k, v := range tools {
+		if x := os.Getenv(strings.ToUpper(k)); x != "" {
+			v = x
+		}
+		v = toolprefix + v
+		v, err = exec.LookPath(v)
+		if err != nil {
+			return err
+		}
+		tools[k] = v
+	}
+	return nil
+}

+ 0 - 0
util/data2c.go → util/src/harvey/cmd/data2c/data2c.go


+ 0 - 0
util/elf2c.go → util/src/harvey/cmd/elf2c/elf2c.go


+ 0 - 0
util/jsonpretty.go → util/src/harvey/cmd/jsonpretty/jsonpretty.go


+ 6 - 6
util/kpdump.go → util/src/harvey/cmd/kpdump/kpdump.go

@@ -6,8 +6,8 @@ import (
 	"flag"
 	"fmt"
 	"io"
-	"os"
 	"math"
+	"os"
 )
 
 const LRES = 3
@@ -46,7 +46,7 @@ func main() {
 		// array and ONE data array. So it's a matter of creating
 		// a virtual memory space with an assumed starting point of
 		// 0x200000, and filling it. We just grow that as needed.
-		
+
 		curstart := v.Vaddr
 		curend := v.Vaddr + v.Memsz
 		// magic numbers, BAH!
@@ -72,9 +72,9 @@ func main() {
 		os.Exit(1)
 	}
 	// maybe we should stop doing LRES ...
-	symname := make([]string, codeend - codestart)
+	symname := make([]string, codeend-codestart)
 	for i := range symname {
-		symname[i] = fmt.Sprintf("[0x%x]", codestart + uint64(i))
+		symname[i] = fmt.Sprintf("[0x%x]", codestart+uint64(i))
 	}
 	for _, v := range s {
 		vstart := v.Value
@@ -82,7 +82,7 @@ func main() {
 		if v.Value > codeend {
 			continue
 		}
-		if v.Value + v.Size < codestart {
+		if v.Value+v.Size < codestart {
 			continue
 		}
 		if vstart < codestart {
@@ -90,7 +90,7 @@ func main() {
 		}
 		if vend > codeend {
 			vend = codeend
-		} 
+		}
 		for i := vstart; i < vend; i++ {
 			symname[i-codestart] = v.Name
 		}

+ 12 - 13
util/mksys.go → util/src/harvey/cmd/mksys/mksys.go

@@ -29,9 +29,9 @@ type Syscall struct {
 	Define  string
 	Sysname string
 	Libname string
-	Fudge   string `json:"-"`
+	Fudge   string   `json:"-"`
 	GoArgs  []string `json:"-"`
-	Ret0    string `json:"-"`
+	Ret0    string   `json:"-"`
 }
 
 type Syserror struct {
@@ -102,7 +102,7 @@ func main() {
 		if os.Getenv("ARCH") != "amd64" {
 			usage("ARCH unsupported or not set")
 		}
-		syscallargs := []string{ "DI", "SI", "DX", "R10", "R8", "R9" };
+		syscallargs := []string{"DI", "SI", "DX", "R10", "R8", "R9"}
 		//funcallregs := []string{ "DI", "SI", "DX", "CX", "R8", "R9" };
 		for i := range syscalls {
 			goargs := []string{}
@@ -122,15 +122,15 @@ func main() {
 			}
 			syscalls[i].GoArgs = goargs
 			switch syscalls[i].Ret[0] {
-				case "int32_t", "uint32_t":
-					syscalls[i].Ret0 = fmt.Sprintf("MOVL	AX, ret+%d(FP)", fpoff)
-					fpoff += 4
-				case "void*", "char*", "char**", "uint8_t*", "int32_t*", "uint64_t*", "int64_t*", "int64_t":
-					fpoff = (fpoff + 7) & ^7
-					syscalls[i].Ret0 = fmt.Sprintf("MOVQ	AX, ret+%d(FP)", fpoff)
-					fpoff += 8
-				default:
-					log.Fatalf("unsupported Ret[0] in syscall: %v", syscalls[i])
+			case "int32_t", "uint32_t":
+				syscalls[i].Ret0 = fmt.Sprintf("MOVL	AX, ret+%d(FP)", fpoff)
+				fpoff += 4
+			case "void*", "char*", "char**", "uint8_t*", "int32_t*", "uint64_t*", "int64_t*", "int64_t":
+				fpoff = (fpoff + 7) & ^7
+				syscalls[i].Ret0 = fmt.Sprintf("MOVQ	AX, ret+%d(FP)", fpoff)
+				fpoff += 8
+			default:
+				log.Fatalf("unsupported Ret[0] in syscall: %v", syscalls[i])
 			}
 		}
 		tmpl, err := template.New("sys_harvey.s").Parse(`/* automatically generated by mksys */
@@ -156,7 +156,6 @@ TEXT runtime·{{ .Libname }}(SB),NOSPLIT,$0
 			log.Fatal(err)
 		}
 
-
 	case "syscallfiles":
 		if os.Getenv("ARCH") != "amd64" {
 			usage("ARCH unsupported or not set")

+ 18 - 18
util/op2.go → util/src/harvey/cmd/op2/op2.go

@@ -9,23 +9,23 @@ import (
 
 /*
 first word
-high 8 bits is ee, which is an invalid address on amd64. 
+high 8 bits is ee, which is an invalid address on amd64.
 next 8 bits is protocol version
-next 16 bits is unused, MBZ. Later, we can make it a packet type. 
+next 16 bits is unused, MBZ. Later, we can make it a packet type.
 next 16 bits is core id
 next 8 bits is unused
 next 8 bits is # words following.
 
 second word is time in ns. (soon to be tsc ticks)
 
-Third and following words are PCs, there must be at least one of them. 
- */
+Third and following words are PCs, there must be at least one of them.
+*/
 type sample struct {
-	Wordcount,_ uint8
-	Coreid uint16
-	_ uint16
+	Wordcount, _    uint8
+	Coreid          uint16
+	_               uint16
 	Version, Marker uint8
-	Ns uint64
+	Ns              uint64
 }
 
 type backtrace struct {
@@ -39,7 +39,7 @@ type hdr struct {
 
 type record struct {
 	Count, Size uint64
-	Pcs []uint64
+	Pcs         []uint64
 }
 
 type trailer struct {
@@ -48,18 +48,18 @@ type trailer struct {
 
 func main() {
 	var s sample
-	
+
 	r := io.Reader(os.Stdin)
 	w := io.Writer(os.Stdout)
 
 	records := make(map[string]uint64, 16384)
-	backtraces := make(map[string][]uint64,1024)
+	backtraces := make(map[string][]uint64, 1024)
 
 	/* ignore the documentation, it's wrong, first word must be zero.
 	 * the perl code that figures word length depends on it.
 	 */
-	hdr := hdr{0,3,0,10000,0}
-	trailer := trailer{0,1,0}
+	hdr := hdr{0, 3, 0, 10000, 0}
+	trailer := trailer{0, 1, 0}
 	start := uint64(0)
 	end := start
 	nsamples := end
@@ -73,9 +73,9 @@ func main() {
 		 * N.B. The fact that we have to mess with the bt values
 		 * is the reason we did not write a stringer for bt.
 		 */
-		for i := range(bt) {
-			bt[i] = bt[i] & ((uint64(1)<<32)-1)
-			record = record + fmt.Sprintf("0x%x ",bt[i])
+		for i := range bt {
+			bt[i] = bt[i] & ((uint64(1) << 32) - 1)
+			record = record + fmt.Sprintf("0x%x ", bt[i])
 		}
 		records[record]++
 		backtraces[record] = bt
@@ -85,7 +85,7 @@ func main() {
 			start = s.Ns
 		}
 		end = s.Ns
-		nsamples ++
+		nsamples++
 	}
 	/* we'll need to fix this once we go to ticks. */
 	hdr.Period = (end - start) / nsamples
@@ -94,7 +94,7 @@ func main() {
 	binary.Write(w, binary.LittleEndian, &hdr)
 	out := make([]uint64, 2)
 	/* note that the backtrace length varies. But we're good with that. */
-	for key, v := range(records) {
+	for key, v := range records {
 		bt := backtraces[key]
 		out[0] = v
 		out[1] = uint64(len(bt))

+ 0 - 0
util/removemach.go → util/src/harvey/cmd/removemach/removemach.go


+ 0 - 0
util/telnet.go → util/src/harvey/cmd/telnet/telnet.go