GN build fixes, mostly for Mac.

This hooks up detection for the "-arch" flag on Mac to set the GYP "ARCH" xcode variable. GN then removes the -arch argument from the compiler args, since GYP will then re-add it based on the ARCH value. Previously, not doing this resulting in mutliple "-arch" arguments to the compiler since GYP would always insert its own.

Disables some warnings on Windows for the re2 target to match the GYP build. The third warning (4018) that the GYP build sets is disabled globally so there's no need to do it for this target.

Hooks up some iOS SDK stuff.
BUG=336667
TBR=thakis@chromium.org

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

Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 5aa8210fe7b367f2fc6eeaf8a8e36ff1a0658bde
diff --git a/tools/gn/gyp_binary_target_writer.cc b/tools/gn/gyp_binary_target_writer.cc
index e32c60b..cb6b4e0 100644
--- a/tools/gn/gyp_binary_target_writer.cc
+++ b/tools/gn/gyp_binary_target_writer.cc
@@ -7,6 +7,7 @@
 #include <set>
 
 #include "base/logging.h"
+#include "base/strings/string_util.h"
 #include "tools/gn/builder_record.h"
 #include "tools/gn/config_values_extractors.h"
 #include "tools/gn/err.h"
@@ -66,6 +67,32 @@
   return "'2'";  // Default value.
 }
 
+// Returns the value from the already-filled in cflags for the processor
+// architecture to set in the GYP file. Additionally, this removes the flag
+// from the given vector so we don't get duplicates.
+std::string GetMacArch(std::vector<std::string>* cflags) {
+  // Searches for the "-arch" option and returns the corresponding GYP value.
+  for (size_t i = 0; i < cflags->size(); i++) {
+    const std::string& cur = (*cflags)[i];
+    if (cur == "-arch") {
+      // This is the first part of a list with ["-arch", "i386"], return the
+      // following item, and delete both of them.
+      if (i < cflags->size() - 1) {
+        std::string ret = (*cflags)[i + 1];
+        cflags->erase(cflags->begin() + i, cflags->begin() + i + 2);
+        return ret;
+      }
+    } else if (StartsWithASCII(cur, "-arch ", true)) {
+      // The arch was passed as one GN string value, e.g. "-arch i386". Return
+      // the stuff following the space and delete the item.
+      std::string ret = cur.substr(6);
+      cflags->erase(cflags->begin() + i);
+      return ret;
+    }
+  }
+  return std::string();
+}
+
 // Finds all values from the given getter from all configs in the given list,
 // and adds them to the given result vector.
 template<typename T>
@@ -350,6 +377,15 @@
 
   Indent(indent) << "'xcode_settings': {\n";
 
+  // Architecture. GYP uses this to write the -arch flag passed to the
+  // compiler, it doesn't look at our -arch flag. So we need to specify it in
+  // this special var and not in the cflags to avoid duplicates or conflicts.
+  std::string arch = GetMacArch(&flags.cflags);
+  if (arch == "i386")
+    Indent(indent + kExtraIndent) << "'ARCHS': [ 'i386' ],\n";
+  else if (arch == "x86_64")
+    Indent(indent + kExtraIndent) << "'ARCHS': [ 'x86_64' ],\n";
+
   // C/C++ flags.
   if (!flags.cflags.empty() || !flags.cflags_c.empty() ||
       !flags.cflags_objc.empty()) {