Skip to content

Commit 9c519d6

Browse files
committed
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 <me@mith.ro>
1 parent b9e7e62 commit 9c519d6

5 files changed

Lines changed: 119 additions & 14 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
run: |
4141
cd test
4242
make env
43-
make env-info
43+
make info
4444
4545
- name: example
4646
run: |

Makefile.template

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ REQUIREMENTS_FILE := requirements.txt
1818
ENVIRONMENT_FILE := environment.yml
1919

2020
# Rule to checkout the git submodule if it wasn't cloned.
21-
$(TOP_DIR)/third_party/make-env/conda.mk: $(TOIP_DIR)/.gitmodules
21+
$(TOP_DIR)/third_party/make-env/env.mk: $(TOIP_DIR)/.gitmodules
2222
cd $(TOP_DIR); git submodule update --init third_party/make-env
23-
touch $(TOP_DIR)/third_party/make-env/conda.mk
23+
touch $(TOP_DIR)/third_party/make-env/env.mk
2424

25-
-include $(TOP_DIR)/third_party/make-env/conda.mk
25+
-include $(TOP_DIR)/third_party/make-env/env.mk
2626

2727
# Example make target which runs commands inside the conda environment.
28-
example-command: | $(CONDA_ENV_PYTHON)
29-
$(IN_CONDA_ENV) echo "Python is $$(which python)"
30-
$(IN_CONDA_ENV) python --version
28+
example-command: | $(ENV_PYTHON)
29+
@echo "IN_ENV=$(IN_ENV)"
30+
@echo "ENV_PYTHON=$(ENV_PYTHON)"
31+
$(IN_ENV) echo "Python is $$(which python)"
32+
$(IN_ENV) python --version
3133

3234
# Check that no system packages are found in the environment.
33-
test-command: | $(CONDA_ENV_PYTHON)
34-
$(IN_CONDA_ENV) python check.py
35+
test-command: | $(ENV_PYTHON)
36+
$(IN_ENV) python check.py

conda.mk

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ env:: $(CONDA_ENV_PYTHON)
150150

151151
.PHONY: env
152152

153-
enter: $(CONDA_ENV_PYTHON)
154-
$(IN_CONDA_ENV) bash
155-
156-
.PHONY: enter
157-
158153
clean::
159154
rm -rf $(CONDA_DIR)
160155

@@ -167,6 +162,8 @@ dist-clean::
167162

168163
FILTER_TOP = sed -e's@$(TOP_DIR)/@$$TOP_DIR/@'
169164
env-info:
165+
@echo
166+
@echo " Using conda environment."
170167
@echo " Currently running on: '$(OS_TYPE) ($(CPU_TYPE))'"
171168
@echo
172169
@echo " Conda environment is named: '$(CONDA_ENV_NAME)'"

env.mk

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (C) 2020 The SymbiFlow Authors.
2+
#
3+
# Use of this source code is governed by a ISC-style
4+
# license that can be found in the LICENSE file or at
5+
# https://opensource.org/licenses/ISC
6+
#
7+
# SPDX-License-Identifier: ISC
8+
9+
.SUFFIXES:
10+
11+
MAKE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
12+
13+
SHELL := bash
14+
15+
# Makefile for downloading and creating environments (with tools like conda).
16+
17+
# Usage
18+
# - Set TOP_DIR to the top directory where environment will be created.
19+
# - Set REQUIREMENTS_FILE to a pip `requirements.txt` file.
20+
# - Set ENVIRONMENT_FILE to a conda `environment.yml` file.
21+
# - Put $(IN_ENV) before commands which should run inside the
22+
# environment.
23+
24+
# Configuration
25+
ifeq (,$(TOP_DIR))
26+
$(error "Set TOP_DIR value before including 'env.mk'.")
27+
endif
28+
29+
ifeq (,$(REQUIREMENTS_FILE))
30+
$(error "Set REQUIREMENTS_FILE value before including 'conda.mk'.")
31+
else
32+
REQUIREMENTS_FILE := $(abspath $(REQUIREMENTS_FILE))
33+
endif
34+
ifeq (,$(wildcard $(REQUIREMENTS_FILE)))
35+
$(error "REQUIREMENTS_FILE ($(REQUIREMENTS_FILE)) does not exist!?")
36+
endif
37+
38+
ifeq (,$(ENVIRONMENT_FILE))
39+
$(error "Set ENVIRONMENT_FILE value before including 'conda.mk'.")
40+
ENVIRONMENT_FILE := $(abspath $(ENVIRONMENT_FILE))
41+
endif
42+
ifeq (,$(wildcard $(ENVIRONMENT_FILE)))
43+
$(error "ENVIRONMENT_FILE ($(ENVIRONMENT_FILE)) does not exist!?")
44+
endif
45+
46+
# Default to conda if no other option is provided.
47+
ifeq (,$(ENV))
48+
ENV := conda
49+
endif
50+
51+
ifeq (,$(wildcard $(MAKE_DIR)/$(ENV).mk))
52+
$(error Unknown environment provider (ENV='$(ENV)')?)
53+
endif
54+
55+
export ENV
56+
UENV := $(shell echo $${ENV^^})
57+
58+
include $(MAKE_DIR)/$(ENV).mk
59+
60+
ENV_PYTHON := $($(UENV)_ENV_PYTHON)
61+
IN_ENV := $(IN_$(UENV)_ENV)
62+
63+
clean::
64+
true
65+
66+
.PHONY: clean
67+
68+
dist-clean::
69+
true
70+
71+
.PHONY: dist-clean
72+
73+
enter: | $(ENV_PYTHON)
74+
$(IN_ENV) bash
75+
76+
.PHONY: enter
77+
78+
info: | $(ENV_PYTHON)
79+
@$(IN_ENV) $(MAKE) --no-print-directory env-info
80+
81+
.PHONY: info

system.mk

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (C) 2020 The SymbiFlow Authors.
2+
#
3+
# Use of this source code is governed by a ISC-style
4+
# license that can be found in the LICENSE file or at
5+
# https://opensource.org/licenses/ISC
6+
#
7+
# SPDX-License-Identifier: ISC
8+
9+
include $(MAKE_DIR)/os.mk
10+
11+
SYSTEM_ENV_PYTHON :=
12+
IN_SYSTEM_ENV :=
13+
14+
FILTER_TOP = sed -e's@$(TOP_DIR)/@$$TOP_DIR/@'
15+
env-info:
16+
@echo
17+
@echo " Using system environment."
18+
@echo " Currently running on: '$(OS_TYPE) ($(CPU_TYPE))'"
19+
@echo
20+
@echo " Git top level directory is: '$$(git rev-parse --show-toplevel)'"
21+
@echo " The version number is: '$$(git describe)'"
22+
@echo " Python binary is: '$$(which python)'"\
23+
| $(FILTER_TOP)
24+
25+
.PHONY: env-info

0 commit comments

Comments
 (0)