github.com/tiagovtristao/plz@v13.4.0+incompatible/src/parse/rules/c_rules.build_defs (about)

     1  """Rules to build C targets.
     2  
     3  These are just wrappers around the C++ rules; the logic of the two is identical
     4  other than using different config settings.
     5  
     6  Both C and C++ can be mixed and matched arbitrarily, but of course to do so
     7  you must pay attention to interoperability when needed (e.g. 'extern "C"' and so forth).
     8  """
     9  
    10  
    11  def c_library(name:str, srcs:list=[], hdrs:list=[], private_hdrs:list=[], deps:list=[],
    12                visibility:list=None, test_only:bool&testonly=False, compiler_flags:list&cflags&copts=[],
    13                linker_flags:list&ldflags&linkopts=[], pkg_config_libs:list=[], pkg_config_cflags:list=[],
    14                includes:list=[], defines:list|dict=[], alwayslink:bool=False):
    15      """Generate a C library target.
    16  
    17      Args:
    18        name (str): Name of the rule
    19        srcs (list): C source files to compile.
    20        hdrs (list): Header files. These will be made available to dependent rules, so the distinction
    21                     between srcs and hdrs is important.
    22        private_hdrs (list): Header files that are available only to this rule and not exported to
    23                             dependent rules.
    24        deps (list): Dependent rules.
    25        visibility (list): Visibility declaration for this rule.
    26        test_only (bool): If True, is only available to other test rules.
    27        compiler_flags (list): Flags to pass to the compiler.
    28        linker_flags (list): Flags to pass to the linker; these will not be used here but will be
    29                             picked up by a c_binary or c_test rule.
    30        pkg_config_libs (list): Libraries to declare a dependency on using pkg-config. Again, the ldflags
    31                                will be picked up by cc_binary or cc_test rules.
    32        pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`.
    33        includes (list): List of include directories to be added to the compiler's path.
    34        defines (list | dict): List of tokens to define in the preprocessor.
    35                               Alternatively can be a dict of name -> value to define, in which case
    36                               values are surrounded by quotes.
    37        alwayslink (bool): If True, any binaries / tests using this library will link in all symbols,
    38                           even if they don't directly reference them. This is useful for e.g. having
    39                           static members that register themselves at construction time.
    40      """
    41      return cc_library(
    42          name = name,
    43          srcs = srcs,
    44          hdrs = hdrs,
    45          private_hdrs = private_hdrs,
    46          deps = deps,
    47          visibility = visibility,
    48          test_only = test_only,
    49          compiler_flags = compiler_flags,
    50          linker_flags = linker_flags,
    51          pkg_config_libs = pkg_config_libs,
    52          pkg_config_cflags=pkg_config_cflags,
    53          includes = includes,
    54          defines = defines,
    55          alwayslink = alwayslink,
    56          _c = True,
    57      )
    58  
    59  
    60  def c_object(name:str, src:str, hdrs:list=[], private_hdrs:list=[], out:str=None, test_only:bool&testonly=False,
    61               compiler_flags:list&cflags&copts=[], linker_flags:list&ldflags&linkopts=[],
    62               pkg_config_libs:list=[], pkg_config_cflags:list=[], includes:list=[], defines:list|dict=[],
    63               alwayslink:bool=False, visibility:list=None, deps:list=[]):
    64      """Generate a C object file from a single source.
    65  
    66      N.B. This is fairly low-level; for most use cases c_library should be preferred.
    67  
    68      Args:
    69        name (str): Name of the rule
    70        src (str): C or C++ source file to compile. This can be another rule, but if so it must
    71                   have exactly one output.
    72        hdrs (list): Header files. These will be made available to dependent rules, so the distinction
    73                     between srcs and hdrs is important.
    74        private_hdrs (list): Header files that are available only to this rule and not exported to
    75                             dependent rules.
    76        out (str): Name of the output file. Defaults to name + .o.
    77        deps (list): Dependent rules.
    78        visibility (list): Visibility declaration for this rule.
    79        test_only (bool): If True, is only available to other test rules.
    80        compiler_flags (list): Flags to pass to the compiler.
    81        linker_flags (list): Flags to pass to the linker; these will not be used here but will be
    82                             picked up by a c_binary or c_test rule.
    83        pkg_config_libs (list): Libraries to declare a dependency on using pkg-config. Again, the ldflags
    84                                will be picked up by cc_binary or cc_test rules.
    85        pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`. Again, the ldflags
    86                                will be picked up by cc_binary or cc_test rules.
    87        includes (list): List of include directories to be added to the compiler's path.
    88        defines (list | dict): List of tokens to define in the preprocessor.
    89                               Alternatively can be a dict of name -> value to define, in which case
    90                               values are surrounded by quotes.
    91        alwayslink (bool): If True, any binaries / tests using this library will link in all symbols,
    92                           even if they don't directly reference them. This is useful for e.g. having
    93                           static members that register themselves at construction time.
    94      """
    95      return cc_object(
    96          name = name,
    97          src = src,
    98          hdrs = hdrs,
    99          private_hdrs = private_hdrs,
   100          out = out,
   101          deps = deps,
   102          visibility = visibility,
   103          test_only = test_only,
   104          compiler_flags = compiler_flags,
   105          linker_flags = linker_flags,
   106          pkg_config_libs = pkg_config_libs,
   107          pkg_config_cflags=pkg_config_cflags,
   108          includes = includes,
   109          defines = defines,
   110          alwayslink = alwayslink,
   111          _c = True,
   112      )
   113  
   114  
   115  def c_static_library(name:str, srcs:list=[], hdrs:list=[], compiler_flags:list&cflags&copts=[], linker_flags:list&ldflags&linkopts=[],
   116                       deps:list=[], visibility:list=None, test_only:bool&testonly=False, pkg_config_libs:list=[], pkg_config_cflags:list=[]):
   117      """Generates a C static library (.a).
   118  
   119      This is essentially just a collection of other c_library rules into a single archive.
   120      Optionally this rule can have sources of its own, but it's quite reasonable just to use
   121      it as a collection of other rules.
   122  
   123      Args:
   124        name (str): Name of the rule
   125        srcs (list): C or C++ source files to compile.
   126        hdrs (list): Header files.
   127        compiler_flags (list): Flags to pass to the compiler.
   128        linker_flags (list): Flags to pass to the linker.
   129        deps (list): Dependent rules.
   130        visibility (list): Visibility declaration for this rule.
   131        test_only (bool): If True, is only available to other test rules.
   132        pkg_config_libs (list): Libraries to declare a dependency on using pkg-config
   133        pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`
   134  
   135      """
   136      return cc_static_library(
   137          name = name,
   138          srcs = srcs,
   139          hdrs = hdrs,
   140          deps = deps,
   141          visibility = visibility,
   142          test_only = test_only,
   143          compiler_flags = compiler_flags,
   144          linker_flags = linker_flags,
   145          pkg_config_libs = pkg_config_libs,
   146          pkg_config_cflags = pkg_config_cflags,
   147          _c = True,
   148      )
   149  
   150  
   151  def c_shared_object(name:str, srcs:list=[], hdrs:list=[], out:str='', compiler_flags:list&cflags&copts=[],
   152                      linker_flags:list&ldflags&linkopts=[], deps:list=[], visibility:list=None, test_only:bool&testonly=False,
   153                      pkg_config_libs:list=[], pkg_config_cflags:list=[], includes:list=[]):
   154      """Generates a C shared object (.so) with its dependencies linked in.
   155  
   156      Args:
   157        name (str): Name of the rule
   158        srcs (list): C or C++ source files to compile.
   159        hdrs (list): Header files. These will be made available to dependent rules, so the distinction
   160                     between srcs and hdrs is important.
   161        out (str): Name of the output .so. Defaults to name + .so.
   162        compiler_flags (list): Flags to pass to the compiler.
   163        linker_flags (list): Flags to pass to the linker.
   164        deps (list): Dependent rules.
   165        visibility (list): Visibility declaration for this rule.
   166        test_only (bool): If True, is only available to other test rules.
   167        pkg_config_libs (list): Libraries to declare a dependency on using pkg-config
   168        pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`
   169        includes (list): Include directories to be added to the compiler's lookup path.
   170      """
   171      return cc_shared_object(
   172          name = name,
   173          srcs = srcs,
   174          hdrs = hdrs,
   175          out = out,
   176          deps = deps,
   177          visibility = visibility,
   178          test_only = test_only,
   179          compiler_flags = compiler_flags,
   180          linker_flags = linker_flags,
   181          pkg_config_libs = pkg_config_libs,
   182          pkg_config_cflags = pkg_config_cflags,
   183          includes = includes,
   184          _c = True,
   185      )
   186  
   187  
   188  def c_binary(name:str, srcs:list=[], hdrs:list=[], private_hdrs:list=[], compiler_flags:list&cflags&copts=[],
   189               linker_flags:list&ldflags&linkopts=[], deps:list=[], visibility:list=None, pkg_config_libs:list=[],
   190               pkg_config_cflags:list=[], test_only:bool&testonly=False, static:bool=False):
   191      """Builds a binary from a collection of C rules.
   192  
   193      Args:
   194        name (str): Name of the rule
   195        srcs (list): C source files to compile.
   196        hdrs (list): Header files.
   197        private_hdrs (list): Header files that are available only to this rule and not exported to
   198                             dependent rules.
   199        compiler_flags (list): Flags to pass to the compiler.
   200        linker_flags (list): Flags to pass to the linker.
   201        deps (list): Dependent rules.
   202        visibility (list): Visibility declaration for this rule.
   203        pkg_config_libs (list): Libraries to declare a dependency on using pkg-config
   204        pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`
   205        test_only (bool): If True, this rule can only be used by tests.
   206        static (bool): If True, the binary will be linked statically.
   207      """
   208      return cc_binary(
   209          name = name,
   210          srcs = srcs,
   211          hdrs = hdrs,
   212          private_hdrs = private_hdrs,
   213          deps = deps,
   214          visibility = visibility,
   215          test_only = test_only,
   216          compiler_flags = compiler_flags,
   217          linker_flags = linker_flags,
   218          pkg_config_libs = pkg_config_libs,
   219          pkg_config_cflags = pkg_config_cflags,
   220          static = static,
   221          _c = True,
   222      )
   223  
   224  
   225  def c_test(name:str, srcs:list=[], hdrs:list=[], compiler_flags:list&cflags&copts=[], linker_flags:list&ldflags&linkopts=[],
   226             pkg_config_libs:list=[], pkg_config_cflags:list=[], deps:list=[], worker:str='', data:list=[], visibility:list=None, flags:str='',
   227             labels:list&features&tags=[], flaky:bool|int=0, test_outputs:list=None, size:str=None, timeout:int=0,
   228             container:bool|dict=False, sandbox:bool=None):
   229      """Defines a C test target.
   230  
   231      Note that you must supply your own main() and test framework (ala cc_test when
   232      write_main=False).
   233  
   234      Args:
   235        name (str): Name of the rule
   236        srcs (list): C or C++ source files to compile.
   237        hdrs (list): Header files.
   238        compiler_flags (list): Flags to pass to the compiler.
   239        linker_flags (list): Flags to pass to the linker.
   240        pkg_config_libs (list): Libraries to declare a dependency on using pkg-config
   241        pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`
   242        deps (list): Dependent rules.
   243        data (list): Runtime data files for this test.
   244        visibility (list): Visibility declaration for this rule.
   245        flags (str): Flags to apply to the test invocation.
   246        labels (list): Labels to attach to this test.
   247        flaky (bool | int): If true the test will be marked as flaky and automatically retried.
   248        test_outputs (list): Extra test output files to generate from this test.
   249        size (str): Test size (enormous, large, medium or small).
   250        timeout (int): Length of time in seconds to allow the test to run for before killing it.
   251        container (bool | dict): If true the test is run in a container (eg. Docker).
   252        sandbox (bool): Sandbox the test on Linux to restrict access to namespaces such as network.
   253      """
   254      return cc_test(
   255          name = name,
   256          srcs = srcs,
   257          hdrs = hdrs,
   258          deps = deps,
   259          worker = worker,
   260          visibility = visibility,
   261          compiler_flags = compiler_flags,
   262          linker_flags = linker_flags,
   263          pkg_config_libs = pkg_config_libs,
   264          pkg_config_cflags = pkg_config_cflags,
   265          data = data,
   266          flags = flags,
   267          labels = labels,
   268          flaky = flaky,
   269          test_outputs = test_outputs,
   270          size = size,
   271          timeout = timeout,
   272          container = container,
   273          sandbox = sandbox,
   274          _c = True,
   275          write_main = False,
   276      )
   277  
   278  
   279  def c_embed_binary(name:str, src:str, deps:list=[], visibility:list=None, test_only:list=False):
   280      """Build rule to embed an arbitrary binary file into a C library.
   281  
   282      You can depend on the output of this as though it were a c_library rule.
   283      There are five functions available to access the data once compiled, all of which are
   284      prefixed with the file's basename:
   285        filename_start(): returns a const char* pointing to the beginning of the data.
   286        filename_end(): returns a const char* pointing to the end of the data.
   287        filename_size(): returns the length of the data in bytes.
   288        filename_start_nc(): returns a char* pointing to the beginning of the data.
   289                             This is a convenience wrapper using const_cast, you should not
   290                             mutate the contents of the returned pointer.
   291        filename_end_nc(): returns a char* pointing to the end of the data.
   292                           Again, don't mutate the contents of the pointer.
   293      You don't own the contents of any of these pointers so don't try to delete them :)
   294  
   295      Args:
   296        name (str): Name of the rule.
   297        src (str): Source file to embed.
   298        deps (list): Dependencies.
   299        visibility (list): Rule visibility.
   300        test_only (bool): If True, is only available to test rules.
   301      """
   302      return cc_embed_binary(
   303          name = name,
   304          src = src,
   305          deps = deps,
   306          visibility = visibility,
   307          test_only = test_only,
   308          _c = True,
   309      )