-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBeginning Bazel, building and testing for Java, Go and More=P.J.McNerney;Note=Erxin.txt
582 lines (469 loc) · 16.6 KB
/
Beginning Bazel, building and testing for Java, Go and More=P.J.McNerney;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
Beginning Bazel, building and testing for Java, Go and More=P.J.McNerney;Note=Erxin
# Introduction
- reference
https://learning.oreilly.com/library/view/beginning-bazel-building/9781484251942/
- windows should eanble developer mode from windows settings
- MSYS2 is a platform that provides some basic tools for software distribution and building; in this context, we will be most interested in the fact that it provides a bash shell for Windows.
- install bazel
+ linux
~$ sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python3
+ windows
install vc 2015 redistributable
msys2 is a platform that provide some basic tools for software distribution and building
http://www.msys2.org
executable from https://github.com/bazelbuild/bazel/releases/download/1.0.0/bazel-1.0.0-windows-x86_64.exe.
Add the path to your MSYS2 .bashrc file.
export PATH="$PATH:/c/Users/<username>/bin"
verify using Bazel you are using.
pjmcn@WINDOWS-HOME MSYS~
$ bazel –-version
bazel 1.0.0
install c++ build tool, Navigate to https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019. Download the Build Tools
install java, Navigate to www.oracle.com/technetwork/java/javase/downloads/index.html
install python, Navigate to www.python.org/downloads/release/python-2716 and download the Windows x86-64 MSI Installer
# Build your first bazel project
- java
+ create hello world java source file
// src/HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!);
}
}
// src/BUILD
java_binary(
name = "HelloWorld",
srcs = ["HelloWorld.java"],
)
+ build with bazel
$ bazel build src:HelloWorld
+ run the output binary
$ src/HelloWorld
or
$ bazel run src:HelloWorld
+ add build dependency
// src/IntMultiplier.java
public class IntMultiplier {
private int a;
private int b;
public IntMultiplier(int a, int b) {
this.a = a;
this.b = b;
}
public int GetProduct() {
return a * b;
}
}
// update src/HelloWorld
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
IntMultiplier im = new IntMultiplier(3, 4);
System.out.println(im.GetProduct());
}
}
// update src/BUILD
java_binary(
name = "HelloWorld",
srcs = [
"HelloWorld.java",
"IntMultiplier.java",
],
)
$ bazel run src:HelloWorld
+ build java library instead of binary
// src/BUILD
java_library(
name = "LibraryExample",
srcs = ["IntMultiplier.java"],
)
//reference the java library
java_binary(
name = "HelloWorld",
srcs = [ "HelloWorld.java"],
deps = [":LibraryExample"],
)
- setup java test dependency
package(default_visibility = ["//visibility:public"])
java_import(
name = "junit4",
jars = [
"hamcrest/hamcrest-core-1.3.jar",
"junit/junit-4.12.jar",
]
)
$ bazel build third_party:junit4
// src/BUILD
java_test(
name = "LibraryExampleTest",
srcs = ["IntMultiplierTest.java"],
deps = [
":LibraryExample",
"//third_party:junit4",
],
test_class = "IntMultiplierTest",
)
$ bazel test src:LibraryExampleTest
- build everything in a directory
$ bazel build src:all
- clean
$ bazel clean
# Workspace file functionality
- The load command will be used both within WORKSPACE and BUILD files
load("//local/path/to/my:file.bzl", "symbol_to_load")
a new file type: .bzl. define rules for Bazel (e.g., build rules) and give us the ability to expand Bazel’s capabilities
- The very first element of the path is @bazel_tools. The @ signifies to Bazel that you are loading from a particular Bazel repository, called bazel_tools
- even if you create new external dependencies in your WORKSPACE file, if you never use anything from said dependencies, Bazel will not download them.
load("//local/path/to/my:file.bzl", "symbol_to_load_1", "symbol_to_load_2", "symbol_to_load_3")
- http_archive is used to reference and retrieve a compressed Bazel repository,
http_archive (
name = "foo",
urls = ["http://my_favorite_url.com/path/to/archive.zip"],
)
- git_repository is used to clone a git repository and check it out at a given commit (or tag).
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "io_bazel_rules_go",
remote = "https://github.com/bazelbuild/rules_go.git",
commit = "f5cfc31d4e8de28bf19d0fb1da2ab8f4be0d2cde",
)
both tag and commit, you can also use “branch” to refer to a specific branch of a Git repo
- http_archive It is also faster to download and extract an archive than to clone a reposition
- employing a new language
go_rules_dependencies()
go_register_toolchains()
+ add go target
go_binary(
name = "hello_world_go",
srcs = ["hello_world.go"],
)
+ locating the go language
io_bazel_rules_go(repo)
- additional languages
Go to https://github.com/bazelbuild and look through the various rules packages they have available
# A simple echo client/server program
- BUILD file and add the following.
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_binary")
- GO echo server
//Save this to chapter_05/src/echo_server.go.
package main
import (
"log"
"net"
)
func main() {
log.Println("Spinning up the Echo Server in Go...")
listen, error := net.Listen("tcp", ":1234")
if error != nil {
log.Panicln("Unable to listen: " + error.Error())
}
defer listen.Close()
connection, error := listen.Accept()
if error != nil {
log.Panicln("Cannot accept a connection! Error: " + error.Error())
}
log.Println("Receiving on a new connection")
defer connection.Close()
defer log.Println("Connection now closed.")
buffer := make([]byte, 2048)
size, error := connection.Read(buffer)
if error != nil {
log.Println("Cannot read from the buffer! Error: " + error.Error())
}
data := string(buffer[:size])
log.Println("Received data: " + data)
connection.Write([]byte("Echoed from Go: " + data))
}
// go BUILD
go_binary(
name = "echo_server",
srcs = ["echo_server.go"],
)
// java client
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class EchoClient {
public static void main (String args[]) {
System.out.println("Spinning up the Echo Client in Java...");
try {
final Socket socketToServer = new Socket("localhost", 1234);
final BufferedReader inputFromServer = new BufferedReader(
new InputStreamReader(socketToServer.getInputStream()));
final BufferedReader commandLineInput = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Waiting on input from the user...");
final String inputFromUser = commandLineInput.readLine();
if (inputFromUser != null) {
System.out.println("Received by Java: " + inputFromUser);
final PrintWriter outputToServer =
new PrintWriter(socketToServer.getOutputStream(), true);
outputToServer.println(inputFromUser);
System.out.println(inputFromServer.readLine());
}
socketToServer.close();
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}
// java BUILD file
java_binary(
name = "EchoClient",
srcs = ["EchoClient.java"],
)
$ bazel build :echo_client
java_binary(
name = "echo_client",
srcs = ["EchoClient.java"],
main_class = "EchoClient",
)
# Protocol buffers and bazel
- Protocol Buffer (often referred to as protobuf). Yet another creation from Google, Protocol Buffers provide a way to describe the structure of objects in a declarative and type-safe fashion and provide a wire format for serialization
- setup workspace
$ mkdir chapter_06
$ cd chapter_06
$ touch WORKSPACE
// WORKSPACE
http_archive(
name = "rules_proto",
strip_prefix = "rules_proto-97d8af4dc474595af3900dd85cb3a29ad28cc313",
urls = ["https://github.com/bazelbuild/rules_proto/archive/97d8af4dc474595af3900dd85cb3a29ad28cc313.tar.gz",],
)
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()
$ touch transmission_object.proto
// transmission_object
syntax = "proto3";
package transmission_object;
message TransmissionObject {
float value = 1;
string message = 2;
}
- the Protocol Buffer (often referred to as protobuf). Yet another creation from Google, Protocol Buffers provide a way to describe the structure of objects in a declarative and type-safe fashion and provide a wire format for serialization.
$ bazel build :transmission_object_proto
// BUILD file .
load("@rules_proto//proto:defs.bzl", "proto_library")
proto_library(
name = "transmission_object_proto",
srcs = ["transmission_object.proto"],
)
- bring in a dependency on the proto library itself (github.com/golang/protobuf/proto) in order to perform the unmarshaling/marshaling of the object
// BUILD
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
go_binary(
name = "echo_server",
srcs = ["echo_server.go"],
deps = [
":transmission_object_go_proto",
"@com_github_golang_protobuf//proto:go_default_library",
],
)
- dependency tracking
echo_client -> transimission_object_java_proto ->| transmission_object_proto
echo_server -> transmision_object_go_proto ->|
# Code organization and bazel
- reference build targets outside of the current package
java_binary(
name = "echo_client",
srcs = ["EchoClient.java"],
main_class = "EchoClient",
deps = ["//proto:transmission_object_java_proto"],
)
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
go_binary(
name = "echo_server",
srcs = ["echo_server.go"],
deps = [
"//proto:transmission_object_go_proto",
"@com_github_golang_protobuf//proto:go_default_library",
],
)
the root of the WORKSPACE (which is indicated by //). This is an important point: dependencies are not specified using paths relative to the current BUILD file
- Package visibility
// Open the proto/BUILD file and add the following directive.
package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
proto_library(
name = "transmission_object_proto",
srcs = ["transmission_object.proto"],
)
java_proto_library(
name = "transmission_object_java_proto",
deps = [":transmission_object_proto"],
)
go_proto_library(
name = "transmission_object_go_proto",
proto = ":transmission_object_proto",
importpath = "transmission_object"
)
+ Fortunately, we can do better. Bazel provides the ability to explicitly specify paths for target visibility
package(default_visibility = ["//src:__pkg__"])
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
+ individual target visibilities.
```
#package(default_visibility = ["//src: :__pkg__"])
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
proto_library(
name = "transmission_object_proto",
srcs = ["transmission_object.proto"],
)
java_proto_library(
name = "transmission_object_java_proto",
deps = [":transmission_object_proto"],
visibility = ["//src:__pkg__"],
)
go_proto_library(
name = "transmission_object_go_proto",
proto = ":transmission_object_proto",
importpath = "transmission_object",
)
```
A package_group allows you to assign metadata (e.g., visibility rules) across a set of packages.
# gRPC and Bazel
- As a corollary, Bazel provided an easy way to depend upon the Protocol Buffers. The Protocol Buffer format is used to define APIs via gRPC.
- The Skylib library contains a number of useful functions and rules that are used when creating custom build rules
- Gazelle is unique in that it is a build file generator for Bazel projects.
- create a new file within the proto directory to house the new API.
syntax = "proto3";
import "proto/transmission_object.proto";
package transceiver;
message EchoRequest {
transmission_object.TransmissionObject from_client = 1;
}
message EchoResponse {
transmission_object.TransmissionObject from_server = 1;
}
service Transceiver {
rpc Echo (EchoRequest) returns (EchoResponse);
}
proto_library(
name = "transceiver_proto",
srcs = ["transceiver.proto"],
deps = [
":transmission_object_proto",
]
)
go_proto_library(
name = "transceiver_go_proto_grpc",
compiler = "@io_bazel_rules_go//proto:go_grpc",
proto = ":transceiver_proto",
importpath = "transceiver",
deps = [":transmission_object_go_proto",],
visibility = ["//server/echo_server:__pkg__"],
)
java_proto_library(
name = "transceiver_java_proto",
deps = [":transceiver_proto"],
visibility = ["//client/echo_client:__subpackages__"],
)
load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library")
java_grpc_library(
name = "transceiver_java_proto_grpc",
srcs = [":transceiver_proto"],
deps = [":transceiver_java_proto"],
visibility = ["//client/echo_client:__subpackages__"],
)
java_binary(
name = "command_line",
srcs = ["EchoClient.java"],
main_class = "EchoClient",
runtime_deps = [
"@io_grpc_grpc_java//netty",
],
deps = [
"//proto:transmission_object_java_proto",
"//proto:transceiver_java_proto",
"//proto:transceiver_java_proto_grpc",
"@io_grpc_grpc_java//api",
]
)
$ bazel build client/echo_client/command_line
# Bazel android
- build file
load("@rules_android//android:rules.bzl", "android_library", "android_binary")
android_library(
name = "echo_client_android_activity",
srcs = ["EchoClientMainActivity.java"],
manifest = "AndroidManifest.xml",
custom_package = "client.echo_client.android",
resource_files = [
"res/layout/echo_client_main_activity.xml"
],
)
- Bazel provides a very useful command mobile-install. In a similar fashion to run, mobile-install builds the target application and then installs onto the connected device
# Bazel and ios
- WORKSPACE file and add the following.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
skylib_version = "0.8.0"
http_archive(
name = "bazel_skylib",
url = "https://github.com/bazelbuild/bazel-skylib/releases/download/{}/bazel-skylib.{}.tar.gz".format(skylib_version, skylib_version),
)
git_repository(
name ="build_bazel_rules_apple",
commit="1445924a158a89ad634f562c84a600a3435ef8c2",
remote="https://github.com/bazelbuild/rules_apple.git",
)
load(
"@build_bazel_rules_apple//apple:repositories.bzl",
"apple_rules_dependencies",
)
apple_rules_dependencies()
load(
"@build_bazel_rules_swift//swift:repositories.bzl",
"swift_rules_dependencies",
)
swift_rules_dependencies()
load(
"@build_bazel_apple_support//lib:repositories.bzl",
"apple_support_dependencies",
)
apple_support_dependencies()
- a BUILD file and add the following to it.
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
swift_library(
name = "Lib",
srcs = [
"AppDelegate.swift",
"MainViewController.swift",
],
)
ios_application(
name = "EchoClient",
bundle_id = "com.beginning-bazel.echo-client",
families = ["iphone"],
infoplists = [":Info.plist"],
minimum_os_version = "11.0",
deps = [":Lib"],
)
- Open Xcode, and navigate to Xcode > Open Developer Tool > Simulator.
# Unix utilities. Specifically:
ar — archive library builder
bzip2 — bzip2 command for distribution generation
bunzip2 — bunzip2 command for distribution checking
chmod — change permissions on a file
cat — output concatenation utility
cp — copy files
date — print the current date/time
echo — print to standard output
egrep — extended regular expression search utility
find — find files/dirs in a file system
grep — regular expression search utility
gzip — gzip command for distribution generation
gunzip — gunzip command for distribution checking
install — install directories/files
mkdir — create a directory
mv — move (rename) files
ranlib — symbol table builder for archive libraries
rm — remove (delete) files and directories
sed — stream editor for transforming output
sh — Bourne shell for make build scripts
tar — tape archive for distribution generation
test — test things in file system
unzip — unzip command for distribution checking
zip — zip command for distribution generation