diff --git a/cmd/commonflags/commonflags.go b/cmd/commonflags/commonflags.go new file mode 100644 index 00000000..e7f785bc --- /dev/null +++ b/cmd/commonflags/commonflags.go @@ -0,0 +1,53 @@ +// Copyright 2017 The Puffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// commonflags holds flag defaults and usage messages that are common to the +// Puffs command line tools. +// +// It also holds functions to parse and validate these flag values. +package commonflags + +const ( + CcompilersDefault = "clang,gcc" + CcompilersUsage = `comma-separated list of C compilers, e.g. "clang,gcc"` + + FocusDefault = "" + FocusUsage = `comma-separated list of tests or benchmarks (name prefixes) to focus on, e.g. "puffs_gif_decode"` + + MimicDefault = false + MimicUsage = `whether to compare Puffs' output with other libraries' output` + + RepsDefault = 5 + RepsMin = 0 + RepsMax = 1000000 + RepsUsage = `the number of repetitions per benchmark` +) + +// IsAlphaNumericIsh returns whether s contains only ASCII alpha-numerics and a +// limited set of punctuation such as commas and slahes, but not containing +// e.g. spaces, semi-colons, colons or backslashes. +// +// The intent is that if s is alpha-numeric-ish, then it should not need +// escaping when passed to other programs as command line arguments. +func IsAlphaNumericIsh(s string) bool { + for i := 0; i < len(s); i++ { + c := s[i] + if c == ',' || c == '-' || c == '.' || c == '/' || ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || + c == '_' || ('a' <= c && c <= 'z') { + continue + } + return false + } + return true +} diff --git a/cmd/puffs-c/genlib.go b/cmd/puffs-c/genlib.go index ee93cb15..de37688e 100644 --- a/cmd/puffs-c/genlib.go +++ b/cmd/puffs-c/genlib.go @@ -21,11 +21,13 @@ import ( "os/exec" "path/filepath" "strings" + + cf "github.com/google/puffs/cmd/commonflags" ) func doGenlib(args []string) error { flags := flag.FlagSet{} - ccompilersFlag := flags.String("ccompilers", ccompilersDefault, ccompilersUsage) + ccompilersFlag := flags.String("ccompilers", cf.CcompilersDefault, cf.CcompilersUsage) dstdirFlag := flags.String("dstdir", "", "directory containing the object files ") srcdirFlag := flags.String("srcdir", "", "directory containing the C source files") if err := flags.Parse(args); err != nil { diff --git a/cmd/puffs-c/main.go b/cmd/puffs-c/main.go index 6e42b672..c00b6764 100644 --- a/cmd/puffs-c/main.go +++ b/cmd/puffs-c/main.go @@ -46,19 +46,3 @@ func main1() error { } return fmt.Errorf("bad sub-command %q", os.Args[1]) } - -const ( - ccompilersDefault = "clang,gcc" - ccompilersUsage = `comma-separated list of C compilers, e.g. "clang,gcc"` - - focusDefault = "" - focusUsage = `comma-separated list of tests or benchmarks (name prefixes) to focus on, e.g. "puffs_gif_decode"` - - mimicDefault = false - mimicUsage = `whether to compare Puffs' output with other libraries' output` - - repsDefault = 5 - repsMin = 0 - repsMax = 1000000 - repsUsage = `the number of repetitions per benchmark` -) diff --git a/cmd/puffs-c/test.go b/cmd/puffs-c/test.go index bb9e44ba..ff61ad2a 100644 --- a/cmd/puffs-c/test.go +++ b/cmd/puffs-c/test.go @@ -23,42 +23,32 @@ import ( "os/exec" "path/filepath" "strings" -) -func isStringList(s string) bool { - for i := 0; i < len(s); i++ { - c := s[i] - if c == ',' || c == '-' || c == '.' || c == '/' || ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || - c == '_' || ('a' <= c && c <= 'z') { - continue - } - return false - } - return true -} + cf "github.com/google/puffs/cmd/commonflags" +) func doBench(args []string) error { return doBenchTest(args, true) } func doTest(args []string) error { return doBenchTest(args, false) } func doBenchTest(args []string, bench bool) error { flags := flag.FlagSet{} - ccompilersFlag := flags.String("ccompilers", ccompilersDefault, ccompilersUsage) - focusFlag := flags.String("focus", focusDefault, focusUsage) - mimicFlag := flags.Bool("mimic", mimicDefault, mimicUsage) - repsFlag := flags.Int("reps", repsDefault, repsUsage) + ccompilersFlag := flags.String("ccompilers", cf.CcompilersDefault, cf.CcompilersUsage) + focusFlag := flags.String("focus", cf.FocusDefault, cf.FocusUsage) + mimicFlag := flags.Bool("mimic", cf.MimicDefault, cf.MimicUsage) + repsFlag := flags.Int("reps", cf.RepsDefault, cf.RepsUsage) if err := flags.Parse(args); err != nil { return err } - if !isStringList(*ccompilersFlag) { + if !cf.IsAlphaNumericIsh(*ccompilersFlag) { return fmt.Errorf("bad -ccompilers flag value %q", *ccompilersFlag) } - if !isStringList(*focusFlag) { + if !cf.IsAlphaNumericIsh(*focusFlag) { return fmt.Errorf("bad -focus flag value %q", *focusFlag) } - if *repsFlag < repsMin || repsMax < *repsFlag { - return fmt.Errorf("bad -reps flag value %d, outside the range [%d..%d]", *repsFlag, repsMin, repsMax) + if *repsFlag < cf.RepsMin || cf.RepsMax < *repsFlag { + return fmt.Errorf("bad -reps flag value %d, outside the range [%d..%d]", *repsFlag, cf.RepsMin, cf.RepsMax) } args = flags.Args() diff --git a/cmd/puffs/main.go b/cmd/puffs/main.go index 72d4c6fa..96216b8a 100644 --- a/cmd/puffs/main.go +++ b/cmd/puffs/main.go @@ -118,23 +118,9 @@ func listDir(puffsRoot string, dirname string, returnSubdirs bool) (filenames [] } const ( - ccompilersDefault = "clang,gcc" - ccompilersUsage = `comma-separated list of C compilers, e.g. "clang,gcc"` - - focusDefault = "" - focusUsage = `comma-separated list of tests or benchmarks (name prefixes) to focus on, e.g. "puffs_gif_decode"` - langsDefault = "c" langsUsage = `comma-separated list of target languages (file extensions), e.g. "c,go,rs"` - mimicDefault = false - mimicUsage = `whether to compare Puffs' output with other libraries' output` - - repsDefault = 5 - repsMin = 0 - repsMax = 1000000 - repsUsage = `the number of repetitions per benchmark` - skipgenDefault = false skipgenUsage = `whether to skip automatically generating code when testing` ) diff --git a/cmd/puffs/test.go b/cmd/puffs/test.go index 7bfedea9..4316a658 100644 --- a/cmd/puffs/test.go +++ b/cmd/puffs/test.go @@ -21,30 +21,20 @@ import ( "os/exec" "path/filepath" "strings" -) -func isStringList(s string) bool { - for i := 0; i < len(s); i++ { - c := s[i] - if c == ',' || c == '-' || c == '.' || c == '/' || ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || - c == '_' || ('a' <= c && c <= 'z') { - continue - } - return false - } - return true -} + cf "github.com/google/puffs/cmd/commonflags" +) func doBench(puffsRoot string, args []string) error { return doBenchTest(puffsRoot, args, true) } func doTest(puffsRoot string, args []string) error { return doBenchTest(puffsRoot, args, false) } func doBenchTest(puffsRoot string, args []string, bench bool) error { flags := flag.NewFlagSet("test", flag.ExitOnError) - ccompilersFlag := flags.String("ccompilers", ccompilersDefault, ccompilersUsage) - focusFlag := flags.String("focus", focusDefault, focusUsage) + ccompilersFlag := flags.String("ccompilers", cf.CcompilersDefault, cf.CcompilersUsage) + focusFlag := flags.String("focus", cf.FocusDefault, cf.FocusUsage) langsFlag := flags.String("langs", langsDefault, langsUsage) - mimicFlag := flags.Bool("mimic", mimicDefault, mimicUsage) - repsFlag := flags.Int("reps", repsDefault, repsUsage) + mimicFlag := flags.Bool("mimic", cf.MimicDefault, cf.MimicUsage) + repsFlag := flags.Int("reps", cf.RepsDefault, cf.RepsUsage) skipgenFlag := flags.Bool("skipgen", skipgenDefault, skipgenUsage) if err := flags.Parse(args); err != nil { @@ -55,14 +45,14 @@ func doBenchTest(puffsRoot string, args []string, bench bool) error { if err != nil { return err } - if !isStringList(*ccompilersFlag) { + if !cf.IsAlphaNumericIsh(*ccompilersFlag) { return fmt.Errorf("bad -ccompilers flag value %q", *ccompilersFlag) } - if !isStringList(*focusFlag) { + if !cf.IsAlphaNumericIsh(*focusFlag) { return fmt.Errorf("bad -focus flag value %q", *focusFlag) } - if *repsFlag < repsMin || repsMax < *repsFlag { - return fmt.Errorf("bad -reps flag value %d, outside the range [%d..%d]", *repsFlag, repsMin, repsMax) + if *repsFlag < cf.RepsMin || cf.RepsMax < *repsFlag { + return fmt.Errorf("bad -reps flag value %d, outside the range [%d..%d]", *repsFlag, cf.RepsMin, cf.RepsMax) } args = flags.Args()