Joey Armstrong | 0205d33 | 2023-04-11 17:29:23 -0400 | [diff] [blame] | 1 | # -*- makefile -*- |
| 2 | ## ----------------------------------------------------------------------- |
| 3 | # Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF 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. |
Joey Armstrong | 7ad5c36 | 2023-07-09 19:10:16 -0400 | [diff] [blame] | 16 | # ----------------------------------------------------------------------- |
Joey Armstrong | c86d035 | 2023-11-09 10:31:10 -0500 | [diff] [blame] | 17 | # ----------------------------------------------------------------------- |
| 18 | # Intent: |
| 19 | # This makefile defines dependencies that will install a python virtualenv |
| 20 | # beneath $(sandbox)/.venv/. The $(activate) macro is used to source |
| 21 | # .venv/bin/activate allowing command python and pip to be used. |
Joey Armstrong | e6a9991 | 2023-09-15 14:47:51 -0400 | [diff] [blame] | 22 | # ----------------------------------------------------------------------- |
| 23 | # Usage: |
Joey Armstrong | c86d035 | 2023-11-09 10:31:10 -0500 | [diff] [blame] | 24 | # include makefiles/virtualenv/include.mk |
Joey Armstrong | e6a9991 | 2023-09-15 14:47:51 -0400 | [diff] [blame] | 25 | # |
Joey Armstrong | c86d035 | 2023-11-09 10:31:10 -0500 | [diff] [blame] | 26 | # Makefile Target Dependencies: |
| 27 | # tgt : $(venv-activate-patched) # python 3.10+ local use |
| 28 | # tgt : $(venv-activate-script) # python < v3.8 |
| 29 | # |
| 30 | # Make definitions (convenience macros used for command access) |
| 31 | # PIP := $(activate) && pip # invoke pip from virtualenv |
| 32 | # PYTHON := $(activate) && python # invoke python from virtualenv |
| 33 | # |
| 34 | # Target declaration and command invocation: |
| 35 | # my-target : $(venv-activate-script) # dependency installs virtualenv |
| 36 | # <tab>$(PYTHON) --version # invoke python with arguments |
| 37 | # <tab>$(PYTHON) my-command.py |
| 38 | # <tab>$(activate) && pip install foobar |
| 39 | # |
| 40 | # % make my-target # Invoke make target from shell |
Joey Armstrong | e98239c | 2023-05-08 17:10:07 -0400 | [diff] [blame] | 41 | # ----------------------------------------------------------------------- |
Joey Armstrong | 0205d33 | 2023-04-11 17:29:23 -0400 | [diff] [blame] | 42 | |
| 43 | $(if $(DEBUG),$(warning ENTER)) |
| 44 | |
| 45 | ##-------------------## |
| 46 | ##---] GLOBALS [---## |
| 47 | ##-------------------## |
Joey Armstrong | e6a9991 | 2023-09-15 14:47:51 -0400 | [diff] [blame] | 48 | .PHONY: venv venv-patched |
Joey Armstrong | 0205d33 | 2023-04-11 17:29:23 -0400 | [diff] [blame] | 49 | |
| 50 | ##------------------## |
| 51 | ##---] LOCALS [---## |
| 52 | ##------------------## |
| 53 | venv-name ?= .venv# # default install directory |
Joey Armstrong | e6a9991 | 2023-09-15 14:47:51 -0400 | [diff] [blame] | 54 | venv-abs-path := $(PWD)/$(venv-name)# # |
| 55 | venv-activate-bin := $(venv-name)/bin# # no whitespace |
Joey Armstrong | f548adc | 2023-09-08 15:59:42 -0400 | [diff] [blame] | 56 | venv-activate-script := $(venv-activate-bin)/activate# # dependency |
Joey Armstrong | 0205d33 | 2023-04-11 17:29:23 -0400 | [diff] [blame] | 57 | |
Joey Armstrong | c86d035 | 2023-11-09 10:31:10 -0500 | [diff] [blame] | 58 | # ------------------------------------------------------------------------ |
| 59 | # Intent: Define macro activate= to access virtualenv activation script. |
| 60 | # Usage: |
| 61 | # - $(activate) && python # Syntax inlined within a target |
| 62 | # - PYTHON := $(activate) && python # Define a named command macro |
| 63 | # ------------------------------------------------------------------------ |
Joey Armstrong | 0205d33 | 2023-04-11 17:29:23 -0400 | [diff] [blame] | 64 | activate ?= set +u && source $(venv-activate-script) && set -u |
| 65 | |
| 66 | ## ----------------------------------------------------------------------- |
| 67 | ## Intent: Activate script path dependency |
| 68 | ## Usage: |
| 69 | ## o place on the right side of colon as a target dependency |
| 70 | ## o When the script does not exist install the virtual env and display. |
| 71 | ## ----------------------------------------------------------------------- |
| 72 | $(venv-activate-script): |
| 73 | @echo |
Joey Armstrong | e6a9991 | 2023-09-15 14:47:51 -0400 | [diff] [blame] | 74 | @echo '=============================' |
| 75 | @echo 'Installing python virtual env' |
| 76 | @echo '=============================' |
Joey Armstrong | 0205d33 | 2023-04-11 17:29:23 -0400 | [diff] [blame] | 77 | virtualenv -p python3 $(venv-name) |
| 78 | $(activate) && python -m pip install --upgrade pip |
| 79 | $(activate) && pip install --upgrade setuptools |
Joey Armstrong | f548adc | 2023-09-08 15:59:42 -0400 | [diff] [blame] | 80 | $(activate) && [[ -r requirements.txt ]] \ |
| 81 | && { python -m pip install -r requirements.txt; } \ |
| 82 | || { /bin/true; } |
| 83 | |
Joey Armstrong | 0205d33 | 2023-04-11 17:29:23 -0400 | [diff] [blame] | 84 | $(activate) && python --version |
| 85 | |
| 86 | ## ----------------------------------------------------------------------- |
| 87 | ## Intent: Explicit named installer target w/o dependencies. |
| 88 | ## Makefile targets should depend on venv-activate-script. |
| 89 | ## ----------------------------------------------------------------------- |
Joey Armstrong | e6a9991 | 2023-09-15 14:47:51 -0400 | [diff] [blame] | 90 | venv-activate-patched := $(venv-activate-script).patched |
| 91 | venv-activate-patched : $(venv-activate-patched) |
| 92 | $(venv-activate-patched) : $(venv-activate-script) |
| 93 | $(call banner-enter,Target $@) |
| 94 | $(ONF_MAKEDIR)/virtualenv/python_310_migration.sh |
| 95 | touch $@ |
| 96 | $(call banner-leave,Target $@) |
| 97 | |
| 98 | ## ----------------------------------------------------------------------- |
| 99 | ## Intent: Explicit named installer target w/o dependencies. |
| 100 | ## Makefile targets should depend on venv-activate-script. |
| 101 | ## ----------------------------------------------------------------------- |
| 102 | venv : $(venv-activate-script) |
| 103 | venv-patched : $(venv-activate-patched) |
Joey Armstrong | 0205d33 | 2023-04-11 17:29:23 -0400 | [diff] [blame] | 104 | |
| 105 | ## ----------------------------------------------------------------------- |
| 106 | ## Intent: Revert installation to a clean checkout |
| 107 | ## ----------------------------------------------------------------------- |
| 108 | sterile :: clean |
| 109 | $(RM) -r "$(venv-abs-path)" |
| 110 | |
| 111 | ## ----------------------------------------------------------------------- |
| 112 | ## ----------------------------------------------------------------------- |
| 113 | help :: |
| 114 | @echo |
| 115 | @echo '[VIRTUAL ENV]' |
| 116 | @echo ' venv Create a python virtual environment' |
| 117 | @echo ' venv-name= Subdir name for virtualenv install' |
| 118 | @echo ' venv-activate-script make macro name' |
| 119 | @echo ' $$(target) dependency install python virtualenv' |
| 120 | @echo ' source $$(macro) && cmd configure env and run cmd' |
| 121 | |
| 122 | $(if $(DEBUG),$(warning LEAVE)) |
| 123 | |
| 124 | # [EOF] |