Merge pull request #222 from mithro/change-to-make-env

infra: Convert Makefile to use SymbiFlow's make-env setup.
diff --git a/.gitmodules b/.gitmodules
index e02f407..12f5cd4 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,7 @@
+[submodule "third_party/make-env"]
+	path = third_party/make-env
+	url = https://github.com/SymbiFlow/make-env.git
+
 # sky130_fd_pr
 [submodule "libraries/sky130_fd_pr/latest"]
 	path = libraries/sky130_fd_pr/latest
diff --git a/.readthedocs.yml b/.readthedocs.yml
index 2546fc3..5321847 100644
--- a/.readthedocs.yml
+++ b/.readthedocs.yml
@@ -26,7 +26,7 @@
   configuration: docs/conf.py
 
 conda:
-  environment: environment.yml
+  environment: docs/environment.yml
 
 submodules:
   include:
diff --git a/Makefile b/Makefile
index 26390f6..1896b7e 100644
--- a/Makefile
+++ b/Makefile
@@ -14,11 +14,25 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 
-include scripts/make/git.mk
-include scripts/make/conda.mk
+# The top directory where environment will be created.
+TOP_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
+
+# A pip `requirements.txt` file.
+# https://pip.pypa.io/en/stable/reference/pip_install/#requirements-file-format
+REQUIREMENTS_FILE := requirements.txt
+
+# A conda `environment.yml` file.
+# https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
+ENVIRONMENT_FILE := environment.yml
+
+$(TOP_DIR)/third_party/make-env/conda.mk: $(TOP_DIR)/.gitmodules
+	cd $(TOP_DIR); git submodule update --init third_party/make-env
+
+-include $(TOP_DIR)/third_party/make-env/conda.mk
 
 .DEFAULT_GOAL := all
 
+include $(TOP_DIR)/scripts/make/git.mk
 README.rst: README.src.rst docs/status.rst Makefile | $(CONDA_ENV_PYTHON)
 	@rm -f README.rst
 	$(IN_CONDA_ENV) rst_include include README.src.rst - \
@@ -52,7 +66,7 @@
 
 .PHONY: check-licenses
 
-lint-python:
+lint-python: | $(CONDA_ENV_PYTHON)
 	$(IN_CONDA_ENV) flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
 
 .PHONY: lint-python
diff --git a/docs/Makefile b/docs/Makefile
index 9c7399f..9bc3b5c 100644
--- a/docs/Makefile
+++ b/docs/Makefile
@@ -1,15 +1,35 @@
-# Minimal makefile for Sphinx documentation
+# Copyright 2020 SkyWater PDK Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
 
-DOCSDIR := $(dir $(lastword $(MAKEFILE_LIST)))
-TOPDIR := $(realpath $(DOCSDIR)/..)
+# The top directory where environment will be created.
+DOCS_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
+TOP_DIR := $(realpath $(DOCS_DIR)/..)
 
-ifndef NO_CONDA
-include $(TOPDIR)/scripts/make/conda.mk
-endif
-ifdef NO_CONDA
-IN_CONDA_ENV :=
-CONDA_ENV_PYTHON :=
-endif
+# A pip `requirements.txt` file.
+# https://pip.pypa.io/en/stable/reference/pip_install/#requirements-file-format
+REQUIREMENTS_FILE := requirements.txt
+
+# A conda `environment.yml` file.
+# https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
+ENVIRONMENT_FILE := environment.yml
+
+$(TOP_DIR)/third_party/make-env/conda.mk: $(TOP_DIR)/.gitmodules
+	cd $(TOP_DIR); git submodule update --init third_party/make-env
+
+-include $(TOP_DIR)/third_party/make-env/conda.mk
 
 SHELL = /bin/bash
 
@@ -22,8 +42,8 @@
 SPHINXBUILD     = $(IN_CONDA_ENV) sphinx-build
 SPHINXAUTOBUILD = $(IN_CONDA_ENV) sphinx-autobuild
 SPHINXPROJ      = SkyWaterPDK
-SOURCEDIR       = $(DOCSDIR)
-BUILDDIR        = $(DOCSDIR)/_build
+SOURCEDIR       = $(DOCS_DIR)
+BUILDDIR        = $(DOCS_DIR)/_build
 
 # Put it first so that "make" without argument is like "make help".
 help: | $(CONDA_ENV_PYTHON)
@@ -75,10 +95,11 @@
 # Catch-all target: route all unknown targets to Sphinx using the new
 # "make mode" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).
 .DEFAULT: | $(CONDA_ENV_PYTHON)
+	@make env
 	@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
 
 
-clean: clean-docs
+clean:: clean-docs
 
 
 clean-docs:
diff --git a/docs/environment.yml b/docs/environment.yml
new file mode 100644
index 0000000..23797fd
--- /dev/null
+++ b/docs/environment.yml
@@ -0,0 +1,28 @@
+# Copyright 2020 SkyWater PDK Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+name: skywater-pdk-docs
+channels:
+- symbiflow
+- conda-forge
+- defaults
+dependencies:
+- python=3.8
+- pip
+- yosys
+- netlistsvg
+# Packages installed from PyPI
+- pip:
+  - -r file:requirements.txt
diff --git a/environment.yml b/environment.yml
index e46f745..ee13527 100644
--- a/environment.yml
+++ b/environment.yml
@@ -16,14 +16,10 @@
 name: skywater-pdk-scripts
 channels:
 - symbiflow
-- conda-forge
 - defaults
 dependencies:
 - python=3.8
 - pip
-- yosys
-- netlistsvg
 # Packages installed from PyPI
 - pip:
   - -r file:requirements.txt
-  - -r file:docs/requirements.txt
diff --git a/scripts/make/conda.mk b/scripts/make/conda.mk
deleted file mode 100644
index 0370295..0000000
--- a/scripts/make/conda.mk
+++ /dev/null
@@ -1,114 +0,0 @@
-# Copyright 2020 SkyWater PDK Authors
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-# SPDX-License-Identifier: Apache-2.0
-
-
-SHELL := /bin/bash
-
-UNAME_S := $(shell uname -s)
-ifneq (, $(findstring Linux, $(UNAME_S)))
-        OSFLAG := Linux
-endif
-ifeq ($(UNAME_S), Darwin)
-        OSFLAG := MacOSX
-endif
-ifneq (, $(findstring Cygwin, $(UNAME_S)))
-        OSFLAG := Linux
-endif
-ifneq (, $(findstring MINGW, $(UNAME_S)))
-        OSFLAG := Linux
-endif
-
-MAKE_DIR          := $(dir $(lastword $(MAKEFILE_LIST)))
-TOP_DIR           := $(realpath $(MAKE_DIR)/../..)
-ENV_DIR           := $(TOP_DIR)/env
-REQUIREMENTS_FILE := $(TOP_DIR)/requirements.txt
-ENVIRONMENT_FILE  := $(TOP_DIR)/environment.yml
-
-CONDA_DIR         := $(ENV_DIR)/conda
-DOWNLOADS_DIR     := $(ENV_DIR)/downloads
-CONDA_PYTHON      := $(CONDA_DIR)/bin/python
-CONDA_PKGS_DIR    := $(DOWNLOADS_DIR)/conda-pkgs
-CONDA_PKGS_DEP    := $(CONDA_PKGS_DIR)/urls.txt
-CONDA_ENV_NAME    := skywater-pdk-scripts
-CONDA_ENV_PYTHON  := $(CONDA_DIR)/envs/$(CONDA_ENV_NAME)/bin/python
-IN_CONDA_ENV_BASE := source $(CONDA_DIR)/bin/activate &&
-IN_CONDA_ENV      := $(IN_CONDA_ENV_BASE) conda activate $(CONDA_ENV_NAME) &&
-
-$(ENV_DIR): | $(DOWNLOADS_DIR)
-	mkdir -p $(END_DIR)
-
-$(DOWNLOADS_DIR):
-	mkdir -p $(DOWNLOADS_DIR)
-
-$(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh: | $(DOWNLOADS_DIR)
-	wget https://repo.anaconda.com/miniconda/Miniconda3-latest-$(OSFLAG)-x86_64.sh -O $(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh
-	chmod a+x $(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh
-
-$(CONDA_PKGS_DEP): $(CONDA_PYTHON)
-	$(IN_CONDA_ENV_BASE) conda config --system --add pkgs_dirs $(CONDA_PKGS_DIR)
-
-$(CONDA_PYTHON): $(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh
-	$(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh -p $(CONDA_DIR) -b -f
-	touch $(CONDA_PYTHON)
-
-$(CONDA_DIR)/envs: $(CONDA_PYTHON)
-	$(IN_CONDA_ENV_BASE) conda config --system --add envs_dirs $(CONDA_DIR)/envs
-
-$(CONDA_ENV_PYTHON): $(ENVIRONMENT_FILE) $(REQUIREMENTS_FILE) | $(CONDA_PYTHON) $(CONDA_DIR)/envs $(CONDA_PKGS_DEP)
-	$(IN_CONDA_ENV_BASE) conda env update --name $(CONDA_ENV_NAME) --file $(ENVIRONMENT_FILE)
-	touch $(CONDA_ENV_PYTHON)
-
-env: $(CONDA_ENV_PYTHON)
-	$(IN_CONDA_ENV) conda info
-
-.PHONY: env
-
-enter: $(CONDA_ENV_PYTHON)
-	$(IN_CONDA_ENV) bash
-
-.PHONY: enter
-
-clean:
-	rm -rf env/conda
-
-.PHONY: clean
-
-dist-clean:
-	rm -rf conda
-
-.PHONY: dist-clean
-
-
-FILTER_TOP = sed -e's@$(TOP_DIR)/@$$TOP_DIR/@'
-env-info:
-	@echo "             Top level directory is: '$(TOP_DIR)'"
-	@echo "              The version number is: '$$(git describe)'"
-	@echo "            Git repository is using: $$(du -h -s $(TOP_DIR)/.git | sed -e's/\s.*//')" \
-		| $(FILTER_TOP)
-	@echo
-	@echo "     Environment setup directory is: '$(ENV_DIR)'" \
-		| $(FILTER_TOP)
-	@echo "    Download and cache directory is: '$(DOWNLOADS_DIR)' (using $$(du -h -s $(DOWNLOADS_DIR) | sed -e's/\s.*//'))" \
-		| $(FILTER_TOP)
-	@echo "               Conda's directory is: '$(CONDA_DIR)' (using $$(du -h -s $(CONDA_DIR) | sed -e's/\s.*//'))" \
-		| $(FILTER_TOP)
-	@echo " Conda's packages download cache is: '$(CONDA_PKGS_DIR)' (using $$(du -h -s $(CONDA_PKGS_DIR) | sed -e's/\s.*//'))" \
-		| $(FILTER_TOP)
-	@echo "           Conda's Python binary is: '$(CONDA_ENV_PYTHON)'"\
-		| $(FILTER_TOP)
-
-
-.PHONY: info
diff --git a/third_party/make-env b/third_party/make-env
new file mode 160000
index 0000000..9b07ad2
--- /dev/null
+++ b/third_party/make-env
@@ -0,0 +1 @@
+Subproject commit 9b07ad2bb62fbf8af789c9e4669715c974b4912d