Add GN args for using orderfile for Chrome OS

This patch adds GN args to use an orderfile for linking Chrome.
By default, it's pointing a path will be updated by autoroller.
Chrome ebuild can also override the path to test a custom orderfile.

This patch also consolidates the efforts across Android and Windows,
to have a configuration of using orderfile in build/config/compiler.

Bug: chromium:950627
Test: Chrome builds locally on Chrome OS. Android and Windows is not tested.

Change-Id: If9fd06368da81e02488cb991c3b7490e6ed91363
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1672231
Commit-Queue: Tiancong Wang <tcwang@google.com>
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: Brian Sheedy <bsheedy@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#675412}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 1189f5932fc0f2e67d332a98d9dc83b7c5172508
diff --git a/compiler/BUILD.gn b/compiler/BUILD.gn
index e50ac81..f560cf1 100644
--- a/compiler/BUILD.gn
+++ b/compiler/BUILD.gn
@@ -177,6 +177,26 @@
       (use_gold || use_lld) && cc_wrapper == ""
 }
 
+if (is_win || is_android || is_chromeos) {
+  # Set the path to use orderfile for linking Chrome
+  # Note that this is for using only one orderfile for linking
+  # the Chrome binary/library.
+  declare_args() {
+    chrome_orderfile_path = ""
+
+    if (defined(default_chrome_orderfile)) {
+      # Allow downstream tools to set orderfile path with
+      # another variable.
+      chrome_orderfile_path = default_chrome_orderfile
+    } else if (is_win && is_clang && is_official_build) {
+      chrome_orderfile_path = "build/chrome.$target_cpu.orderfile"
+    } else if (is_chromeos) {
+      # FIXME(tcwang): update this with autoroller.
+      chrome_orderfile_path = ""
+    }
+  }
+}
+
 # default_include_dirs ---------------------------------------------------------
 #
 # This is a separate config so that third_party code (which would not use the
@@ -2430,3 +2450,45 @@
     cflags_objcc = common_flags
   }
 }
+
+if (is_chromeos) {
+  # This config is intended to be a temporary to facilitate
+  # the transition to use orderfile in Chrome OS. Once orderfile
+  # use becomes a default in Chrome OS, this config should not
+  # be needed.
+  config("use_orderfile_for_hugepage") {
+    if (chrome_orderfile_path != "") {
+      defines = [ "CHROMEOS_ORDERFILE_USE" ]
+    }
+  }
+}
+
+if (is_win || is_android || is_chromeos) {
+  # Use orderfile for linking Chrome on win, android, and Chrome OS.
+  # This config enables using an orderfile for linking in LLD.
+  config("chrome_orderfile_config") {
+    assert(use_lld)
+    if (chrome_orderfile_path != "") {
+      _rebased_orderfile = rebase_path(chrome_orderfile_path, root_build_dir)
+      if (is_android || is_chromeos) {
+        ldflags = [
+          "-Wl,--symbol-ordering-file",
+          "-Wl,$_rebased_orderfile",
+          "-Wl,--no-warn-symbol-ordering",
+        ]
+      } else {
+        ldflags = [
+          "/order:@$rebased_orderfile",
+
+          # Ignore warnings about missing functions or functions not in their
+          # own section.
+          "/ignore:4037",
+          "/ignore:4065",
+        ]
+      }
+      inputs = [
+        chrome_orderfile_path,
+      ]
+    }
+  }
+}