github.com/prysmaticlabs/prysm@v1.4.4/tools/cross-toolchain/configs/clang/bazel_3.7.0/cc/cc_toolchain_config.bzl (about)

     1  # Copyright 2019 The Bazel Authors. All rights reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #    http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  """A Starlark cc_toolchain configuration rule"""
    16  
    17  load(
    18      "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
    19      "feature",
    20      "feature_set",
    21      "flag_group",
    22      "flag_set",
    23      "tool_path",
    24      "variable_with_value",
    25      "with_feature_set",
    26  )
    27  load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
    28  
    29  def layering_check_features(compiler):
    30      if compiler != "clang":
    31          return []
    32      return [
    33          feature(
    34              name = "use_module_maps",
    35              requires = [feature_set(features = ["module_maps"])],
    36              flag_sets = [
    37                  flag_set(
    38                      actions = [
    39                          ACTION_NAMES.c_compile,
    40                          ACTION_NAMES.cpp_compile,
    41                          ACTION_NAMES.cpp_header_parsing,
    42                          ACTION_NAMES.cpp_module_compile,
    43                      ],
    44                      flag_groups = [
    45                          flag_group(
    46                              flags = [
    47                                  "-fmodule-name=%{module_name}",
    48                                  "-fmodule-map-file=%{module_map_file}",
    49                              ],
    50                          ),
    51                      ],
    52                  ),
    53              ],
    54          ),
    55  
    56          # Tell blaze we support module maps in general, so they will be generated
    57          # for all c/c++ rules.
    58          # Note: not all C++ rules support module maps; thus, do not imply this
    59          # feature from other features - instead, require it.
    60          feature(name = "module_maps", enabled = True),
    61          feature(
    62              name = "layering_check",
    63              implies = ["use_module_maps"],
    64              flag_sets = [
    65                  flag_set(
    66                      actions = [
    67                          ACTION_NAMES.c_compile,
    68                          ACTION_NAMES.cpp_compile,
    69                          ACTION_NAMES.cpp_header_parsing,
    70                          ACTION_NAMES.cpp_module_compile,
    71                      ],
    72                      flag_groups = [
    73                          flag_group(flags = [
    74                              "-fmodules-strict-decluse",
    75                              "-Wprivate-header",
    76                          ]),
    77                          flag_group(
    78                              iterate_over = "dependent_module_map_files",
    79                              flags = [
    80                                  "-fmodule-map-file=%{dependent_module_map_files}",
    81                              ],
    82                          ),
    83                      ],
    84                  ),
    85              ],
    86          ),
    87      ]
    88  
    89  all_compile_actions = [
    90      ACTION_NAMES.c_compile,
    91      ACTION_NAMES.cpp_compile,
    92      ACTION_NAMES.linkstamp_compile,
    93      ACTION_NAMES.assemble,
    94      ACTION_NAMES.preprocess_assemble,
    95      ACTION_NAMES.cpp_header_parsing,
    96      ACTION_NAMES.cpp_module_compile,
    97      ACTION_NAMES.cpp_module_codegen,
    98      ACTION_NAMES.clif_match,
    99      ACTION_NAMES.lto_backend,
   100  ]
   101  
   102  all_cpp_compile_actions = [
   103      ACTION_NAMES.cpp_compile,
   104      ACTION_NAMES.linkstamp_compile,
   105      ACTION_NAMES.cpp_header_parsing,
   106      ACTION_NAMES.cpp_module_compile,
   107      ACTION_NAMES.cpp_module_codegen,
   108      ACTION_NAMES.clif_match,
   109  ]
   110  
   111  preprocessor_compile_actions = [
   112      ACTION_NAMES.c_compile,
   113      ACTION_NAMES.cpp_compile,
   114      ACTION_NAMES.linkstamp_compile,
   115      ACTION_NAMES.preprocess_assemble,
   116      ACTION_NAMES.cpp_header_parsing,
   117      ACTION_NAMES.cpp_module_compile,
   118      ACTION_NAMES.clif_match,
   119  ]
   120  
   121  codegen_compile_actions = [
   122      ACTION_NAMES.c_compile,
   123      ACTION_NAMES.cpp_compile,
   124      ACTION_NAMES.linkstamp_compile,
   125      ACTION_NAMES.assemble,
   126      ACTION_NAMES.preprocess_assemble,
   127      ACTION_NAMES.cpp_module_codegen,
   128      ACTION_NAMES.lto_backend,
   129  ]
   130  
   131  all_link_actions = [
   132      ACTION_NAMES.cpp_link_executable,
   133      ACTION_NAMES.cpp_link_dynamic_library,
   134      ACTION_NAMES.cpp_link_nodeps_dynamic_library,
   135  ]
   136  
   137  lto_index_actions = [
   138      ACTION_NAMES.lto_index_for_executable,
   139      ACTION_NAMES.lto_index_for_dynamic_library,
   140      ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
   141  ]
   142  
   143  def _impl(ctx):
   144      tool_paths = [
   145          tool_path(name = name, path = path)
   146          for name, path in ctx.attr.tool_paths.items()
   147      ]
   148      action_configs = []
   149  
   150      supports_pic_feature = feature(
   151          name = "supports_pic",
   152          enabled = True,
   153      )
   154      supports_start_end_lib_feature = feature(
   155          name = "supports_start_end_lib",
   156          enabled = True,
   157      )
   158  
   159      default_compile_flags_feature = feature(
   160          name = "default_compile_flags",
   161          enabled = True,
   162          flag_sets = [
   163              flag_set(
   164                  actions = all_compile_actions,
   165                  flag_groups = ([
   166                      flag_group(
   167                          flags = ctx.attr.compile_flags,
   168                      ),
   169                  ] if ctx.attr.compile_flags else []),
   170              ),
   171              flag_set(
   172                  actions = all_compile_actions,
   173                  flag_groups = ([
   174                      flag_group(
   175                          flags = ctx.attr.dbg_compile_flags,
   176                      ),
   177                  ] if ctx.attr.dbg_compile_flags else []),
   178                  with_features = [with_feature_set(features = ["dbg"])],
   179              ),
   180              flag_set(
   181                  actions = all_compile_actions,
   182                  flag_groups = ([
   183                      flag_group(
   184                          flags = ctx.attr.opt_compile_flags,
   185                      ),
   186                  ] if ctx.attr.opt_compile_flags else []),
   187                  with_features = [with_feature_set(features = ["opt"])],
   188              ),
   189              flag_set(
   190                  actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
   191                  flag_groups = ([
   192                      flag_group(
   193                          flags = ctx.attr.cxx_flags,
   194                      ),
   195                  ] if ctx.attr.cxx_flags else []),
   196              ),
   197          ],
   198      )
   199  
   200      default_link_flags_feature = feature(
   201          name = "default_link_flags",
   202          enabled = True,
   203          flag_sets = [
   204              flag_set(
   205                  actions = all_link_actions + lto_index_actions,
   206                  flag_groups = ([
   207                      flag_group(
   208                          flags = ctx.attr.link_flags,
   209                      ),
   210                  ] if ctx.attr.link_flags else []),
   211              ),
   212              flag_set(
   213                  actions = all_link_actions + lto_index_actions,
   214                  flag_groups = ([
   215                      flag_group(
   216                          flags = ctx.attr.opt_link_flags,
   217                      ),
   218                  ] if ctx.attr.opt_link_flags else []),
   219                  with_features = [with_feature_set(features = ["opt"])],
   220              ),
   221          ],
   222      )
   223  
   224      dbg_feature = feature(name = "dbg")
   225  
   226      opt_feature = feature(name = "opt")
   227  
   228      sysroot_feature = feature(
   229          name = "sysroot",
   230          enabled = True,
   231          flag_sets = [
   232              flag_set(
   233                  actions = [
   234                      ACTION_NAMES.preprocess_assemble,
   235                      ACTION_NAMES.linkstamp_compile,
   236                      ACTION_NAMES.c_compile,
   237                      ACTION_NAMES.cpp_compile,
   238                      ACTION_NAMES.cpp_header_parsing,
   239                      ACTION_NAMES.cpp_module_compile,
   240                      ACTION_NAMES.cpp_module_codegen,
   241                      ACTION_NAMES.lto_backend,
   242                      ACTION_NAMES.clif_match,
   243                  ] + all_link_actions + lto_index_actions,
   244                  flag_groups = [
   245                      flag_group(
   246                          flags = ["--sysroot=%{sysroot}"],
   247                          expand_if_available = "sysroot",
   248                      ),
   249                  ],
   250              ),
   251          ],
   252      )
   253  
   254      fdo_optimize_feature = feature(
   255          name = "fdo_optimize",
   256          flag_sets = [
   257              flag_set(
   258                  actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
   259                  flag_groups = [
   260                      flag_group(
   261                          flags = [
   262                              "-fprofile-use=%{fdo_profile_path}",
   263                              "-fprofile-correction",
   264                          ],
   265                          expand_if_available = "fdo_profile_path",
   266                      ),
   267                  ],
   268              ),
   269          ],
   270          provides = ["profile"],
   271      )
   272  
   273      supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True)
   274  
   275      user_compile_flags_feature = feature(
   276          name = "user_compile_flags",
   277          enabled = True,
   278          flag_sets = [
   279              flag_set(
   280                  actions = all_compile_actions,
   281                  flag_groups = [
   282                      flag_group(
   283                          flags = ["%{user_compile_flags}"],
   284                          iterate_over = "user_compile_flags",
   285                          expand_if_available = "user_compile_flags",
   286                      ),
   287                  ],
   288              ),
   289          ],
   290      )
   291  
   292      unfiltered_compile_flags_feature = feature(
   293          name = "unfiltered_compile_flags",
   294          enabled = True,
   295          flag_sets = [
   296              flag_set(
   297                  actions = all_compile_actions,
   298                  flag_groups = ([
   299                      flag_group(
   300                          flags = ctx.attr.unfiltered_compile_flags,
   301                      ),
   302                  ] if ctx.attr.unfiltered_compile_flags else []),
   303              ),
   304          ],
   305      )
   306  
   307      library_search_directories_feature = feature(
   308          name = "library_search_directories",
   309          flag_sets = [
   310              flag_set(
   311                  actions = all_link_actions + lto_index_actions,
   312                  flag_groups = [
   313                      flag_group(
   314                          flags = ["-L%{library_search_directories}"],
   315                          iterate_over = "library_search_directories",
   316                          expand_if_available = "library_search_directories",
   317                      ),
   318                  ],
   319              ),
   320          ],
   321      )
   322  
   323      static_libgcc_feature = feature(
   324          name = "static_libgcc",
   325          enabled = True,
   326          flag_sets = [
   327              flag_set(
   328                  actions = [
   329                      ACTION_NAMES.cpp_link_executable,
   330                      ACTION_NAMES.cpp_link_dynamic_library,
   331                      ACTION_NAMES.lto_index_for_executable,
   332                      ACTION_NAMES.lto_index_for_dynamic_library,
   333                  ],
   334                  flag_groups = [flag_group(flags = ["-static-libgcc"])],
   335                  with_features = [
   336                      with_feature_set(features = ["static_link_cpp_runtimes"]),
   337                  ],
   338              ),
   339          ],
   340      )
   341  
   342      pic_feature = feature(
   343          name = "pic",
   344          enabled = True,
   345          flag_sets = [
   346              flag_set(
   347                  actions = [
   348                      ACTION_NAMES.assemble,
   349                      ACTION_NAMES.preprocess_assemble,
   350                      ACTION_NAMES.linkstamp_compile,
   351                      ACTION_NAMES.c_compile,
   352                      ACTION_NAMES.cpp_compile,
   353                      ACTION_NAMES.cpp_module_codegen,
   354                      ACTION_NAMES.cpp_module_compile,
   355                  ],
   356                  flag_groups = [
   357                      flag_group(flags = ["-fPIC"], expand_if_available = "pic"),
   358                  ],
   359              ),
   360          ],
   361      )
   362  
   363      per_object_debug_info_feature = feature(
   364          name = "per_object_debug_info",
   365          flag_sets = [
   366              flag_set(
   367                  actions = [
   368                      ACTION_NAMES.assemble,
   369                      ACTION_NAMES.preprocess_assemble,
   370                      ACTION_NAMES.c_compile,
   371                      ACTION_NAMES.cpp_compile,
   372                      ACTION_NAMES.cpp_module_codegen,
   373                  ],
   374                  flag_groups = [
   375                      flag_group(
   376                          flags = ["-gsplit-dwarf"],
   377                          expand_if_available = "per_object_debug_info_file",
   378                      ),
   379                  ],
   380              ),
   381          ],
   382      )
   383  
   384      preprocessor_defines_feature = feature(
   385          name = "preprocessor_defines",
   386          enabled = True,
   387          flag_sets = [
   388              flag_set(
   389                  actions = [
   390                      ACTION_NAMES.preprocess_assemble,
   391                      ACTION_NAMES.linkstamp_compile,
   392                      ACTION_NAMES.c_compile,
   393                      ACTION_NAMES.cpp_compile,
   394                      ACTION_NAMES.cpp_header_parsing,
   395                      ACTION_NAMES.cpp_module_compile,
   396                      ACTION_NAMES.clif_match,
   397                  ],
   398                  flag_groups = [
   399                      flag_group(
   400                          flags = ["-D%{preprocessor_defines}"],
   401                          iterate_over = "preprocessor_defines",
   402                      ),
   403                  ],
   404              ),
   405          ],
   406      )
   407  
   408      cs_fdo_optimize_feature = feature(
   409          name = "cs_fdo_optimize",
   410          flag_sets = [
   411              flag_set(
   412                  actions = [ACTION_NAMES.lto_backend],
   413                  flag_groups = [
   414                      flag_group(
   415                          flags = [
   416                              "-fprofile-use=%{fdo_profile_path}",
   417                              "-Wno-profile-instr-unprofiled",
   418                              "-Wno-profile-instr-out-of-date",
   419                              "-fprofile-correction",
   420                          ],
   421                          expand_if_available = "fdo_profile_path",
   422                      ),
   423                  ],
   424              ),
   425          ],
   426          provides = ["csprofile"],
   427      )
   428  
   429      autofdo_feature = feature(
   430          name = "autofdo",
   431          flag_sets = [
   432              flag_set(
   433                  actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
   434                  flag_groups = [
   435                      flag_group(
   436                          flags = [
   437                              "-fauto-profile=%{fdo_profile_path}",
   438                              "-fprofile-correction",
   439                          ],
   440                          expand_if_available = "fdo_profile_path",
   441                      ),
   442                  ],
   443              ),
   444          ],
   445          provides = ["profile"],
   446      )
   447  
   448      runtime_library_search_directories_feature = feature(
   449          name = "runtime_library_search_directories",
   450          flag_sets = [
   451              flag_set(
   452                  actions = all_link_actions + lto_index_actions,
   453                  flag_groups = [
   454                      flag_group(
   455                          iterate_over = "runtime_library_search_directories",
   456                          flag_groups = [
   457                              flag_group(
   458                                  flags = [
   459                                      "-Wl,-rpath,$EXEC_ORIGIN/%{runtime_library_search_directories}",
   460                                  ],
   461                                  expand_if_true = "is_cc_test",
   462                              ),
   463                              flag_group(
   464                                  flags = [
   465                                      "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}",
   466                                  ],
   467                                  expand_if_false = "is_cc_test",
   468                              ),
   469                          ],
   470                          expand_if_available =
   471                              "runtime_library_search_directories",
   472                      ),
   473                  ],
   474                  with_features = [
   475                      with_feature_set(features = ["static_link_cpp_runtimes"]),
   476                  ],
   477              ),
   478              flag_set(
   479                  actions = all_link_actions + lto_index_actions,
   480                  flag_groups = [
   481                      flag_group(
   482                          iterate_over = "runtime_library_search_directories",
   483                          flag_groups = [
   484                              flag_group(
   485                                  flags = [
   486                                      "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}",
   487                                  ],
   488                              ),
   489                          ],
   490                          expand_if_available =
   491                              "runtime_library_search_directories",
   492                      ),
   493                  ],
   494                  with_features = [
   495                      with_feature_set(
   496                          not_features = ["static_link_cpp_runtimes"],
   497                      ),
   498                  ],
   499              ),
   500          ],
   501      )
   502  
   503      fission_support_feature = feature(
   504          name = "fission_support",
   505          flag_sets = [
   506              flag_set(
   507                  actions = all_link_actions + lto_index_actions,
   508                  flag_groups = [
   509                      flag_group(
   510                          flags = ["-Wl,--gdb-index"],
   511                          expand_if_available = "is_using_fission",
   512                      ),
   513                  ],
   514              ),
   515          ],
   516      )
   517  
   518      shared_flag_feature = feature(
   519          name = "shared_flag",
   520          flag_sets = [
   521              flag_set(
   522                  actions = [
   523                      ACTION_NAMES.cpp_link_dynamic_library,
   524                      ACTION_NAMES.cpp_link_nodeps_dynamic_library,
   525                      ACTION_NAMES.lto_index_for_dynamic_library,
   526                      ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
   527                  ],
   528                  flag_groups = [flag_group(flags = ["-shared"])],
   529              ),
   530          ],
   531      )
   532  
   533      random_seed_feature = feature(
   534          name = "random_seed",
   535          enabled = True,
   536          flag_sets = [
   537              flag_set(
   538                  actions = [
   539                      ACTION_NAMES.c_compile,
   540                      ACTION_NAMES.cpp_compile,
   541                      ACTION_NAMES.cpp_module_codegen,
   542                      ACTION_NAMES.cpp_module_compile,
   543                  ],
   544                  flag_groups = [
   545                      flag_group(
   546                          flags = ["-frandom-seed=%{output_file}"],
   547                          expand_if_available = "output_file",
   548                      ),
   549                  ],
   550              ),
   551          ],
   552      )
   553  
   554      includes_feature = feature(
   555          name = "includes",
   556          enabled = True,
   557          flag_sets = [
   558              flag_set(
   559                  actions = [
   560                      ACTION_NAMES.preprocess_assemble,
   561                      ACTION_NAMES.linkstamp_compile,
   562                      ACTION_NAMES.c_compile,
   563                      ACTION_NAMES.cpp_compile,
   564                      ACTION_NAMES.cpp_header_parsing,
   565                      ACTION_NAMES.cpp_module_compile,
   566                      ACTION_NAMES.clif_match,
   567                      ACTION_NAMES.objc_compile,
   568                      ACTION_NAMES.objcpp_compile,
   569                  ],
   570                  flag_groups = [
   571                      flag_group(
   572                          flags = ["-include", "%{includes}"],
   573                          iterate_over = "includes",
   574                          expand_if_available = "includes",
   575                      ),
   576                  ],
   577              ),
   578          ],
   579      )
   580  
   581      fdo_instrument_feature = feature(
   582          name = "fdo_instrument",
   583          flag_sets = [
   584              flag_set(
   585                  actions = [
   586                      ACTION_NAMES.c_compile,
   587                      ACTION_NAMES.cpp_compile,
   588                  ] + all_link_actions + lto_index_actions,
   589                  flag_groups = [
   590                      flag_group(
   591                          flags = [
   592                              "-fprofile-generate=%{fdo_instrument_path}",
   593                              "-fno-data-sections",
   594                          ],
   595                          expand_if_available = "fdo_instrument_path",
   596                      ),
   597                  ],
   598              ),
   599          ],
   600          provides = ["profile"],
   601      )
   602  
   603      cs_fdo_instrument_feature = feature(
   604          name = "cs_fdo_instrument",
   605          flag_sets = [
   606              flag_set(
   607                  actions = [
   608                      ACTION_NAMES.c_compile,
   609                      ACTION_NAMES.cpp_compile,
   610                      ACTION_NAMES.lto_backend,
   611                  ] + all_link_actions + lto_index_actions,
   612                  flag_groups = [
   613                      flag_group(
   614                          flags = [
   615                              "-fcs-profile-generate=%{cs_fdo_instrument_path}",
   616                          ],
   617                          expand_if_available = "cs_fdo_instrument_path",
   618                      ),
   619                  ],
   620              ),
   621          ],
   622          provides = ["csprofile"],
   623      )
   624  
   625      include_paths_feature = feature(
   626          name = "include_paths",
   627          enabled = True,
   628          flag_sets = [
   629              flag_set(
   630                  actions = [
   631                      ACTION_NAMES.preprocess_assemble,
   632                      ACTION_NAMES.linkstamp_compile,
   633                      ACTION_NAMES.c_compile,
   634                      ACTION_NAMES.cpp_compile,
   635                      ACTION_NAMES.cpp_header_parsing,
   636                      ACTION_NAMES.cpp_module_compile,
   637                      ACTION_NAMES.clif_match,
   638                      ACTION_NAMES.objc_compile,
   639                      ACTION_NAMES.objcpp_compile,
   640                  ],
   641                  flag_groups = [
   642                      flag_group(
   643                          flags = ["-iquote", "%{quote_include_paths}"],
   644                          iterate_over = "quote_include_paths",
   645                      ),
   646                      flag_group(
   647                          flags = ["-I%{include_paths}"],
   648                          iterate_over = "include_paths",
   649                      ),
   650                      flag_group(
   651                          flags = ["-isystem", "%{system_include_paths}"],
   652                          iterate_over = "system_include_paths",
   653                      ),
   654                  ],
   655              ),
   656          ],
   657      )
   658  
   659      symbol_counts_feature = feature(
   660          name = "symbol_counts",
   661          flag_sets = [
   662              flag_set(
   663                  actions = all_link_actions + lto_index_actions,
   664                  flag_groups = [
   665                      flag_group(
   666                          flags = [
   667                              "-Wl,--print-symbol-counts=%{symbol_counts_output}",
   668                          ],
   669                          expand_if_available = "symbol_counts_output",
   670                      ),
   671                  ],
   672              ),
   673          ],
   674      )
   675  
   676      llvm_coverage_map_format_feature = feature(
   677          name = "llvm_coverage_map_format",
   678          flag_sets = [
   679              flag_set(
   680                  actions = [
   681                      ACTION_NAMES.preprocess_assemble,
   682                      ACTION_NAMES.c_compile,
   683                      ACTION_NAMES.cpp_compile,
   684                      ACTION_NAMES.cpp_module_compile,
   685                      ACTION_NAMES.objc_compile,
   686                      ACTION_NAMES.objcpp_compile,
   687                  ],
   688                  flag_groups = [
   689                      flag_group(
   690                          flags = [
   691                              "-fprofile-instr-generate",
   692                              "-fcoverage-mapping",
   693                          ],
   694                      ),
   695                  ],
   696              ),
   697              flag_set(
   698                  actions = all_link_actions + lto_index_actions + [
   699                      "objc-executable",
   700                      "objc++-executable",
   701                  ],
   702                  flag_groups = [
   703                      flag_group(flags = ["-fprofile-instr-generate"]),
   704                  ],
   705              ),
   706          ],
   707          requires = [feature_set(features = ["coverage"])],
   708          provides = ["profile"],
   709      )
   710  
   711      strip_debug_symbols_feature = feature(
   712          name = "strip_debug_symbols",
   713          flag_sets = [
   714              flag_set(
   715                  actions = all_link_actions + lto_index_actions,
   716                  flag_groups = [
   717                      flag_group(
   718                          flags = ["-Wl,-S"],
   719                          expand_if_available = "strip_debug_symbols",
   720                      ),
   721                  ],
   722              ),
   723          ],
   724      )
   725  
   726      build_interface_libraries_feature = feature(
   727          name = "build_interface_libraries",
   728          flag_sets = [
   729              flag_set(
   730                  actions = [
   731                      ACTION_NAMES.cpp_link_dynamic_library,
   732                      ACTION_NAMES.cpp_link_nodeps_dynamic_library,
   733                      ACTION_NAMES.lto_index_for_dynamic_library,
   734                      ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
   735                  ],
   736                  flag_groups = [
   737                      flag_group(
   738                          flags = [
   739                              "%{generate_interface_library}",
   740                              "%{interface_library_builder_path}",
   741                              "%{interface_library_input_path}",
   742                              "%{interface_library_output_path}",
   743                          ],
   744                          expand_if_available = "generate_interface_library",
   745                      ),
   746                  ],
   747                  with_features = [
   748                      with_feature_set(
   749                          features = ["supports_interface_shared_libraries"],
   750                      ),
   751                  ],
   752              ),
   753          ],
   754      )
   755  
   756      libraries_to_link_feature = feature(
   757          name = "libraries_to_link",
   758          flag_sets = [
   759              flag_set(
   760                  actions = all_link_actions + lto_index_actions,
   761                  flag_groups = [
   762                      flag_group(
   763                          iterate_over = "libraries_to_link",
   764                          flag_groups = [
   765                              flag_group(
   766                                  flags = ["-Wl,--start-lib"],
   767                                  expand_if_equal = variable_with_value(
   768                                      name = "libraries_to_link.type",
   769                                      value = "object_file_group",
   770                                  ),
   771                              ),
   772                              flag_group(
   773                                  flags = ["-Wl,-whole-archive"],
   774                                  expand_if_true =
   775                                      "libraries_to_link.is_whole_archive",
   776                              ),
   777                              flag_group(
   778                                  flags = ["%{libraries_to_link.object_files}"],
   779                                  iterate_over = "libraries_to_link.object_files",
   780                                  expand_if_equal = variable_with_value(
   781                                      name = "libraries_to_link.type",
   782                                      value = "object_file_group",
   783                                  ),
   784                              ),
   785                              flag_group(
   786                                  flags = ["%{libraries_to_link.name}"],
   787                                  expand_if_equal = variable_with_value(
   788                                      name = "libraries_to_link.type",
   789                                      value = "object_file",
   790                                  ),
   791                              ),
   792                              flag_group(
   793                                  flags = ["%{libraries_to_link.name}"],
   794                                  expand_if_equal = variable_with_value(
   795                                      name = "libraries_to_link.type",
   796                                      value = "interface_library",
   797                                  ),
   798                              ),
   799                              flag_group(
   800                                  flags = ["%{libraries_to_link.name}"],
   801                                  expand_if_equal = variable_with_value(
   802                                      name = "libraries_to_link.type",
   803                                      value = "static_library",
   804                                  ),
   805                              ),
   806                              flag_group(
   807                                  flags = ["-l%{libraries_to_link.name}"],
   808                                  expand_if_equal = variable_with_value(
   809                                      name = "libraries_to_link.type",
   810                                      value = "dynamic_library",
   811                                  ),
   812                              ),
   813                              flag_group(
   814                                  flags = ["-l:%{libraries_to_link.name}"],
   815                                  expand_if_equal = variable_with_value(
   816                                      name = "libraries_to_link.type",
   817                                      value = "versioned_dynamic_library",
   818                                  ),
   819                              ),
   820                              flag_group(
   821                                  flags = ["-Wl,-no-whole-archive"],
   822                                  expand_if_true = "libraries_to_link.is_whole_archive",
   823                              ),
   824                              flag_group(
   825                                  flags = ["-Wl,--end-lib"],
   826                                  expand_if_equal = variable_with_value(
   827                                      name = "libraries_to_link.type",
   828                                      value = "object_file_group",
   829                                  ),
   830                              ),
   831                          ],
   832                          expand_if_available = "libraries_to_link",
   833                      ),
   834                      flag_group(
   835                          flags = ["-Wl,@%{thinlto_param_file}"],
   836                          expand_if_true = "thinlto_param_file",
   837                      ),
   838                  ],
   839              ),
   840          ],
   841      )
   842  
   843      user_link_flags_feature = feature(
   844          name = "user_link_flags",
   845          flag_sets = [
   846              flag_set(
   847                  actions = all_link_actions + lto_index_actions,
   848                  flag_groups = [
   849                      flag_group(
   850                          flags = ["%{user_link_flags}"],
   851                          iterate_over = "user_link_flags",
   852                          expand_if_available = "user_link_flags",
   853                      ),
   854                  ] + ([flag_group(flags = ctx.attr.link_libs)] if ctx.attr.link_libs else []),
   855              ),
   856          ],
   857      )
   858  
   859      fdo_prefetch_hints_feature = feature(
   860          name = "fdo_prefetch_hints",
   861          flag_sets = [
   862              flag_set(
   863                  actions = [
   864                      ACTION_NAMES.c_compile,
   865                      ACTION_NAMES.cpp_compile,
   866                      ACTION_NAMES.lto_backend,
   867                  ],
   868                  flag_groups = [
   869                      flag_group(
   870                          flags = [
   871                              "-mllvm",
   872                              "-prefetch-hints-file=%{fdo_prefetch_hints_path}",
   873                          ],
   874                          expand_if_available = "fdo_prefetch_hints_path",
   875                      ),
   876                  ],
   877              ),
   878          ],
   879      )
   880  
   881      linkstamps_feature = feature(
   882          name = "linkstamps",
   883          flag_sets = [
   884              flag_set(
   885                  actions = all_link_actions + lto_index_actions,
   886                  flag_groups = [
   887                      flag_group(
   888                          flags = ["%{linkstamp_paths}"],
   889                          iterate_over = "linkstamp_paths",
   890                          expand_if_available = "linkstamp_paths",
   891                      ),
   892                  ],
   893              ),
   894          ],
   895      )
   896  
   897      gcc_coverage_map_format_feature = feature(
   898          name = "gcc_coverage_map_format",
   899          flag_sets = [
   900              flag_set(
   901                  actions = [
   902                      ACTION_NAMES.preprocess_assemble,
   903                      ACTION_NAMES.c_compile,
   904                      ACTION_NAMES.cpp_compile,
   905                      ACTION_NAMES.cpp_module_compile,
   906                      ACTION_NAMES.objc_compile,
   907                      ACTION_NAMES.objcpp_compile,
   908                      "objc-executable",
   909                      "objc++-executable",
   910                  ],
   911                  flag_groups = [
   912                      flag_group(
   913                          flags = ["-fprofile-arcs", "-ftest-coverage"],
   914                          expand_if_available = "gcov_gcno_file",
   915                      ),
   916                  ],
   917              ),
   918              flag_set(
   919                  actions = all_link_actions + lto_index_actions,
   920                  flag_groups = [flag_group(flags = ["--coverage"])],
   921              ),
   922          ],
   923          requires = [feature_set(features = ["coverage"])],
   924          provides = ["profile"],
   925      )
   926  
   927      archiver_flags_feature = feature(
   928          name = "archiver_flags",
   929          flag_sets = [
   930              flag_set(
   931                  actions = [ACTION_NAMES.cpp_link_static_library],
   932                  flag_groups = [
   933                      flag_group(flags = ["rcsD"]),
   934                      flag_group(
   935                          flags = ["%{output_execpath}"],
   936                          expand_if_available = "output_execpath",
   937                      ),
   938                  ],
   939              ),
   940              flag_set(
   941                  actions = [ACTION_NAMES.cpp_link_static_library],
   942                  flag_groups = [
   943                      flag_group(
   944                          iterate_over = "libraries_to_link",
   945                          flag_groups = [
   946                              flag_group(
   947                                  flags = ["%{libraries_to_link.name}"],
   948                                  expand_if_equal = variable_with_value(
   949                                      name = "libraries_to_link.type",
   950                                      value = "object_file",
   951                                  ),
   952                              ),
   953                              flag_group(
   954                                  flags = ["%{libraries_to_link.object_files}"],
   955                                  iterate_over = "libraries_to_link.object_files",
   956                                  expand_if_equal = variable_with_value(
   957                                      name = "libraries_to_link.type",
   958                                      value = "object_file_group",
   959                                  ),
   960                              ),
   961                          ],
   962                          expand_if_available = "libraries_to_link",
   963                      ),
   964                  ],
   965              ),
   966          ],
   967      )
   968  
   969      force_pic_flags_feature = feature(
   970          name = "force_pic_flags",
   971          flag_sets = [
   972              flag_set(
   973                  actions = [
   974                      ACTION_NAMES.cpp_link_executable,
   975                      ACTION_NAMES.lto_index_for_executable,
   976                  ],
   977                  flag_groups = [
   978                      flag_group(
   979                          flags = ["-pie"],
   980                          expand_if_available = "force_pic",
   981                      ),
   982                  ],
   983              ),
   984          ],
   985      )
   986  
   987      dependency_file_feature = feature(
   988          name = "dependency_file",
   989          enabled = True,
   990          flag_sets = [
   991              flag_set(
   992                  actions = [
   993                      ACTION_NAMES.assemble,
   994                      ACTION_NAMES.preprocess_assemble,
   995                      ACTION_NAMES.c_compile,
   996                      ACTION_NAMES.cpp_compile,
   997                      ACTION_NAMES.cpp_module_compile,
   998                      ACTION_NAMES.objc_compile,
   999                      ACTION_NAMES.objcpp_compile,
  1000                      ACTION_NAMES.cpp_header_parsing,
  1001                      ACTION_NAMES.clif_match,
  1002                  ],
  1003                  flag_groups = [
  1004                      flag_group(
  1005                          flags = ["-MD", "-MF", "%{dependency_file}"],
  1006                          expand_if_available = "dependency_file",
  1007                      ),
  1008                  ],
  1009              ),
  1010          ],
  1011      )
  1012  
  1013      dynamic_library_linker_tool_path = tool_paths
  1014      dynamic_library_linker_tool_feature = feature(
  1015          name = "dynamic_library_linker_tool",
  1016          flag_sets = [
  1017              flag_set(
  1018                  actions = [
  1019                      ACTION_NAMES.cpp_link_dynamic_library,
  1020                      ACTION_NAMES.cpp_link_nodeps_dynamic_library,
  1021                      ACTION_NAMES.lto_index_for_dynamic_library,
  1022                      ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
  1023                  ],
  1024                  flag_groups = [
  1025                      flag_group(
  1026                          flags = [" + cppLinkDynamicLibraryToolPath + "],
  1027                          expand_if_available = "generate_interface_library",
  1028                      ),
  1029                  ],
  1030                  with_features = [
  1031                      with_feature_set(
  1032                          features = ["supports_interface_shared_libraries"],
  1033                      ),
  1034                  ],
  1035              ),
  1036          ],
  1037      )
  1038  
  1039      output_execpath_flags_feature = feature(
  1040          name = "output_execpath_flags",
  1041          flag_sets = [
  1042              flag_set(
  1043                  actions = all_link_actions + lto_index_actions,
  1044                  flag_groups = [
  1045                      flag_group(
  1046                          flags = ["-o", "%{output_execpath}"],
  1047                          expand_if_available = "output_execpath",
  1048                      ),
  1049                  ],
  1050              ),
  1051          ],
  1052      )
  1053  
  1054      # Note that we also set --coverage for c++-link-nodeps-dynamic-library. The
  1055      # generated code contains references to gcov symbols, and the dynamic linker
  1056      # can't resolve them unless the library is linked against gcov.
  1057      coverage_feature = feature(
  1058          name = "coverage",
  1059          provides = ["profile"],
  1060          flag_sets = [
  1061              flag_set(
  1062                  actions = [
  1063                      ACTION_NAMES.preprocess_assemble,
  1064                      ACTION_NAMES.c_compile,
  1065                      ACTION_NAMES.cpp_compile,
  1066                      ACTION_NAMES.cpp_header_parsing,
  1067                      ACTION_NAMES.cpp_module_compile,
  1068                  ],
  1069                  flag_groups = ([
  1070                      flag_group(flags = ctx.attr.coverage_compile_flags),
  1071                  ] if ctx.attr.coverage_compile_flags else []),
  1072              ),
  1073              flag_set(
  1074                  actions = all_link_actions + lto_index_actions,
  1075                  flag_groups = ([
  1076                      flag_group(flags = ctx.attr.coverage_link_flags),
  1077                  ] if ctx.attr.coverage_link_flags else []),
  1078              ),
  1079          ],
  1080      )
  1081  
  1082      thinlto_feature = feature(
  1083          name = "thin_lto",
  1084          flag_sets = [
  1085              flag_set(
  1086                  actions = [
  1087                      ACTION_NAMES.c_compile,
  1088                      ACTION_NAMES.cpp_compile,
  1089                  ] + all_link_actions + lto_index_actions,
  1090                  flag_groups = [
  1091                      flag_group(flags = ["-flto=thin"]),
  1092                      flag_group(
  1093                          expand_if_available = "lto_indexing_bitcode_file",
  1094                          flags = [
  1095                              "-Xclang",
  1096                              "-fthin-link-bitcode=%{lto_indexing_bitcode_file}",
  1097                          ],
  1098                      ),
  1099                  ],
  1100              ),
  1101              flag_set(
  1102                  actions = [ACTION_NAMES.linkstamp_compile],
  1103                  flag_groups = [flag_group(flags = ["-DBUILD_LTO_TYPE=thin"])],
  1104              ),
  1105              flag_set(
  1106                  actions = lto_index_actions,
  1107                  flag_groups = [
  1108                      flag_group(flags = [
  1109                          "-flto=thin",
  1110                          "-Wl,-plugin-opt,thinlto-index-only%{thinlto_optional_params_file}",
  1111                          "-Wl,-plugin-opt,thinlto-emit-imports-files",
  1112                          "-Wl,-plugin-opt,thinlto-prefix-replace=%{thinlto_prefix_replace}",
  1113                      ]),
  1114                      flag_group(
  1115                          expand_if_available = "thinlto_object_suffix_replace",
  1116                          flags = [
  1117                              "-Wl,-plugin-opt,thinlto-object-suffix-replace=%{thinlto_object_suffix_replace}",
  1118                          ],
  1119                      ),
  1120                      flag_group(
  1121                          expand_if_available = "thinlto_merged_object_file",
  1122                          flags = [
  1123                              "-Wl,-plugin-opt,obj-path=%{thinlto_merged_object_file}",
  1124                          ],
  1125                      ),
  1126                  ],
  1127              ),
  1128              flag_set(
  1129                  actions = [ACTION_NAMES.lto_backend],
  1130                  flag_groups = [
  1131                      flag_group(flags = [
  1132                          "-c",
  1133                          "-fthinlto-index=%{thinlto_index}",
  1134                          "-o",
  1135                          "%{thinlto_output_object_file}",
  1136                          "-x",
  1137                          "ir",
  1138                          "%{thinlto_input_bitcode_file}",
  1139                      ]),
  1140                  ],
  1141              ),
  1142          ],
  1143      )
  1144  
  1145      is_linux = ctx.attr.target_libc != "macosx"
  1146  
  1147      # TODO(#8303): Mac crosstool should also declare every feature.
  1148      if is_linux:
  1149          features = [
  1150              dependency_file_feature,
  1151              random_seed_feature,
  1152              pic_feature,
  1153              per_object_debug_info_feature,
  1154              preprocessor_defines_feature,
  1155              includes_feature,
  1156              include_paths_feature,
  1157              fdo_instrument_feature,
  1158              cs_fdo_instrument_feature,
  1159              cs_fdo_optimize_feature,
  1160              thinlto_feature,
  1161              fdo_prefetch_hints_feature,
  1162              autofdo_feature,
  1163              build_interface_libraries_feature,
  1164              dynamic_library_linker_tool_feature,
  1165              symbol_counts_feature,
  1166              shared_flag_feature,
  1167              linkstamps_feature,
  1168              output_execpath_flags_feature,
  1169              runtime_library_search_directories_feature,
  1170              library_search_directories_feature,
  1171              archiver_flags_feature,
  1172              force_pic_flags_feature,
  1173              fission_support_feature,
  1174              strip_debug_symbols_feature,
  1175              coverage_feature,
  1176              supports_pic_feature,
  1177          ] + (
  1178              [
  1179                  supports_start_end_lib_feature,
  1180              ] if ctx.attr.supports_start_end_lib else []
  1181          ) + [
  1182              default_compile_flags_feature,
  1183              default_link_flags_feature,
  1184              libraries_to_link_feature,
  1185              user_link_flags_feature,
  1186              static_libgcc_feature,
  1187              fdo_optimize_feature,
  1188              supports_dynamic_linker_feature,
  1189              dbg_feature,
  1190              opt_feature,
  1191              user_compile_flags_feature,
  1192              sysroot_feature,
  1193              unfiltered_compile_flags_feature,
  1194          ] + layering_check_features(ctx.attr.compiler)
  1195      else:
  1196          features = [
  1197              supports_pic_feature,
  1198          ] + (
  1199              [
  1200                  supports_start_end_lib_feature,
  1201              ] if ctx.attr.supports_start_end_lib else []
  1202          ) + [
  1203              coverage_feature,
  1204              default_compile_flags_feature,
  1205              default_link_flags_feature,
  1206              fdo_optimize_feature,
  1207              supports_dynamic_linker_feature,
  1208              dbg_feature,
  1209              opt_feature,
  1210              user_compile_flags_feature,
  1211              sysroot_feature,
  1212              unfiltered_compile_flags_feature,
  1213          ] + layering_check_features(ctx.attr.compiler)
  1214  
  1215      return cc_common.create_cc_toolchain_config_info(
  1216          ctx = ctx,
  1217          features = features,
  1218          action_configs = action_configs,
  1219          cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories,
  1220          toolchain_identifier = ctx.attr.toolchain_identifier,
  1221          host_system_name = ctx.attr.host_system_name,
  1222          target_system_name = ctx.attr.target_system_name,
  1223          target_cpu = ctx.attr.cpu,
  1224          target_libc = ctx.attr.target_libc,
  1225          compiler = ctx.attr.compiler,
  1226          abi_version = ctx.attr.abi_version,
  1227          abi_libc_version = ctx.attr.abi_libc_version,
  1228          tool_paths = tool_paths,
  1229      )
  1230  
  1231  cc_toolchain_config = rule(
  1232      implementation = _impl,
  1233      attrs = {
  1234          "cpu": attr.string(mandatory = True),
  1235          "compiler": attr.string(mandatory = True),
  1236          "toolchain_identifier": attr.string(mandatory = True),
  1237          "host_system_name": attr.string(mandatory = True),
  1238          "target_system_name": attr.string(mandatory = True),
  1239          "target_libc": attr.string(mandatory = True),
  1240          "abi_version": attr.string(mandatory = True),
  1241          "abi_libc_version": attr.string(mandatory = True),
  1242          "cxx_builtin_include_directories": attr.string_list(),
  1243          "tool_paths": attr.string_dict(),
  1244          "compile_flags": attr.string_list(),
  1245          "dbg_compile_flags": attr.string_list(),
  1246          "opt_compile_flags": attr.string_list(),
  1247          "cxx_flags": attr.string_list(),
  1248          "link_flags": attr.string_list(),
  1249          "link_libs": attr.string_list(),
  1250          "opt_link_flags": attr.string_list(),
  1251          "unfiltered_compile_flags": attr.string_list(),
  1252          "coverage_compile_flags": attr.string_list(),
  1253          "coverage_link_flags": attr.string_list(),
  1254          "supports_start_end_lib": attr.bool(),
  1255      },
  1256      provides = [CcToolchainConfigInfo],
  1257  )