Skip to content

Commit

Permalink
Add Docker-based build process
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriykovalev committed Jun 19, 2020
1 parent 642a289 commit f8cac10
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 112 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bazel-*
out/
WORKSPACE
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ information on using pull requests.
## Community Guidelines

This project follows
[Google's Open Source Community Guidelines](https://opensource.google/conduct/).
[Google's Open Source Community Guidelines](https://opensource.google/conduct/).
64 changes: 53 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ OS := $(shell uname -s)

ifeq ($(OS),Linux)
CPU ?= k8
WORKSPACE_PLATFORM_FILE := WORKSPACE.linux
else ifeq ($(OS),Darwin)
CPU ?= darwin
WORKSPACE_PLATFORM_FILE := WORKSPACE.darwin
else
$(error $(OS) is not supported)
endif
Expand All @@ -37,12 +39,9 @@ BAZEL_OUT_DIR := $(MAKEFILE_DIR)/bazel-out/$(CPU)-$(COMPILATION_MODE)/bin

# Linux-specific parameters
BAZEL_BUILD_TARGET_Linux := //tflite/public:libedgetpu_direct_all.so
# --experimental_repo_remote_exec for remotable parameter used in
# --`repository_rule` from TF.
BAZEL_BUILD_FLAGS_Linux := --crosstool_top=@crosstool//:toolchains \
--compiler=gcc \
--linkopt=-l:libusb-1.0.so \
--experimental_repo_remote_exec
--linkopt=-l:libusb-1.0.so
BAZEL_BUILD_OUTPUT_FILE_Linux := libedgetpu.so.1.0
BAZEL_BUILD_OUTPUT_SYMLINK_Linux := libedgetpu.so.1

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

# Common parameters
BAZEL_BUILD_FLAGS := --sandbox_debug --subcommands \
--experimental_repo_remote_exec \
--compilation_mode=$(COMPILATION_MODE) \
--define darwinn_portable=1 \
--copt=-DSTRIP_LOG=1 \
--copt=-DEDGETPU_EXTERNAL_RELEASE_RUNTIME \
--copt=-fno-rtti \
--copt=-fno-exceptions \
--copt='-D__FILE__=""' \
Expand All @@ -93,23 +92,66 @@ endef
endif
endif

.PHONY: libedgetpu \
libedgetpu-direct \
libedgetpu-throttled \
workspace \
clean

libedgetpu: libedgetpu-direct libedgetpu-throttled

libedgetpu-direct:
libedgetpu-direct: workspace
bazel build $(BAZEL_BUILD_FLAGS) $(BAZEL_BUILD_TARGET)
$(call copy_out,direct)
$(call strip_out,direct)

libedgetpu-throttled:
libedgetpu-throttled: workspace
bazel build $(BAZEL_BUILD_FLAGS) --copt=-DTHROTTLE_EDGE_TPU $(BAZEL_BUILD_TARGET)
$(call copy_out,throttled)
$(call strip_out,throttled)

workspace: bazel/WORKSPACE bazel/$(WORKSPACE_PLATFORM_FILE)
cat $^ > WORKSPACE

clean:
rm -rf $(OUT_DIR)

ifdef DOCKER_MK
################################################################################
# Docker commands
################################################################################
DOCKER_CONTEXT_DIR := $(MAKEFILE_DIR)/docker
DOCKER_WORKSPACE := $(MAKEFILE_DIR)
DOCKER_TAG_BASE=coral-libedgetpu
include $(DOCKER_MK)
endif
DOCKER_CONTAINER_WORKSPACE := /workspace
DOCKER_CPUS ?= k8 armv7a armv6 aarch64
DOCKER_TARGETS ?=
DOCKER_IMAGE ?= debian:stretch
DOCKER_TAG_BASE ?= libedgetpu-cross
DOCKER_TAG := "$(DOCKER_TAG_BASE)-$(subst :,-,$(DOCKER_IMAGE))"
DOCKER_SHELL_COMMAND ?=

DOCKER_MAKE_COMMAND := \
for cpu in $(DOCKER_CPUS); do \
make CPU=\$${cpu} -C $(DOCKER_CONTAINER_WORKSPACE) $(DOCKER_TARGETS) || exit 1; \
done

define docker_run_command
chmod a+w /; \
groupadd --gid $(shell id -g) $(shell id -g -n); \
useradd -m -e '' -s /bin/bash --gid $(shell id -g) --uid $(shell id -u) $(shell id -u -n); \
echo '$(shell id -u -n) ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers; \
su $(shell id -u -n) $(if $(1),-c '$(1)',)
endef

docker-image:
docker build $(DOCKER_IMAGE_OPTIONS) -t $(DOCKER_TAG) \
--build-arg IMAGE=$(DOCKER_IMAGE) $(DOCKER_CONTEXT_DIR)

docker-shell: docker-image
docker run --rm -i --tty --workdir $(DOCKER_CONTAINER_WORKSPACE) \
-v $(DOCKER_WORKSPACE):$(DOCKER_CONTAINER_WORKSPACE) \
$(DOCKER_TAG) /bin/bash -c "$(call docker_run_command,$(DOCKER_SHELL_COMMAND))"

docker-build: docker-image
docker run --rm -i $(shell tty -s && echo --tty) \
-v $(DOCKER_WORKSPACE):$(DOCKER_CONTAINER_WORKSPACE) \
$(DOCKER_TAG) /bin/bash -c "$(call docker_run_command,$(DOCKER_MAKE_COMMAND))"
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,31 @@ This software is distributed in the binary form at [coral.ai/software](https://c

## Building

At present only Bazel build system is supported, but it can be invoked from the Makefile.
At present only [Bazel](https://bazel.build/) build system is supported. For
proper environment setup check `docker` directory.

Build native binaries on Linux and macOS:
```
$ make
```

Build native binaries on Windows:
```
$ build.bat
```

Cross-compile for ARMv7-A (32 bit), and ARMv8-A (64 bit) on Linux:
```
$ CPU=armv7a make
$ CPU=aarch64 make
```

Build Linux binaries inside Docker container (works on Linux and macOS):
```
$ DOCKER_CPUS="k8 armv7a aarch64" DOCKER_TARGETS=libedgetpu make docker-build
```

All built binaries go to the `out` directory.

## Support

Expand Down
75 changes: 0 additions & 75 deletions WORKSPACE

This file was deleted.

15 changes: 10 additions & 5 deletions api/watchdog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ util::StatusOr<int64> TimerFdWatchdog::Activate() {
case WatchdogState::ACTIVE:
break; // Already active: Return old activation_id.
case WatchdogState::BARKING:
return util::FailedPreconditionError(
"Cannot activate a barking watchdog.");
VLOG(1) << "A barking watchdog was re-activated.";
RETURN_IF_ERROR(timer_->Set(timeout_ns_));
state_ = WatchdogState::ACTIVE;
activation_id_ = GetNextActivationId(activation_id_);
break;
case WatchdogState::INACTIVE:
VLOG(5) << "Activating the watchdog.";
RETURN_IF_ERROR(timer_->Set(timeout_ns_));
Expand Down Expand Up @@ -190,9 +193,11 @@ void TimerFdWatchdog::Watcher() {
// Acquire lock again to update shared state after calling expire_.
{
StdMutexLock lock(&mutex_);
// Watchdog might be destroyed while callback was running.
// In that case, retain the 'DESTROYED' state.
if (state_ != WatchdogState::DESTROYED) {
// While the watchdog was executing the expire_ callback (ie BARKING):
// If ~Watchdog was called, retain DESTROYED state.
// If Activate was called (re-activated), retain ACTIVE state.
// If Deactivate was called, state will change to INACTIVE now.
if (state_ == WatchdogState::BARKING) {
state_ = WatchdogState::INACTIVE;
}
}
Expand Down
10 changes: 7 additions & 3 deletions api/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ class TimerFdWatchdog : public Watchdog {
// The timer to be used for keeping track of expiration deadlines.
std::unique_ptr<Timer> timer_;

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

// Watchdog state machine:
Expand Down Expand Up @@ -280,9 +282,11 @@ class CascadeWatchdog : public Watchdog {
// A mutex to protect mutable class fields.
std::mutex mutex_;

// Specifies which child watchdog is currently active. It can be used as an
// index to configs_ or watchdogs_. -1 means all watchdogs are inactive.
// Value that indicates all watchdogs are inactive.
static constexpr int kNoneActive = -1;

// Specifies which child watchdog is currently active. A value other than
// kNoneActive can be used as an index to configs_ or watchdogs_.
int currently_active_ GUARDED_BY(mutex_){kNoneActive};

// The current/last generated activation ID for the caller of Activate on this
Expand Down
5 changes: 4 additions & 1 deletion build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ echo off

setlocal

type bazel\WORKSPACE bazel\WORKSPACE.windows > WORKSPACE 2>NUL

set THROTTLED=0
set COMPILATION_MODE=opt
set OUT_DIR=%~dp0\out
Expand Down Expand Up @@ -48,7 +50,8 @@ set BAZEL_BUILD_FLAGS= ^
--define darwinn_portable=1 ^
--copt=/DSTRIP_LOG=1 ^
--copt=/DABSL_FLAGS_STRIP_NAMES ^
--copt=/DEDGETPU_EXTERNAL_RELEASE_RUNTIME ^
--copt=/D_HAS_DEPRECATED_RESULT_OF ^
--copt=/D_HAS_DEPRECATED_ADAPTOR_TYPEDEFS ^
--copt=/GR- ^
--copt=/DWIN32_LEAN_AND_MEAN ^
--copt=/D_WINSOCKAPI_ ^
Expand Down
38 changes: 38 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ARG IMAGE
FROM ${IMAGE}

COPY update_sources.sh /
RUN /update_sources.sh

RUN dpkg --add-architecture armhf
RUN dpkg --add-architecture arm64
RUN apt-get update && apt-get install -y \
python \
python-future \
python-numpy \
build-essential \
crossbuild-essential-armhf \
crossbuild-essential-arm64 \
libusb-1.0-0-dev \
libusb-1.0-0-dev:arm64 \
libusb-1.0-0-dev:armhf \
zlib1g-dev \
zlib1g-dev:armhf \
zlib1g-dev:arm64 \
sudo \
pkg-config \
zip \
unzip \
curl \
wget \
git \
$(grep Ubuntu /etc/os-release > /dev/null && echo vim-common || echo xxd)

RUN git clone https://github.com/raspberrypi/tools.git && \
cd tools && \
git reset --hard 4a335520900ce55e251ac4f420f52bf0b2ab6b1f

ARG BAZEL_VERSION=3.2.0
RUN wget -O /bazel https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh && \
bash /bazel && \
rm -f /bazel
42 changes: 42 additions & 0 deletions docker/Dockerfile.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM mcr.microsoft.com/windows/servercore:1903
SHELL ["powershell", "-command"]

# Install chocolatey + Win10 SDK + VS build tools
RUN "Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
RUN choco install -y windows-sdk-10
RUN choco install -y visualstudio2019buildtools
RUN choco install -y visualstudio2019-workload-vctools
RUN choco install -y python3
RUN python -m pip install six numpy wheel

# Install 7-zip
RUN choco install -y 7zip
RUN setx /M PATH $($Env:PATH + ';C:\Program Files\7-Zip')

# Install msys2
ARG MSYS_VERSION=20200602
ADD http://repo.msys2.org/distrib/x86_64/msys2-base-x86_64-${MSYS_VERSION}.tar.xz c:/windows/temp
RUN 7z.exe x c:\windows\temp\msys2-base-x86_64-$env:MSYS_VERSION.tar.xz
RUN 7z.exe x c:\msys2-base-x86_64-$env:MSYS_VERSION.tar -o"c:\\"
RUN setx /M PATH $($Env:PATH + ';C:\msys64\usr\bin')

# Install patch
ARG PATCH_VERSION=2.7.6-1
ADD http://repo.msys2.org/msys/x86_64/patch-${PATCH_VERSION}-x86_64.pkg.tar.xz c:/windows/temp
RUN 7z.exe x -y c:\windows\temp\patch-$env:PATCH_VERSION-x86_64.pkg.tar.xz
RUN 7z.exe x -y c:\patch-$env:PATCH_VERSION-x86_64.pkg.tar -o"c:\\msys64"

# Install vim (for xxd)
ARG VIM_VERSION=8.2.0592-1
ADD http://repo.msys2.org/msys/x86_64/vim-${VIM_VERSION}-x86_64.pkg.tar.xz c:/windows/temp
RUN 7z.exe x -y c:\windows\temp\vim-$env:VIM_VERSION-x86_64.pkg.tar.xz
RUN 7z.exe x -y c:\vim-$env:VIM_VERSION-x86_64.pkg.tar -o"c:\\msys64"

# Install libusb release package
ARG LIBUSB_VERSION=1.0.22
ADD https://github.com/libusb/libusb/releases/download/v${LIBUSB_VERSION}/libusb-${LIBUSB_VERSION}.7z c:/windows/temp
RUN 7z x -oc:\libusb-1.0.22 c:\windows\temp\libusb-1.0.22.7z

# Install Bazel
ARG BAZEL_VERSION=3.2.0
ADD https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-windows-x86_64.exe c:/windows/system32/bazel.exe
12 changes: 12 additions & 0 deletions docker/update_sources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
. /etc/os-release

[[ "${NAME}" == "Ubuntu" ]] || exit 0

sed -i "s/deb\ /deb \[arch=amd64\]\ /g" /etc/apt/sources.list

cat <<EOT >> /etc/apt/sources.list
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME} main universe
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-updates main universe
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-security main universe
EOT
Loading

0 comments on commit f8cac10

Please sign in to comment.