Skip to content

Commit

Permalink
Merge pull request halide#2118 from halide/bazel-lite
Browse files Browse the repository at this point in the history
Add Bazel build rules to distrib packages
  • Loading branch information
steven-johnson authored Jul 13, 2017
2 parents bed1bba + 17d6033 commit 4486d40
Show file tree
Hide file tree
Showing 14 changed files with 1,414 additions and 18 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
/apps/*/bin
/apps/HelloMatlab/blurred.png
/apps/HelloMatlab/iir_blur.mex
bazel-bin
bazel-genfiles
bazel-Halide
bazel-out
bazel-testlogs
bazel-*
bin/*
build-64/*
build-ios/*
Expand Down
28 changes: 17 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@ compiler:
#- clang
- gcc
env:
# Configurations
#
# Each line in the ``env`` section represents a set of environment
# variables passed to a build configuration
#
# Test a mix of llvm versions and a mix of build systems.
# Note that the Make build ignores the HALIDE_SHARED_LIBRARY option.
# Don't build as a static library with cmake. It risks exceeding the travis memory limit.
- LLVM_VERSION=3.7.1 BUILD_SYSTEM=MAKE CXX_=g++-4.8 CC_=gcc-4.8
- LLVM_VERSION=3.8.1 BUILD_SYSTEM=MAKE CXX_=g++-4.8 CC_=gcc-4.8
- LLVM_VERSION=3.8.1 BUILD_SYSTEM=CMAKE CXX_=g++-4.8 CC_=gcc-4.8 HALIDE_SHARED_LIBRARY=1
global:
- BAZEL_VERSION="0.5.2"
- JDK=openjdk8
matrix:
# Configurations
#
# Each line in the ``env`` section represents a set of environment
# variables passed to a build configuration
#
# Test a mix of llvm versions, a mix of build systems, and a mix of shared vs static library
# Don't build as a static library with cmake. It risks exceeding the travis memory limit.
- LLVM_VERSION=3.7.1 BUILD_SYSTEM=MAKE CXX_=g++-4.8 CC_=gcc-4.8
- LLVM_VERSION=3.8.1 BUILD_SYSTEM=MAKE CXX_=g++-4.8 CC_=gcc-4.8
- LLVM_VERSION=3.8.1 BUILD_SYSTEM=CMAKE CXX_=g++-4.8 CC_=gcc-4.8 HALIDE_SHARED_LIBRARY=1
cache: apt
dist: trusty
# Note the commands below are written assuming Ubuntu 12.04LTS
before_install:
# Needed for new libstdc++ and gcc4.8
- export CXX=${CXX_}
- export CC=${CC_}
- sudo add-apt-repository --yes ppa:ubuntu-toolchain-r/test/
- sudo apt-get update
- wget https://github.com/bazelbuild/bazel/releases/download/"${BAZEL_VERSION}"/bazel_"${BAZEL_VERSION}"-linux-x86_64.deb
- sudo dpkg -i bazel_"${BAZEL_VERSION}"-linux-x86_64.deb
install:
- sudo apt-get -y --force-yes install ${CXX} ${CC} libedit-dev
# Make gcc4.8 the default gcc version
Expand Down
47 changes: 45 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ else
endif
endif

BAZEL ?= $(shell which bazel)

SHELL = bash
CXX ?= g++
PREFIX ?= /usr/local
Expand Down Expand Up @@ -1425,6 +1427,24 @@ test_apps: $(LIB_DIR)/libHalide.a $(BIN_DIR)/libHalide.$(SHARED_EXT) $(INCLUDE_D
make -C apps/linear_algebra test HALIDE_BIN_PATH=$(CURDIR) HALIDE_SRC_PATH=$(ROOT_DIR) ; \
fi

# Bazel depends on the distrib archive being built
.PHONY: test_bazel
test_bazel: $(DISTRIB_DIR)/halide.tgz
# Only test bazeldemo if Bazel is installed
if [ -z "$(BAZEL)" ]; then echo "Bazel is not installed"; exit 1; fi
mkdir -p apps
# Make a local copy of the apps if we're building out-of-tree,
# because the app Makefiles are written to build in-tree
if [ "$(ROOT_DIR)" != "$(CURDIR)" ]; then \
echo "Building out-of-tree, so making local copy of apps"; \
cp -r $(ROOT_DIR)/apps/bazeldemo apps; \
cp -r $(ROOT_DIR)/tools .; \
fi
cd apps/bazeldemo; \
CXX=`echo ${CXX} | sed 's/ccache //'` \
CC=`echo ${CC} | sed 's/ccache //'` \
bazel build --verbose_failures :all

.PHONY: test_python
test_python: $(LIB_DIR)/libHalide.a $(INCLUDE_DIR)/Halide.h
mkdir -p python_bindings
Expand Down Expand Up @@ -1524,7 +1544,11 @@ ifeq ($(UNAME), Darwin)
install_name_tool -id $(PREFIX)/lib/libHalide.$(SHARED_EXT) $(PREFIX)/lib/libHalide.$(SHARED_EXT)
endif

$(DISTRIB_DIR)/halide.tgz: $(LIB_DIR)/libHalide.a $(BIN_DIR)/libHalide.$(SHARED_EXT) $(INCLUDE_DIR)/Halide.h $(RUNTIME_EXPORTED_INCLUDES)
$(BUILD_DIR)/halide_config.bzl: $(ROOT_DIR)/bazel/create_halide_config.sh
-mkdir -p $(BUILD_DIR)
$< > $@

$(DISTRIB_DIR)/halide.tgz: $(LIB_DIR)/libHalide.a $(BIN_DIR)/libHalide.$(SHARED_EXT) $(INCLUDE_DIR)/Halide.h $(RUNTIME_EXPORTED_INCLUDES) $(ROOT_DIR)/bazel/* $(BUILD_DIR)/halide_config.bzl
mkdir -p $(DISTRIB_DIR)/include $(DISTRIB_DIR)/bin $(DISTRIB_DIR)/lib $(DISTRIB_DIR)/tutorial $(DISTRIB_DIR)/tutorial/images $(DISTRIB_DIR)/tools $(DISTRIB_DIR)/tutorial/figures
cp $(BIN_DIR)/libHalide.$(SHARED_EXT) $(DISTRIB_DIR)/bin
cp $(LIB_DIR)/libHalide.a $(DISTRIB_DIR)/lib
Expand All @@ -1546,8 +1570,27 @@ $(DISTRIB_DIR)/halide.tgz: $(LIB_DIR)/libHalide.a $(BIN_DIR)/libHalide.$(SHARED_
cp $(ROOT_DIR)/tools/halide_image_io.h $(DISTRIB_DIR)/tools
cp $(ROOT_DIR)/tools/halide_image_info.h $(DISTRIB_DIR)/tools
cp $(ROOT_DIR)/README.md $(DISTRIB_DIR)
cp $(ROOT_DIR)/bazel/BUILD $(DISTRIB_DIR)
cp $(ROOT_DIR)/bazel/halide.bzl $(DISTRIB_DIR)
cp $(ROOT_DIR)/bazel/README_bazel.md $(DISTRIB_DIR)
cp $(ROOT_DIR)/bazel/WORKSPACE $(DISTRIB_DIR)
cp $(BUILD_DIR)/halide_config.bzl $(DISTRIB_DIR)
ln -sf $(DISTRIB_DIR) halide
tar -czf $(DISTRIB_DIR)/halide.tgz halide/bin halide/lib halide/include halide/tutorial halide/README.md halide/tools/mex_halide.m halide/tools/*.cpp halide/tools/halide_image.h halide/tools/halide_image_io.h halide/tools/halide_image_info.h
tar -czf $(DISTRIB_DIR)/halide.tgz \
halide/bin \
halide/lib \
halide/include \
halide/tutorial \
halide/BUILD \
halide/README.md \
halide/README_bazel.md \
halide/WORKSPACE \
halide/*.bzl \
halide/tools/mex_halide.m \
halide/tools/*.cpp \
halide/tools/halide_image.h \
halide/tools/halide_image_io.h \
halide/tools/halide_image_info.h
rm -rf halide

.PHONY: distrib
Expand Down
15 changes: 15 additions & 0 deletions apps/bazeldemo/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@halide//:halide.bzl", "halide_library")

halide_library(
name="bazeldemo",
srcs=["bazeldemo_generator.cpp"]
)

cc_binary(
name = "main",
srcs = ["main.cpp"],
deps = [
":bazeldemo",
"@halide//:halide_buffer"
],
)
5 changes: 5 additions & 0 deletions apps/bazeldemo/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Note that this requires you to have built the toplevel Halide 'distrib' folder via 'make distrib'
local_repository(
name = "halide",
path = "../../distrib",
)
33 changes: 33 additions & 0 deletions apps/bazeldemo/bazeldemo_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "Halide.h"

namespace {

class BazelDemo : public Halide::Generator<BazelDemo> {
public:
GeneratorParam<bool> vectorize{"vectorize", true};
GeneratorParam<bool> parallelize{"parallelize", true};

Input<Buffer<float>> input{"input", 2};
Input<float> scale{"scale"};

Output<Buffer<float>> output{"output", 2};

void generate() {
output(x, y) = input(x, y) * scale;
}
void schedule() {
if (vectorize) {
output.vectorize(x, natural_vector_size<float>());
}
if (parallelize) {
output.parallel(y);
}
}

private:
Var x, y;
};

HALIDE_REGISTER_GENERATOR(BazelDemo, "bazeldemo")

} // namespace
39 changes: 39 additions & 0 deletions apps/bazeldemo/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <cmath>
#include <cstdio>

#include "HalideBuffer.h"
#include "bazeldemo.h" // Generated by Bazel via halide_library() rule

int main(int argc, char **argv) {
constexpr int kEdge = 30;
constexpr float kMax = kEdge * kEdge;

Halide::Runtime::Buffer<float> input(kEdge, kEdge);
for (int x = 0; x < kEdge; ++x) {
for (int y = 0; y < kEdge; ++y) {
input(x, y) = static_cast<float>(x + y) / kMax;
}
}

const float kScale = 0.5f;
Halide::Runtime::Buffer<float> output(kEdge, kEdge);
int result = bazeldemo(input, kScale, output);
if (result != 0) {
fprintf(stderr, "Failure: %d\n", result);
return -1;
}

for (int x = 0; x < kEdge; ++x) {
for (int y = 0; y < kEdge; ++y) {
const float expected = input(x, y) * kScale;
constexpr float kEpsilon = 0.00001f;
if (fabs(expected - output(x, y)) > kEpsilon) {
fprintf(stderr, "Expected %f, Got %f\n", expected, output(x, y));
return -1;
}
}
}

printf("Success!\n");
return 0;
}
87 changes: 87 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Bazel build rules for clients using Halize distributions.
# Note that these rules aren't meant to build Halide itself;
# they assume that a Halide library has already been built,
# and that a downstream client wants to use it.
#
# These rules have only been tested with Bazel 0.5+, and are unlikely
# to work with earlier versions of Bazel.

package(
default_visibility = ["//visibility:public"],
)

load(
"@halide//:halide.bzl",
"halide_config_settings",
"halide_language_copts",
"halide_library_runtimes",
)

halide_config_settings()

halide_library_runtimes()

cc_library(
name = "language",
hdrs = ["include/Halide.h"],
copts = halide_language_copts(),
includes = ["include"],
deps = [
":lib_halide_static",
":runtime",
],
)

# You should rarely need to add an explicit dep on this library
# (the halide_library() rule will add it for you), but there are
# unusual circumstances where it is necessary.
cc_library(
name = "runtime",
hdrs = glob(["include/HalideRuntime*.h"]),
includes = ["include"],
)

# Header-only library to let clients to use Halide::Buffer at runtime.
# (Generators should never need to use this library.)
cc_library(
name = "halide_buffer",
hdrs = glob(["include/HalideBuffer*.h"]),
includes = ["include"],
)

# Config setting to catch the case where someone is trying to build
# on Windows, but forgot to specify --host_cpu=x64_windows_msvc AND
# --cpu=x64_windows_msvc.
config_setting(
name = "windows_not_using_msvc",
values = {"cpu": "x64_windows"},
)

cc_library(
name = "lib_halide_static",
srcs = select({
":windows_not_using_msvc": [
"please_set_host_cpu_and_cpu_to_x86_64_windows",
],
"@halide//:halide_config_x86_64_windows": [
"Release/Halide.lib",
"Release/Halide.dll",
],
"//conditions:default": [
"lib/libHalide.a",
],
}),
visibility = ["//visibility:private"],
)

# This library is visibility:public, because any package that uses the
# halide_library() rule will implicitly need access to it; that said, it is
# intended only for the private, internal use of the halide_library() rule.
# Please don't depend on it directly; doing so will likely break your code at
# some point in the future.
cc_library(
name = "internal_halide_generator_glue",
srcs = ["tools/GenGen.cpp"],
includes = ["include"],
deps = [":language"],
)
Loading

0 comments on commit 4486d40

Please sign in to comment.