Joey Armstrong | 62ae139 | 2024-03-17 20:49:30 -0400 | [diff] [blame] | 1 | # -*- makefile -*- |
Joey Armstrong | dc04c93 | 2024-04-01 12:14:21 -0400 | [diff] [blame] | 2 | # ----------------------------------------------------------------------- |
| 3 | # Copyright 2017-2024 Open Networking Foundation Contributors |
Joey Armstrong | 62ae139 | 2024-03-17 20:49:30 -0400 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
Joey Armstrong | dc04c93 | 2024-04-01 12:14:21 -0400 | [diff] [blame] | 9 | # http:#www.apache.org/licenses/LICENSE-2.0 |
Joey Armstrong | 62ae139 | 2024-03-17 20:49:30 -0400 | [diff] [blame] | 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # ----------------------------------------------------------------------- |
Joey Armstrong | dc04c93 | 2024-04-01 12:14:21 -0400 | [diff] [blame] | 17 | # SPDX-FileCopyrightText: 2017-2024 Open Networking Foundation Contributors |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ----------------------------------------------------------------------- |
| 20 | # Intent: |
Joey Armstrong | 62ae139 | 2024-03-17 20:49:30 -0400 | [diff] [blame] | 21 | # ----------------------------------------------------------------------- |
| 22 | |
| 23 | $(if $(DEBUG),$(warning ENTER)) |
| 24 | |
| 25 | ##-------------------## |
| 26 | ##---] GLOBALS [---## |
| 27 | ##-------------------## |
| 28 | .PHONY: venv |
| 29 | |
| 30 | ##------------------## |
| 31 | ##---] LOCALS [---## |
| 32 | ##------------------## |
| 33 | venv-name ?= .venv# # default install directory |
| 34 | venv-abs-path := $(PWD)/$(venv-name) |
| 35 | venv-activate-bin := $(venv-name)/bin |
| 36 | venv-activate-script := $(venv-activate-bin)/activate# # dependency |
| 37 | |
| 38 | # Intent: activate= is a macro for accessing the virtualenv activation script# |
| 39 | # Usage: $(activate) && python |
| 40 | activate ?= set +u && source $(venv-activate-script) && set -u |
| 41 | |
| 42 | venv-version : venv-requirements |
| 43 | venv-requirements : venv-install |
| 44 | venv-install : $(venv-activate-script) |
| 45 | |
| 46 | ## ----------------------------------------------------------------------- |
| 47 | ## Intent: Activate script path dependency |
| 48 | ## Usage: |
| 49 | ## o place on the right side of colon as a target dependency |
| 50 | ## o When the script does not exist install the virtual env and display. |
| 51 | ## ----------------------------------------------------------------------- |
| 52 | $(venv-activate-script) : |
| 53 | |
| 54 | $(call banner-enter,(virtualenv -p python)) |
| 55 | virtualenv -p python3 $(venv-name) |
| 56 | $(activate) && python -m pip install --upgrade pip |
| 57 | $(activate) && pip install --upgrade setuptools |
| 58 | |
| 59 | @$(MAKE) --no-print-directory venv-requirements venv-version |
| 60 | $(call banner-leave,(virtualenv -t python)) |
| 61 | |
| 62 | ## ---------------------------------------------------------------------- |
| 63 | ## Intent: pip install with dependencies |
| 64 | ## ---------------------------------------------------------------------- |
| 65 | # venv-requirements-txt := .venv/makedep/requirements.txt.ts |
| 66 | # venv-requirements : $(venv-requirements-txt) |
| 67 | # venv-install : $(venv-activate-script) |
| 68 | # venv-requirements : venv-install |
| 69 | |
| 70 | $(venv-requirements-txt) : requirements.txt |
| 71 | |
| 72 | $(activate) && python -m pip install -r requirements.txt |
| 73 | @mkdir -p $(dir $@) |
| 74 | @touch $@ |
| 75 | |
| 76 | ## ---------------------------------------------------------------------- |
| 77 | ## ---------------------------------------------------------------------- |
| 78 | venv-version : |
| 79 | $(activate) && python --version |
| 80 | |
| 81 | ## ----------------------------------------------------------------------- |
| 82 | ## Intent: Activate script path dependency |
| 83 | ## Usage: |
| 84 | ## o place on the right side of colon as a target dependency |
| 85 | ## o When the script does not exist install the virtual env and display. |
| 86 | ## ---------------------------------------------------------------------- |
| 87 | $(venv-activate-script)-orig : |
| 88 | @echo |
| 89 | @echo "=============================" |
| 90 | @echo "Installing python virtual env" |
| 91 | @echo "=============================" |
| 92 | virtualenv -p python3 $(venv-name) |
| 93 | $(activate) && python -m pip install --upgrade pip |
| 94 | $(activate) && pip install --upgrade setuptools |
| 95 | $(activate) && [[ -r requirements.txt ]] \ |
| 96 | && { python -m pip install -r requirements.txt; } \ |
| 97 | || { /bin/true; } |
| 98 | |
| 99 | $(activate) && python --version |
| 100 | |
| 101 | ## ----------------------------------------------------------------------- |
| 102 | ## Intent: Explicit named installer target w/o dependencies. |
| 103 | ## Makefile targets should depend on venv-activate-script. |
| 104 | ## ----------------------------------------------------------------------- |
| 105 | venv-activate-patched := $(venv-activate-script).patched |
| 106 | venv-activate-patched : $(venv-activate-patched) |
| 107 | $(venv-activate-patched) : $(venv-activate-script) |
| 108 | $(call banner-enter,Target $@) |
| 109 | $(onf-mk-top)/../patches/python_310_migration.sh --venv "$(venv-name)" 'apply' |
| 110 | touch $@ |
| 111 | $(call banner-leave,Target $@) |
| 112 | |
| 113 | ## ----------------------------------------------------------------------- |
| 114 | ## Intent: Explicit named installer target w/o dependencies. |
| 115 | ## Makefile targets should depend on venv-activate-script. |
| 116 | ## ----------------------------------------------------------------------- |
| 117 | venv += $(venv-activate-script) |
| 118 | venv += $(venv-requirements-txt) |
| 119 | venv: $(venv) |
| 120 | |
| 121 | ## ----------------------------------------------------------------------- |
| 122 | ## Intent: Revert installation to a clean checkout |
| 123 | ## ----------------------------------------------------------------------- |
| 124 | sterile :: clean |
| 125 | $(RM) -r "$(venv-abs-path)" |
| 126 | |
| 127 | ## ----------------------------------------------------------------------- |
| 128 | ## ----------------------------------------------------------------------- |
| 129 | help :: |
| 130 | @echo |
| 131 | @echo '[VIRTUAL ENV]' |
| 132 | @echo ' venv Create a python virtual environment' |
| 133 | @echo ' venv-name= Subdir name for virtualenv install' |
| 134 | @echo ' venv-activate-script make macro name' |
| 135 | @echo ' $$(target) dependency install python virtualenv' |
| 136 | @echo ' source $$(macro) && cmd configure env and run cmd' |
| 137 | |
| 138 | $(if $(DEBUG),$(warning LEAVE)) |
| 139 | |
| 140 | # [EOF] |