1/* Copyright (c) 2016, 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
15package main
16
17import (
18	"flag"
19	"fmt"
20	"os"
21	"os/exec"
22	"path/filepath"
23	"strings"
24	"syscall"
25)
26
27var (
28	boringsslDir = flag.String("boringssl", ".", "The path to the BoringSSL checkout.")
29	opensslDir   = flag.String("openssl", filepath.Join("..", "openssl"), "The path to the OpenSSL checkout.")
30)
31
32func mapName(path string) string {
33	switch filepath.ToSlash(path) {
34	case "crypto/rand/asm/rdrand-x86_64.pl":
35		return ""
36	case "crypto/ec/asm/p256-x86_64-asm.pl":
37		return filepath.FromSlash("crypto/ec/asm/ecp_nistz256-x86_64.pl")
38	}
39	return path
40}
41
42func diff(from, to string) error {
43	cmd := exec.Command("diff", "-u", "--", from, to)
44	cmd.Stdout = os.Stdout
45	cmd.Stderr = os.Stderr
46	err := cmd.Run()
47	// diff returns exit code 1 if the files differ but it was otherwise
48	// successful.
49	if exitError, ok := err.(*exec.ExitError); ok && exitError.Sys().(syscall.WaitStatus).ExitStatus() == 1 {
50		return nil
51	}
52	return err
53}
54
55func main() {
56	flag.Usage = func() {
57		fmt.Fprintf(os.Stderr, "Usage: diff_asm [flag...] [filter...]\n")
58		fmt.Fprintf(os.Stderr, "Filter arguments limit to assembly files which match arguments.\n")
59		fmt.Fprintf(os.Stderr, "If not using a filter, piping to `diffstat` may be useful.\n\n")
60		flag.PrintDefaults()
61	}
62	flag.Parse()
63
64	// Find all the assembly files.
65	var files []string
66	err := filepath.Walk(*boringsslDir, func(path string, info os.FileInfo, err error) error {
67		if err != nil {
68			return nil
69		}
70
71		path, err = filepath.Rel(*boringsslDir, path)
72		if err != nil {
73			return err
74		}
75
76		dir := filepath.Base(filepath.Dir(path))
77		if !info.IsDir() && (dir == "asm" || dir == "perlasm") && strings.HasSuffix(filepath.Base(path), ".pl") {
78			files = append(files, path)
79		}
80
81		return nil
82	})
83	if err != nil {
84		fmt.Fprintf(os.Stderr, "Error finding assembly: %s\n", err)
85		os.Exit(1)
86	}
87
88	for _, file := range files {
89		opensslFile := mapName(file)
90		if len(opensslFile) == 0 {
91			continue
92		}
93
94		if flag.NArg() > 0 {
95			var found bool
96			for _, arg := range flag.Args() {
97				if strings.Contains(file, arg) {
98					found = true
99					break
100				}
101			}
102			if !found {
103				continue
104			}
105		}
106
107		if err := diff(filepath.Join(*opensslDir, opensslFile), filepath.Join(*boringsslDir, file)); err != nil {
108			fmt.Fprintf(os.Stderr, "Error comparing %s: %s\n", file, err)
109			os.Exit(1)
110		}
111	}
112}
113