diff --git a/Makefile b/Makefile index 90e8cf65..85adae80 100644 --- a/Makefile +++ b/Makefile @@ -96,7 +96,7 @@ build/tgo: *.go # Build IR with the Go compiler. build/%.o: src/examples/% src/examples/%/*.go build/tgo src/runtime/*.go build/runtime-$(TARGET)-combined.bc - ./build/tgo $(TGOFLAGS) -runtime build/runtime-$(TARGET)-combined.bc -o $@ $(subst src/,,$<) + ./build/tgo build $(TGOFLAGS) -runtime build/runtime-$(TARGET)-combined.bc -o $@ $(subst src/,,$<) # Compile C sources for the runtime. build/%.bc: src/runtime/%.c src/runtime/*.h diff --git a/main.go b/main.go index 5bccfe56..6c6ac4f7 100644 --- a/main.go +++ b/main.go @@ -76,6 +76,14 @@ func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool return nil } +func usage() { + fmt.Fprintf(os.Stderr, "usage: %s command [-printir] -runtime= [-target=] -o \n", os.Args[0]) + fmt.Fprintln(os.Stderr, "\ncommands:") + fmt.Fprintln(os.Stderr, " build: compile packages and dependencies") + fmt.Fprintln(os.Stderr, "\nflags:") + flag.PrintDefaults() +} + func main() { outpath := flag.String("o", "", "output filename") printIR := flag.Bool("printir", false, "print LLVM IR") @@ -83,19 +91,32 @@ func main() { runtime := flag.String("runtime", "", "runtime LLVM bitcode files (from C sources)") target := flag.String("target", llvm.DefaultTargetTriple(), "LLVM target") - flag.Parse() + if len(os.Args) < 2 { + usage() + os.Exit(1) + } + command := os.Args[1] + + flag.CommandLine.Parse(os.Args[2:]) if *outpath == "" || flag.NArg() != 1 { - fmt.Fprintf(os.Stderr, "usage: %s [-printir] -runtime= [-target=] -o ", os.Args[0]) - flag.PrintDefaults() - return + usage() + os.Exit(1) } os.Setenv("CC", "clang -target="+*target) - err := Compile(flag.Args()[0], *runtime, *outpath, *target, *printIR, *dumpSSA) - if err != nil { - fmt.Fprintln(os.Stderr, "error:", err) + switch command { + case "build": + err := Compile(flag.Arg(0), *runtime, *outpath, *target, *printIR, *dumpSSA) + if err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + os.Exit(1) + } + case "help": + usage() + default: + usage() os.Exit(1) } }