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 ba6ce7c..f22129b 100644 --- a/Makefile.template +++ b/Makefile.template @@ -17,13 +17,18 @@ 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/env.mk: $(TOP_DIR)/.gitmodules + cd $(TOP_DIR); git submodule update --init third_party/make-env + touch $(TOP_DIR)/third_party/make-env/env.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 5eaa342..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 @@ -47,12 +50,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))) @@ -144,11 +153,6 @@ env:: $(CONDA_ENV_PYTHON) .PHONY: env -enter: $(CONDA_ENV_PYTHON) - $(IN_CONDA_ENV) bash - -.PHONY: enter - clean:: rm -rf $(CONDA_DIR) @@ -161,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 diff --git a/test/.gitmodules b/test/.gitmodules new file mode 100644 index 0000000..e69de29