blob: 9ae62a49d80ee1e7ced0bcfea0ef8e1fc2a510c5 [file] [log] [blame]
Joey Armstrong6f63edf2024-01-12 11:30:08 -05001# -*- 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.
16# -----------------------------------------------------------------------
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.
22# -----------------------------------------------------------------------
23# Usage:
24# include makefiles/virtualenv/include.mk
25#
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
41# -----------------------------------------------------------------------
42
43$(if $(DEBUG),$(warning ENTER))
44
45##-------------------##
46##---] GLOBALS [---##
47##-------------------##
48.PHONY: venv venv-patched
49
50##------------------##
51##---] LOCALS [---##
52##------------------##
53venv-name ?= .venv# # default install directory
54venv-abs-path := $(PWD)/$(venv-name)# #
55venv-activate-bin := $(venv-name)/bin# # no whitespace
56venv-activate-script := $(venv-activate-bin)/activate# # dependency
57venv-tmp := $(venv-abs-path)/tmp
58
59# ------------------------------------------------------------------------
60# Intent: Define macro activate= to access virtualenv activation script.
61# Usage:
62# - $(activate) && python # Syntax inlined within a target
63# - PYTHON := $(activate) && python # Define a named command macro
64# ------------------------------------------------------------------------
65activate ?= set +u && source $(venv-activate-script) && set -u
66
67## -----------------------------------------------------------------------
68## Intent: Activate script path dependency
69## Usage:
70## o place on the right side of colon as a target dependency
71## o When the script does not exist install the virtual env and display.
72## -----------------------------------------------------------------------
73$(venv-activate-script):
74 @echo
75 @echo '============================='
76 @echo 'Installing python virtual env'
77 @echo '============================='
78 virtualenv -p python3 $(venv-name)
79 $(activate) && python -m pip install --upgrade pip
80 $(activate) && pip install --upgrade setuptools
81 $(activate) && [[ -r requirements.txt ]] \
82 && { python -m pip install -r requirements.txt; } \
83 || { /bin/true; }
84
85 $(activate) && python --version
86
87## -----------------------------------------------------------------------
88## Intent: Explicit named installer target w/o dependencies.
89## Makefile targets should depend on venv-activate-script.
90## -----------------------------------------------------------------------
91venv-activate-patched := $(venv-activate-script).patched
92venv-activate-patched : $(venv-activate-patched)
93$(venv-activate-patched) : $(venv-activate-script)
94 $(call banner-enter,Target $@)
95 $(ONF_MAKEDIR)/virtualenv/python_310_migration.sh
96 touch $@
97 $(call banner-leave,Target $@)
98
99## -----------------------------------------------------------------------
100## Intent: Explicit named installer target w/o dependencies.
101## Makefile targets should depend on venv-activate-script.
102## -----------------------------------------------------------------------
103venv : $(venv-activate-script)
104venv-patched : $(venv-activate-patched)
105
106## -----------------------------------------------------------------------
107## Intent: Revert installation to a clean checkout
108## -----------------------------------------------------------------------
109sterile :: clean
110 $(RM) -r "$(venv-abs-path)"
111
112## -----------------------------------------------------------------------
113## -----------------------------------------------------------------------
114help ::
115 @echo
116 @echo '[VIRTUAL ENV]'
117 @echo ' venv Create a python virtual environment'
118 @echo ' venv-name= Subdir name for virtualenv install'
119 @echo ' venv-activate-script make macro name'
120 @echo ' $$(target) dependency install python virtualenv'
121 @echo ' source $$(macro) && cmd configure env and run cmd'
122
123$(if $(DEBUG),$(warning LEAVE))
124
125# [EOF]