Ejemplo n.º 1
0
func test_handler(t *testing.T, src, expected string) {
	defer tearDown()

	comp.CompileFile("test", src)

	out, err := exec.Command("gcc"+ext, "-Wall", "-Wextra", "-std=c99",
		"--output=test"+ext, "test.c").CombinedOutput()

	if err != nil {
		t.Log(string(out))
		t.Fatal(err)
	}
	var output []byte

	switch runtime.GOOS {
	case "windows":
		output, err = exec.Command("test" + ext).Output()
	default:
		output, err = exec.Command("./test").Output()
	}

	if err != nil {
		t.Fatal(err)
	}
	if string(output) != expected {
		t.Fatal("For " + src + " expected " + expected + " got " + string(output))
	}
}
Ejemplo n.º 2
0
func test_handler(t *testing.T, src, expected string) {
	defer tearDown()

	err := ioutil.WriteFile("test.calc", []byte(src), os.ModePerm)
	if err != nil {
		t.Fatal(err)
	}
	comp.CompileFile("test.calc")
	os.Remove("test.calc")

	runpath, _ := filepath.Abs("../runtime")
	runlib := filepath.Join(runpath, "runtime.a")
	out, err := exec.Command("gcc"+ext, "-Wall", "-Wextra", "-std=c99",
		"-I", runpath, "--output=test"+ext,
		"test.c", runlib).CombinedOutput()
	if err != nil {
		t.Log(string(out))
		t.Fatal(err)
	}
	var output []byte

	switch runtime.GOOS {
	case "windows":
		output, err = exec.Command("test" + ext).Output()
	default:
		output, err = exec.Command("./test").Output()
	}
	output = []byte(strings.TrimSpace(string(output)))
	t.Log("len output:", len(output))
	t.Log("len expected:", len(expected))

	if string(output) != expected {
		t.Fatal("For " + src + " expected " + expected + " got " + string(output))
	}
}
Ejemplo n.º 3
0
Archivo: calcc.go Proyecto: unreal/calc
func main() {
	ext := ""
	if runtime.GOOS == "windows" {
		ext = ".exe"
	}
	flag.Usage = func() {
		printVersion()
		fmt.Fprintln(os.Stderr, "\nUsage of:", os.Args[0])
		fmt.Fprintln(os.Stderr, os.Args[0], "[flags] <filename>")
		flag.PrintDefaults()
	}
	var (
		asm  = flag.Bool("s", false, "generate C code but do not compile")
		cc   = flag.String("cc", "gcc", "C compiler to use")
		cfl  = flag.String("cflags", "-c -std=gnu99", "C compiler flags")
		cout = flag.String("cout", "--output=", "C compiler output flag")
		ld   = flag.String("ld", "gcc", "linker")
		ldf  = flag.String("ldflags", "", "linker flags")
		ver  = flag.Bool("v", false, "Print version number and exit")
	)
	flag.Parse()

	if *ver {
		printVersion()
		os.Exit(1)
	}
	var path string
	switch flag.NArg() {
	case 0:
		path, _ = filepath.Abs(".")
	case 1:
		path, _ = filepath.Abs(flag.Arg(0))
	default:
		flag.Usage()
		os.Exit(1)
	}

	/* do a preemptive search to see if runtime can be found. Does not
	 * guarantee it will be there at link time */
	rpath := findRuntime()
	if rpath == "" {
		fatal("Unable to find runtime in GOPATH. Make sure 'make' command was " +
			"run in source directory")
	}

	fi, err := os.Stat(path)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if fi.IsDir() {
		comp.CompileDir(path)
		path = filepath.Join(path, filepath.Base(path))
	} else {
		comp.CompileFile(path)
	}
	path = path[:len(path)-len(filepath.Ext(path))]
	if !*asm {
		/* compile to object code */
		var out []byte
		args := make_args(*cfl, "-I "+rpath, *cout+path+".o", path+".c")
		out, err := exec.Command(*cc+ext,
			strings.Split(args, " ")...).CombinedOutput()
		if err != nil {
			cleanup(path)
			fatal(string(out), err)
		}

		/* link to executable */
		args = make_args(*ldf, *cout+path+ext, path+".o", rpath+"/runtime.a")
		out, err = exec.Command(*ld+ext,
			strings.Split(args, " ")...).CombinedOutput()
		if err != nil {
			cleanup(path)
			fatal(string(out), err)
		}
		cleanup(path)
	}
}