GN: add skia_use_mesa for OSMesa in tools.

I tested by running
  $ out/dm --src gm --config mesa
  $ out/nanobench --config mesa --match Xfermode
... but I ran out of patience waiting for nanobench to finish.  Probably works.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2357703002

Review-Url: https://codereview.chromium.org/2357703002
diff --git a/BUILD.gn b/BUILD.gn
index 3c6ac0f..d9f7d79 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -13,6 +13,7 @@
   skia_use_libjpeg_turbo = true
   skia_use_libpng = true
   skia_use_libwebp = !is_fuchsia
+  skia_use_mesa = false
   skia_use_sfntly = !is_fuchsia
   skia_use_vulkan = is_android && defined(ndk_api) && ndk_api >= 24
   skia_use_zlib = true
@@ -595,6 +596,9 @@
   template("test_lib") {
     config(target_name + "_config") {
       include_dirs = invoker.public_include_dirs
+      if (defined(invoker.public_defines)) {
+        defines = invoker.public_defines
+      }
     }
     source_set(target_name) {
       forward_variables_from(invoker, "*", [ "public_include_dirs" ])
@@ -612,6 +616,7 @@
   }
 
   test_lib("gpu_tool_utils") {
+    public_defines = []
     public_include_dirs = [ "tools/gpu" ]
     sources = [
       "tools/gpu/GrContextFactory.cpp",
@@ -640,6 +645,11 @@
     if (skia_use_vulkan) {
       sources += [ "tools/gpu/vk/VkTestContext.cpp" ]
     }
+    if (skia_use_mesa) {
+      public_defines += [ "SK_MESA" ]
+      sources += [ "tools/gpu/gl/mesa/GLTestContext_mesa.cpp" ]
+      libs += [ "OSMesa" ]
+    }
   }
 
   test_lib("flags") {
diff --git a/infra/bots/recipe_modules/flavor/gn_flavor.py b/infra/bots/recipe_modules/flavor/gn_flavor.py
index ba37e17..c91de14 100644
--- a/infra/bots/recipe_modules/flavor/gn_flavor.py
+++ b/infra/bots/recipe_modules/flavor/gn_flavor.py
@@ -16,6 +16,7 @@
       'SAN' in extra_config,
       extra_config == 'Fast',
       extra_config == 'GN',
+      extra_config == 'Mesa',
       extra_config == 'NoGPU',
       extra_config.startswith('SK'),
       os == 'Ubuntu' and target_arch == 'x86',
@@ -64,6 +65,8 @@
       args['is_debug'] = 'false'
     if extra_config == 'MSAN':
       args['skia_use_fontconfig'] = 'false'
+    if extra_config == 'Mesa':
+      args['skia_use_mesa'] = 'true'
     if extra_config == 'NoGPU':
       args['skia_enable_gpu'] = 'false'
 
diff --git a/infra/bots/recipes/swarm_compile.expected/Build-Ubuntu-GCC-x86_64-Release-Mesa.json b/infra/bots/recipes/swarm_compile.expected/Build-Ubuntu-GCC-x86_64-Release-Mesa.json
index 03253fb..59fe37f 100644
--- a/infra/bots/recipes/swarm_compile.expected/Build-Ubuntu-GCC-x86_64-Release-Mesa.json
+++ b/infra/bots/recipes/swarm_compile.expected/Build-Ubuntu-GCC-x86_64-Release-Mesa.json
@@ -121,19 +121,38 @@
   },
   {
     "cmd": [
-      "make",
-      "most"
+      "[CUSTOM_/_B_WORK]/skia/bin/fetch-gn"
     ],
     "cwd": "[CUSTOM_/_B_WORK]/skia",
     "env": {
-      "BUILDTYPE": "Release",
-      "CHROME_HEADLESS": "1",
-      "CHROME_PATH": "[SLAVE_BUILD]/src",
-      "GYP_DEFINES": "skia_arch_type=x86_64 skia_mesa=1 skia_warnings_as_errors=1",
-      "PATH": "%(PATH)s:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]",
-      "SKIA_OUT": "[CUSTOM_/_B_WORK]/skia/out/Build-Ubuntu-GCC-x86_64-Release-Mesa"
+      "PATH": "%(PATH)s:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]"
     },
-    "name": "build most"
+    "name": "fetch-gn"
+  },
+  {
+    "cmd": [
+      "gn",
+      "gen",
+      "[CUSTOM_/_B_WORK]/skia/out/Build-Ubuntu-GCC-x86_64-Release-Mesa/Release",
+      "--args=cc=\"gcc\" cxx=\"g++\" is_debug=false skia_use_mesa=true"
+    ],
+    "cwd": "[CUSTOM_/_B_WORK]/skia",
+    "env": {
+      "PATH": "%(PATH)s:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]"
+    },
+    "name": "gn gen"
+  },
+  {
+    "cmd": [
+      "ninja",
+      "-C",
+      "[CUSTOM_/_B_WORK]/skia/out/Build-Ubuntu-GCC-x86_64-Release-Mesa/Release"
+    ],
+    "cwd": "[CUSTOM_/_B_WORK]/skia",
+    "env": {
+      "PATH": "%(PATH)s:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]:RECIPE_PACKAGE_REPO[depot_tools]"
+    },
+    "name": "ninja"
   },
   {
     "cmd": [