blob: fb8ffe745d828ae36e76b0c515006dac366e0f28 [file] [log] [blame] [edit]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"encoding/json"
"fidl/compiler/backend/cpp"
"fidl/compiler/backend/dart"
"fidl/compiler/backend/golang"
"fidl/compiler/backend/rust"
"fidl/compiler/backend/types"
"flag"
"io/ioutil"
"log"
"os"
)
type GenerateFidl interface {
GenerateFidl(fidl types.Root, config *types.Config) error
}
var generators = map[string]GenerateFidl{
"cpp": cpp.FidlGenerator{},
"go": golang.FidlGenerator{},
"dart": dart.FidlGenerator{},
"rust": rust.FidlGenerator{},
}
func main() {
jsonPath := flag.String("json", "",
"relative path to the FIDL intermediate representation.")
outputBase := flag.String("output-base", "",
"the base file name for files generated by this generator.")
includeBase := flag.String("include-base", "",
"the directory to which C and C++ includes should be relative.")
var generatorNames CommaSeparatedList
flag.Var(&generatorNames, "generators",
"Comma-separated list of names of generators to run")
flag.Parse()
if !flag.Parsed() {
flag.PrintDefaults()
os.Exit(1)
}
if *jsonPath == "" || *outputBase == "" || *includeBase == "" {
flag.PrintDefaults()
os.Exit(1)
}
bytes, err := ioutil.ReadFile(*jsonPath)
if err != nil {
log.Fatalf("Error reading from %s: %v", *jsonPath, err)
}
var fidl types.Root
err = json.Unmarshal(bytes, &fidl)
if err != nil {
log.Fatalf("Error parsing JSON as FIDL data: %v", err)
}
for _, l := range fidl.Libraries {
for k, v := range l.Decls {
fidl.Decls[types.EnsureLibrary(l.Name, k)] = v
}
}
running := 0
results := make(chan error)
didError := false
generatorNames = []string(generatorNames)
config := &types.Config{
*outputBase,
*includeBase,
}
for _, generatorName := range generatorNames {
if generator, ok := generators[generatorName]; ok {
running++
go func() {
results <- generator.GenerateFidl(fidl, config)
}()
} else {
log.Printf("Error: generator %s not found", generatorName)
didError = true
}
}
for running > 0 {
err = <-results
if err != nil {
log.Printf("Error running generator: %v", err)
didError = true
}
running--
}
if didError {
os.Exit(1)
}
}