compiler: sort packages in export data more deterministically

We can have multiple packages with the same name, so also sort by pkgpath.
To avoid an inconsistent sort, sort by symbol and pointer address if
we somehow get two different packages with the same name and pkgpath.

Change-Id: Iaff153bb63e4ff53cc885397068d0820a4ac40b8
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/171032
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/go/export.cc b/go/export.cc
index 963f0bf..066455a 100644
--- a/go/export.cc
+++ b/go/export.cc
@@ -509,7 +509,24 @@
 static bool
 packages_compare(const Package* a, const Package* b)
 {
-  return a->package_name() < b->package_name();
+  if (a->package_name() < b->package_name())
+    return true;
+  else if (a->package_name() > b->package_name())
+    return false;
+
+  if (a->pkgpath() < b->pkgpath())
+    return true;
+  else if (a->pkgpath() > b->pkgpath())
+    return false;
+
+  // In principle if we get here then a == b.  Try to do something sensible
+  // even if the import information is inconsistent.
+  if (a->pkgpath_symbol() < b->pkgpath_symbol())
+    return true;
+  else if (a->pkgpath_symbol() > b->pkgpath_symbol())
+    return false;
+
+  return a < b;
 }
 
 // Write out all the known packages whose pkgpath symbol is not a