-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBazel online document;Note=Erxin.txt
7346 lines (5311 loc) · 217 KB
/
Bazel online document;Note=Erxin.txt
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
Bazel online document;Note=Erxin
# introduction
- reference
https://docs.bazel.build/versions/main/tutorial/cpp.html#home
# IDE
- vscode
https://marketplace.visualstudio.com/items?itemName=BazelBuild.vscode-bazel
- emacs
https://github.com/bazelbuild/emacs-bazel-mode
- vim
https://github.com/bazelbuild/vim-bazel
- autocompelete for source code
c language family
https://github.com/hedronvision/bazel-compile-commands-extractor
# install
- reference
https://docs.bazel.build/versions/main/install-windows.html
- install prerequest cv redistributable
- install bazel
- add bazel*.exe to PATH
- install compiler for
+ MSYS2 x64 provide bash for windows
Common MSYS2 packages
+ VS 2019
+ jdk
+ python
- Bazelisk is a wrapper for Bazel written in Go. It automatically picks a good version of Bazel given your current working directory, downloads it from the official server
On macOS: brew install bazelisk.
On Windows: choco install bazelisk.
supports a .bazeliskrc file in the root directory of a workspace and the user home directory.
BAZELISK_BASE_URL
BAZELISK_CLEAN
BAZELISK_GITHUB_TOKEN
BAZELISK_HOME
BAZELISK_INCOMPATIBLE_FLAGS
BAZELISK_SHUTDOWN
BAZELISK_SKIP_WRAPPER
BAZELISK_USER_AGENT
USE_BAZEL_VERSION
- https://github.com/bazelbuild/bazel-toolchains is a repository where Google hosts the source code for a CLI tool that can be used to generate Bazel toolchain configs.
C/C++ CROSSTOOL file,
BUILD file with toolchain rules, and
wrapper scripts.
- Starlark (formerly known as Skylark) is a language intended for use as a configuration language. It was designed for the Bazel build system
https://github.com/bazelbuild/starlark
```
# Define a number
number = 18
# Define a dictionary
people = {
"Alice": 22,
"Bob": 40,
"Charlie": 55,
"Dave": 14,
}
names = ", ".join(people.keys()) # Alice, Bob, Charlie, Dave
```
- startlark source code
```
case WINDOWS:
switch (CPU.getCurrent()) {
case X86_64:
return "x64_windows";
...
case LINUX:
switch (CPU.getCurrent()) {
case X86_64:
return "k8";
```
# Define a function
```
def greet(name):
"""Return a greeting."""
return "Hello {}!".format(name)
greeting = greet(names)
above30 = [name for name, age in people.items() if age >= 30]
print("{} people are above 30.".format(len(above30)))
def fizz_buzz(n):
"""Print Fizz Buzz numbers from 1 to n."""
for i in range(1, n + 1):
s = ""
if i % 3 == 0:
s += "Fizz"
if i % 5 == 0:
s += "Buzz"
print(s if s else i)
fizz_buzz(20)
```
- Skylib is a library of Starlark functions for manipulating collections, file paths, and various other data types in the domain of Bazel build rules.
https://github.com/bazelbuild/bazel-skylib
.bzl files in the lib directory defines a "module"—a struct that contains a set of related functions and/or other symbols that can be loaded
```
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()
```
+ modules (in lib/)
collections
dicts
partial
paths
selects
sets - deprecated, use new_sets
new_sets
shell
structs
subpackages
types
unittest
versions
+ rules (in rules/)
analysis_test
build_test
common_settings
copy_directory
copy_file
diff_test
expand_template
native_binary and native_test
run_binary
select_file
write_file
+ add a module to Skylib:
Create a new .bzl file in the lib directory.
Write the functions or other symbols (such as constants) in that file, defining them privately (prefixed by an underscore).
Create the exported module struct, mapping the public names of the symbols to their implementations
Add unit test for your module in the tests directory
```
def _manipulate():
...
things = struct(
manipulate=_manipulate,
)
```
# Bazel tutorial, build a c++ project
- examples
git clone https://github.com/bazelbuild/examples
- a single target residing in a single package.
- The WORKSPACE file , which identifies the directory and its contents as a Bazel workspace
- One or more BUILD files , which tell Bazel how to build different parts of the project. A directory within the workspace that contains a BUILD file is a package.
- BUILD file requires at least one rule as a set of instructions
- Each instance of a build rule in the BUILD file is called a target and points to a specific set of source files and dependencies
+ c build file
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
)
cc_binary rule. The rule tells Bazel to build a self-contained executable binary from the hello-world.cc
- build project
$ bazel build //main:hello-world
- review the dependency graph. To visualize the sample project’s dependencies, you can generate a text representation of the dependency graph
$ bazel query --notool_deps --noimplicit_deps "deps(//main:hello-world)" --output graph
$ xdot <(bazel query --notool_deps --noimplicit_deps "deps(//main:hello-world)" --output graph)
- refine your bazel build
- multiple build targets, split larger projects into multiple targets and packages. This allows for fast incremental builds. add two cc_binary
```
cc_library(
name = "hello-greet",
srcs = ["hello-greet.cc"],
hdrs = ["hello-greet.h"],
)
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [
":hello-greet",
],
)
```
The deps attribute in the hello-world target tells Bazel that the hello-greet library is required to build the hello-world binary
- multiple packages
//main/BUILD
```
cc_library(
name = "hello-time",
srcs = ["hello-time.cc"],
hdrs = ["hello-time.h"],
visibility = ["//main:__pkg__"],
)
```
//lib/BUILD
```
cc_library(
name = "hello-greet",
srcs = ["hello-greet.cc"],
hdrs = ["hello-greet.h"],
)
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [
":hello-greet",
"//lib:hello-time",
],
)
```
# First build guide
https://bazel.build/start
## C++
## Java
## Android
## iOS
# Build Concepts
## Workspaces, packages, targets
- A workspace is a directory tree on your filesystem that contains the source files for the software you want to build
WORKSPACE.bazel file as an alias of WORKSPACE file.
- Code is organized in repositories.
- Pacakges, The primary unit of code organization in a repository is the package. A package is defined as a directory containing a file named BUILD (or BUILD.bazel).
- Targets, A package is a container of targets, which are defined in the package's BUILD file. Most targets are one of two principal kinds, files and rules.
An invariant of all rules is that the files generated by a rule always belong to the same package as the rule itself; it is not possible to generate files into another package.
- Package groups are sets of packages whose purpose is to limit accessibility of certain rules.
## Labels
- All targets belong to exactly one package. The name of a target is called its label. Every label uniquely identifies a target.
@myrepo//my/app/main:app_binary
The first part of the label is the repository name, @myrepo// the repository identifier may be abbreviated as //. The labels equal to
//my/app/main:app_binary
The second part of the label is the un-qualified package name my/app/main the path to the package relative to the repository root.
the label refers to the same package it is used in, the package name (and optionally, the colon) may be omitted. Target in another pacakge should be reference with full name.
```
app_binary
//equal to
:app_binary
```
The name of a file target in a subdirectory of the package is the file's path relative to the package root
- use of //my/app to refer to a package is encouraged in the specification of a package_group or in .bzl files, because it clearly communicates that the package name is absolute and rooted in the top-level directory of the workspace.
- The part of the label after the colon, app_binary is the un-qualified target name. When it matches the last component of the package path
//my/app/main:testdata/input.txt
- Labels starting with @// are references to the main repository, which will still work even from external repositories.
Therefore @//a/b/c is different from //a/b/c
@//a/b/c is reference back to main repository
//a/b/c is reference to external repository itself
- Target names, is the name of the target within the package.
- Package name is the directory containing the BUILD file relative to the workspace root directory
- Rules A rule specifies the relationship between inputs and outputs, and the steps to build the outputs. Rules can be of one of many different kinds
BUILD files declare targets by invoking rules.
```
cc_binary(
name = "my_app",
srcs = ["my_app.cc"],
deps = [
"//absl/base",
"//absl/strings",
],
)
```
Every rule has a set of attributes; the applicable attributes for a given rule, and the significance and semantics of each attribute are a function of the rule's kind.
## BUILD files
- BUILD files are evaluated using an imperative language, Starlark. a build rule function, such as cc_library, is executed, it creates a new target in the graph. This target can later be referred using a label.
- Loading an extension Bazel extensions are files ending in .bzl. Use the load statement to import a symbol from an extension.
```
load("//foo/bar:file.bzl", "some_library")
```
The first argument of load is a label identifying a .bzl file. If it's a relative label, it is resolved with respect to the package containing the current bzl file.
```
load(":my_rules.bzl", "some_rule", nice_alias = "some_other_rule")
```
In a .bzl file, symbols starting with _ are not exported and cannot be loaded from another file.
+ *_binary rules build executable in a given language. build tool's binary output tree at the corresponding name for the rule's label, so //my:program would appear at (for example) $(BINDIR)/my/program.
+ *_test rules are a specialization of a *_binary rule
+ *_library rules specify separately-compiled modules in the given programming language.
- types of depdencies
scrs dependencies
deps dependencies
data dependencies, if a binary/library/test needs some files to run, specify them (or a build rule containing them) in data
${TEST_SRCDIR}/workspace/path/to/data/file.
glob() to be recursive. Recommended — data = glob(["testdata/**"])
## Dependencies
- actual and declared dependencies
A target X is actually dependent on target Y if Y must be present, built, and up-to-date in order for X to be built correctly
- BUILD file writers must explicitly declare all of the actual direct dependencies for every rule to the build system
## Visibility
- Target visibility controls who may depend on your target
//visibility:public
//visibility:private": Does not grant any additional access;
//foo/bar:__pkg__ Grants access to //foo/bar
//foo/bar:__subpackages__ Grants access //foo/bar and all of its direct and indirect subpackages.
//some_pkg:my_package_group Grants access to all of the packages that are part of the given package_group.
- package group use different syntax compare to target
the forms "//foo/bar:__pkg__" and "//foo/bar:__subpackages__" are respectively replaced by "//foo/bar" and "//foo/bar/...".
Likewise, "//visibility:public" and "//visibility:private" are just "public" and "private".
- Use package group instead of specifying in each target
- Rule target visibility
The value of the default_visibility argument of the package statement in the target's BUILD file. otherwise default is private
```
# This target is visible to everyone
cc_binary(
name = "executable",
visibility = ["//visibility:public"],
deps = [":library"],
)
# This target is visible only to targets declared in the same package
cc_library(
name = "library",
# No visibility -- defaults to private since no
# package(default_visibility = ...) was used.
)
cc_library(
name = "subject",
visibility = [
"//noun:__pkg__",
"//object:__pkg__",
],
)
# Our friends are packages //frobber, //fribber and any
# subpackage of //fribber.
package_group(
name = "friends",
packages = [
"//fribber/...",
"//frobber",
],
)
```
- generated file has the same visibility as the rule target that generates it.
- source file target visibility, explicitly set the visibility of a source file target by calling exports_files. When no visibility argument is passed to exports_files, it makes the visibility public
the flag --incompatible_no_implicit_file_export. If the flag is set, the visibility is private. Else, the legacy behavior applies: The visibility is the same as the BUILD file's default_visibility
```
//frobber/data/BUILD
exports_files(["readme.txt"])
//frobber/bin/BUILD
cc_binary(
name = "my-program",
data = ["//frobber/data:readme.txt"],
)
```
- config setting visibility is not enforced by default
flags to control the behavior
```
--incompatible_enforce_config_setting_visibility enables visibility checking for these targets.
--incompatible_config_setting_private_default_visibility causes config_settings that do not specify a visibility to respect the package's default_visibility
```
- package group targets visibility are always public
- visibility of implicit dependencies
some rules have implicit, such as cc_library might create dependency to an executable target.
the target being depended on must be visible to every instance of the rule.
change this behavior by setting --incompatible_visibility_private_attributes_at_definition. When enabled,
- load visibility
To set the load visibility of a .bzl file, call the visibility() function from within the file.
disable load visibility enforcement by setting --check_bzl_visibility=false
- declaring load visibility, the default load visibility is always public.
```
# Available to subpackages and to mylib's tests.
visibility(["//mylib/...", "//tests/mylib/..."])
visibility("public")
```
- load visibility practices
+ combined multiple load visibility into one central file
```
# //mylib/internal_defs.bzl
visibility("private")
clients = [
"//foo",
"//bar/baz/...",
...
]
# //mylib/feature_A.bzl
load(":internal_defs.bzl", "clients")
visibility(clients)
...
# //mylib/feature_B.bzl
load(":internal_defs.bzl", "clients")
visibility(clients)
```
- composing visibilities
```
# //mylib/macros.bzl
load(":internal_defs.bzl", "our_packages")
load("//some_big_client:defs.bzl", "their_remaining_uses)
# List concatenation. Duplicates are fine.
visibility(our_packages + their_remaining_uses)
```
- protecting individual symbols
```
# //mylib/internal_defs.bzl
# Can't be public, because internal_helper shouldn't be exposed to the world.
visibility("private")
# Can't be underscore-prefixed, because this is
# needed by other .bzl files in mylib.
def internal_helper(...):
...
def public_util(...):
...
# //mylib/defs.bzl
load(":internal_defs", "internal_helper", _public_util="public_util")
visibility("public")
# Re-export public_util from this file by assigning it to a global variable.
public_util = _public_util
```
- There is a Buildifier lint that provides a warning if users load a file from a directory named internal or private
## Hermeticity
- a hermetic build system always returns the same output by isolating the build from changes to the host system.
+ Isolation: Hermetic build systems treat tools as source code. They download copies of tools and manage their storage and use inside managed file trees
+ Source identity: Hermetic build systems try to ensure the sameness of inputs. Check code hash
- benefits
speed
parallel execution '
multiple builds
reproducibility
# Command line completion
- bash
the Bash completion script is already installed in /etc/bash_completion.d.
+ apt get will automatic install
+ homebrew will automatic install
+ from installer
# Bazel Glossary
- action, a command to run during the build
- action cache, stores a mapping of executed actions
- action graph in-memory graph of actions and the artifacts
- action graph query, a query tool that can query over build actions.
- action key, the cache key of an action
- analysis phase, The second phase of a build. Processes the target graph specified in BUILD files to produce in memory action graph
- artifact
- aspect, create additional actions in their dependencies
- A composition mechanism whereby aspects can be applied to the results of other aspects.
- attribute A parameter to a rule, used to express per-target build information
- BUILD file is the main configuration file that tells Bazel what software outputs to build
- BUILD.bazel file, Takes precedence over a BUILD file
- .bzl file defines rules, macros and constants written in starlark
- A Starlark-defined piece of configuration. Transitions can set build settings to change a subgraph's configuration
- clean build
- client-server model
- command like bazel build
- command flags
- configuration
- configuration trimming, only including the pieces of configuration a target actually needs. For example, if you build Java binary //:j with C++ dependency //:c, it's wasteful to include the value of --javacopt
- configured query, A query tool that queries over configured targets
- configured target, The result of evaluating a target with a configuration. The analysis phase produces this by combining the build's options
- correctness
- dependency
- depset, depsets is time and space efficient, because it’s common to have very large depsets (hundreds of thousands of files).
- disk cache, a local on disk blob store
- distdir a read-only directory containing files that bazel would other wise fetch
- dynamic execution
- execution phase, the thrid phase of a build
- execution root, the workspace's output base
- A build is hermetic if there are no external influences on its build and test operations, which helps to make sure that results are deterministic and correct
- incremental build
- label
- loading phase build where Bazel parses WORKSPACE, BUILD, and .bzl files to create packages.
- macro
- mnemonic, Mnemonics can be used as identifiers for spawn strategy selections.
- native rules, Rules that are built into Bazel and implemented in Java.
- output groups, Rules put their usual outputs in the "default output group" (e.g the .jar file of a java_library, .a and .so for cc_library targets)
- output user root, the directory name is derived from the user's system
- package
- package group
- platform
- provider, A schema describing a unit of information to pass between rule targets along dependency relationships. like compiler options, transitive source or output files, and build metadata. conjunction with depsets to store accumulated transitive data.
- A schema for defining rule targets in a BUILD file, such as cc_library.
- A target that is an instance of a rule.
- The runtime dependencies of an executable target. Most commonly, the executable is the executable output of a test rule
- sandboxing
- stamping, a feature to embed additional information into bazel-built artifacts
- starlark, the extension language for writing rules and macros
- startup flags, The set of flags specified between bazel and the command, for example, bazel --host_jvm_debug build.
- target, An object that is defined in a BUILD file and identified by a label. Targets represent the buildable units of a workspace. declare by instantiating a rule.
- target graph, An in-memory graph of targets and their dependencies.
- target pattern, A way to specify a group of targets on the command line. Commonly used patterns are :all (all rule targets), :* (all rule + file targets), ...
- A set of tools to build outputs for a language. Typically, a toolchain includes compilers, linkers, interpreters or/and linters.
- A build target is top-level if it’s requested on the Bazel command line.
- transition A mapping of configuration state from one value to another. Enables targets in the build graph to have different configurations. certain parts of the target graph is forked with distinct configurations for each fork.
- tree artifact, An artifact that represents a collection of files. Since these files are not themselves artifacts
- visibility, target visibility to control where the target can be referenced. load visibility for controlling a build or .bzl file may load a given .bzl
- workspace, containing a workspace file start, label that start with // are relative to workspace directory
- workspace file can be empty although it usually contains external repositorty declarations to fetch
# Language specific rules
https://bazel.build/reference/be/overview
- c/c++
+ rules
cc_binary
cc_import
cc_library
cc_proto_library
fdo_prefetch_hints
fdo_profile
propeller_optimize
cc_test
cc_toolchain
cc_toolchain_suite
+ cc_binary implicit output targets
cc_binary(name, deps, srcs, data, additional_linker_inputs, args, compatible_with, copts, defines, deprecation, distribs, env, exec_compatible_with, exec_properties, features, includes, licenses, linkopts, linkshared, linkstatic, local_defines, malloc, nocopts, output_licenses, restricted_to, stamp, tags, target_compatible_with, testonly, toolchains, visibility, win_def_file)
+ cc_import, allows users to import precompiled c/c++ libraries
cc_import(name, data, hdrs, alwayslink, compatible_with, deprecation, distribs, features, interface_library, licenses, restricted_to, shared_library, static_library, system_provided, tags, target_compatible_with, testonly, visibility)
link static library
```
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
static_library = "libmylib.a",
# If alwayslink is turned on,
# libmylib.a will be forcely linked into any binary that depends on it.
# alwayslink = 1,
)
```
linking shared library
```
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
shared_library = "libmylib.so",
)
```
linking a shared library with interface library
```
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
# mylib.lib is an import library for mylib.dll which will be passed to linker
interface_library = "mylib.lib",
# mylib.dll will be available for runtime
shared_library = "mylib.dll",
)
```
linking a shared library with system_provided=True
```
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
# mylib.lib is an import library for mylib.dll which will be passed to linker
interface_library = "mylib.lib",
# mylib.dll is provided by system environment, for example it can be found in PATH.
# This indicates that Bazel is not responsible for making mylib.dll available.
system_provided = 1,
)
```
linking to static or shared library
```
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
static_library = "libmylib.a",
shared_library = "libmylib.so",
)
# first will link to libmylib.a
cc_binary(
name = "first",
srcs = ["first.cc"],
deps = [":mylib"],
linkstatic = 1, # default value
)
# second will link to libmylib.so
cc_binary(
name = "second",
srcs = ["second.cc"],
deps = [":mylib"],
linkstatic = 0,
)
```
on windows
```
cc_import(
name = "mylib",
hdrs = ["mylib.h"],
static_library = "libmylib.lib", # A normal static library
interface_library = "mylib.lib", # An import library for mylib.dll
shared_library = "mylib.dll",
)
# first will link to libmylib.lib
cc_binary(
name = "first",
srcs = ["first.cc"],
deps = [":mylib"],
linkstatic = 1, # default value
)
# second will link to mylib.dll through mylib.lib
cc_binary(
name = "second",
srcs = ["second.cc"],
deps = [":mylib"],
linkstatic = 0,
)
```
+ cc_library
cc_library(name, deps, srcs, data, hdrs, alwayslink, compatible_with, copts, defines, deprecation, distribs, exec_compatible_with, exec_properties, features, implementation_deps, include_prefix, includes, licenses, linkopts, linkstamp, linkstatic, local_defines, nocopts, restricted_to, strip_include_prefix, tags, target_compatible_with, testonly, textual_hdrs, toolchains, visibility, win_def_file)
All header files that are used in the build must be declared in the hdrs or srcs of cc_* rules. This is enforced.
cc_binary and cc_test rules do not have an exported interface, so they also do not have a hdrs attribute. All headers that belong to the binary or test directly should be listed in the srcs
```
cc_binary(
name = "foo",
srcs = [
"foo.cc",
"foo.h",
],
deps = [":bar"],
)
cc_library(
name = "bar",
srcs = [
"bar.cc",
"bar-impl.h",
],
hdrs = ["bar.h"],
deps = [":baz"],
)
cc_library(
name = "baz",
srcs = [
"baz.cc",
"baz-impl.h",
],
hdrs = ["baz.h"],
)
```
+ cc_proto_library
cc_proto_library(name, deps, data, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, features, licenses, restricted_to, tags, target_compatible_with, testonly, visibility)
cc_proto_library generates C++ code from .proto files.
```
cc_library(
name = "lib",
deps = [":foo_cc_proto"],
)
cc_proto_library(
name = "foo_cc_proto",
deps = [":foo_proto"],
)
proto_library(
name = "foo_proto",
)
```
+ fdo_prefetch_hints
fdo_prefetch_hints(name, compatible_with, deprecation, distribs, features, licenses, profile, restricted_to, tags, target_compatible_with, testonly, visibility)
Represents an FDO prefetch hints profile that is either in the workspace or at a specified absolute path.
```
fdo_prefetch_hints(
name = "hints",
profile = "//path/to/hints:profile.afdo",
)
fdo_profile(
name = "hints_abs",
absolute_path_profile = "/absolute/path/profile.afdo",
)
```
+ fdo_profile
fdo_profile(name, absolute_path_profile, compatible_with, deprecation, distribs, features, licenses, profile, proto_profile, restricted_to, tags, target_compatible_with, testonly, visibility)
Represents an FDO profile that is either in the workspace or at a specified absolute path.
```
fdo_profile(
name = "fdo",
profile = "//path/to/fdo:profile.zip",
)
fdo_profile(
name = "fdo_abs",
absolute_path_profile = "/absolute/path/profile.zip",
)
```
+ propeller_optimize
propeller_optimize(name, compatible_with, deprecation, distribs, features, ld_profile, licenses, restricted_to, tags, target_compatible_with, testonly, visibility)
Represents a Propeller optimization profile in the workspace
```
propeller_optimize(
name = "layout",
cc_profile = "//path:cc_profile.txt",
ld_profile = "//path:ld_profile.txt"
)
propeller_optimize(
name = "layout_absolute",
absolute_cc_profile = "/absolute/cc_profile.txt",
absolute_ld_profile = "/absolute/ld_profile.txt"
)
```
+ cc_test
cc_test(name, deps, srcs, data, additional_linker_inputs, args, compatible_with, copts, defines, deprecation, distribs, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, includes, licenses, linkopts, linkstatic, local, local_defines, malloc, nocopts, restricted_to, shard_count, size, stamp, tags, target_compatible_with, testonly, timeout, toolchains, visibility, win_def_file)
+ cc_toolchain
cc_toolchain(name, all_files, ar_files, as_files, compatible_with, compiler, compiler_files, compiler_files_without_includes, coverage_files, cpu, deprecation, distribs, dwp_files, dynamic_runtime_lib, exec_transition_for_inputs, features, libc_top, licenses, linker_files, module_map, objcopy_files, restricted_to, static_runtime_lib, strip_files, supports_header_parsing, supports_param_files, tags, target_compatible_with, testonly, toolchain_config, toolchain_identifier, visibility)
+ cc_toolchain_suite
cc_toolchain_suite(name, compatible_with, deprecation, distribs, features, licenses, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
toolchains a collections of C++ toolchains.
```
cc_toolchain_suite(
name = "toolchain",
toolchains = {
"piii|gcc": ":my_cc_toolchain_for_piii_using_gcc",
"piii": ":my_cc_toolchain_for_piii_using_default_compiler",
},
)
```
- parameters
hdrs, List of labels
copts, List of strings; optional
strip_include_prefix, String; optional, The prefix to strip from the paths of the headers of this rule.
textual_hdrs List of labels; optional The list of header files published by this library to be textually included by sources in dependent rules
win_def_fiel The Windows DEF file to be passed to linker.
linkopts Add these flags to the C++ linker command.
linkstatic cc_binary and cc_test: link the binary in static mode. For cc_library.linkstatic
local_defines List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization
malloc By default, C++ binaries are linked against //tools/cpp:malloc, which is an empty library so the binary ends up using libc malloc
nocopts Remove matching options from the C++ compilation command. Subject to "Make" variable substitution.
stamp Whether to encode build information into the binary.
win_def_file The Windows DEF file to be passed to linker.
toolchains Dictionary mapping strings to labels; A map from "<cpu>" or "<cpu>|<compiler>" strings to a cc_toolchain label.
- python
+ py_binary, an executable Python program consisting of a collection of .py source files (possibly belonging to other py_library rules), a *.runfiles directory tree containing all the code
py_binary(name, deps, srcs, data, args, compatible_with, deprecation, distribs, env, exec_compatible_with, exec_properties, features, imports, legacy_create_init, licenses, main, output_licenses, python_version, restricted_to, srcs_version, stamp, tags, target_compatible_with, testonly, toolchains, visibility)
```
py_binary(
name = "foo",
srcs = ["foo.py"],
data = [":transform"], # a cc_binary which we invoke at run time
deps = [
":foolib", # a py_library
],
)
```
+ py_library
py_library(name, deps, srcs, data, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, features, imports, licenses, restricted_to, srcs_version, tags, target_compatible_with, testonly, visibility)
+ py_test
py_test(name, deps, srcs, data, args, compatible_with, deprecation, distribs, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, imports, legacy_create_init, licenses, local, main, python_version, restricted_to, shard_count, size, srcs_version, stamp, tags, target_compatible_with, testonly, timeout, toolchains, visibility)
```
py_test(
name = "runtest_test",
srcs = ["runtest_test.py"],
deps = [
"//path/to/a/py/library",
],
)
py_test(
name = "runtest_test",
srcs = [
"runtest_main.py",
"runtest_lib.py",