xref: /aosp_15_r20/external/boringssl/src/util/check_filenames.go (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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//go:build ignore
16
17// check_filenames.go checks that filenames are unique. Some of our consumers do
18// not support multiple files with the same name in the same build target, even
19// if they are in different directories.
20package main
21
22import (
23	"fmt"
24	"os"
25	"path/filepath"
26	"strings"
27)
28
29func isSourceFile(in string) bool {
30	return strings.HasSuffix(in, ".c") || strings.HasSuffix(in, ".cc")
31}
32
33func main() {
34	var roots = []string{
35		"crypto",
36		filepath.Join("third_party", "fiat"),
37		"ssl",
38	}
39
40	names := make(map[string]string)
41	var foundCollisions bool
42	for _, root := range roots {
43		err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
44			if err != nil {
45				return err
46			}
47			if info.IsDir() {
48				return nil
49			}
50			name := strings.ToLower(info.Name()) // Windows and macOS are case-insensitive.
51			if isSourceFile(name) {
52				if oldPath, ok := names[name]; ok {
53					fmt.Printf("Filename collision found: %s and %s\n", path, oldPath)
54					foundCollisions = true
55				} else {
56					names[name] = path
57				}
58			}
59			return nil
60		})
61		if err != nil {
62			fmt.Printf("Error traversing %s: %s\n", root, err)
63			os.Exit(1)
64		}
65	}
66	if foundCollisions {
67		os.Exit(1)
68	}
69}
70