#!/usr/bin/env python3
#
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import distutils.util

import setuptools

with open("../VERSION") as f:
    version = f.read().strip()
with open("requirements.txt") as f:
    requirements = [ln.split("#")[0].rstrip() for ln in f.readlines()]

setuptools.setup(
    name="compiler_gym_examples",
    version=version,
    description="Example code for CompilerGym",
    author="Facebook AI Research",
    url="https://github.com/facebookresearch/CompilerGym",
    license="MIT",
    install_requires=requirements,
    packages=[
        "llvm_autotuning",
        "llvm_autotuning.autotuners",
        "llvm_rl",
        "llvm_rl.model",
    ],
    python_requires=">=3.8",
    platforms=[distutils.util.get_platform()],
    zip_safe=False,
)

examples/setup.py
#!/usr/bin/env python3
#
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import distutils.util

import setuptools

with open("../VERSION") as f:
    version = f.read().strip()
with open("requirements.txt") as f:
    requirements = [ln.split("#")[0].rstrip() for ln in f.readlines()]
with open("../tests/requirements.txt") as f:
    requirements += [ln.split("#")[0].rstrip() for ln in f.readlines()]

setuptools.setup(
    name="compiler_gym_examples",
    version=version,
    description="Example code for CompilerGym",
    author="Facebook AI Research",
    url="https://github.com/facebookresearch/CompilerGym",
    license="MIT",
    install_requires=requirements,
    packages=[
        "llvm_autotuning",
        "llvm_autotuning.autotuners",
        "llvm_rl",
        "llvm_rl.model",
    ],
    python_requires=">=3.8",
    platforms=[distutils.util.get_platform()],
    zip_safe=False,
)

examples/setup.py
diff --git a/.github/actions/install-runtime-dependencies/action.yaml b/.github/actions/install-runtime-dependencies/action.yaml
index 7b2fad2cf..a1c111567 100644
--- a/.github/actions/install-runtime-dependencies/action.yaml
+++ b/.github/actions/install-runtime-dependencies/action.yaml
@@ -9,11 +9,5 @@ runs:
               if [ "$(uname)" = "Darwin" ]; then
                   brew install zlib
               fi
-          shell: bash
-
-        # DGL creates ~/.dgl on first run and I have found that this will fail
-        # if run from pytest / bazel. Suppress errors if DGL is not installed.
-        - name: Initialize DGL
-          run: |
-              python3 -c 'import dgl; print(dgl.__version__)' || true
+              python -m pip install -U pip wheel
           shell: bash
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 2f6f41716..878dd44c4 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -475,14 +475,14 @@ jobs:
             - name: Install wheel
               run: python -m pip install *.whl
 
-            - name: Install test dependencies
-              run: python -m pip install -r examples/requirements.txt -r tests/requirements.txt
-
             - name: Install runtime dependencies
               uses: ./.github/actions/install-runtime-dependencies
 
+            - name: Install test dependencies
+              run: make -C examples install
+
             - name: Run the test suite
-              run: make examples-test
+              run: make -C examples test
 
             - name: Upload coverage report to Codecov
               uses: codecov/codecov-action@v2
@@ -510,14 +510,14 @@ jobs:
             - name: Install wheel
               run: python -m pip install *.whl
 
-            - name: Install test dependencies
-              run: python -m pip install -r examples/requirements.txt -r tests/requirements.txt
-
             - name: Install runtime dependencies
               uses: ./.github/actions/install-runtime-dependencies
 
+            - name: Install test dependencies
+              run: make -C examples install
+
             - name: Run the test suite
-              run: make examples-test
+              run: make -C examples test
 
             - name: Upload coverage report to Codecov
               uses: codecov/codecov-action@v2
diff --git a/Makefile b/Makefile
index eabaeff5a..f375ef586 100644
--- a/Makefile
+++ b/Makefile
@@ -51,10 +51,6 @@ Post-installation Tests
         the fuzz tests for a minimum of one minute. This requires that the
         CompilerGym package has been installed (`make install`).
 
-    make examples-test
-        Run pytest in the examples directory. This requires that the CompilerGym
-        package has been installed (`make install`).
-
 
 Documentation
 -------------
@@ -169,9 +165,6 @@ DISTTOOLS_OUTS := \
 	build \
 	compiler_gym.egg-info \
 	dist \
-	examples/build \
-	examples/compiler_gym_examples.egg-info \
-	examples/dist \
 	$(NULL)
 
 BUILD_TARGET ?= //:package
@@ -333,12 +326,6 @@ endef
 install-test: | install-test-setup
 	$(call pytest,--no-success-flaky-report --benchmark-disable -n auto -k "not fuzz" --durations=5)
 
-examples-pip-install:
-	cd examples && python setup.py install
-
-examples-test: examples-pip-install
-	cd examples && pytest --no-success-flaky-report --benchmark-disable -n auto --durations=5 . --cov=compiler_gym --cov-report=xml:$(COV_REPORT) $(PYTEST_ARGS)
-
 # Note we export $CI=1 so that the tests always run as if within the CI
 # environement. This is to ensure that the reported coverage matches that of
 # the value on: https://codecov.io/gh/facebookresearch/CompilerGym
@@ -352,11 +339,7 @@ FUZZ_SECONDS ?= 300
 install-fuzz: install-test-setup
 	$(call pytest,--no-success-flaky-report -p no:sugar -x -vv -k fuzz --seconds=$(FUZZ_SECONDS))
 
-post-install-test:
-	$(MAKE) -C examples/makefile_integration clean
-	SEARCH_TIME=3 $(MAKE) -C examples/makefile_integration test
-
-.PHONY: test post-install-test examples-pip-install examples-test
+.PHONY: test post-install-test
 
 
 ################
@@ -370,6 +353,7 @@ pip-install: uninstall
 	$(PYTHON) setup.py install
 
 install: |  init-runtime-requirements bazel-build pip-install
+	$(MAKE) -C examples install
 
 .PHONY: pip-install install
 
@@ -402,6 +386,7 @@ distclean: clean
 
 uninstall:
 	$(PYTHON) -m pip uninstall -y compiler_gym
+	$(MAKE) -C examples uninstall
 
 purge: distclean uninstall
 	rm -rf $(COMPILER_GYM_DATA_FILE_LOCATIONS)
diff --git a/examples/Makefile b/examples/Makefile
index 14ca67d1f..6b9bcf4d2 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -9,14 +9,59 @@ CompilerGym usage examples. See README.md for details. Available targets:
         Install the example python packages.
 
     make test
-        Run the example unit tests.
+        Run pytest in this directory.
+
+    make clean
+        Remove build artifacts.
+
+    make uninstall
+        Uninstall the example python packages.
 endef
 export HELP
 
+# The path of the repository reoot.
+ROOT := $(realpath $(dir $(realpath $(firstword $(MAKEFILE_LIST))))/..)
+
 PYTHON ?= python3
 
+# Extra command line arguments for pytest.
+PYTEST_ARGS ?=
+
+# The path of the XML pytest coverage report to generate when running the
+# install-test-cov target.
+COV_REPORT ?= $(ROOT)/coverage.xml
+
+DISTTOOLS_OUTS := \
+	build \
+	compiler_gym_examples.egg-info \
+	dist \
+	$(NULL)
+
+.DEFAULT_GOAL := help
+
+help:
+	@echo "$$HELP"
+
 install:
+	$(PYTHON) -m pip install -r requirements.txt
 	$(PYTHON) setup.py install
 
-test:
-	$(PYTHON) -m pytest .
+uninstall:
+	$(PYTHON) -m pip uninstall -y compiler_gym_examples
+
+# DGL creates ~/.dgl on first run and I have found that this will fail if tests
+# are invoked before this file exists. Suppress errors.
+dgl-init:
+	python3 -c 'import dgl; print(dgl.__version__)' &>/dev/null || true
+
+test: dgl-init
+	pytest --no-success-flaky-report --benchmark-disable -n auto --durations=5 . --cov=compiler_gym --cov-report=xml:$(COV_REPORT) $(PYTEST_ARGS)
+
+clean:
+	rm -rf $(DISTTOOLS_OUTS)
+
+disclean: clean
+
+purge: distclean uninstall
+
+.PHONY: help install uninstall dgl-init test clean distclean purge
diff --git a/examples/README.md b/examples/README.md
index 12457f46a..1595cf9d9 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -1,11 +1,22 @@
 # CompilerGym Examples <!-- omit in toc -->
 
-This directory contains code samples for everything from implementing simple
-RL agents to adding support for entirely new compilers. Is there an example that
-you think is missing? If so, please [contribute](/CONTRIBUTING.md)!
+This directory contains standalone code samples for everything from implementing
+simple RL agents to adding support for entirely new compilers. You can run these
+examples using a `pip` installed CompilerGym, though there are some additional
+dependencies that can be installed using:
 
+    make install
 
-**Table of contents:**
+Then run the python scripts from this directory. E.g.:
+
+    python actor_critic.py
+
+Is there an example that you think is missing? If so, please file a [feature
+request](https://github.com/facebookresearch/CompilerGym/issues/new/choose) or
+[contribute](/CONTRIBUTING.md)!
+
+
+## Table of contents  <!-- omit in toc -->
 
 - [Autotuning](#autotuning)
   - [Performing a random walk of an environment](#performing-a-random-walk-of-an-environment)
diff --git a/examples/setup.py b/examples/setup.py
index 3e375d78e..90e7b0f08 100644
--- a/examples/setup.py
+++ b/examples/setup.py
@@ -13,6 +13,8 @@
     version = f.read().strip()
 with open("requirements.txt") as f:
     requirements = [ln.split("#")[0].rstrip() for ln in f.readlines()]
+with open("../tests/requirements.txt") as f:
+    requirements += [ln.split("#")[0].rstrip() for ln in f.readlines()]
 
 setuptools.setup(
     name="compiler_gym_examples",

Improve examples documentation to make it clear that they are standalone
## 🚀 Feature

Tangentially to #532, I think it would be good to add a "Usage" section to examples/README.md that makes it clear that these example scripts can be used through pip-installed CompilerGym, and possibly split the examples rules out of the top level makefile into an examples/Makefile file for standalone usage.

## Motivation

It is not clear whether the included examples require building from source (they don't) or can be used on their own (they can).
