1// Copyright 2018 The BoringSSL Authors
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//     https://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//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