1// Copyright (c) 2018, Google Inc.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15// check_filenames.go checks that filenames are unique. Some of our consumers do
16// not support multiple files with the same name in the same build target, even
17// if they are in different directories.
18package main
19
20import (
21	"fmt"
22	"os"
23	"path/filepath"
24	"strings"
25)
26
27func isSourceFile(in string) bool {
28	return strings.HasSuffix(in, ".c") || strings.HasSuffix(in, ".cc")
29}
30
31func main() {
32	var roots = []string{
33		"crypto",
34		filepath.Join("third_party", "fiat"),
35		"ssl",
36	}
37
38	names := make(map[string]string)
39	var foundCollisions bool
40	for _, root := range roots {
41		err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
42			if err != nil {
43				return err
44			}
45			if info.IsDir() {
46				return nil
47			}
48			name := strings.ToLower(info.Name()) // Windows and macOS are case-insensitive.
49			if isSourceFile(name) {
50				if oldPath, ok := names[name]; ok {
51					fmt.Printf("Filename collision found: %s and %s\n", path, oldPath)
52					foundCollisions = true
53				} else {
54					names[name] = path
55				}
56			}
57			return nil
58		})
59		if err != nil {
60			fmt.Printf("Error traversing %s: %s\n", root, err)
61			os.Exit(1)
62		}
63	}
64	if foundCollisions {
65		os.Exit(1)
66	}
67}
68