summaryrefslogtreecommitdiffstats
path: root/build/config/compiler/BUILD.gn
blob: 1a7df3025fa011bbc8e1c257f3169229c99594ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/android/config.gni")
import("//build/config/chrome_build.gni")
if (current_cpu == "arm") {
  import("//build/config/arm.gni")
}
if (current_cpu == "mipsel" || current_cpu == "mips64el") {
  import("//build/config/mips.gni")
}
if (is_posix) {
  import("//build/config/gcc/gcc_version.gni")
}
if (is_win) {
  import("//build/config/win/visual_studio_version.gni")
}

import("//build/toolchain/ccache.gni")
import("//build/config/sanitizers/sanitizers.gni")

declare_args() {
  # Normally, Android builds are lightly optimized, even for debug builds, to
  # keep binary size down. Setting this flag to true disables such optimization
  android_full_debug = false

  # Whether to use the binary binutils checked into third_party/binutils.
  # These are not multi-arch so cannot be used except on x86 and x86-64 (the
  # only two architectures that are currently checked in). Turn this off when
  # you are using a custom toolchain and need to control -B in cflags.
  linux_use_bundled_binutils = is_linux && current_cpu == "x64"
  binutils_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
                              root_build_dir)

  # Compile in such a way as to enable profiling of the generated code. For
  # example, don't omit the frame pointer and leave in symbols.
  enable_profiling = false

  # Compile in such a way as to make it possible for the profiler to unwind full
  # stack frames. Setting this flag has a large effect on the performance of the
  # generated code than just setting profiling, but gives the profiler more
  # information to analyze.
  # Requires profiling to be set to true.
  enable_full_stack_frames_for_profiling = false

  # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
  # address space, and it doesn't support cross-compiling).
  use_gold = is_linux && current_cpu == "x64"

  # When we are going to use gold we need to find it.
  if (use_gold) {
    gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
                            root_build_dir)
  } else {
    gold_path = ""
  }

  # use_debug_fission: whether to use split DWARF debug info
  # files. This can reduce link time significantly, but is incompatible
  # with some utilities such as icecc and ccache. Requires gold and
  # gcc >= 4.8 or clang.
  # http://gcc.gnu.org/wiki/DebugFission
  use_debug_fission = is_debug && !is_win && use_gold &&
                      linux_use_bundled_binutils && !use_ccache

  if (is_win) {
    # Whether the VS xtree header has been patched to disable warning 4702. If
    # it has, then we don't need to disable 4702 (unreachable code warning).
    # The patch is preapplied to the internal toolchain and hence all bots.
    msvs_xtree_patched = false
  }
}

# default_include_dirs ---------------------------------------------------------
#
# This is a separate config so that third_party code (which would not use the
# source root and might have conflicting versions of some headers) can remove
# this and specify their own include paths.
config("default_include_dirs") {
  include_dirs = [
    "//",
    root_gen_dir,
  ]
}

# TODO(GYP) bug 527515: is_ubsan, is_ubsan_vptr
if (!is_win) {
  using_sanitizer = is_asan || is_lsan || is_tsan || is_msan
}

# compiler ---------------------------------------------------------------------
#
# Base compiler configuration.
#
# See also "runtime_library" below for related stuff and a discussion about
# where stuff should go. Put warning related stuff in the "warnings" config.

config("compiler") {
  cflags = []
  cflags_c = []
  cflags_cc = []
  cflags_objc = []
  cflags_objcc = []
  ldflags = []
  defines = []

  # In general, Windows is totally different, but all the other builds share
  # some common GCC configuration. This section sets up Windows and the common
  # GCC flags, and then we handle the other non-Windows platforms specifically
  # below.
  if (is_win) {
    # Windows compiler flags setup.
    # -----------------------------
    cflags += [
      "/Gy",  # Enable function-level linking.
      "/GS",  # Enable buffer security checking.
      "/FS",  # Preserve previous PDB behavior.
      "/bigobj",  # Some of our files are bigger than the regular limits.
    ]

    if (visual_studio_version == "2015") {
      # Work around crbug.com/526851, bug in VS 2015 RTM compiler.
      cflags += [ "/Zc:sizedDealloc-" ]
    }

    # Force C/C++ mode for the given GN detected file type. This is necessary
    # for precompiled headers where the same source file is compiled in both
    # modes.
    cflags_c += [ "/TC" ]
    cflags_cc += [ "/TP" ]

    # Building with Clang on Windows is a work in progress and very
    # experimental. See crbug.com/82385.
    # Keep this in sync with the similar block in build/common.gypi
    if (is_clang) {
      cflags += [
        # Many files use intrinsics without including this header.
        # TODO(hans): Fix those files, or move this to sub-GYPs.
        "/FIIntrin.h",
      ]

      if (visual_studio_version == "2013") {
        cflags += [ "-fmsc-version=1800" ]
      } else if (visual_studio_version == "2015") {
        cflags += [ "-fmsc-version=1900" ]
      }

      if (current_cpu == "x86") {
        cflags += [ "-m32" ]
      } else {
        cflags += [ "-m64" ]
      }

      if (exec_script("//build/win/use_ansi_codes.py", [], "trim string") ==
          "True") {
        cflags += [
          # cmd.exe doesn't understand ANSI escape codes by default,
          # so only enable them if something emulating them is around.
          "-fansi-escape-codes",
        ]
      }
    }

    if (is_syzyasan) {
      # SyzyAsan needs /PROFILE turned on to produce appropriate pdbs.
      assert(!is_win_fastlink, "/PROFILE and /DEBUG:FASTLINK are incompatible")
      ldflags += [ "/PROFILE" ]
    }
  } else {
    # Common GCC compiler flags setup.
    # --------------------------------
    cflags += [ "-fno-strict-aliasing" ]  # See http://crbug.com/32204
    cflags_cc += [
      "-fno-threadsafe-statics",

      # Not exporting C++ inline functions can generally be applied anywhere
      # so we do so here. Normal function visibility is controlled by
      # //build/config/gcc:symbol_visibility_hidden.
      "-fvisibility-inlines-hidden",
    ]

    # Stack protection.
    if (is_mac) {
      cflags += [ "-fstack-protector-all" ]
    } else if (is_posix && !is_chromeos && !is_nacl) {
      # TODO(phajdan.jr): Use -fstack-protector-strong when our gcc supports it.
      cflags += [ "--param=ssp-buffer-size=4" ]
      if (is_android && (current_cpu == "arm64" || current_cpu == "x86")) {
        cflags += [ "-fno-stack-protector" ]
      } else {
        cflags += [ "-fstack-protector" ]
      }
    }

    # Linker warnings.
    if (!(is_chromeos && current_cpu == "arm") && !is_mac && !is_ios) {
      # TODO(jochen): Enable this on chromeos on arm. http://crbug.com/356580
      ldflags += [ "-Wl,--fatal-warnings" ]
    }

    # Common options for AddressSanitizer, LeakSanitizer, ThreadSanitizer,
    # MemorySanitizer and non-official CFI builds.
    if (using_sanitizer || (is_cfi && !is_official_build)) {
      cflags += [
        "-fno-omit-frame-pointer",
        "-gline-tables-only",
      ]
    }
    if (is_asan) {
      asan_blacklist_path =
          rebase_path("//tools/memory/asan/blacklist.txt", root_build_dir)
      cflags += [
        "-fsanitize=address",
        "-fsanitize-blacklist=$asan_blacklist_path",
      ]
      if (is_mac) {
        cflags += [ "-mllvm -asan-globals=0" ]  # http://crbug.com/352073
        # TODO(GYP): deal with mac_bundles.
      }
    }
    if (is_lsan) {
      cflags += [ "-fsanitize=leak" ]
    }
    if (is_tsan) {
      tsan_blacklist_path =
          rebase_path("//tools/memory/tsan_v2/ignores.txt", root_build_dir)
      cflags += [
        "-fsanitize=thread",
        "-fsanitize-blacklist=$tsan_blacklist_path",
      ]
    }
    if (is_msan) {
      msan_blacklist_path =
          rebase_path("//tools/msan/blacklist.txt", root_build_dir)
      cflags += [
        "-fsanitize=memory",
        "-fsanitize-memory-track-origins=$msan_track_origins",
        "-fsanitize-blacklist=$msan_blacklist_path",
      ]
    }
    if (is_cfi && !is_nacl) {
      cfi_blacklist_path =
          rebase_path("//tools/cfi/blacklist.txt", root_build_dir)
      cflags += [
        "-flto",
        "-fsanitize=cfi-vcall",
        "-fsanitize=cfi-derived-cast",
        "-fsanitize=cfi-unrelated-cast",
        "-fsanitize-blacklist=$cfi_blacklist_path",
      ]
      ldflags += [
        "-flto",
        "-fsanitize=cfi-vcall",
        "-fsanitize=cfi-derived-cast",
        "-fsanitize=cfi-unrelated-cast",
      ]

      # Apply a lower LTO optimization level in non-official builds.
      if (!is_official_build) {
        if (is_linux) {
          ldflags += [ "-Wl,-plugin-opt,O1" ]
        } else if (is_mac) {
          ldflags += [ "-Wl,-mllvm,-O1" ]
        }
      }

      # Work-around for http://openradar.appspot.com/20356002
      if (is_mac) {
        ldflags += [ "-Wl,-all_load" ]
      }

      # Without this flag, LTO produces a .text section that is larger
      # than the maximum call displacement, preventing the linker from
      # relocating calls (http://llvm.org/PR22999).
      if (current_cpu == "arm") {
        ldflags += [ "-Wl,-plugin-opt,-function-sections" ]
      }

      if (use_cfi_diag) {
        cflags += [
          "-fno-sanitize-trap=cfi",
          "-fsanitize-recover=cfi",
        ]
        ldflags += [
          "-fno-sanitize-trap=cfi",
          "-fsanitize-recover=cfi",
        ]
      } else {
        defines += [ "CFI_ENFORCEMENT" ]
      }
    }

    if (use_custom_libcxx) {
      cflags_cc += [ "-nostdinc++" ]
      include_dirs = [
        "//buildtools/third_party/libc++/trunk/include",
        "//buildtools/third_party/libc++abi/trunk/include",
      ]
    }
  }

  if (is_clang && is_debug) {
    # Allow comparing the address of references and 'this' against 0
    # in debug builds. Technically, these can never be null in
    # well-defined C/C++ and Clang can optimize such checks away in
    # release builds, but they may be used in asserts in debug builds.
    cflags_cc += [
      "-Wno-undefined-bool-conversion",
      "-Wno-tautological-undefined-compare",
    ]
  }

  if (is_clang && !is_nacl) {
    # This is here so that all files get recompiled after a clang roll and
    # when turning clang on or off. (defines are passed via the command line,
    # and build system rebuild things when their commandline changes). Nothing
    # should ever read this define.
    defines +=
        [ "CR_CLANG_REVISION=" + exec_script("//tools/clang/scripts/update.py",
                                             [ "--print-revision" ],
                                             "trim string") ]
  }

  # Mac-specific compiler flags setup.
  # ----------------------------------
  if (is_mac || is_ios) {
    # These flags are shared between the C compiler and linker.
    common_mac_flags = []

    # CPU architecture.
    if (current_cpu == "x64") {
      common_mac_flags += [
        "-arch",
        "x86_64",
      ]
    } else if (current_cpu == "x86") {
      common_mac_flags += [
        "-arch",
        "i386",
      ]
    } else if (current_cpu == "arm") {
      # TODO(GYP): we may need to distinguish between "arm64", "armv7",
      # and "armv7s" for iOS, and hence need multiple current_cpu values
      # rather than just "arm".
      common_mac_flags += [
        "-arch",
        "arm64",
      ]
    }

    cflags += common_mac_flags

    # Without this, the constructors and destructors of a C++ object inside
    # an Objective C struct won't be called, which is very bad.
    cflags_objcc += [ "-fobjc-call-cxx-cdtors" ]

    cflags_c += [ "-std=c99" ]

    ldflags += common_mac_flags
  } else if (is_posix) {
    # Non-Mac Posix compiler flags setup.
    # -----------------------------------
    if (enable_profiling && !is_debug) {
      # The GYP build spams this define into every compilation unit, as we do
      # here, but it only appears to be used in base and a couple other places.
      # TODO(abarth): Should we move this define closer to where it's used?
      defines += [ "ENABLE_PROFILING" ]

      cflags += [
        "-fno-omit-frame-pointer",
        "-g",
      ]

      if (enable_full_stack_frames_for_profiling) {
        cflags += [
          "-fno-inline",
          "-fno-optimize-sibling-calls",
        ]
      }
    }

    # CPU architecture. We may or may not be doing a cross compile now, so for
    # simplicity we always explicitly set the architecture.
    if (current_cpu == "x64") {
      cflags += [
        "-m64",
        "-march=x86-64",
      ]
      ldflags += [ "-m64" ]
    } else if (current_cpu == "x86") {
      cflags += [ "-m32" ]
      ldflags += [ "-m32" ]
      if (is_clang) {
        cflags += [
          # Else building libyuv gives clang's register allocator issues,
          # see llvm.org/PR15798 / crbug.com/233709
          "-momit-leaf-frame-pointer",

          # Align the stack on 16-byte boundaries, http://crbug.com/418554.
          "-mstack-alignment=16",
          "-mstackrealign",
        ]
      }
    } else if (current_cpu == "arm") {
      cflags += [
        "-march=$arm_arch",
        "-mfloat-abi=$arm_float_abi",
      ]
      if (arm_tune != "") {
        cflags += [ "-mtune=$arm_tune" ]
      }
      if (arm_use_thumb) {
        cflags += [ "-mthumb" ]
        if (is_android && !is_clang) {  # Clang doesn't support this option.
          cflags += [ "-mthumb-interwork" ]
        }
      }
      if (!is_clang) {
        # Clang doesn't support these flags.
        cflags += [
          # The tree-sra optimization (scalar replacement for
          # aggregates enabling subsequent optimizations) leads to
          # invalid code generation when using the Android NDK's
          # compiler (r5-r7). This can be verified using
          # webkit_unit_tests' WTF.Checked_int8_t test.
          "-fno-tree-sra",

          # The following option is disabled to improve binary
          # size and performance in gcc 4.9.
          "-fno-caller-saves",
        ]
      }
    } else if (current_cpu == "mipsel") {
      if (mips_arch_variant == "r6") {
        cflags += [
          "-mips32r6",
          "-Wa,-mips32r6",
        ]
        if (is_android) {
          ldflags += [
            "-mips32r6",
            "-Wl,-melf32ltsmip",
          ]
        }
      } else if (mips_arch_variant == "r2") {
        cflags += [
          "-mips32r2",
          "-Wa,-mips32r2",
        ]
        if (mips_float_abi == "hard" && mips_fpu_mode != "") {
          cflags += [ "-m$mips_fpu_mode" ]
        }
      } else if (mips_arch_variant == "r1") {
        cflags += [
          "-mips32",
          "-Wa,-mips32",
        ]
      }

      if (mips_dsp_rev == 1) {
        cflags += [ "-mdsp" ]
      } else if (mips_dsp_rev == 2) {
        cflags += [ "-mdspr2" ]
      }

      cflags += [ "-m${mips_float_abi}-float" ]
    } else if (current_cpu == "mips64el") {
      if (mips_arch_variant == "r6") {
        cflags += [
          "-mips64r6",
          "-Wa,-mips64r6",
        ]
        ldflags += [ "-mips64r6" ]
      } else if (mips_arch_variant == "r2") {
        cflags += [
          "-mips64r2",
          "-Wa,-mips64r2",
        ]
        ldflags += [ "-mips64r2" ]
      }
    }

    defines += [ "_FILE_OFFSET_BITS=64" ]

    if (!is_android) {
      defines += [
        "_LARGEFILE_SOURCE",
        "_LARGEFILE64_SOURCE",
      ]
    }

    # Omit unwind support in official builds to save space. We can use breakpad
    # for these builds.
    if (is_chrome_branded && is_official_build) {
      cflags += [
        "-fno-unwind-tables",
        "-fno-asynchronous-unwind-tables",
      ]
      defines += [ "NO_UNWIND_TABLES" ]
    } else {
      cflags += [ "-funwind-tables" ]
    }
  }

  # Linux/Android common flags setup.
  # ---------------------------------
  if (is_linux || is_android) {
    cflags += [
      "-fPIC",
      "-pipe",  # Use pipes for communicating between sub-processes. Faster.
    ]

    ldflags += [
      "-fPIC",
      "-Wl,-z,noexecstack",
      "-Wl,-z,now",
      "-Wl,-z,relro",
    ]
    if (!using_sanitizer && !use_cfi_diag) {
      ldflags += [ "-Wl,-z,defs" ]
    }
  }

  # Linux-specific compiler flags setup.
  # ------------------------------------
  if (is_linux) {
    cflags += [ "-pthread" ]
    ldflags += [ "-pthread" ]
  }
  if (use_gold) {
    ldflags += [
      "-B$gold_path",

      # Newer gccs and clangs support -fuse-ld, use the flag to force gold
      # selection.
      # gcc -- http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Optimize-Options.html
      "-fuse-ld=gold",

      # Experimentation found that using four linking threads
      # saved ~20% of link time.
      # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
      # Only apply this to the target linker, since the host
      # linker might not be gold, but isn't used much anyway.
      # TODO(raymes): Disable threading because gold is frequently
      # crashing on the bots: crbug.com/161942.
      #"-Wl,--threads",
      #"-Wl,--thread-count=4",
    ]

    if (!is_asan && !is_msan && !is_lsan && !is_tsan) {
      # TODO(brettw) common.gypi has this only for target toolset.
      ldflags += [ "-Wl,--icf=all" ]
    }

    # TODO(thestig): Make this flag work with GN.
    #if (!is_official_build && !is_chromeos && !(is_asan || is_lsan || is_tsan || is_msan)) {
    #  ldflags += [
    #    "-Wl,--detect-odr-violations",
    #  ]
    #}
  }

  if (linux_use_bundled_binutils) {
    cflags += [ "-B$binutils_path" ]
  }

  # Clang-specific compiler flags setup.
  # ------------------------------------
  if (is_clang) {
    cflags += [ "-fcolor-diagnostics" ]
  }

  # C++11 compiler flags setup.
  # ---------------------------
  if (is_linux || is_android || (is_nacl && is_clang)) {
    # gnu++11 instead of c++11 is needed because some code uses typeof() (a
    # GNU extension).
    # TODO(thakis): Eventually switch this to c++11 instead,
    # http://crbug.com/427584
    cflags_cc += [ "-std=gnu++11" ]
  } else if (!is_win && !is_nacl) {
    # TODO(mcgrathr) - the NaCl GCC toolchain doesn't support either gnu++11
    # or c++11; we technically don't need this toolchain any more, but there
    # are still a few buildbots using it, so until those are turned off
    # we need the !is_nacl clause and the (is_nacl && is_clang) clause, above.
    cflags_cc += [ "-std=c++11" ]
  }

  # Android-specific flags setup.
  # -----------------------------
  if (is_android) {
    cflags += [
      "-ffunction-sections",
      "-funwind-tables",
      "-fno-short-enums",
    ]
    if (!is_clang) {
      # Clang doesn't support these flags.
      cflags += [ "-finline-limit=64" ]
    }
    if (is_clang) {
      rebased_android_toolchain_root =
          rebase_path(android_toolchain_root, root_build_dir)
      if (current_cpu == "arm") {
        cflags += [
          # TODO(hans) Enable integrated-as (crbug.com/124610).
          "-no-integrated-as",
          "-B${rebased_android_toolchain_root}/bin",  # Else /usr/bin/as gets picked up.
        ]
      }
    }
    if (is_asan) {
      # Android build relies on -Wl,--gc-sections removing unreachable code.
      # ASan instrumentation for globals inhibits this and results in a library
      # with unresolvable relocations.
      # TODO(eugenis): find a way to reenable this.
      cflags += [ "-mllvm -asan-globals=0" ]
    }

    defines += [ "ANDROID" ]

    # The NDK has these things, but doesn't define the constants
    # to say that it does. Define them here instead.
    defines += [ "HAVE_SYS_UIO_H" ]

    # Use gold for Android for most CPU architectures.
    if (current_cpu == "x86" || current_cpu == "x64" || current_cpu == "arm") {
      ldflags += [ "-fuse-ld=gold" ]
      if (is_clang) {
        # Let clang find the ld.gold in the NDK.
        ldflags += [ "--gcc-toolchain=$rebased_android_toolchain_root" ]
      }

      # Use -mstackrealign due to a bug on ia32 Jelly Bean.
      # See crbug.com/521527
      if (current_cpu == "x86") {
        cflags += [ "-mstackrealign" ]
      }
    }

    ldflags += [
      "-Wl,--no-undefined",

      # Don't allow visible symbols from libgcc or libc++ to be
      # re-exported.
      "-Wl,--exclude-libs=libgcc.a",
      "-Wl,--exclude-libs=libc++_static.a",

      # Don't allow visible symbols from libraries that contain
      # assembly code with symbols that aren't hidden properly.
      # http://crbug.com/448386
      "-Wl,--exclude-libs=libvpx_assembly_arm.a",
    ]
    if (current_cpu == "arm") {
      ldflags += [
        # Enable identical code folding to reduce size.
        "-Wl,--icf=safe",
      ]
    }

    if (is_clang) {
      if (current_cpu == "arm") {
        _abi_target = "arm-linux-androideabi"
      } else if (current_cpu == "x86") {
        _abi_target = "i686-linux-androideabi"
      } else if (current_cpu == "arm64") {
        # Place holder for arm64 support, not tested.
        _abi_target = "aarch64-linux-androideabi"
      } else if (current_cpu == "x64") {
        # Place holder for x64 support, not tested.
        # TODO: Enable clang support for Android x64. http://crbug.com/346626
        _abi_target = "x86_64-linux-androideabi"
      } else if (current_cpu == "mipsel") {
        # Place holder for mips support, not tested.
        _abi_target = "mipsel-linux-androideabi"
      } else if (current_cpu == "mips64el") {
        # Place holder for mips64 support, not tested.
        _abi_target = "mips64el-linux-androideabi"
      } else {
        assert(false, "Architecture not supported")
      }
      cflags += [
        "-target",
        _abi_target,
      ]
      ldflags += [
        "-target",
        _abi_target,
      ]
    }
  }

  # Pass the same C/C++ flags to the objective C/C++ compiler.
  cflags_objc += cflags_c
  cflags_objcc += cflags_cc
}

config("compiler_arm_fpu") {
  if (current_cpu == "arm" && !is_ios) {
    cflags = [ "-mfpu=$arm_fpu" ]
  }
}

# runtime_library -------------------------------------------------------------
#
# Sets the runtime library and associated options.
#
# How do you determine what should go in here vs. "compiler" above? Consider if
# a target might choose to use a different runtime library (ignore for a moment
# if this is possible or reasonable on your system). If such a target would want
# to change or remove your option, put it in the runtime_library config. If a
# target wants the option regardless, put it in the compiler config.

config("runtime_library") {
  cflags = []
  defines = []
  ldflags = []
  lib_dirs = []
  libs = []

  if (is_component_build) {
    # Component mode: dynamic CRT.
    defines += [ "COMPONENT_BUILD" ]
    if (is_win) {
      # Since the library is shared, it requires exceptions or will give errors
      # about things not matching, so keep exceptions on.
      if (is_debug) {
        cflags += [ "/MDd" ]
      } else {
        cflags += [ "/MD" ]
      }
    }
  } else {
    if (is_win && current_os != "win") {
      # WindowsRT: use the dynamic CRT.
      if (is_debug) {
        cflags += [ "/MDd" ]
      } else {
        cflags += [ "/MD" ]
      }
    } else if (is_win) {
      # Desktop Windows: static CRT.
      if (is_debug) {
        cflags += [ "/MTd" ]
      } else {
        cflags += [ "/MT" ]
      }
    }
  }

  if (is_win) {
    defines += [
      "__STD_C",
      "_CRT_RAND_S",
      "_CRT_SECURE_NO_DEPRECATE",
      "_HAS_EXCEPTIONS=0",
      "_SCL_SECURE_NO_DEPRECATE",
    ]
  }

  # Android standard library setup.
  if (is_android) {
    if (is_clang) {
      # Work around incompatibilities between bionic and clang headers.
      defines += [
        "__compiler_offsetof=__builtin_offsetof",
        "nan=__builtin_nan",
      ]
    }

    defines += [ "__GNU_SOURCE=1" ]  # Necessary for clone().

    # TODO(jdduke) Re-enable on mips after resolving linking
    # issues with libc++ (crbug.com/456380).
    if (current_cpu != "mipsel" && current_cpu != "mips64el") {
      ldflags += [ "-Wl,--warn-shared-textrel" ]
    }
    ldflags += [ "-nostdlib" ]

    # NOTE: The libc++ header include paths below are specified in cflags
    # rather than include_dirs because they need to come after include_dirs.
    # Think of them like system headers, but don't use '-isystem' because the
    # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
    # strange errors. The include ordering here is important; change with
    # caution.
    cflags += [
      "-isystem" +
          rebase_path("$android_libcpp_root/libcxx/include", root_build_dir),
      "-isystem" + rebase_path(
              "$android_ndk_root/sources/cxx-stl/llvm-libc++abi/libcxxabi/include",
              root_build_dir),
      "-isystem" +
          rebase_path("$android_ndk_root/sources/android/support/include",
                      root_build_dir),
    ]

    lib_dirs += [ "$android_libcpp_root/libs/$android_app_abi" ]
    libs += [ "$android_libcpp_library" ]

    if (current_cpu == "mipsel") {
      libs += [
        # ld linker is used for mips Android, and ld does not accept library
        # absolute path prefixed by "-l"; Since libgcc does not exist in mips
        # sysroot the proper library will be linked.
        # TODO(gordanac): Remove once gold linker is used for mips Android.
        "gcc",
      ]
    } else {
      libs += [
        # Manually link the libgcc.a that the cross compiler uses. This is
        # absolute because the linker will look inside the sysroot if it's not.
        rebase_path(android_libgcc_file),
      ]
    }

    libs += [
      "c",
      "dl",
      "m",
    ]

    # Clang with libc++ does not require an explicit atomic library reference.
    if (!is_clang) {
      libs += [ "atomic" ]
    }
  }
}

# default_warning_flags collects all warning flags that are used by default.
# This is in a variable instead of a config so that it can be used in
# both chromium_code and no_chromium_code.  This way these flags are guaranteed
# to appear on the compile command line after -Wall.

default_warning_flags = []
default_warning_flags_cc = []
if (is_win) {
  default_warning_flags += [
    # Treat warnings as errors.
    "/WX",

    # Warnings permanently disabled:

    # C4127: conditional expression is constant
    # This warning can in theory catch dead code and other problems, but
    # triggers in far too many desirable cases where the conditional
    # expression is either set by macros or corresponds some legitimate
    # compile-time constant expression (due to constant template args,
    # conditionals comparing the sizes of different types, etc.).  Some of
    # these can be worked around, but it's not worth it.
    "/wd4127",

    # C4251: 'identifier' : class 'type' needs to have dll-interface to be
    #        used by clients of class 'type2'
    # This is necessary for the shared library build.
    "/wd4251",

    # C4351: new behavior: elements of array 'array' will be default
    #        initialized
    # This is a silly "warning" that basically just alerts you that the
    # compiler is going to actually follow the language spec like it's
    # supposed to, instead of not following it like old buggy versions did.
    # There's absolutely no reason to turn this on.
    "/wd4351",

    # C4355: 'this': used in base member initializer list
    # It's commonly useful to pass |this| to objects in a class' initializer
    # list.  While this warning can catch real bugs, most of the time the
    # constructors in question don't attempt to call methods on the passed-in
    # pointer (until later), and annotating every legit usage of this is
    # simply more hassle than the warning is worth.
    "/wd4355",

    # C4503: 'identifier': decorated name length exceeded, name was
    #        truncated
    # This only means that some long error messages might have truncated
    # identifiers in the presence of lots of templates.  It has no effect on
    # program correctness and there's no real reason to waste time trying to
    # prevent it.
    "/wd4503",

    # Warning C4589 says: "Constructor of abstract class ignores
    # initializer for virtual base class." Disable this warning because it
    # is flaky in VS 2015 RTM. It triggers on compiler generated
    # copy-constructors in some cases.
    "/wd4589",

    # C4611: interaction between 'function' and C++ object destruction is
    #        non-portable
    # This warning is unavoidable when using e.g. setjmp/longjmp.  MSDN
    # suggests using exceptions instead of setjmp/longjmp for C++, but
    # Chromium code compiles without exception support.  We therefore have to
    # use setjmp/longjmp for e.g. JPEG decode error handling, which means we
    # have to turn off this warning (and be careful about how object
    # destruction happens in such cases).
    "/wd4611",

    # Warnings to evaluate and possibly fix/reenable later:

    "/wd4100",  # Unreferenced formal function parameter.
    "/wd4121",  # Alignment of a member was sensitive to packing.
    "/wd4244",  # Conversion: possible loss of data.
    "/wd4481",  # Nonstandard extension: override specifier.
    "/wd4505",  # Unreferenced local function has been removed.
    "/wd4510",  # Default constructor could not be generated.
    "/wd4512",  # Assignment operator could not be generated.
    "/wd4610",  # Class can never be instantiated, constructor required.
    "/wd4995",  # 'X': name was marked as #pragma deprecated
    "/wd4996",  # Deprecated function warning.
  ]

  # VS xtree header file needs to be patched or 4702 (unreachable code
  # warning) is reported if _HAS_EXCEPTIONS=0. Disable the warning if xtree is
  # not patched.
  if (!msvs_xtree_patched &&
      exec_script("../../win_is_xtree_patched.py", [], "value") == 0) {
    default_warning_flags += [ "/wd4702" ]  # Unreachable code.
  }

  # Building with Clang on Windows is a work in progress and very
  # experimental. See crbug.com/82385.
  # Keep this in sync with the similar block in build/common.gypi
  if (is_clang) {
    default_warning_flags += [
      # TODO(hans): Make this list shorter eventually, http://crbug.com/504657
      "-Qunused-arguments",  # http://crbug.com/504658
      "-Wno-microsoft-enum-value",  # http://crbug.com/505296
      "-Wno-unknown-pragmas",  # http://crbug.com/505314
      "-Wno-unused-value",  # http://crbug.com/505318
    ]
  }
} else {
  # Common GCC warning setup.
  default_warning_flags += [
    # Enables.
    "-Wendif-labels",  # Weird old-style text after an #endif.
    "-Werror",  # Warnings as errors.

    # Disables.
    "-Wno-missing-field-initializers",  # "struct foo f = {0};"
    "-Wno-unused-parameter",  # Unused function parameters.
  ]

  if (is_mac) {
    default_warning_flags += [ "-Wnewline-eof" ]
    if (!is_nacl) {
      # When compiling Objective-C, warns if a method is used whose
      # availability is newer than the deployment target. This is not
      # required when compiling Chrome for iOS.
      default_warning_flags += [ "-Wpartial-availability" ]
    }
  }

  if (gcc_version >= 48) {
    default_warning_flags_cc += [
      # See comment for -Wno-c++11-narrowing.
      "-Wno-narrowing",
    ]
    if (!is_clang) {
      default_warning_flags_cc += [
        # TODO(thakis): Remove, http://crbug.com/263960
        "-Wno-literal-suffix",
      ]
    }
  }

  # Suppress warnings about ABI changes on ARM (Clang doesn't give this
  # warning).
  if (current_cpu == "arm" && !is_clang) {
    default_warning_flags += [ "-Wno-psabi" ]
  }

  if (is_android) {
    # Disable any additional warnings enabled by the Android build system but
    # which chromium does not build cleanly with (when treating warning as
    # errors).
    default_warning_flags += [
      "-Wno-extra",
      "-Wno-ignored-qualifiers",
      "-Wno-type-limits",
    ]
    default_warning_flags_cc += [
      # Disabling c++0x-compat should be handled in WebKit, but
      # this currently doesn't work because gcc_version is not set
      # correctly when building with the Android build system.
      # TODO(torne): Fix this in WebKit.
      "-Wno-error=c++0x-compat",

      # Other things unrelated to -Wextra:
      "-Wno-non-virtual-dtor",
      "-Wno-sign-promo",
    ]
  }

  if (gcc_version >= 48) {
    # Don't warn about the "typedef 'foo' locally defined but not used"
    # for gcc 4.8.
    # TODO: remove this flag once all builds work. See crbug.com/227506
    default_warning_flags += [ "-Wno-unused-local-typedefs" ]
  }
}
if (is_clang) {
  default_warning_flags += [
    # This warns on using ints as initializers for floats in
    # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
    # which happens in several places in chrome code. Not sure if
    # this is worth fixing.
    "-Wno-c++11-narrowing",

    # Don't die on dtoa code that uses a char as an array index.
    # This is required solely for base/third_party/dmg_fp/dtoa.cc.
    # TODO(brettw) move this to that project then!
    "-Wno-char-subscripts",

    # Warns on switches on enums that cover all enum values but
    # also contain a default: branch. Chrome is full of that.
    "-Wno-covered-switch-default",

    # Clang considers the `register` keyword as deprecated, but e.g.
    # code generated by flex (used in angle) contains that keyword.
    # http://crbug.com/255186
    "-Wno-deprecated-register",

    # TODO(thakis): This used to be implied by -Wno-unused-function,
    # which we no longer use. Check if it makes sense to remove
    # this as well. http://crbug.com/316352
    "-Wno-unneeded-internal-declaration",
  ]

  if (is_posix && !is_mac && !is_ios) {
    default_warning_flags += [
      # TODO(thakis): Remove, http://crbug.com/263960
      "-Wno-reserved-user-defined-literal",
    ]
  }

  # NaCl's Clang compiler and Chrome's hermetic Clang compiler will almost
  # always have different versions. Certain flags may not be recognized by
  # one version or the other.
  if (!is_nacl) {
    # Flags NaCl does not recognize.
    default_warning_flags += [
      # TODO(hans): Get this cleaned up, http://crbug.com/428099
      "-Wno-inconsistent-missing-override",

      # TODO(thakis): Enable this, crbug.com/507717
      "-Wno-shift-negative-value",
    ]
  }
}

# chromium_code ---------------------------------------------------------------
#
# Toggles between higher and lower warnings for code that is (or isn't)
# part of Chromium.

config("chromium_code") {
  if (is_win) {
    cflags = [ "/W4" ]  # Warning level 4.
  } else {
    cflags = [
      "-Wall",

      # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
      # so we specify it explicitly.
      # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
      # http://code.google.com/p/chromium/issues/detail?id=90453
      "-Wsign-compare",
    ]

    # In Chromium code, we define __STDC_foo_MACROS in order to get the
    # C99 macros on Mac and Linux.
    defines = [
      "__STDC_CONSTANT_MACROS",
      "__STDC_FORMAT_MACROS",
    ]

    if (!is_debug && !using_sanitizer &&
        (!is_linux || !is_clang || is_official_build)) {
      # _FORTIFY_SOURCE isn't really supported by Clang now, see
      # http://llvm.org/bugs/show_bug.cgi?id=16821.
      # It seems to work fine with Ubuntu 12 headers though, so use it in
      # official builds.
      #
      # Non-chromium code is not guaranteed to compile cleanly with
      # _FORTIFY_SOURCE. Also, fortified build may fail when optimizations are
      # disabled, so only do that for Release build.
      defines += [ "_FORTIFY_SOURCE=2" ]
    }
  }
  cflags += default_warning_flags
  cflags_cc = default_warning_flags_cc
}
config("no_chromium_code") {
  cflags = []
  cflags_cc = []
  defines = []

  if (is_win) {
    cflags += [
      "/W3",  # Warning level 3.
      "/wd4800",  # Disable warning when forcing value to bool.
      "/wd4267",  # TODO(jschuh): size_t to int.
      "/wd4996",  # Deprecated function warning.
    ]
    defines += [
      "_CRT_NONSTDC_NO_WARNINGS",
      "_CRT_NONSTDC_NO_DEPRECATE",
    ]
  }

  if (is_linux) {
    # Don't warn about ignoring the return value from e.g. close(). This is
    # off by default in some gccs but on by default in others. BSD systems do
    # not support this option, since they are usually using gcc 4.2.1, which
    # does not have this flag yet.
    cflags += [ "-Wno-unused-result" ]
  }

  if (is_clang) {
    cflags += [
      # TODO(thakis): Move this suppression into individual third-party
      # libraries as required. http://crbug.com/505316.
      "-Wno-unused-function",

      # Lots of third-party libraries have unused variables. Instead of
      # suppressing them individually, we just blanket suppress them here.
      "-Wno-unused-variable",
    ]
  }

  if (is_linux || is_android) {
    cflags += [
      # Don't warn about printf format problems. This is off by default in gcc
      # but on in Ubuntu's gcc(!).
      "-Wno-format",
    ]
    cflags_cc += [
      # Don't warn about hash_map in third-party code.
      "-Wno-deprecated",
    ]
  }
  cflags += default_warning_flags
  cflags_cc += default_warning_flags_cc
}

# rtti ------------------------------------------------------------------------
#
# Allows turning Run-Time Type Identification on or off.

config("rtti") {
  if (is_win) {
    cflags_cc = [ "/GR" ]
  }
}
config("no_rtti") {
  # CFI diagnostics require RTTI.
  if (!use_cfi_diag) {
    if (is_win) {
      cflags_cc = [ "/GR-" ]
    } else {
      cflags_cc = [ "-fno-rtti" ]
      cflags_objcc = cflags_cc
    }
  }
}

# Warnings ---------------------------------------------------------------------

# This will generate warnings when using Clang if code generates exit-time
# destructors, which will slow down closing the program.
# TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
config("wexit_time_destructors") {
  # TODO: Enable on Windows too, http://crbug.com/404525
  if (is_clang && !is_win) {
    cflags = [ "-Wexit-time-destructors" ]
  }
}

# On Windows compiling on x64, VC will issue a warning when converting
# size_t to int because it will truncate the value. Our code should not have
# these warnings and one should use a static_cast or a checked_cast for the
# conversion depending on the case. However, a lot of code still needs to be
# fixed. Apply this config to such targets to disable the warning.
#
# Note that this can be applied regardless of platform and architecture to
# clean up the call sites. This will only apply the flag when necessary.
#
# TODO(jschuh): crbug.com/167187 fix this and delete this config.
config("no_size_t_to_int_warning") {
  if (is_win && current_cpu == "x64") {
    cflags = [ "/wd4267" ]
  }
}

# Some code presumes that pointers to structures/objects are compatible
# regardless of whether what they point to is already known to be valid.
# gcc 4.9 and earlier had no way of suppressing this warning without
# supressing the rest of them.  Here we centralize the identification of
# the gcc 4.9 toolchains.
config("no_incompatible_pointer_warnings") {
  cflags = []
  if (is_clang) {
    cflags += [ "-Wno-incompatible-pointer-types" ]
  } else if (current_cpu == "mipsel") {
    cflags += [ "-w" ]
  } else if (is_chromeos && current_cpu == "arm") {
    cflags += [ "-w" ]
  }
}

# Optimization -----------------------------------------------------------------
#
# Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
# which it will assign to the config it implicitly applies to every target. If
# you want to override the optimization level for your target, remove this
# config (which will expand differently for debug or release builds), and then
# add back the one you want to override it with:
#
#   configs -= default_optimization_config
#   configs += [ "//build/config/compiler/optimize_max" ]

# Shared settings for both "optimize" and "optimize_max" configs.
# IMPORTANT: On Windows "/O1" and "/O2" must go before the common flags.
if (is_win) {
  common_optimize_on_cflags = [
    "/Ob2",  # Both explicit and auto inlining.
    "/Oy-",  # Disable omitting frame pointers, must be after /O2.
    "/d2Zi+",  # Improve debugging of optimized code.
    "/Zc:inline",  # Remove unreferenced COMDAT (faster links).
  ]
  if (!is_asan) {
    common_optimize_on_cflags += [
      # Put data in separate COMDATs. This allows the linker
      # to put bit-identical constants at the same address even if
      # they're unrelated constants, which saves binary size.
      # This optimization can't be used when ASan is enabled because
      # it is not compatible with the ASan ODR checker.
      "/Gw",
    ]
  }
  common_optimize_on_ldflags = [ "/OPT:ICF" ]
  if (is_official_build) {
    common_optimize_on_ldflags += [
      # Link-time code generation.
      "/LTCG",

      # Set the number of LTCG code-gen threads to eight. The default is four.
      # This gives a 5-10% link speedup.
      "/cgthreads:8",
    ]
  }
} else {
  common_optimize_on_cflags = [
    # Don't emit the GCC version ident directives, they just end up in the
    # .comment section taking up binary size.
    "-fno-ident",

    # Put data and code in their own sections, so that unused symbols
    # can be removed at link time with --gc-sections.
    "-fdata-sections",
    "-ffunction-sections",
  ]
  common_optimize_on_ldflags = []

  if (is_android) {
    if (!using_sanitizer) {
      common_optimize_on_cflags += [ "-fomit-frame-pointer" ]
    }

    # TODO(jdduke) Re-enable on mips after resolving linking
    # issues with libc++ (crbug.com/456380).
    if (current_cpu != "mipsel" && current_cpu != "mips64el") {
      common_optimize_on_ldflags += [
        # Warn in case of text relocations.
        "-Wl,--warn-shared-textrel",
      ]
    }
  }

  if (is_mac || is_ios) {
    if (symbol_level == 2) {
      # Mac dead code stripping requires symbols.
      common_optimize_on_ldflags += [ "-Wl,-dead_strip" ]
    }
  } else {
    # Non-Mac Posix linker flags.
    common_optimize_on_ldflags += [
      # Specifically tell the linker to perform optimizations.
      # See http://lwn.net/Articles/192624/ .
      "-Wl,-O1",
      "-Wl,--gc-sections",
    ]

    if (!using_sanitizer) {
      # Functions interposed by the sanitizers can make ld think
      # that some libraries aren't needed when they actually are,
      # http://crbug.com/234010. As workaround, disable --as-needed.
      common_optimize_on_ldflags += [ "-Wl,--as-needed" ]
    }
  }
}

# Default "optimization on" config. On Windows, this favors size over speed.
config("optimize") {
  if (is_win) {
    # Favor size over speed, /O1 must be before the common flags. The GYP
    # build also specifies /Os and /GF but these are implied by /O1.
    cflags = [ "/O1" ] + common_optimize_on_cflags + [ "/Oi" ]
  } else if (is_android || is_ios) {
    cflags = [ "-Os" ] + common_optimize_on_cflags  # Favor size over speed.
  } else {
    cflags = [ "-O2" ] + common_optimize_on_cflags
  }
  ldflags = common_optimize_on_ldflags
}

# Turn off optimizations.
config("no_optimize") {
  if (is_win) {
    cflags = [
      "/Od",  # Disable optimization.
      "/Ob0",  # Disable all inlining (on by default).
      "/RTC1",  # Runtime checks for stack frame and uninitialized variables.
    ]
  } else if (is_android && !android_full_debug) {
    # On Android we kind of optimize some things that don't affect debugging
    # much even when optimization is disabled to get the binary size down.
    cflags = [
      "-Os",
      "-fdata-sections",
      "-ffunction-sections",
    ]
    if (!using_sanitizer) {
      cflags += [ "-fomit-frame-pointer" ]
    }
    ldflags = common_optimize_on_ldflags
  } else {
    cflags = [ "-O0" ]
  }
}

# Turns up the optimization level. On Windows, this implies whole program
# optimization and link-time code generation which is very expensive and should
# be used sparingly.
config("optimize_max") {
  ldflags = common_optimize_on_ldflags
  if (is_win) {
    # Favor speed over size, /O2 must be before the common flags. The GYP
    # build also specifies /Ot, /Oi, and /GF, but these are implied by /O2.
    cflags = [ "/O2" ] + common_optimize_on_cflags
    if (is_official_build) {
      # TODO(GYP): TODO(dpranke): Should these only be on in an official
      # build, or on all the time? For now we'll require official build so
      # that the compile is clean.
      cflags += [
        "/GL",  # Whole program optimization.

        # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds.
        # Probably anything that this would catch that wouldn't be caught in a
        # normal build isn't going to actually be a bug, so the incremental
        # value of C4702 for PGO builds is likely very small.
        "/wd4702",
      ]
    }
  } else {
    cflags = [ "-O2" ] + common_optimize_on_cflags
  }
}

# Symbols ----------------------------------------------------------------------

config("symbols") {
  if (is_win) {
    import("//build/toolchain/goma.gni")
    if (use_goma) {
      cflags = [ "/Z7" ]  # No PDB file
    } else {
      cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
    }
    if (is_win_fastlink) {
      # Tell VS 2015+ to create a PDB that references debug
      # information in .obj and .lib files instead of copying
      # it all. This flag is incompatible with /PROFILE
      ldflags = [ "/DEBUG:FASTLINK" ]
    } else {
      ldflags = [ "/DEBUG" ]
    }
  } else {
    cflags = [ "-g2" ]
    if (use_debug_fission) {
      cflags += [ "-gsplit-dwarf" ]
    }
  }
}

config("minimal_symbols") {
  if (is_win) {
    # Linker symbols for backtraces only.
    if (is_win_fastlink) {
      # Tell VS 2015+ to create a PDB that references debug
      # information in .obj and .lib files instead of copying
      # it all. This flag is incompatible with /PROFILE
      ldflags = [ "/DEBUG:FASTLINK" ]
    } else {
      ldflags = [ "/DEBUG" ]
    }
  } else {
    cflags = [ "-g1" ]
    if (use_debug_fission) {
      cflags += [ "-gsplit-dwarf" ]
    }
  }
}

config("no_symbols") {
  if (!is_win) {
    cflags = [ "-g0" ]
  }
}