compiler: improve command line argument parsing

To get more compatibility with the go command, implement a similar
command line interface (with "tinygo build <package>" etc.).
Other than that, an all-round cleanup of command parsing.
Этот коммит содержится в:
Ayke van Laethem 2018-09-11 20:26:13 +02:00
родитель 81b9edbe65
коммит 0779ee6088
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
2 изменённых файлов: 29 добавлений и 8 удалений

Просмотреть файл

@ -96,7 +96,7 @@ build/tgo: *.go
# Build IR with the Go compiler. # Build IR with the Go compiler.
build/%.o: src/examples/% src/examples/%/*.go build/tgo src/runtime/*.go build/runtime-$(TARGET)-combined.bc 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. # Compile C sources for the runtime.
build/%.bc: src/runtime/%.c src/runtime/*.h build/%.bc: src/runtime/%.c src/runtime/*.h

35
main.go
Просмотреть файл

@ -76,6 +76,14 @@ func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool
return nil return nil
} }
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s command [-printir] -runtime=<runtime.bc> [-target=<target>] -o <output> <input>\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() { func main() {
outpath := flag.String("o", "", "output filename") outpath := flag.String("o", "", "output filename")
printIR := flag.Bool("printir", false, "print LLVM IR") 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)") runtime := flag.String("runtime", "", "runtime LLVM bitcode files (from C sources)")
target := flag.String("target", llvm.DefaultTargetTriple(), "LLVM target") 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 { if *outpath == "" || flag.NArg() != 1 {
fmt.Fprintf(os.Stderr, "usage: %s [-printir] -runtime=<runtime.bc> [-target=<target>] -o <output> <input>", os.Args[0]) usage()
flag.PrintDefaults() os.Exit(1)
return
} }
os.Setenv("CC", "clang -target="+*target) os.Setenv("CC", "clang -target="+*target)
err := Compile(flag.Args()[0], *runtime, *outpath, *target, *printIR, *dumpSSA) switch command {
if err != nil { case "build":
fmt.Fprintln(os.Stderr, "error:", err) 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) os.Exit(1)
} }
} }