blob: bac3772be1798e6c896b7e1e86c98d5bd51741e2 [file] [log] [blame]
Dan Willemsen43398532018-02-21 02:10:29 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// This tool reads "make"-like dependency files, and outputs a canonical version
16// that can be used by ninja. Ninja doesn't support multiple output files (even
17// though it doesn't care what the output file is, or whether it matches what is
18// expected).
19package main
20
21import (
22 "bytes"
23 "flag"
24 "fmt"
25 "io/ioutil"
26 "log"
27 "os"
28)
29
30func main() {
31 flag.Usage = func() {
32 fmt.Fprintf(os.Stderr, "Usage: %s <depfile.d>")
33 flag.PrintDefaults()
34 }
35 output := flag.String("o", "", "Optional output file (defaults to rewriting source if necessary)")
36 flag.Parse()
37
38 if flag.NArg() != 1 {
39 log.Fatal("Expected a single file as an argument")
40 }
41
42 old, err := ioutil.ReadFile(flag.Arg(0))
43 if err != nil {
44 log.Fatalf("Error opening %q: %v", flag.Arg(0), err)
45 }
46
47 deps, err := Parse(flag.Arg(0), bytes.NewBuffer(append([]byte(nil), old...)))
48 if err != nil {
49 log.Fatalf("Failed to parse: %v", err)
50 }
51
52 new := deps.Print()
53
54 if *output == "" || *output == flag.Arg(0) {
55 if !bytes.Equal(old, new) {
56 err := ioutil.WriteFile(flag.Arg(0), new, 0666)
57 if err != nil {
58 log.Fatalf("Failed to write: %v", err)
59 }
60 }
61 } else {
62 err := ioutil.WriteFile(*output, new, 0666)
63 if err != nil {
64 log.Fatalf("Failed to write to %q: %v", *output, err)
65 }
66 }
67}