From e57c8d77db479add708e36e910ce9cac16037267 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 3 Nov 2017 15:05:19 +1100 Subject: [PATCH] Add script/inline-c-relative-includes.go. --- script/inline-c-relative-includes.go | 105 +++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 script/inline-c-relative-includes.go diff --git a/script/inline-c-relative-includes.go b/script/inline-c-relative-includes.go new file mode 100644 index 00000000..a3deb730 --- /dev/null +++ b/script/inline-c-relative-includes.go @@ -0,0 +1,105 @@ +// 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. + +// +build ignore + +package main + +// inline-c-relative-includes.go echoes the given C file to stdout, expanding +// #include lines that uses "quoted files" (but not ). +// +// The output should be a stand-alone C file that other people can easily +// compile and run with no further dependencies, other than testdata files. +// +// Usage: go run inline-c-relative-includes.go path/to/foo.c + +import ( + "bufio" + "bytes" + "fmt" + "os" + "path/filepath" +) + +func main() { + if err := main1(); err != nil { + os.Stderr.WriteString(err.Error() + "\n") + os.Exit(1) + } +} + +func main1() error { + if len(os.Args) != 2 { + return fmt.Errorf("inline-c-relative-includes takes exactly 1 argument") + } + w := bufio.NewWriter(os.Stdout) + defer w.Flush() + return do(w, ".", os.Args[1], 0) +} + +var ( + prefix = []byte(`#include "`) + suffix = []byte(`"`) + + seen = map[string]bool{} +) + +func do(w *bufio.Writer, dir string, filename string, depth int) error { + if depth == 100 { + return fmt.Errorf("recursion too deep") + } + if depth != 0 { + fmt.Fprintf(w, "// BEGIN INLINE #include %q\n", filename) + defer fmt.Fprintf(w, "// END INLINE #include %q\n", filename) + } + depth++ + + filename = filepath.Join(dir, filename) + if seen[filename] { + return fmt.Errorf("duplicate filename %q", filename) + } + seen[filename] = true + + dir, _ = filepath.Split(filename) + f, err := os.Open(filename) + if err != nil { + return err + } + defer f.Close() + + r := bufio.NewScanner(f) + for r.Scan() { + s := r.Bytes() + + if bytes.HasPrefix(s, prefix) { + s = s[len(prefix):] + if bytes.HasSuffix(s, suffix) { + s = s[:len(s)-len(suffix)] + if err := do(w, dir, string(s), depth); err == nil { + continue + } else if os.IsNotExist(err) { + // This is probably an #include of a system header, like + // `#include "zlib.h"`, and not of Puffs code. Fall through + // and print the #include line as normal. + } else { + return err + } + } + } + + w.Write(s) + w.WriteByte('\n') + } + return r.Err() +}