From 0482a9f54e0b56ef2a48a08f545b5df4d30c389a Mon Sep 17 00:00:00 2001 From: Tim 'mithro' Ansell Date: Sat, 7 Nov 2020 21:14:28 -0800 Subject: [PATCH 1/3] Improve the Makefile template. Add rule to checkout the git submodule if the include doesn't exist. Signed-off-by: Tim 'mithro' Ansell --- Makefile.template | 7 ++++++- test/.gitmodules | 0 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 test/.gitmodules diff --git a/Makefile.template b/Makefile.template index ba6ce7c..e9498ab 100644 --- a/Makefile.template +++ b/Makefile.template @@ -17,7 +17,12 @@ REQUIREMENTS_FILE := requirements.txt # https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html ENVIRONMENT_FILE := environment.yml -include third_party/make-env/conda.mk +# Rule to checkout the git submodule if it wasn't cloned. +$(TOP_DIR)/third_party/make-env/conda.mk: $(TOP_DIR)/.gitmodules + cd $(TOP_DIR); git submodule update --init third_party/make-env + touch $(TOP_DIR)/third_party/make-env/conda.mk + +-include $(TOP_DIR)/third_party/make-env/conda.mk # Example make target which runs commands inside the conda environment. example-command: | $(CONDA_ENV_PYTHON) diff --git a/test/.gitmodules b/test/.gitmodules new file mode 100644 index 0000000..e69de29 From 9df3dec9383b3383e3c26c8a3d6ba304994153d2 Mon Sep 17 00:00:00 2001 From: Tim 'mithro' Ansell Date: Sat, 7 Nov 2020 22:14:07 -0800 Subject: [PATCH 2/3] Don't force the environment.yml / requirements.txt file to be in top directory. Signed-off-by: Tim 'mithro' Ansell --- conda.mk | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/conda.mk b/conda.mk index 5eaa342..35b6b89 100644 --- a/conda.mk +++ b/conda.mk @@ -47,12 +47,18 @@ endif ifeq (,$(REQUIREMENTS_FILE)) $(error "Set REQUIREMENTS_FILE value before including 'conda.mk'.") else -REQUIREMENTS_FILE := $(abspath $(TOP_DIR)$(SEP)$(REQUIREMENTS_FILE)) +REQUIREMENTS_FILE := $(abspath $(REQUIREMENTS_FILE)) +endif +ifeq (,$(wildcard $(REQUIREMENTS_FILE))) +$(error "REQUIREMENTS_FILE ($(REQUIREMENTS_FILE)) does not exist!?") endif ifeq (,$(ENVIRONMENT_FILE)) $(error "Set ENVIRONMENT_FILE value before including 'conda.mk'.") -ENVIRONMENT_FILE := $(abspath $(TOP_DIR)$(SEP)$(ENVIRONMENT_FILE)) +ENVIRONMENT_FILE := $(abspath $(ENVIRONMENT_FILE)) +endif +ifeq (,$(wildcard $(ENVIRONMENT_FILE))) +$(error "ENVIRONMENT_FILE ($(ENVIRONMENT_FILE)) does not exist!?") endif CONDA_ENV_NAME := $(strip $(patsubst name:%,,$(CONDA_ENV_NAME_LINE))) From 11223434847f2844a5f2150de4a07d406be0d0d7 Mon Sep 17 00:00:00 2001 From: Tim 'mithro' Ansell Date: Sat, 7 Nov 2020 22:42:29 -0800 Subject: [PATCH 3/3] Support multiple environment providers. * Include `third_party/make-env/env.mk` and use `ENV_PYTHON` / `IN_ENV` inside your makefile. See updated templated. * Support both conda and system environments. Default to using conda and use `ENV=system` to instead use the system environment. Signed-off-by: Tim 'mithro' Ansell --- .github/workflows/test.yml | 4 +- Makefile.template | 16 ++++---- conda.mk | 10 ++--- env.mk | 82 ++++++++++++++++++++++++++++++++++++++ system.mk | 28 +++++++++++++ 5 files changed, 125 insertions(+), 15 deletions(-) create mode 100644 env.mk create mode 100644 system.mk diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index baf1211..9afb03c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -40,7 +40,7 @@ jobs: run: | cd test make env - make env-info + make info - name: example run: | @@ -50,4 +50,4 @@ jobs: - name: test run: | cd test - make test-command + make test-command || make -d test-command diff --git a/Makefile.template b/Makefile.template index e9498ab..f22129b 100644 --- a/Makefile.template +++ b/Makefile.template @@ -18,17 +18,17 @@ REQUIREMENTS_FILE := requirements.txt ENVIRONMENT_FILE := environment.yml # Rule to checkout the git submodule if it wasn't cloned. -$(TOP_DIR)/third_party/make-env/conda.mk: $(TOP_DIR)/.gitmodules +$(TOP_DIR)/third_party/make-env/env.mk: $(TOP_DIR)/.gitmodules cd $(TOP_DIR); git submodule update --init third_party/make-env - touch $(TOP_DIR)/third_party/make-env/conda.mk + touch $(TOP_DIR)/third_party/make-env/env.mk --include $(TOP_DIR)/third_party/make-env/conda.mk +-include $(TOP_DIR)/third_party/make-env/env.mk # Example make target which runs commands inside the conda environment. -example-command: | $(CONDA_ENV_PYTHON) - $(IN_CONDA_ENV) echo "Python is $$(which python)" - $(IN_CONDA_ENV) python --version +example-command: | $(ENV_PYTHON) + $(IN_ENV) echo "Python is $$(which python)" + @$(IN_ENV) python --version # Check that no system packages are found in the environment. -test-command: | $(CONDA_ENV_PYTHON) - $(IN_CONDA_ENV) python check.py +test-command: | $(ENV_PYTHON) + $(IN_ENV) python check.py diff --git a/conda.mk b/conda.mk index 35b6b89..fdd1b00 100644 --- a/conda.mk +++ b/conda.mk @@ -8,6 +8,9 @@ .SUFFIXES: +ENV := conda +UENV := CONDA + MAKE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) SHELL := bash @@ -150,11 +153,6 @@ env:: $(CONDA_ENV_PYTHON) .PHONY: env -enter: $(CONDA_ENV_PYTHON) - $(IN_CONDA_ENV) bash - -.PHONY: enter - clean:: rm -rf $(CONDA_DIR) @@ -167,6 +165,8 @@ dist-clean:: FILTER_TOP = sed -e's@$(TOP_DIR)/@$$TOP_DIR/@' env-info: + @echo + @echo " Using conda environment." @echo " Currently running on: '$(OS_TYPE) ($(CPU_TYPE))'" @echo @echo " Conda environment is named: '$(CONDA_ENV_NAME)'" diff --git a/env.mk b/env.mk new file mode 100644 index 0000000..9fbb215 --- /dev/null +++ b/env.mk @@ -0,0 +1,82 @@ +# Copyright (C) 2020 The SymbiFlow Authors. +# +# Use of this source code is governed by a ISC-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/ISC +# +# SPDX-License-Identifier: ISC + +.SUFFIXES: + +MAKE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) + +SHELL := bash + +# Makefile for downloading and creating environments (with tools like conda). + +# Usage +# - Set TOP_DIR to the top directory where environment will be created. +# - Set REQUIREMENTS_FILE to a pip `requirements.txt` file. +# - Set ENVIRONMENT_FILE to a conda `environment.yml` file. +# - Put $(IN_ENV) before commands which should run inside the +# environment. + +# Configuration +ifeq (,$(TOP_DIR)) +$(error "Set TOP_DIR value before including 'env.mk'.") +endif + +ifeq (,$(REQUIREMENTS_FILE)) +$(error "Set REQUIREMENTS_FILE value before including 'conda.mk'.") +else +REQUIREMENTS_FILE := $(abspath $(REQUIREMENTS_FILE)) +endif +ifeq (,$(wildcard $(REQUIREMENTS_FILE))) +$(error "REQUIREMENTS_FILE ($(REQUIREMENTS_FILE)) does not exist!?") +endif + +ifeq (,$(ENVIRONMENT_FILE)) +$(error "Set ENVIRONMENT_FILE value before including 'conda.mk'.") +ENVIRONMENT_FILE := $(abspath $(ENVIRONMENT_FILE)) +endif +ifeq (,$(wildcard $(ENVIRONMENT_FILE))) +$(error "ENVIRONMENT_FILE ($(ENVIRONMENT_FILE)) does not exist!?") +endif + +# Default to conda if no other option is provided. +ifeq (,$(ENV)) +ENV := conda +endif + +ifeq (,$(wildcard $(MAKE_DIR)/$(ENV).mk)) +$(error Unknown environment provider (ENV='$(ENV)')?) +endif + +export ENV +include $(MAKE_DIR)/$(ENV).mk +ifeq (,$(UENV)) +$(error Missing UENV definition) +endif + +ENV_PYTHON := $($(UENV)_ENV_PYTHON) +IN_ENV := $(IN_$(UENV)_ENV) + +clean:: + true + +.PHONY: clean + +dist-clean:: + true + +.PHONY: dist-clean + +enter: | $(ENV_PYTHON) + $(IN_ENV) bash + +.PHONY: enter + +info: | $(ENV_PYTHON) + @$(IN_ENV) $(MAKE) --no-print-directory env-info + +.PHONY: info diff --git a/system.mk b/system.mk new file mode 100644 index 0000000..5becbd0 --- /dev/null +++ b/system.mk @@ -0,0 +1,28 @@ +# Copyright (C) 2020 The SymbiFlow Authors. +# +# Use of this source code is governed by a ISC-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/ISC +# +# SPDX-License-Identifier: ISC + +ENV := system +UENV := SYSTEM + +include $(MAKE_DIR)/os.mk + +SYSTEM_ENV_PYTHON := +IN_SYSTEM_ENV := + +FILTER_TOP = sed -e's@$(TOP_DIR)/@$$TOP_DIR/@' +env-info: + @echo + @echo " Using system environment." + @echo " Currently running on: '$(OS_TYPE) ($(CPU_TYPE))'" + @echo + @echo " Git top level directory is: '$$(git rev-parse --show-toplevel)'" + @echo " The version number is: '$$(git describe)'" + @echo " Python binary is: '$$(which python)'"\ + | $(FILTER_TOP) + +.PHONY: env-info