Skip to content

Commit f8cac10

Browse files
Add Docker-based build process
1 parent 642a289 commit f8cac10

19 files changed

+269
-112
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bazel-*
2+
out/
3+
WORKSPACE

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ information on using pull requests.
2525
## Community Guidelines
2626

2727
This project follows
28-
[Google's Open Source Community Guidelines](https://opensource.google/conduct/).
28+
[Google's Open Source Community Guidelines](https://opensource.google/conduct/).

Makefile

+53-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ OS := $(shell uname -s)
1818

1919
ifeq ($(OS),Linux)
2020
CPU ?= k8
21+
WORKSPACE_PLATFORM_FILE := WORKSPACE.linux
2122
else ifeq ($(OS),Darwin)
2223
CPU ?= darwin
24+
WORKSPACE_PLATFORM_FILE := WORKSPACE.darwin
2325
else
2426
$(error $(OS) is not supported)
2527
endif
@@ -37,12 +39,9 @@ BAZEL_OUT_DIR := $(MAKEFILE_DIR)/bazel-out/$(CPU)-$(COMPILATION_MODE)/bin
3739

3840
# Linux-specific parameters
3941
BAZEL_BUILD_TARGET_Linux := //tflite/public:libedgetpu_direct_all.so
40-
# --experimental_repo_remote_exec for remotable parameter used in
41-
# --`repository_rule` from TF.
4242
BAZEL_BUILD_FLAGS_Linux := --crosstool_top=@crosstool//:toolchains \
4343
--compiler=gcc \
44-
--linkopt=-l:libusb-1.0.so \
45-
--experimental_repo_remote_exec
44+
--linkopt=-l:libusb-1.0.so
4645
BAZEL_BUILD_OUTPUT_FILE_Linux := libedgetpu.so.1.0
4746
BAZEL_BUILD_OUTPUT_SYMLINK_Linux := libedgetpu.so.1
4847

@@ -63,10 +62,10 @@ BAZEL_BUILD_OUTPUT_SYMLINK_Darwin := libedgetpu.1.dylib
6362

6463
# Common parameters
6564
BAZEL_BUILD_FLAGS := --sandbox_debug --subcommands \
65+
--experimental_repo_remote_exec \
6666
--compilation_mode=$(COMPILATION_MODE) \
6767
--define darwinn_portable=1 \
6868
--copt=-DSTRIP_LOG=1 \
69-
--copt=-DEDGETPU_EXTERNAL_RELEASE_RUNTIME \
7069
--copt=-fno-rtti \
7170
--copt=-fno-exceptions \
7271
--copt='-D__FILE__=""' \
@@ -93,23 +92,66 @@ endef
9392
endif
9493
endif
9594

95+
.PHONY: libedgetpu \
96+
libedgetpu-direct \
97+
libedgetpu-throttled \
98+
workspace \
99+
clean
100+
96101
libedgetpu: libedgetpu-direct libedgetpu-throttled
97102

98-
libedgetpu-direct:
103+
libedgetpu-direct: workspace
99104
bazel build $(BAZEL_BUILD_FLAGS) $(BAZEL_BUILD_TARGET)
100105
$(call copy_out,direct)
101106
$(call strip_out,direct)
102107

103-
libedgetpu-throttled:
108+
libedgetpu-throttled: workspace
104109
bazel build $(BAZEL_BUILD_FLAGS) --copt=-DTHROTTLE_EDGE_TPU $(BAZEL_BUILD_TARGET)
105110
$(call copy_out,throttled)
106111
$(call strip_out,throttled)
107112

113+
workspace: bazel/WORKSPACE bazel/$(WORKSPACE_PLATFORM_FILE)
114+
cat $^ > WORKSPACE
115+
108116
clean:
109117
rm -rf $(OUT_DIR)
110118

111-
ifdef DOCKER_MK
119+
################################################################################
120+
# Docker commands
121+
################################################################################
122+
DOCKER_CONTEXT_DIR := $(MAKEFILE_DIR)/docker
112123
DOCKER_WORKSPACE := $(MAKEFILE_DIR)
113-
DOCKER_TAG_BASE=coral-libedgetpu
114-
include $(DOCKER_MK)
115-
endif
124+
DOCKER_CONTAINER_WORKSPACE := /workspace
125+
DOCKER_CPUS ?= k8 armv7a armv6 aarch64
126+
DOCKER_TARGETS ?=
127+
DOCKER_IMAGE ?= debian:stretch
128+
DOCKER_TAG_BASE ?= libedgetpu-cross
129+
DOCKER_TAG := "$(DOCKER_TAG_BASE)-$(subst :,-,$(DOCKER_IMAGE))"
130+
DOCKER_SHELL_COMMAND ?=
131+
132+
DOCKER_MAKE_COMMAND := \
133+
for cpu in $(DOCKER_CPUS); do \
134+
make CPU=\$${cpu} -C $(DOCKER_CONTAINER_WORKSPACE) $(DOCKER_TARGETS) || exit 1; \
135+
done
136+
137+
define docker_run_command
138+
chmod a+w /; \
139+
groupadd --gid $(shell id -g) $(shell id -g -n); \
140+
useradd -m -e '' -s /bin/bash --gid $(shell id -g) --uid $(shell id -u) $(shell id -u -n); \
141+
echo '$(shell id -u -n) ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers; \
142+
su $(shell id -u -n) $(if $(1),-c '$(1)',)
143+
endef
144+
145+
docker-image:
146+
docker build $(DOCKER_IMAGE_OPTIONS) -t $(DOCKER_TAG) \
147+
--build-arg IMAGE=$(DOCKER_IMAGE) $(DOCKER_CONTEXT_DIR)
148+
149+
docker-shell: docker-image
150+
docker run --rm -i --tty --workdir $(DOCKER_CONTAINER_WORKSPACE) \
151+
-v $(DOCKER_WORKSPACE):$(DOCKER_CONTAINER_WORKSPACE) \
152+
$(DOCKER_TAG) /bin/bash -c "$(call docker_run_command,$(DOCKER_SHELL_COMMAND))"
153+
154+
docker-build: docker-image
155+
docker run --rm -i $(shell tty -s && echo --tty) \
156+
-v $(DOCKER_WORKSPACE):$(DOCKER_CONTAINER_WORKSPACE) \
157+
$(DOCKER_TAG) /bin/bash -c "$(call docker_run_command,$(DOCKER_MAKE_COMMAND))"

README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,31 @@ This software is distributed in the binary form at [coral.ai/software](https://c
66

77
## Building
88

9-
At present only Bazel build system is supported, but it can be invoked from the Makefile.
9+
At present only [Bazel](https://bazel.build/) build system is supported. For
10+
proper environment setup check `docker` directory.
11+
12+
Build native binaries on Linux and macOS:
13+
```
14+
$ make
15+
```
16+
17+
Build native binaries on Windows:
18+
```
19+
$ build.bat
20+
```
21+
22+
Cross-compile for ARMv7-A (32 bit), and ARMv8-A (64 bit) on Linux:
23+
```
24+
$ CPU=armv7a make
25+
$ CPU=aarch64 make
26+
```
27+
28+
Build Linux binaries inside Docker container (works on Linux and macOS):
29+
```
30+
$ DOCKER_CPUS="k8 armv7a aarch64" DOCKER_TARGETS=libedgetpu make docker-build
31+
```
32+
33+
All built binaries go to the `out` directory.
1034

1135
## Support
1236

WORKSPACE

-75
This file was deleted.

api/watchdog.cc

+10-5
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ util::StatusOr<int64> TimerFdWatchdog::Activate() {
8686
case WatchdogState::ACTIVE:
8787
break; // Already active: Return old activation_id.
8888
case WatchdogState::BARKING:
89-
return util::FailedPreconditionError(
90-
"Cannot activate a barking watchdog.");
89+
VLOG(1) << "A barking watchdog was re-activated.";
90+
RETURN_IF_ERROR(timer_->Set(timeout_ns_));
91+
state_ = WatchdogState::ACTIVE;
92+
activation_id_ = GetNextActivationId(activation_id_);
93+
break;
9194
case WatchdogState::INACTIVE:
9295
VLOG(5) << "Activating the watchdog.";
9396
RETURN_IF_ERROR(timer_->Set(timeout_ns_));
@@ -190,9 +193,11 @@ void TimerFdWatchdog::Watcher() {
190193
// Acquire lock again to update shared state after calling expire_.
191194
{
192195
StdMutexLock lock(&mutex_);
193-
// Watchdog might be destroyed while callback was running.
194-
// In that case, retain the 'DESTROYED' state.
195-
if (state_ != WatchdogState::DESTROYED) {
196+
// While the watchdog was executing the expire_ callback (ie BARKING):
197+
// If ~Watchdog was called, retain DESTROYED state.
198+
// If Activate was called (re-activated), retain ACTIVE state.
199+
// If Deactivate was called, state will change to INACTIVE now.
200+
if (state_ == WatchdogState::BARKING) {
196201
state_ = WatchdogState::INACTIVE;
197202
}
198203
}

api/watchdog.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ class TimerFdWatchdog : public Watchdog {
162162
// The timer to be used for keeping track of expiration deadlines.
163163
std::unique_ptr<Timer> timer_;
164164

165-
// A single mutex to protect mutable fields in this class.
165+
// A single mutex to protect mutable fields in this class. This mutex is not
166+
// held while the watchdog callback is being executed. It is safe to signal,
167+
// activate, deactivate or destroy a barking watchdog.
166168
std::mutex mutex_;
167169

168170
// Watchdog state machine:
@@ -280,9 +282,11 @@ class CascadeWatchdog : public Watchdog {
280282
// A mutex to protect mutable class fields.
281283
std::mutex mutex_;
282284

283-
// Specifies which child watchdog is currently active. It can be used as an
284-
// index to configs_ or watchdogs_. -1 means all watchdogs are inactive.
285+
// Value that indicates all watchdogs are inactive.
285286
static constexpr int kNoneActive = -1;
287+
288+
// Specifies which child watchdog is currently active. A value other than
289+
// kNoneActive can be used as an index to configs_ or watchdogs_.
286290
int currently_active_ GUARDED_BY(mutex_){kNoneActive};
287291

288292
// The current/last generated activation ID for the caller of Activate on this

build.bat

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ echo off
1515

1616
setlocal
1717

18+
type bazel\WORKSPACE bazel\WORKSPACE.windows > WORKSPACE 2>NUL
19+
1820
set THROTTLED=0
1921
set COMPILATION_MODE=opt
2022
set OUT_DIR=%~dp0\out
@@ -48,7 +50,8 @@ set BAZEL_BUILD_FLAGS= ^
4850
--define darwinn_portable=1 ^
4951
--copt=/DSTRIP_LOG=1 ^
5052
--copt=/DABSL_FLAGS_STRIP_NAMES ^
51-
--copt=/DEDGETPU_EXTERNAL_RELEASE_RUNTIME ^
53+
--copt=/D_HAS_DEPRECATED_RESULT_OF ^
54+
--copt=/D_HAS_DEPRECATED_ADAPTOR_TYPEDEFS ^
5255
--copt=/GR- ^
5356
--copt=/DWIN32_LEAN_AND_MEAN ^
5457
--copt=/D_WINSOCKAPI_ ^

docker/Dockerfile

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
ARG IMAGE
2+
FROM ${IMAGE}
3+
4+
COPY update_sources.sh /
5+
RUN /update_sources.sh
6+
7+
RUN dpkg --add-architecture armhf
8+
RUN dpkg --add-architecture arm64
9+
RUN apt-get update && apt-get install -y \
10+
python \
11+
python-future \
12+
python-numpy \
13+
build-essential \
14+
crossbuild-essential-armhf \
15+
crossbuild-essential-arm64 \
16+
libusb-1.0-0-dev \
17+
libusb-1.0-0-dev:arm64 \
18+
libusb-1.0-0-dev:armhf \
19+
zlib1g-dev \
20+
zlib1g-dev:armhf \
21+
zlib1g-dev:arm64 \
22+
sudo \
23+
pkg-config \
24+
zip \
25+
unzip \
26+
curl \
27+
wget \
28+
git \
29+
$(grep Ubuntu /etc/os-release > /dev/null && echo vim-common || echo xxd)
30+
31+
RUN git clone https://github.com/raspberrypi/tools.git && \
32+
cd tools && \
33+
git reset --hard 4a335520900ce55e251ac4f420f52bf0b2ab6b1f
34+
35+
ARG BAZEL_VERSION=3.2.0
36+
RUN wget -O /bazel https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh && \
37+
bash /bazel && \
38+
rm -f /bazel

docker/Dockerfile.windows

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM mcr.microsoft.com/windows/servercore:1903
2+
SHELL ["powershell", "-command"]
3+
4+
# Install chocolatey + Win10 SDK + VS build tools
5+
RUN "Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
6+
RUN choco install -y windows-sdk-10
7+
RUN choco install -y visualstudio2019buildtools
8+
RUN choco install -y visualstudio2019-workload-vctools
9+
RUN choco install -y python3
10+
RUN python -m pip install six numpy wheel
11+
12+
# Install 7-zip
13+
RUN choco install -y 7zip
14+
RUN setx /M PATH $($Env:PATH + ';C:\Program Files\7-Zip')
15+
16+
# Install msys2
17+
ARG MSYS_VERSION=20200602
18+
ADD http://repo.msys2.org/distrib/x86_64/msys2-base-x86_64-${MSYS_VERSION}.tar.xz c:/windows/temp
19+
RUN 7z.exe x c:\windows\temp\msys2-base-x86_64-$env:MSYS_VERSION.tar.xz
20+
RUN 7z.exe x c:\msys2-base-x86_64-$env:MSYS_VERSION.tar -o"c:\\"
21+
RUN setx /M PATH $($Env:PATH + ';C:\msys64\usr\bin')
22+
23+
# Install patch
24+
ARG PATCH_VERSION=2.7.6-1
25+
ADD http://repo.msys2.org/msys/x86_64/patch-${PATCH_VERSION}-x86_64.pkg.tar.xz c:/windows/temp
26+
RUN 7z.exe x -y c:\windows\temp\patch-$env:PATCH_VERSION-x86_64.pkg.tar.xz
27+
RUN 7z.exe x -y c:\patch-$env:PATCH_VERSION-x86_64.pkg.tar -o"c:\\msys64"
28+
29+
# Install vim (for xxd)
30+
ARG VIM_VERSION=8.2.0592-1
31+
ADD http://repo.msys2.org/msys/x86_64/vim-${VIM_VERSION}-x86_64.pkg.tar.xz c:/windows/temp
32+
RUN 7z.exe x -y c:\windows\temp\vim-$env:VIM_VERSION-x86_64.pkg.tar.xz
33+
RUN 7z.exe x -y c:\vim-$env:VIM_VERSION-x86_64.pkg.tar -o"c:\\msys64"
34+
35+
# Install libusb release package
36+
ARG LIBUSB_VERSION=1.0.22
37+
ADD https://github.com/libusb/libusb/releases/download/v${LIBUSB_VERSION}/libusb-${LIBUSB_VERSION}.7z c:/windows/temp
38+
RUN 7z x -oc:\libusb-1.0.22 c:\windows\temp\libusb-1.0.22.7z
39+
40+
# Install Bazel
41+
ARG BAZEL_VERSION=3.2.0
42+
ADD https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-windows-x86_64.exe c:/windows/system32/bazel.exe

docker/update_sources.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
. /etc/os-release
3+
4+
[[ "${NAME}" == "Ubuntu" ]] || exit 0
5+
6+
sed -i "s/deb\ /deb \[arch=amd64\]\ /g" /etc/apt/sources.list
7+
8+
cat <<EOT >> /etc/apt/sources.list
9+
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME} main universe
10+
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-updates main universe
11+
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-security main universe
12+
EOT

0 commit comments

Comments
 (0)