blob: 0340d860e259b398be028b6a7ef804cb9fc021b2 [file] [log] [blame]
// 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 cmdline
import (
"encoding/json"
"fidl/compiler/backend/types"
"flag"
"io/ioutil"
"log"
)
// Flags common to all FIDL backends.
type Flags struct {
jsonPath *string
outputBase *string
includeBase *string
}
// BaseFlags returns the common set of flags for FIDL backends.
func BaseFlags() Flags {
return Flags{
flag.String("json", "",
"relative path to the FIDL intermediate representation."),
flag.String("output-base", "",
"the base file name for files generated by this generator."),
flag.String("include-base", "",
"the directory to which C and C++ includes should be relative."),
}
}
// Valid returns true if the parsed flags are valid.
func (f Flags) Valid() bool {
return *f.jsonPath != "" && *f.outputBase != "" && *f.includeBase != ""
}
// FidlTypes returns the root FIDL type information from the JSON file specified as an argument.
func (f Flags) FidlTypes() types.Root {
bytes, err := ioutil.ReadFile(*f.jsonPath)
if err != nil {
log.Fatalf("Error reading from %s: %v", *f.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
}
}
return fidl
}
// Config returns the Config object for the supplied flags.
func (f Flags) Config() types.Config {
return types.Config{
OutputBase: *f.outputBase,
IncludeBase: *f.includeBase,
}
}