GN: add extra_cflags et al.
authormtklein <mtklein@chromium.org>
Tue, 16 Aug 2016 16:31:16 +0000 (09:31 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 16 Aug 2016 16:31:16 +0000 (09:31 -0700)
Adding flags to the end of cc or cxx is pretty useful, but these always end up
on the command line before the GN generated flags, thus setting defaults that
GN will override.

For full flexibility we want to be able to add flags after the flags GN has
added, so that custom flags can override _it_.

I've updated the Fast bots with an example here: if we said cc="clang -O3 ...",
that '-O3' would be overriden later by the default Release-mode '-Os'.  By
putting it in extra_cflags, we get the last word: our '-O3' overrides the
default '-Os'.

Another good use case is a hypothetical Actually-Shippable-Release mode.  Our
Release mode bundles in tons of debug symbols via '-g'.  libskia.a is about 10x
larger than it needs to be when built that way, but it helps us debug the bot
failures immensely.  To build a libskia.{a,so} that you'd really ship, you can
now set extra_cflags="-g0" to override '-g'.  You could set '-march' flags there
too, '-fomit-frame-pointer', etc.

There are lots of flags that won't matter where they end up in the command line.
To keep everything simple I've put them in extra_cflags with the rest.  This means
the only time we change 'cc' or 'cxx' in our recipes is to prefix 'ccache'.

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

Review-Url: https://codereview.chromium.org/2241263003

gn/BUILD.gn
infra/bots/recipe_modules/flavor/gn_flavor.py
infra/bots/recipes/swarm_compile.expected/Build-Ubuntu-Clang-x86_64-Debug-GN.json
infra/bots/recipes/swarm_compile.expected/Build-Ubuntu-GCC-x86_64-Debug-GN.json
infra/bots/recipes/swarm_compile.expected/Build-Ubuntu-GCC-x86_64-Debug-SK_USE_DISCARDABLE_SCALEDIMAGECACHE.json
infra/bots/recipes/swarm_compile.expected/Build-Ubuntu-GCC-x86_64-Release-Fast.json
infra/bots/recipes/swarm_compile.expected/Build-Win-MSVC-x86-Release-GN.json

index 1f7bd82..26e80f8 100644 (file)
@@ -7,6 +7,10 @@ declare_args() {
   ar = "ar"
   cc = "cc"
   cxx = "c++"
+
+  extra_cflags = ""
+  extra_cflags_c = ""
+  extra_cflags_cc = ""
 }
 
 config("no_rtti") {
@@ -64,7 +68,7 @@ toolchain("gcc_like") {
 
   tool("cc") {
     depfile = "{{output}}.d"
-    command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
+    command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} $extra_cflags $extra_cflags_c -c {{source}} -o {{output}}"
     depsformat = "gcc"
     outputs = [
       "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
@@ -74,7 +78,7 @@ toolchain("gcc_like") {
 
   tool("cxx") {
     depfile = "{{output}}.d"
-    command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
+    command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} $extra_cflags $extra_cflags_cc -c {{source}} -o {{output}}"
     depsformat = "gcc"
     outputs = [
       "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
index ef25896..75c318f 100644 (file)
@@ -21,12 +21,8 @@ class GNFlavorUtils(default_flavor.DefaultFlavorUtils):
     configuration = self.m.vars.builder_cfg.get('configuration', '')
     extra_config  = self.m.vars.builder_cfg.get('extra_config',  '')
 
-    gn_args = []
-    if configuration != 'Debug':
-      gn_args.append('is_debug=false')
-
     cc, cxx = 'cc', 'c++'
-    cflags = []
+    extra_cflags = []
 
     if compiler == 'Clang':
       cc, cxx = 'clang', 'clang++'
@@ -38,19 +34,24 @@ class GNFlavorUtils(default_flavor.DefaultFlavorUtils):
       cc, cxx = '%s %s' % (ccache, cc), '%s %s' % (ccache, cxx)
       if compiler == 'Clang':
         # Stifle "argument unused during compilation: ..." warnings.
-        cflags.append('-Qunused-arguments')
+        extra_cflags.append('-Qunused-arguments')
 
     if extra_config == 'Fast':
-      cflags.extend(['-march=native', '-fomit-frame-pointer'])
+      extra_cflags.extend(['-march=native', '-fomit-frame-pointer', '-O3'])
     if extra_config.startswith('SK'):
-      cflags.append('-D' + extra_config)
+      extra_cflags.append('-D' + extra_config)
 
-    cflags = ' '.join(cflags)
-    gn_args += [ 'cc="%s %s"' % (cc, cflags), 'cxx="%s %s"' % (cxx, cflags) ]
+    quote = lambda x: '"%s"' % x
+    gn_args = ' '.join('%s=%s' % (k,v) for (k,v) in {
+        'cc': quote(cc),
+        'cxx': quote(cxx),
+        'extra_cflags': quote(' '.join(extra_cflags)),
+        'is_debug': 'true' if configuration == 'Debug' else 'false',
+    }.iteritems())
 
     run = lambda title, cmd: self.m.run(self.m.step, title, cmd=cmd,
                                         cwd=self.m.vars.skia_dir, **kwargs)
 
     run('fetch-gn', [self.m.vars.skia_dir.join('bin', 'fetch-gn')])
-    run('gn gen', ['gn', 'gen', self.out_dir, '--args=%s' % ' '.join(gn_args)])
+    run('gn gen', ['gn', 'gen', self.out_dir, '--args=' + gn_args])
     run('ninja', ['ninja', '-C', self.out_dir])
index 40c9a2b..3e3ef0e 100644 (file)
       "gn",
       "gen",
       "[CUSTOM_/_B_WORK]/skia/out/Build-Ubuntu-Clang-x86_64-Debug-GN/Debug",
-      "--args=cc=\"/usr/bin/ccache clang -Qunused-arguments\" cxx=\"/usr/bin/ccache clang++ -Qunused-arguments\""
+      "--args=cc=\"/usr/bin/ccache clang\" cxx=\"/usr/bin/ccache clang++\" is_debug=true extra_cflags=\"-Qunused-arguments\""
     ],
     "cwd": "[CUSTOM_/_B_WORK]/skia",
     "env": {
index 5ad5558..10ff8c3 100644 (file)
       "gn",
       "gen",
       "[CUSTOM_/_B_WORK]/skia/out/Build-Ubuntu-GCC-x86_64-Debug-GN/Debug",
-      "--args=cc=\"/usr/bin/ccache gcc \" cxx=\"/usr/bin/ccache g++ \""
+      "--args=cc=\"/usr/bin/ccache gcc\" cxx=\"/usr/bin/ccache g++\" is_debug=true extra_cflags=\"\""
     ],
     "cwd": "[CUSTOM_/_B_WORK]/skia",
     "env": {
index 5d0dd78..12314f8 100644 (file)
       "gn",
       "gen",
       "[CUSTOM_/_B_WORK]/skia/out/Build-Ubuntu-GCC-x86_64-Debug-SK_USE_DISCARDABLE_SCALEDIMAGECACHE/Debug",
-      "--args=cc=\"gcc -DSK_USE_DISCARDABLE_SCALEDIMAGECACHE\" cxx=\"g++ -DSK_USE_DISCARDABLE_SCALEDIMAGECACHE\""
+      "--args=cc=\"gcc\" cxx=\"g++\" is_debug=true extra_cflags=\"-DSK_USE_DISCARDABLE_SCALEDIMAGECACHE\""
     ],
     "cwd": "[CUSTOM_/_B_WORK]/skia",
     "env": {
index 5dd7797..091b29e 100644 (file)
       "gn",
       "gen",
       "[CUSTOM_/_B_WORK]/skia/out/Build-Ubuntu-GCC-x86_64-Release-Fast/Release",
-      "--args=is_debug=false cc=\"gcc -march=native -fomit-frame-pointer\" cxx=\"g++ -march=native -fomit-frame-pointer\""
+      "--args=cc=\"gcc\" cxx=\"g++\" is_debug=false extra_cflags=\"-march=native -fomit-frame-pointer -O3\""
     ],
     "cwd": "[CUSTOM_/_B_WORK]/skia",
     "env": {
index 1cd02d0..fe8c242 100644 (file)
@@ -93,7 +93,7 @@
       "gn",
       "gen",
       "[CUSTOM_C:\\_B_WORK]\\skia\\out\\Build-Win-MSVC-x86-Release-GN\\Release",
-      "--args=is_debug=false cc=\"cc \" cxx=\"c++ \""
+      "--args=cc=\"cc\" cxx=\"c++\" is_debug=false extra_cflags=\"\""
     ],
     "cwd": "[CUSTOM_C:\\_B_WORK]\\skia",
     "env": {