blob: 9e4d4572d82473c6d6ca37037f2fc9233259a592 [file] [log] [blame]
Joey Armstrong62ae1392024-03-17 20:49:30 -04001# -*- makefile -*-
Joey Armstrongf2f0a3f2024-04-04 15:50:09 -04002# -----------------------------------------------------------------------
3# Copyright 2017-2024 Open Networking Foundation Contributors
Joey Armstrong62ae1392024-03-17 20:49:30 -04004#
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 Armstrongf2f0a3f2024-04-04 15:50:09 -04009# http:#www.apache.org/licenses/LICENSE-2.0
Joey Armstrong62ae1392024-03-17 20:49:30 -040010#
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 Armstrongf2f0a3f2024-04-04 15:50:09 -040017# SPDX-FileCopyrightText: 2017-2024 Open Networking Foundation Contributors
18# SPDX-License-Identifier: Apache-2.0
19# -----------------------------------------------------------------------
20# Intent:
Joey Armstrong62ae1392024-03-17 20:49:30 -040021# -----------------------------------------------------------------------
22
23$(if $(DEBUG),$(warning ENTER))
24
25##-------------------##
26##---] GLOBALS [---##
27##-------------------##
28.PHONY: venv
29
30##------------------##
31##---] LOCALS [---##
32##------------------##
33venv-name ?= .venv# # default install directory
34venv-abs-path := $(PWD)/$(venv-name)
35venv-activate-bin := $(venv-name)/bin
36venv-activate-script := $(venv-activate-bin)/activate# # dependency
37
38# Intent: activate= is a macro for accessing the virtualenv activation script#
39# Usage: $(activate) && python
40activate ?= set +u && source $(venv-activate-script) && set -u
41
42venv-version : venv-requirements
43venv-requirements : venv-install
44venv-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## ----------------------------------------------------------------------
78venv-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## -----------------------------------------------------------------------
105venv-activate-patched := $(venv-activate-script).patched
106venv-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## -----------------------------------------------------------------------
117venv += $(venv-activate-script)
118venv += $(venv-requirements-txt)
119venv: $(venv)
120
121## -----------------------------------------------------------------------
122## Intent: Revert installation to a clean checkout
123## -----------------------------------------------------------------------
124sterile :: clean
125 $(RM) -r "$(venv-abs-path)"
126
127## -----------------------------------------------------------------------
128## -----------------------------------------------------------------------
129help ::
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]