Example #1
0
//TODO fix this mess
func gc() {

	var (
		A string // A:architecture
		S string // S:suffix
		C string // C:compiler
		L string // L:linker
		R string // R:goroot
		O string // O:goOS
	)

	var err error

	A = handy.GOARCH()
	R = handy.GOROOT()
	O = handy.GOOS()

	switch A {
	case "arm":
		S = ".5"
		C = "5g"
		L = "5l"
	case "amd64":
		S = ".6"
		C = "6g"
		L = "6l"
	case "386":
		S = ".8"
		C = "8g"
		L = "8l"
	default:
		log.Fatalf("[ERROR] unknown architecture: %s\n", A)
	}

	path_C := filepath.Join(R, "pkg", "tool", (O + "_" + A), C)

	pathCompiler, err = exec.LookPath(path_C)

	if err != nil {
		log.Fatalf("[ERROR] could not find compiler: %s\n", C)
	}

	path_L := filepath.Join(R, "pkg", "tool", (O + "_" + A), L)

	pathLinker, err = exec.LookPath(path_L)

	if err != nil {
		log.Fatalf("[ERROR] could not find linker: %s\n", L)
	}

	suffix = S

}
Example #2
0
func init() {

	// initialize option parser
	getopt = gopt.New()

	// add all options (bool/string)
	getopt.BoolOption("-h -help --help help")
	getopt.BoolOption("-c -clean --clean clean")
	getopt.BoolOption("-S -static --static")
	getopt.BoolOption("-v -version --version version")
	getopt.BoolOption("-s -sort --sort sort")
	getopt.BoolOption("-p -print --print print")
	getopt.BoolOption("-d -dryrun --dryrun dryrun")
	getopt.BoolOption("-t -test --test test")
	getopt.BoolOption("-l -list --list list")
	getopt.BoolOption("-q -quiet --quiet")
	getopt.BoolOption("-V -verbose --verbose")
	getopt.BoolOption("-f -fmt --fmt fmt")
	getopt.BoolOption("-T -tab --tab")
	getopt.BoolOption("-a -all --all")
	getopt.BoolOption("-y -strip --strip strip")
	getopt.BoolOption("-e -external --external")
	getopt.BoolOption("-u -updatex --updatex " +
		"-update-external --update-external")
	getopt.StringOption("-I -I=")
	getopt.StringOption("-mkcomplete")
	getopt.StringOptionFancy("-D --dot")
	getopt.StringOptionFancy("-L --lib")
	getopt.StringOptionFancy("-g --gdmk")
	getopt.StringOptionFancy("-w --tabwidth")
	getopt.StringOptionFancy("-r --rewrite")
	getopt.StringOptionFancy("-o --output")
	getopt.StringOptionFancy("-M --main")
	getopt.StringOptionFancy("-b --bench")
	getopt.StringOptionFancy("-m --match")
	getopt.StringOptionFancy("--test-bin")
	getopt.StringOptionFancy("-B --backend")

	// new test options and aliases
	getopt.BoolOption("-test.short --test.short")
	getopt.BoolOption("-test.v --test.v")
	getopt.StringOptionFancy("--test.bench")
	getopt.StringOptionFancy("--test.benchtime")
	getopt.StringOptionFancy("--test.cpu")
	getopt.StringOptionFancy("--test.cpuprofile")
	getopt.StringOptionFancy("--test.memprofile")
	getopt.StringOptionFancy("--test.memprofilerate")
	getopt.StringOptionFancy("--test.timeout")
	getopt.StringOptionFancy("--test.parallel")

	// override IncludeFile to make walker pick up only .go files
	walker.IncludeFile = noTestFilesFilter

	// override IncludeDir to make walker ignore 'hidden' directories
	walker.IncludeDir = func(s string) bool {
		_, dirname := filepath.Split(s)
		return dirname[0] != '.'
	}

	for _, bkey := range bools {
		global.SetBool(bkey, false)
	}

	for _, skey := range strs {
		global.SetString(skey, "")
	}

	// Testing on Windows requires .exe ending
	goos := handy.GOOS()

	if goos == "windows" {
		global.SetString("-test-bin", "gdtest.exe")
	} else {
		global.SetString("-test-bin", "gdtest")
	}

	global.SetString("-backend", runtime.Compiler)
	global.SetString("-I", "")

}
Example #3
0
func main() {

	var (
		ok, up2date bool
		e           error
		argv, args  []string
		config      [4]string
	)

	timer.Start("everything")
	defer reportTime()

	// possible config locations
	config[0] = filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "godag", "gdrc")
	config[1] = filepath.Join(os.Getenv("HOME"), ".config", "godag", "gdrc")
	config[2] = filepath.Join(os.Getenv("HOME"), ".gdrc")
	config[3] = filepath.Join(os.Getenv("PWD"), ".gdrc")

	for _, conf := range config {

		argv, ok = handy.ConfigToArgv(conf)

		if ok {
			args = parseArgv(argv)
			if len(args) > 0 {
				log.Print("[WARNING] non-option arguments in config file\n")
			}
		}
	}

	// small gorun version if single go-file is given
	if len(os.Args) > 1 && strings.HasSuffix(os.Args[1], ".go") && handy.IsFile(os.Args[1]) {
		say.Mute() // be silent unless error here
		single, name := dag.ParseSingle(os.Args[1])
		compiler.InitBackend()
		compiler.CreateArgv(single)
		up2date = compiler.Compile(single)
		if handy.GOOS() == "windows" {
			name = name + ".exe"
		}
		compiler.ForkLink(name, single, nil, up2date)
		args = os.Args[1:]
		args[0] = name
		handy.StdExecve(args, true)
		os.Exit(0)
	}

	// command line arguments overrides/appends config
	args = parseArgv(os.Args[1:])

	mkcomplete := global.GetString("-mkcomplete")
	if mkcomplete != "" {
		targets := dag.GetMakeTargets(mkcomplete)
		for _, t := range targets {
			fmt.Println(t)
		}
		os.Exit(0)
	}

	if len(args) > 0 {
		if len(args) > 1 {
			log.Print("[WARNING] len(input directories) > 1\n")
		}
		srcdir = args[0]
		if srcdir == "." {
			srcdir, e = os.Getwd()
			if e != nil {
				log.Fatal("[ERROR] can't find working directory\n")
			}
		}
	}

	// expand variables in includes
	for i := 0; i < len(includes); i++ {
		includes[i] = os.ExpandEnv(includes[i])
	}

	// expand variables in -lib
	global.SetString("-lib", os.ExpandEnv(global.GetString("-lib")))

	// expand variables in -output
	global.SetString("-output", os.ExpandEnv(global.GetString("-output")))

	if global.GetBool("-list") {
		printListing()
		os.Exit(0)
	}

	if global.GetBool("-help") {
		printHelp()
		os.Exit(0)
	}

	if global.GetBool("-version") {
		printVersion()
		os.Exit(0)
	}

	if len(args) == 0 {
		// give nice feedback if missing input dir
		if !handy.IsDir("src") {
			fmt.Printf("usage: gd [OPTIONS] src-directory\n")
			os.Exit(1)
		}
	}

	if global.GetBool("-quiet") {
		say.Mute()
	}

	handy.DirOrExit(srcdir)
	files = walker.PathWalk(filepath.Clean(srcdir))

	// gofmt on all files gathered
	if global.GetBool("-fmt") {
		compiler.FormatFiles(files)
		os.Exit(0)
	}

	// parse the source code, look for dependencies
	dgrph := dag.New()
	dgrph.Parse(srcdir, files)

	// print collected dependency info
	if global.GetBool("-print") {
		dgrph.PrintInfo()
		os.Exit(0)
	}

	// draw graphviz dot graph
	if global.GetString("-dot") != "" {
		dgrph.MakeDotGraph(global.GetString("-dot"))
		os.Exit(0)
	}

	// build  all external dependencies
	if global.GetBool("-external") {
		// update external dependencies
		if global.GetBool("-update-external") {
			dgrph.External(true)
		} else {
			dgrph.External(false)
		}
		os.Exit(0)
	}

	// sort graph based on dependencies
	dgrph.GraphBuilder()
	sorted := dgrph.Topsort()

	// clean only what we possibly could have generated…
	if global.GetBool("-clean") {
		compiler.DeleteObjects(srcdir, sorted)
		os.Exit(0)
	}

	// print packages sorted
	if global.GetBool("-sort") {
		for i := 0; i < len(sorted); i++ {
			fmt.Printf("%s\n", sorted[i].Name)
		}
		os.Exit(0)
	}

	// compile argv
	compiler.Init(srcdir, includes)
	if global.GetString("-lib") != "" {
		compiler.CreateLibArgv(sorted)
	} else {
		compiler.CreateArgv(sorted)
	}

	// gdmk
	if global.GetString("-gdmk") != "" {
		gdmake.Make(global.GetString("-gdmk"), sorted, dgrph.Alien().Slice())
		os.Exit(0)
	}

	// compile; up2date == true => 0 packages modified
	if global.GetBool("-dryrun") {
		compiler.Dryrun(sorted)
	} else {
		up2date = compiler.Compile(sorted) // updated parallel
	}

	// test
	if global.GetBool("-test") {
		os.Setenv("SRCROOT", srcdir)
		testMain, testDir, testLib := dgrph.MakeMainTest(srcdir)
		if global.GetString("-lib") != "" {
			compiler.CreateLibArgv(testMain)
		} else {
			compiler.CreateArgv(testMain)
		}
		if !global.GetBool("-dryrun") {
			compiler.Compile(testMain)
		}
		switch global.GetString("-backend") {
		case "gc", "express":
			compiler.ForkLink(global.GetString("-test-bin"), testMain, nil, false)
		case "gccgo", "gcc":
			compiler.ForkLink(global.GetString("-test-bin"), testMain, sorted, false)
		default:
			log.Fatalf("[ERROR] '%s' unknown back-end\n", global.GetString("-backend"))
		}
		compiler.DeletePackages(testMain)
		handy.Delete(testDir, false)
		if testLib != "" {
			handy.Delete(testLib, false)
		}
		testArgv := compiler.CreateTestArgv()
		if global.GetBool("-dryrun") {
			testArgv[0] = filepath.Base(testArgv[0])
			say.Printf("%s\n", strings.Join(testArgv, " "))
		} else {
			say.Printf("testing  : ")
			if global.GetBool("-verbose") || global.GetBool("-test.v") {
				say.Printf("\n")
			}
			ok = handy.StdExecve(testArgv, false)
			handy.Delete(global.GetString("-test-bin"), false)
			if !ok {
				os.Exit(1)
			}
		}

		// if packages contain both test-files and regular files
		// test-files should not be part of the objects, i.e. init
		// functions in test-packages can cause unexpected behaviour

		if compiler.ReCompile(sorted) {
			say.Printf("recompile: --tests\n")
			compiler.Compile(sorted)
		}

	}

	// link if ! up2date
	if global.GetString("-output") != "" {
		compiler.ForkLink(global.GetString("-output"), sorted, nil, up2date)
	} else if global.GetBool("-all") {
		compiler.ForkLinkAll(sorted, up2date)
	}

}