Add build_defs.bzl, README, CONTRIBUTING.md
diff --git a/BUILD b/BUILD
new file mode 100644
index 0000000..5c4e8ab
--- /dev/null
+++ b/BUILD
@@ -0,0 +1,4 @@
+# Bazel extensions for pybind11
+licenses(["notice"])
+
+exports_files(["LICENSE"])
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..dd6849a
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,38 @@
+Thank you for your interest in this project! Please refer to the following
+sections on how to contribute code and bug reports.
+
+### Reporting bugs
+
+Assuming that you have identified a previously unknown problem or an important
+question, it's essential that you submit a self-contained and minimal piece of
+code that reproduces the problem. In other words: no external dependencies,
+isolate the function(s) that cause breakage, submit matched and complete C++
+and Python snippets that can be easily compiled and run on my end.
+
+## Pull requests
+Contributions are submitted, reviewed, and accepted using Github pull requests.
+Please refer to [this
+article](https://help.github.com/articles/using-pull-requests) for details and
+adhere to the following rules to make the process as smooth as possible:
+
+* Make a new branch for every feature you're working on.
+* Make small and clean pull requests that are easy to review but make sure they
+  do add value by themselves.
+* This project has a strong focus on providing general solutions using a
+  minimal amount of code, thus small pull requests are greatly preferred.
+
+### Licensing of contributions
+
+pybind11_bazel is provided under a BSD-style license that can be found in the
+``LICENSE`` file. By using, distributing, or contributing to this project, you
+agree to the terms and conditions of this license.
+
+You are under no obligation whatsoever to provide any bug fixes, patches, or
+upgrades to the features, functionality or performance of the source code
+("Enhancements") to anyone; however, if you choose to make your Enhancements
+available either publicly, or directly to the author of this software, without
+imposing a separate written license agreement for such Enhancements, then you
+hereby grant the following license: a non-exclusive, royalty-free perpetual
+license to install, use, modify, prepare derivative works, incorporate into
+other computer software, distribute, and sublicense such enhancements or
+derivative works thereof, in binary and source code form.
diff --git a/README.md b/README.md
index e8d6bda..2949919 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,21 @@
 # Bazel extensions for pybind11
+
+In your build file:
+
+```
+load("//third_party/pybind11_bazel:build_defs.bzl", "pybind_extension")
+```
+
+Provided rules:
+
+- `pybind_extension`: Builds a python extension, automatically adding the
+  required build flags and pybind11 dependencies. It also defines a .so target
+  which can be manually built and copied. The arguments match a `py_extension`.
+- `pybind_library`: Builds a C++ library, automatically adding the required
+  build flags and pybind11 dependencies. This library can then be used as a
+  dependency of a `pybind_extension`. The arguments match a `cc_library`.
+- `pybind_library_test`: Builds a C++ test for a `pybind_library`. The arguments
+  match a cc_test.
+
+To test a `pybind_extension`, the most common approach is to write the test in
+python and use the standard `py_test` build rule.
diff --git a/build_defs.bzl b/build_defs.bzl
new file mode 100644
index 0000000..d5d3fe2
--- /dev/null
+++ b/build_defs.bzl
@@ -0,0 +1,113 @@
+# Copyright (c) 2019 The Pybind Development Team. All rights reserved.
+#
+# All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+
+"""Build rules for pybind11."""
+
+def register_extension_info(**kwargs): pass
+
+PYBIND_COPTS = [
+    "-fexceptions",
+]
+
+PYBIND_FEATURES = [
+    "-use_header_modules",  # Required for pybind11.
+    "-parse_headers",
+]
+
+PYBIND_DEPS = [
+    "@pybind11",
+    "@local_config_python//:python_headers",
+]
+
+# Builds a Python extension module using pybind11.
+# This can be directly used in python with the import statement.
+# This adds rules for both a py_extension for use in google3 and a
+# .so binary file that can be copied and used outside of google3.
+#
+# The .so binary must be manually built.
+# To select which Python version to use, pass the `--python_version` to blaze.
+# e.g. blaze build -c opt --python_version=PY3 path/to/my/extension.so
+def pybind_extension(
+        name,
+        copts = [],
+        features = [],
+        tags = [],
+        deps = [],
+        **kwargs):
+    # Mark common dependencies as required for build_cleaner.
+    tags = tags + ["req_dep=%s" % dep for dep in PYBIND_DEPS]
+
+    # External shared library for non-google3 users.
+    native.cc_binary(
+        name = name + ".so",
+        copts = copts + PYBIND_COPTS + ["-fvisibility=hidden"],
+        features = features + PYBIND_FEATURES,
+        linkopts = [
+            "-Wl,-Bsymbolic",
+        ],
+        linkshared = 1,
+        tags = tags + ["local", "manual"],
+        deps = deps + PYBIND_DEPS,
+        **kwargs
+    )
+
+# Builds a pybind11 compatible library. This can be linked to a pybind_extension.
+def pybind_library(
+        name,
+        copts = [],
+        features = [],
+        tags = [],
+        deps = [],
+        **kwargs):
+    # Mark common dependencies as required for build_cleaner.
+    tags = tags + ["req_dep=%s" % dep for dep in PYBIND_DEPS]
+
+    native.cc_library(
+        name = name,
+        copts = copts + PYBIND_COPTS,
+        features = features + PYBIND_FEATURES,
+        tags = tags,
+        deps = deps + PYBIND_DEPS,
+        **kwargs
+    )
+
+# Builds a C++ test for a pybind_library.
+def pybind_library_test(
+        name,
+        copts = [],
+        features = [],
+        tags = [],
+        deps = [],
+        **kwargs):
+    # Mark common dependencies as required for build_cleaner.
+    tags = tags + ["req_dep=%s" % dep for dep in PYBIND_DEPS]
+
+    native.cc_test(
+        name = name,
+        copts = copts + PYBIND_COPTS,
+        features = features + PYBIND_FEATURES,
+        tags = tags,
+        deps = deps + PYBIND_DEPS + [
+            "//util/python:python_impl",
+            "//util/python:test_main",
+        ],
+        **kwargs
+    )
+
+# Register extension with build_cleaner.
+register_extension_info(
+    extension = pybind_extension,
+    label_regex_for_dep = "{extension_name}",
+)
+
+register_extension_info(
+    extension = pybind_library,
+    label_regex_for_dep = "{extension_name}",
+)
+
+register_extension_info(
+    extension = pybind_library_test,
+    label_regex_for_dep = "{extension_name}",
+)