blob: 8e85e1f04121ac130a07fc35a9e79e6c35103e9d [file] [log] [blame]
Joey Armstrong0205d332023-04-11 17:29:23 -04001# -*- 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 Armstrong7ad5c362023-07-09 19:10:16 -040016# -----------------------------------------------------------------------
Joey Armstrongc86d0352023-11-09 10:31:10 -050017# Intent:
18# This makefile defines dependencies that will install a python virtualenv
19# beneath $(sandbox)/.venv/. The $(activate) macro is used to source
20# .venv/bin/activate allowing command python and pip to be used.
Joey Armstronge6a99912023-09-15 14:47:51 -040021# -----------------------------------------------------------------------
22# Usage:
Joey Armstrongc86d0352023-11-09 10:31:10 -050023# include makefiles/virtualenv/include.mk
Joey Armstronge6a99912023-09-15 14:47:51 -040024#
Joey Armstrongc86d0352023-11-09 10:31:10 -050025# Makefile Target Dependencies:
26# tgt : $(venv-activate-patched) # python 3.10+ local use
27# tgt : $(venv-activate-script) # python < v3.8
28#
29# Make definitions (convenience macros used for command access)
30# PIP := $(activate) && pip # invoke pip from virtualenv
31# PYTHON := $(activate) && python # invoke python from virtualenv
32#
33# Target declaration and command invocation:
34# my-target : $(venv-activate-script) # dependency installs virtualenv
35# <tab>$(PYTHON) --version # invoke python with arguments
36# <tab>$(PYTHON) my-command.py
37# <tab>$(activate) && pip install foobar
38#
39# % make my-target # Invoke make target from shell
Joey Armstronge98239c2023-05-08 17:10:07 -040040# -----------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040041
42$(if $(DEBUG),$(warning ENTER))
43
44##-------------------##
45##---] GLOBALS [---##
46##-------------------##
Joey Armstronge6a99912023-09-15 14:47:51 -040047.PHONY: venv venv-patched
Joey Armstrong0205d332023-04-11 17:29:23 -040048
49##------------------##
50##---] LOCALS [---##
51##------------------##
52venv-name ?= .venv# # default install directory
Joey Armstrong62ae1392024-03-17 20:49:30 -040053venv-abs-path := $(sandbox-root)/$(venv-name)# # Install directory
Joey Armstronge6a99912023-09-15 14:47:51 -040054venv-activate-bin := $(venv-name)/bin# # no whitespace
Joey Armstrongf548adc2023-09-08 15:59:42 -040055venv-activate-script := $(venv-activate-bin)/activate# # dependency
Joey Armstrong0205d332023-04-11 17:29:23 -040056
Joey Armstrong62ae1392024-03-17 20:49:30 -040057##--------------------##
58##---] INCLUDES [---##
59##--------------------##
60include $(ONF_MAKEDIR)/virtualenv/requirements-txt.mk
61include $(ONF_MAKEDIR)/virtualenv/version.mk
62
Joey Armstrongc86d0352023-11-09 10:31:10 -050063# ------------------------------------------------------------------------
64# Intent: Define macro activate= to access virtualenv activation script.
Joey Armstrong62ae1392024-03-17 20:49:30 -040065## -----------------------------------------------------------------------
Joey Armstrongc86d0352023-11-09 10:31:10 -050066# Usage:
67# - $(activate) && python # Syntax inlined within a target
68# - PYTHON := $(activate) && python # Define a named command macro
69# ------------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040070activate ?= set +u && source $(venv-activate-script) && set -u
71
72## -----------------------------------------------------------------------
Joey Armstrong62ae1392024-03-17 20:49:30 -040073## Intent: Explicit named installer target w/o dependencies.
74## Makefile targets should depend on venv-activate-script.
75## -----------------------------------------------------------------------
76venv := $(null)
77venv += $(venv-activate-script)# # virtualenv -p python3
78venv += $(venv-requirements-txt)# # pip install -r requirements.txt
79venv: $(venv)
80
81venv-patched : $(venv-activate-patched)
82
83## -----------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040084## Intent: Activate script path dependency
85## Usage:
86## o place on the right side of colon as a target dependency
87## o When the script does not exist install the virtual env and display.
88## -----------------------------------------------------------------------
89$(venv-activate-script):
Joey Armstrong62ae1392024-03-17 20:49:30 -040090
91 $(call banner-enter,(virtualenv -p python))
92
Joey Armstrong0205d332023-04-11 17:29:23 -040093 virtualenv -p python3 $(venv-name)
94 $(activate) && python -m pip install --upgrade pip
95 $(activate) && pip install --upgrade setuptools
Joey Armstrongf548adc2023-09-08 15:59:42 -040096
Joey Armstrong62ae1392024-03-17 20:49:30 -040097 $(HIDE)$(MAKE) --no-print-directory venv-requirements venv-version
98
99 $(call banner-leave,(virtualenv -t python))
Joey Armstrong0205d332023-04-11 17:29:23 -0400100
101## -----------------------------------------------------------------------
102## Intent: Explicit named installer target w/o dependencies.
103## Makefile targets should depend on venv-activate-script.
104## -----------------------------------------------------------------------
Joey Armstronge6a99912023-09-15 14:47:51 -0400105venv-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_MAKEDIR)/virtualenv/python_310_migration.sh
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## -----------------------------------------------------------------------
Joey Armstrong62ae1392024-03-17 20:49:30 -0400117# venv : $(venv-activate-script)
Joey Armstrong0205d332023-04-11 17:29:23 -0400118
119## -----------------------------------------------------------------------
120## Intent: Revert installation to a clean checkout
121## -----------------------------------------------------------------------
122sterile :: clean
123 $(RM) -r "$(venv-abs-path)"
124
125## -----------------------------------------------------------------------
126## -----------------------------------------------------------------------
127help ::
Joey Armstrong62ae1392024-03-17 20:49:30 -0400128 @printf ' %-33.33s %s\n' 'venv' \
129 'Create a python virtual environment'
130 @printf ' %-33.33s %s\n' 'venv-help' \
131 'Extended target help'
132
133## -----------------------------------------------------------------------
134## -----------------------------------------------------------------------
135venv-help ::
136 @printf ' %-33.33s %s\n' 'venv-patched' \
137 'venv patched for v3.10.6+ use'
138
139 @printf ' %-33.33s %s\n' 'venv' \
140 'Create a python virtual environment'
141 @printf ' %-33.33s %s\n' ' venv-name' \
142 'virtualenv installation directory name'
143
144## -----------------------------------------------------------------------
145## -----------------------------------------------------------------------
146todo ::
147 @echo "Rename include.mk into virtualenv.mk"
148 @echo "Create include.mk as a simple primary include file for the directory"
Joey Armstrong0205d332023-04-11 17:29:23 -0400149 @echo
Joey Armstrong62ae1392024-03-17 20:49:30 -0400150 @echo "Extract venv patch logic into a separate makefile'
Joey Armstrong0205d332023-04-11 17:29:23 -0400151
152$(if $(DEBUG),$(warning LEAVE))
153
154# [EOF]