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