Joey Armstrong | 498c8c7 | 2024-05-01 18:11:48 -0400 | [diff] [blame] | 1 | # -*- makefile -*- |
| 2 | # ----------------------------------------------------------------------- |
| 3 | # Copyright 2017-2024 Open Networking Foundation Contributors |
| 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 | # |
| 9 | # http:#www.apache.org/licenses/LICENSE-2.0 |
| 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 | # ----------------------------------------------------------------------- |
| 17 | # SPDX-FileCopyrightText: 2017-2024 Open Networking Foundation Contributors |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ----------------------------------------------------------------------- |
| 20 | # ----------------------------------------------------------------------- |
| 21 | # Intent: |
| 22 | # This makefile defines dependencies that will install a python virtualenv |
| 23 | # beneath $(sandbox)/.venv/. The $(activate) macro is used to source |
| 24 | # .venv/bin/activate allowing command python and pip to be used. |
| 25 | # ----------------------------------------------------------------------- |
| 26 | # Usage: |
| 27 | # include makefiles/virtualenv/include.mk |
| 28 | # |
| 29 | # Makefile Target Dependencies: |
| 30 | # tgt : $(venv-activate-patched) # python 3.10+ local use |
| 31 | # tgt : $(venv-activate-script) # python < v3.8 |
| 32 | # |
| 33 | # Make definitions (convenience macros used for command access) |
| 34 | # PIP := $(activate) && pip # invoke pip from virtualenv |
| 35 | # PYTHON := $(activate) && python # invoke python from virtualenv |
| 36 | # |
| 37 | # Target declaration and command invocation: |
| 38 | # my-target : $(venv-activate-script) # dependency installs virtualenv |
| 39 | # <tab>$(PYTHON) --version # invoke python with arguments |
| 40 | # <tab>$(PYTHON) my-command.py |
| 41 | # <tab>$(activate) && pip install foobar |
| 42 | # |
| 43 | # % make my-target # Invoke make target from shell |
| 44 | # ----------------------------------------------------------------------- |
| 45 | |
| 46 | $(if $(DEBUG),$(warning ENTER)) |
| 47 | |
| 48 | ##-------------------## |
| 49 | ##---] GLOBALS [---## |
| 50 | ##-------------------## |
| 51 | .PHONY: venv venv-patched |
| 52 | |
| 53 | ##------------------## |
| 54 | ##---] LOCALS [---## |
| 55 | ##------------------## |
| 56 | venv-name ?= .venv# # default install directory |
| 57 | venv-abs-path := $(sandbox-root)/$(venv-name)# # Install directory |
| 58 | venv-activate-bin := $(venv-name)/bin# # no whitespace |
| 59 | venv-activate-script := $(venv-activate-bin)/activate# # dependency |
| 60 | |
| 61 | ##--------------------## |
| 62 | ##---] INCLUDES [---## |
| 63 | ##--------------------## |
| 64 | include $(onf-mk-dir)/virtualenv/requirements-txt.mk |
| 65 | include $(onf-mk-dir)/virtualenv/version.mk |
| 66 | |
| 67 | # ------------------------------------------------------------------------ |
| 68 | # Intent: Define macro activate= to access virtualenv activation script. |
| 69 | ## ----------------------------------------------------------------------- |
| 70 | # Usage: |
| 71 | # - $(activate) && python # Syntax inlined within a target |
| 72 | # - PYTHON := $(activate) && python # Define a named command macro |
| 73 | # ------------------------------------------------------------------------ |
| 74 | activate ?= set +u && source $(venv-activate-script) && set -u |
| 75 | |
| 76 | ## ----------------------------------------------------------------------- |
| 77 | ## Intent: Explicit named installer target w/o dependencies. |
| 78 | ## Makefile targets should depend on venv-activate-script. |
| 79 | ## ----------------------------------------------------------------------- |
| 80 | venv := $(null) |
| 81 | venv += $(venv-activate-script)# # virtualenv -p python3 |
| 82 | venv += $(venv-requirements-txt)# # pip install -r requirements.txt |
| 83 | |
| 84 | venv: $(venv) |
| 85 | |
| 86 | venv-patched : $(venv-activate-patched) |
| 87 | |
| 88 | ## ----------------------------------------------------------------------- |
| 89 | ## Intent: Activate script path dependency |
| 90 | ## Usage: |
| 91 | ## o place on the right side of colon as a target dependency |
| 92 | ## o When the script does not exist install the virtual env and display. |
| 93 | ## ----------------------------------------------------------------------- |
| 94 | $(venv-activate-script): |
| 95 | |
| 96 | $(call banner-enter,(virtualenv -p python)) |
| 97 | |
| 98 | virtualenv -p python3 $(venv-name) |
| 99 | $(activate) && python -m pip install --upgrade pip |
| 100 | $(activate) && pip install --upgrade setuptools |
| 101 | |
| 102 | $(HIDE)$(MAKE) --no-print-directory venv-requirements venv-version |
| 103 | |
| 104 | $(call banner-leave,(virtualenv -t python)) |
| 105 | |
| 106 | ## ----------------------------------------------------------------------- |
| 107 | ## Intent: Explicit named installer target w/o dependencies. |
| 108 | ## Makefile targets should depend on venv-activate-script. |
| 109 | ## ----------------------------------------------------------------------- |
| 110 | venv-activate-patched := $(venv-activate-script).patched |
| 111 | venv-activate-patched : $(venv-activate-patched) |
| 112 | $(venv-activate-patched) : $(venv-activate-script) |
| 113 | $(call banner-enter,Target $@) |
| 114 | $(onf-mk-dir)/virtualenv/python_310_migration.sh |
| 115 | touch $@ |
| 116 | $(call banner-leave,Target $@) |
| 117 | |
| 118 | ## ----------------------------------------------------------------------- |
| 119 | ## Intent: Explicit named installer target w/o dependencies. |
| 120 | ## Makefile targets should depend on venv-activate-script. |
| 121 | ## ----------------------------------------------------------------------- |
| 122 | # venv : $(venv-activate-script) |
| 123 | |
| 124 | ## ----------------------------------------------------------------------- |
| 125 | ## Intent: Revert installation to a clean checkout |
| 126 | ## ----------------------------------------------------------------------- |
| 127 | sterile :: clean |
| 128 | $(RM) -r "$(venv-abs-path)" |
| 129 | |
| 130 | ## ----------------------------------------------------------------------- |
| 131 | ## ----------------------------------------------------------------------- |
| 132 | help :: |
| 133 | @printf ' %-33.33s %s\n' 'venv' \ |
| 134 | 'Create a python virtual environment' |
| 135 | @printf ' %-33.33s %s\n' 'venv-help' \ |
| 136 | 'Extended target help' |
| 137 | |
| 138 | ## ----------------------------------------------------------------------- |
| 139 | ## ----------------------------------------------------------------------- |
| 140 | .PHONY: venv-help |
| 141 | venv-help :: |
| 142 | @printf ' %-33.33s %s\n' 'venv-patched' \ |
| 143 | 'venv patched for v3.10.6+ use' |
| 144 | |
| 145 | @printf ' %-33.33s %s\n' 'venv' \ |
| 146 | 'Create a python virtual environment' |
| 147 | @printf ' %-33.33s %s\n' ' venv-name' \ |
| 148 | 'virtualenv installation directory name' |
| 149 | |
| 150 | # include $(onf-mk-dir)/virtualenv/todo.mk |
| 151 | |
| 152 | $(if $(DEBUG),$(warning LEAVE)) |
| 153 | |
| 154 | # [EOF] |