GN: aix port along with linux_s390x, linux_ppc64 and linux_ppc64le support.

Most of the porting involves files inside /base.

This also fixes build/build_config.h not identifying PPC architectures correctly. Finally, it adds aix support to files inside tools/gn/, including the bootstrap script.

R=machenbach@chromium.org, dpranke@chromium.org, adamk@chromium.org
BUG=706728

Review-Url: https://codereview.chromium.org/2807463004
Cr-Original-Commit-Position: refs/heads/master@{#467484}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 0088ee5017f4bef6c27243a54d8998f993db11b8
diff --git a/tools/gn/args.cc b/tools/gn/args.cc
index 3bb40b4..800996b 100644
--- a/tools/gn/args.cc
+++ b/tools/gn/args.cc
@@ -302,6 +302,8 @@
   os = "android";
 #elif defined(OS_NETBSD)
   os = "netbsd";
+#elif defined(OS_AIX)
+  os = "aix";
 #else
   #error Unknown OS type.
 #endif
@@ -331,7 +333,10 @@
     arch = kMips;
   else if (os_arch == "s390x")
     arch = kS390X;
-  else if (os_arch == "mips")
+  else if (os_arch == "ppc64" || os_arch == "ppc64le")
+    // We handle the endianness inside //build/config/host_byteorder.gni.
+    // This allows us to use the same toolchain as ppc64 BE
+    // and specific flags are included using the host_byteorder logic.
     arch = kPPC64;
   else
     CHECK(false) << "OS architecture not handled. (" << os_arch << ")";
diff --git a/tools/gn/bootstrap/bootstrap.py b/tools/gn/bootstrap/bootstrap.py
index 5b71d94..9b5bbcc 100755
--- a/tools/gn/bootstrap/bootstrap.py
+++ b/tools/gn/bootstrap/bootstrap.py
@@ -32,7 +32,8 @@
 is_win = sys.platform.startswith('win')
 is_linux = sys.platform.startswith('linux')
 is_mac = sys.platform.startswith('darwin')
-is_posix = is_linux or is_mac
+is_aix = sys.platform.startswith('aix')
+is_posix = is_linux or is_mac or is_aix
 
 def check_call(cmd, **kwargs):
   logging.debug('Running: %s', ' '.join(cmd))
@@ -223,6 +224,8 @@
     template_filename = 'build_vs.ninja.template'
   elif is_mac:
     template_filename = 'build_mac.ninja.template'
+  elif is_aix:
+    template_filename = 'build_aix.ninja.template'
   else:
     template_filename = 'build.ninja.template'
 
@@ -298,6 +301,11 @@
     cxx = os.environ.get('CXX', 'cl.exe')
     ld = os.environ.get('LD', 'link.exe')
     ar = os.environ.get('AR', 'lib.exe')
+  elif is_aix:
+    cc = os.environ.get('CC', 'gcc')
+    cxx = os.environ.get('CXX', 'c++')
+    ld = os.environ.get('LD', cxx)
+    ar = os.environ.get('AR', 'ar -X64')
   else:
     cc = os.environ.get('CC', 'cc')
     cxx = os.environ.get('CXX', 'c++')
@@ -318,6 +326,12 @@
     if options.debug:
       cflags.extend(['-O0', '-g'])
     else:
+      # The linux::ppc64 BE binary doesn't "work" when
+      # optimization level is set to 2 (0 works fine).
+      # Note that the current bootstrap script has no way to detect host_cpu.
+      # This can be easily fixed once we start building using a GN binary,
+      # as the optimization flag can then just be set using the
+      # logic inside //build/toolchain.
       cflags.extend(['-O2', '-g0'])
 
     cflags.extend([
@@ -328,6 +342,9 @@
         '-fno-exceptions'
     ])
     cflags_cc.extend(['-std=c++11', '-Wno-c++11-narrowing'])
+    if is_aix:
+     cflags.extend(['-maix64'])
+     ldflags.extend([ '-maix64 -Wl,-bbigtoc' ])
   elif is_win:
     if not options.debug:
       cflags.extend(['/Ox', '/DNDEBUG', '/GL'])
@@ -503,6 +520,10 @@
       'base/threading/thread_restrictions.cc',
       'base/threading/thread_task_runner_handle.cc',
       'base/threading/worker_pool.cc',
+      'base/time/clock.cc',
+      'base/time/default_clock.cc',
+      'base/time/default_tick_clock.cc',
+      'base/time/tick_clock.cc',
       'base/time/time.cc',
       'base/timer/elapsed_timer.cc',
       'base/timer/timer.cc',
@@ -598,8 +619,7 @@
         'cflags': cflags + ['-DHAVE_CONFIG_H'],
     }
 
-  if is_linux:
-    libs.extend(['-lrt', '-latomic'])
+  if is_linux or is_aix:
     ldflags.extend(['-pthread'])
 
     static_libraries['xdg_user_dirs'] = {
@@ -609,8 +629,6 @@
         'tool': 'cxx',
     }
     static_libraries['base']['sources'].extend([
-        'base/allocator/allocator_shim.cc',
-        'base/allocator/allocator_shim_default_dispatch_to_glibc.cc',
         'base/memory/shared_memory_posix.cc',
         'base/memory/shared_memory_tracker.cc',
         'base/nix/xdg_util.cc',
@@ -626,13 +644,29 @@
         'base/threading/platform_thread_linux.cc',
         'base/trace_event/malloc_dump_provider.cc',
     ])
-    static_libraries['libevent']['include_dirs'].extend([
-        os.path.join(SRC_ROOT, 'base', 'third_party', 'libevent', 'linux')
-    ])
-    static_libraries['libevent']['sources'].extend([
-        'base/third_party/libevent/epoll.c',
-    ])
-
+    if is_linux:
+      static_libraries['base']['sources'].extend([
+        'base/allocator/allocator_shim.cc',
+        'base/allocator/allocator_shim_default_dispatch_to_glibc.cc',
+      ])
+      libs.extend(['-lrt', '-latomic'])
+      static_libraries['libevent']['include_dirs'].extend([
+          os.path.join(SRC_ROOT, 'base', 'third_party', 'libevent', 'linux')
+      ])
+      static_libraries['libevent']['sources'].extend([
+         'base/third_party/libevent/epoll.c',
+      ])
+    else:
+      libs.extend(['-lrt'])
+      static_libraries['base']['sources'].extend([
+          'base/process/internal_aix.cc'
+      ])
+      static_libraries['libevent']['include_dirs'].extend([
+          os.path.join(SRC_ROOT, 'base', 'third_party', 'libevent', 'aix')
+      ])
+      static_libraries['libevent']['include_dirs'].extend([
+          os.path.join(SRC_ROOT, 'base', 'third_party', 'libevent', 'compat')
+      ])
 
   if is_mac:
     static_libraries['base']['sources'].extend([
diff --git a/tools/gn/bootstrap/build_aix.ninja.template b/tools/gn/bootstrap/build_aix.ninja.template
new file mode 100644
index 0000000..5cd36d6
--- /dev/null
+++ b/tools/gn/bootstrap/build_aix.ninja.template
@@ -0,0 +1,19 @@
+rule cc
+  command = $cc -MMD -MF $out.d $defines $includes $cflags $cflags_c -c $in -o $out
+  description = CC $out
+  depfile = $out.d
+  deps = gcc
+
+rule cxx
+  command = $cxx -MMD -MF $out.d $defines $includes $cflags $cflags_cc -c $in -o $out
+  description = CXX $out
+  depfile = $out.d
+  deps = gcc
+
+rule alink_thin
+  command = rm -f $out && $ar rcsT $out $in
+  description = AR $out
+
+rule link
+  command = $ld $ldflags -o $out $in $libs $solibs
+  description = LINK $out