blob: 32fe4a9b29a321621f3b0ee94ead99d45e9d6014 [file] [log] [blame]
Joey Armstrong0205d332023-04-11 17:29:23 -04001# -*- makefile -*-
Joey Armstrongdc04c932024-04-01 12:14:21 -04002# -----------------------------------------------------------------------
3# Copyright 2017-2024 Open Networking Foundation Contributors
Joey Armstrong0205d332023-04-11 17:29:23 -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 Armstrongdc04c932024-04-01 12:14:21 -04009# http:#www.apache.org/licenses/LICENSE-2.0
Joey Armstrong0205d332023-04-11 17:29:23 -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.
Joey Armstrong7ad5c362023-07-09 19:10:16 -040016# -----------------------------------------------------------------------
Joey Armstrongdc04c932024-04-01 12:14:21 -040017# SPDX-FileCopyrightText: 2017-2024 Open Networking Foundation Contributors
18# SPDX-License-Identifier: Apache-2.0
19# -----------------------------------------------------------------------
Joey Armstrongc86d0352023-11-09 10:31:10 -050020# Intent:
21# This makefile defines dependencies that will install a python virtualenv
22# beneath $(sandbox)/.venv/. The $(activate) macro is used to source
23# .venv/bin/activate allowing command python and pip to be used.
Joey Armstronge6a99912023-09-15 14:47:51 -040024# -----------------------------------------------------------------------
25# Usage:
Joey Armstrongc86d0352023-11-09 10:31:10 -050026# include makefiles/virtualenv/include.mk
Joey Armstronge6a99912023-09-15 14:47:51 -040027#
Joey Armstrongc86d0352023-11-09 10:31:10 -050028# Makefile Target Dependencies:
29# tgt : $(venv-activate-patched) # python 3.10+ local use
30# tgt : $(venv-activate-script) # python < v3.8
31#
32# Make definitions (convenience macros used for command access)
33# PIP := $(activate) && pip # invoke pip from virtualenv
34# PYTHON := $(activate) && python # invoke python from virtualenv
35#
36# Target declaration and command invocation:
37# my-target : $(venv-activate-script) # dependency installs virtualenv
38# <tab>$(PYTHON) --version # invoke python with arguments
39# <tab>$(PYTHON) my-command.py
40# <tab>$(activate) && pip install foobar
41#
42# % make my-target # Invoke make target from shell
Joey Armstronge98239c2023-05-08 17:10:07 -040043# -----------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040044
45$(if $(DEBUG),$(warning ENTER))
46
47##-------------------##
48##---] GLOBALS [---##
49##-------------------##
Joey Armstronge6a99912023-09-15 14:47:51 -040050.PHONY: venv venv-patched
Joey Armstrong0205d332023-04-11 17:29:23 -040051
52##------------------##
53##---] LOCALS [---##
54##------------------##
55venv-name ?= .venv# # default install directory
Joey Armstrong62ae1392024-03-17 20:49:30 -040056venv-abs-path := $(sandbox-root)/$(venv-name)# # Install directory
Joey Armstronge6a99912023-09-15 14:47:51 -040057venv-activate-bin := $(venv-name)/bin# # no whitespace
Joey Armstrongf548adc2023-09-08 15:59:42 -040058venv-activate-script := $(venv-activate-bin)/activate# # dependency
Joey Armstrong0205d332023-04-11 17:29:23 -040059
Joey Armstrong62ae1392024-03-17 20:49:30 -040060##--------------------##
61##---] INCLUDES [---##
62##--------------------##
63include $(ONF_MAKEDIR)/virtualenv/requirements-txt.mk
64include $(ONF_MAKEDIR)/virtualenv/version.mk
65
Joey Armstrongc86d0352023-11-09 10:31:10 -050066# ------------------------------------------------------------------------
67# Intent: Define macro activate= to access virtualenv activation script.
Joey Armstrong62ae1392024-03-17 20:49:30 -040068## -----------------------------------------------------------------------
Joey Armstrongc86d0352023-11-09 10:31:10 -050069# Usage:
70# - $(activate) && python # Syntax inlined within a target
71# - PYTHON := $(activate) && python # Define a named command macro
72# ------------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040073activate ?= set +u && source $(venv-activate-script) && set -u
74
75## -----------------------------------------------------------------------
Joey Armstrong62ae1392024-03-17 20:49:30 -040076## Intent: Explicit named installer target w/o dependencies.
77## Makefile targets should depend on venv-activate-script.
78## -----------------------------------------------------------------------
79venv := $(null)
80venv += $(venv-activate-script)# # virtualenv -p python3
81venv += $(venv-requirements-txt)# # pip install -r requirements.txt
82venv: $(venv)
83
84venv-patched : $(venv-activate-patched)
85
86## -----------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040087## Intent: Activate script path dependency
88## Usage:
89## o place on the right side of colon as a target dependency
90## o When the script does not exist install the virtual env and display.
91## -----------------------------------------------------------------------
92$(venv-activate-script):
Joey Armstrong62ae1392024-03-17 20:49:30 -040093
94 $(call banner-enter,(virtualenv -p python))
95
Joey Armstrong0205d332023-04-11 17:29:23 -040096 virtualenv -p python3 $(venv-name)
97 $(activate) && python -m pip install --upgrade pip
98 $(activate) && pip install --upgrade setuptools
Joey Armstrongf548adc2023-09-08 15:59:42 -040099
Joey Armstrong62ae1392024-03-17 20:49:30 -0400100 $(HIDE)$(MAKE) --no-print-directory venv-requirements venv-version
101
102 $(call banner-leave,(virtualenv -t python))
Joey Armstrong0205d332023-04-11 17:29:23 -0400103
104## -----------------------------------------------------------------------
105## Intent: Explicit named installer target w/o dependencies.
106## Makefile targets should depend on venv-activate-script.
107## -----------------------------------------------------------------------
Joey Armstronge6a99912023-09-15 14:47:51 -0400108venv-activate-patched := $(venv-activate-script).patched
109venv-activate-patched : $(venv-activate-patched)
110$(venv-activate-patched) : $(venv-activate-script)
111 $(call banner-enter,Target $@)
112 $(ONF_MAKEDIR)/virtualenv/python_310_migration.sh
113 touch $@
114 $(call banner-leave,Target $@)
115
116## -----------------------------------------------------------------------
117## Intent: Explicit named installer target w/o dependencies.
118## Makefile targets should depend on venv-activate-script.
119## -----------------------------------------------------------------------
Joey Armstrong62ae1392024-03-17 20:49:30 -0400120# venv : $(venv-activate-script)
Joey Armstrong0205d332023-04-11 17:29:23 -0400121
122## -----------------------------------------------------------------------
123## Intent: Revert installation to a clean checkout
124## -----------------------------------------------------------------------
125sterile :: clean
126 $(RM) -r "$(venv-abs-path)"
127
128## -----------------------------------------------------------------------
129## -----------------------------------------------------------------------
130help ::
Joey Armstrong62ae1392024-03-17 20:49:30 -0400131 @printf ' %-33.33s %s\n' 'venv' \
132 'Create a python virtual environment'
133 @printf ' %-33.33s %s\n' 'venv-help' \
134 'Extended target help'
135
136## -----------------------------------------------------------------------
137## -----------------------------------------------------------------------
138venv-help ::
139 @printf ' %-33.33s %s\n' 'venv-patched' \
140 'venv patched for v3.10.6+ use'
141
142 @printf ' %-33.33s %s\n' 'venv' \
143 'Create a python virtual environment'
144 @printf ' %-33.33s %s\n' ' venv-name' \
145 'virtualenv installation directory name'
146
147## -----------------------------------------------------------------------
148## -----------------------------------------------------------------------
149todo ::
150 @echo "Rename include.mk into virtualenv.mk"
151 @echo "Create include.mk as a simple primary include file for the directory"
Joey Armstrong0205d332023-04-11 17:29:23 -0400152 @echo
Joey Armstrong62ae1392024-03-17 20:49:30 -0400153 @echo "Extract venv patch logic into a separate makefile'
Joey Armstrong0205d332023-04-11 17:29:23 -0400154
155$(if $(DEBUG),$(warning LEAVE))
156
157# [EOF]