Set ZERO_AR_DATE=1 when running libtool.

Ref: http://www.opensource.apple.com/source/cctools/cctools-809/misc/libtool.c

Adhoc testing with base_unittests reduced non-deterministic bytes from ~2347 to
~174. It's definitely the lowest hanging fruit, which will permit us to focus on
the remaining bytes.

R=thakis@chromium.org
BUG=chromium:330262

Review URL: https://codereview.chromium.org/699083004

git-svn-id: http://gyp.googlecode.com/svn/trunk@1998 78cadc50-ecff-11dd-a971-7dbc132099af
diff --git a/pylib/gyp/mac_tool.py b/pylib/gyp/mac_tool.py
index e5d8a2b..a25754c 100755
--- a/pylib/gyp/mac_tool.py
+++ b/pylib/gyp/mac_tool.py
@@ -223,11 +223,25 @@
         r'^.*libtool: warning for library: ' +
         r'.* the table of contents is empty ' +
         r'\(no object file members in the library define global symbols\)$')
-    libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE)
+    env = os.environ.copy()
+    # Ref:
+    # http://www.opensource.apple.com/source/cctools/cctools-809/misc/libtool.c
+    # The problem with this flag is that it resets the file mtime on the file to
+    # epoch=0, e.g. 1970-1-1 or 1969-12-31 depending on daylight saving.
+    env['ZERO_AR_DATE'] = '1'
+    libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env)
     _, err = libtoolout.communicate()
     for line in err.splitlines():
       if not libtool_re.match(line) and not libtool_re5.match(line):
         print >>sys.stderr, line
+    # Unconditionally touch any file .a file on the command line if present if
+    # succeeded. A bit hacky.
+    if not libtoolout.returncode:
+      archives = [
+        cmd for cmd in cmd_list if cmd.endswith('.a') and os.path.isfile(cmd)
+      ]
+      if len(archives) == 1:
+        os.utime(archives[0], None)
     return libtoolout.returncode
 
   def ExecPackageFramework(self, framework, version):
diff --git a/test/mac/gyptest-libtool-zero.py b/test/mac/gyptest-libtool-zero.py
new file mode 100644
index 0000000..ae5b7e6
--- /dev/null
+++ b/test/mac/gyptest-libtool-zero.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2014 Google Inc. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+Verifies libraries have proper mtime.
+"""
+
+import TestGyp
+
+import sys
+
+if sys.platform == 'darwin':
+  test = TestGyp.TestGyp(formats=['ninja', 'make', 'xcode'])
+
+  CHDIR = 'libtool-zero'
+
+  test.run_gyp('test.gyp', chdir=CHDIR)
+
+  test.build('test.gyp', 'mylib', chdir=CHDIR)
+
+  test.up_to_date('test.gyp', 'mylib', chdir=CHDIR)
+
+  test.pass_test()
diff --git a/test/mac/libtool-zero/mylib.c b/test/mac/libtool-zero/mylib.c
new file mode 100644
index 0000000..b26d61b
--- /dev/null
+++ b/test/mac/libtool-zero/mylib.c
@@ -0,0 +1,7 @@
+// Copyright (c) 2014 Google Inc. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+int my_foo(int x) {
+  return x + 1;
+}
diff --git a/test/mac/libtool-zero/test.gyp b/test/mac/libtool-zero/test.gyp
new file mode 100644
index 0000000..2f2c3f1
--- /dev/null
+++ b/test/mac/libtool-zero/test.gyp
@@ -0,0 +1,15 @@
+# Copyright (c) 2014 Google Inc. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+{
+  'targets': [
+    {
+      'target_name': 'mylib',
+      'type': 'static_library',
+      'sources': [
+        'mylib.c',
+      ],
+    },
+  ],
+}