From 13ebf8cb6d3af136dde9f605ecf3c257b4bee808 Mon Sep 17 00:00:00 2001 From: Patrick Hiesel Date: Thu, 25 Aug 2016 16:26:00 +0200 Subject: [PATCH] Add a piece of go code to find mismatching lib versions in bzl/bck Tested with: go run contrib/build-consistency.go Gives (before the current fix on master): SHA1 of lib gson does not match: buck has 751f548c85fa49f330cecbb1875893f971b33c4e while bazel has f1bc476cc167b18e66c297df599b2377131a8947 SHA1 of lib jsr305 does not match: buck has f7be08ec23c21485b9b5a1cf1654c2ec8c58168d while bazel has 516c03b21d50a644d538de0f0369c620989cd8f0 SHA1 of lib auto_value does not match: buck has b764e0fb7e11353fbff493b22fd6e83bf091a179 while bazel has 6873fed014fe1de1051aae2af68ba266d2934471 SHA1 of lib junit does not match: buck has dc7edb9c3060655c7fb93ab9b9349e815bab266f while bazel has 4e031bb61df09069aeb2bffb4019e7a5034a4ee0 SHA1 of lib joda_time does not match: buck has 1c295b462f16702ebe720bbb08f62e1ba80da41b while bazel has 9f2785d7184b97d005a44241ccaf980f43b9ccdb SHA1 of lib joda_convert does not match: buck has 675642ac208e0b741bc9118dcbcae44c271b992a while bazel has 35ec554f0cd00c956cc69051514d9488b1374dec Can't parse lib sha1/name on target maven_jars(name='lucene_core_and_backward_codecs', srcs=[':backward_codecs_jar',':lucene_core',],visibility=['PUBLIC'], SHA1 of lib lucene_core does not match: buck has a74fd869bb5ad7fe6b4cd29df9543a34aea81164 while bazel has c52b2088e2c30dfd95fd296ab6fb9cf8de9855ab SHA1 of lib lucene_analyzers_common does not match: buck has 1e0e8243a4410be20c34683034fafa7bb52e55cc while bazel has c2aa2c4e00eb9cdeb5ac00dc0495e70c441f681e SHA1 of lib lucene_misc does not match: buck has 504d855a1a38190622fdf990b2298c067e7d60ca while bazel has 95f433b9d7dd470cc0aa5076e0f233907745674b SHA1 of lib lucene_queryparser does not match: buck has 0fddc49725b562fd48dff0cff004336ad2a090a4 while bazel has dccd5279bfa656dec21af444a7a66820eb1cd618 SHA1 of lib ow2_asm does not match: buck has 5ef31c4fe953b1fd00b8a88fa1d6820e8785bb45 while bazel has dcc2193db20e19e1feca8b1240dbbc4e190824fa SHA1 of lib ow2_asm_analysis does not match: buck has 6d1bf8989fc7901f868bee3863c44f21aa63d110 while bazel has c7126aded0e8e13fed5f913559a0dd7b770a10f3 SHA1 of lib ow2_asm_commons does not match: buck has 25d8a575034dd9cfcb375a39b5334f0ba9c8474e while bazel has a7111830132c7f87d08fe48cb0ca07630f8cb91c SHA1 of lib ow2_asm_tree does not match: buck has 87b38c12a0ea645791ead9d3e74ae5268d1d6c34 while bazel has 287749b48ba7162fb67c93a026d690b29f410bed SHA1 of lib ow2_asm_util does not match: buck has b60e33a6bd0d71831e0c249816d01e6c1dd90a47 while bazel has 1512e5571325854b05fb1efce1db75fcced54389 Change-Id: I536b3eecf61a2cd1e7ce9d1c19335f1c2238b6c5 --- contrib/build-consistency.go | 108 +++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 contrib/build-consistency.go diff --git a/contrib/build-consistency.go b/contrib/build-consistency.go new file mode 100644 index 0000000000..db63a274dd --- /dev/null +++ b/contrib/build-consistency.go @@ -0,0 +1,108 @@ +// Copyright (C) 2016 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path/filepath" + "regexp" + "strings" +) + +var ( + // Define regex to find a comment in the build files + commentRE = regexp.MustCompile("#.*") + // Define regexes to extract the lib name and sha1 + mvnRE = regexp.MustCompile("maven_jar([^)]*)") + sha1RE = regexp.MustCompile("sha1=[\"'](?P[^,]*)[\"']") + bSha1RE = regexp.MustCompile("bin_sha1=[\"'](?P[^,]*)[\"']") + libNameRE = regexp.MustCompile("name=[\"'](?P[^,]*)[\"']") +) + +func sanitize(s string) string { + // Strip out comments + s = commentRE.ReplaceAllString(s, "") + // Remove newlines and blanks + s = strings.Replace(s, "\n", "", -1) + s = strings.Replace(s, " ", "", -1) + // WORKSPACE syntax disallows the dash char in artifact name and we use an underscore + // So we make this a consistent underscore in all files + s = strings.Replace(s, "-", "_", -1) + return s +} + +func main() { + // Load bazel WORKSPACE file + bzlDat, err := ioutil.ReadFile("WORKSPACE") + if err != nil { + log.Fatal(err) + } + bzlStr := sanitize(string(bzlDat)) + + // Walk all files nested under lib. Find, load and sanitize BUCK files + bckStrs := []string{} + err = filepath.Walk("lib/", func(path string, f os.FileInfo, err error) error { + bckFile := filepath.Join(path, "BUCK") + if _, err := os.Stat(bckFile); err == nil { + bckDat, err := ioutil.ReadFile(bckFile) + if err != nil { + return err + } + bckStrs = append(bckStrs, sanitize(string(bckDat))) + } + return nil + }) + if err != nil { + log.Fatal(err) + } + bckStr := strings.Join(bckStrs, "") + + // Find all bazel dependencies + // bzlVersions maps from a lib name to the referenced sha1 + bzlVersions := make(map[string]string) + for _, mvn := range mvnRE.FindAllString(bzlStr, -1) { + sha1s := sha1RE.FindStringSubmatch(mvn) + names := libNameRE.FindStringSubmatch(mvn) + if len(sha1s) > 1 && len(names) > 1 { + bzlVersions[names[1]] = sha1RE.FindStringSubmatch(mvn)[1] + } else { + fmt.Printf("Can't parse lib sha1/name of target %s\n", mvn) + } + } + + // Find all buck dependencies and check if we have the correct bazel dependency on file + for _, mvn := range mvnRE.FindAllString(bckStr, -1) { + sha1s := bSha1RE.FindStringSubmatch(mvn) + if len(sha1s) < 2 { + // Buck knows two dep version representations: just a SHA1 or a bin_sha1 and src_sha1 + // We try to extract the bin_sha1 first. If that fails, we use the sha1 + sha1s = sha1RE.FindStringSubmatch(mvn) + } + names := libNameRE.FindStringSubmatch(mvn) + if len(sha1s) > 1 && len(names) > 1 { + if _, ok := bzlVersions[names[1]]; !ok { + // TODO(hiesel) This produces too many false positives. + //fmt.Printf("Don't have lib %s in bazel\n", names[1]) + } else if bzlVersions[names[1]] != sha1s[1] { + fmt.Printf("SHA1 of lib %s does not match: buck has %s while bazel has %s\n", names[1], sha1s[1], bzlVersions[names[1]]) + } + } else { + fmt.Printf("Can't parse lib sha1/name on target %s\n", mvn) + } + } +}