blob: 052f959878d499d7c286aff25abc89985c57e141 [file] [log] [blame]
Joey Armstrong0205d332023-04-11 17:29:23 -04001# -*- makefile -*-
Joey Armstrongf2f0a3f2024-04-04 15:50:09 -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 Armstrongf2f0a3f2024-04-04 15:50:09 -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 Armstrongf2f0a3f2024-04-04 15:50:09 -040017# SPDX-FileCopyrightText: 2017-2024 Open Networking Foundation Contributors
18# SPDX-License-Identifier: Apache-2.0
19# -----------------------------------------------------------------------
20# -----------------------------------------------------------------------
Joey Armstrongc86d0352023-11-09 10:31:10 -050021# Intent:
22# This makefile defines dependencies that will install a python virtualenv
23# beneath $(sandbox)/.venv/. The $(activate) macro is used to source
24# .venv/bin/activate allowing command python and pip to be used.
Joey Armstronge6a99912023-09-15 14:47:51 -040025# -----------------------------------------------------------------------
26# Usage:
Joey Armstrongc86d0352023-11-09 10:31:10 -050027# include makefiles/virtualenv/include.mk
Joey Armstronge6a99912023-09-15 14:47:51 -040028#
Joey Armstrongc86d0352023-11-09 10:31:10 -050029# Makefile Target Dependencies:
30# tgt : $(venv-activate-patched) # python 3.10+ local use
31# tgt : $(venv-activate-script) # python < v3.8
32#
33# Make definitions (convenience macros used for command access)
34# PIP := $(activate) && pip # invoke pip from virtualenv
35# PYTHON := $(activate) && python # invoke python from virtualenv
36#
37# Target declaration and command invocation:
38# my-target : $(venv-activate-script) # dependency installs virtualenv
39# <tab>$(PYTHON) --version # invoke python with arguments
40# <tab>$(PYTHON) my-command.py
41# <tab>$(activate) && pip install foobar
42#
43# % make my-target # Invoke make target from shell
Joey Armstronge98239c2023-05-08 17:10:07 -040044# -----------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040045
46$(if $(DEBUG),$(warning ENTER))
47
48##-------------------##
49##---] GLOBALS [---##
50##-------------------##
Joey Armstronge6a99912023-09-15 14:47:51 -040051.PHONY: venv venv-patched
Joey Armstrong0205d332023-04-11 17:29:23 -040052
53##------------------##
54##---] LOCALS [---##
55##------------------##
56venv-name ?= .venv# # default install directory
Joey Armstrong62ae1392024-03-17 20:49:30 -040057venv-abs-path := $(sandbox-root)/$(venv-name)# # Install directory
Joey Armstronge6a99912023-09-15 14:47:51 -040058venv-activate-bin := $(venv-name)/bin# # no whitespace
Joey Armstrongf548adc2023-09-08 15:59:42 -040059venv-activate-script := $(venv-activate-bin)/activate# # dependency
Joey Armstrong0205d332023-04-11 17:29:23 -040060
Joey Armstrong62ae1392024-03-17 20:49:30 -040061##--------------------##
62##---] INCLUDES [---##
63##--------------------##
Joey Armstronga5d86792024-04-22 15:49:29 -040064include $(onf-mk-dir)/virtualenv/requirements-txt.mk
65include $(onf-mk-dir)/virtualenv/version.mk
Joey Armstrong62ae1392024-03-17 20:49:30 -040066
Joey Armstrongc86d0352023-11-09 10:31:10 -050067# ------------------------------------------------------------------------
68# Intent: Define macro activate= to access virtualenv activation script.
Joey Armstrong62ae1392024-03-17 20:49:30 -040069## -----------------------------------------------------------------------
Joey Armstrongc86d0352023-11-09 10:31:10 -050070# Usage:
71# - $(activate) && python # Syntax inlined within a target
72# - PYTHON := $(activate) && python # Define a named command macro
73# ------------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040074activate ?= set +u && source $(venv-activate-script) && set -u
75
76## -----------------------------------------------------------------------
Joey Armstrong62ae1392024-03-17 20:49:30 -040077## Intent: Explicit named installer target w/o dependencies.
78## Makefile targets should depend on venv-activate-script.
79## -----------------------------------------------------------------------
80venv := $(null)
81venv += $(venv-activate-script)# # virtualenv -p python3
82venv += $(venv-requirements-txt)# # pip install -r requirements.txt
83venv: $(venv)
84
85venv-patched : $(venv-activate-patched)
86
87## -----------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040088## Intent: Activate script path dependency
89## Usage:
90## o place on the right side of colon as a target dependency
91## o When the script does not exist install the virtual env and display.
92## -----------------------------------------------------------------------
93$(venv-activate-script):
Joey Armstrong62ae1392024-03-17 20:49:30 -040094
95 $(call banner-enter,(virtualenv -p python))
96
Joey Armstrong0205d332023-04-11 17:29:23 -040097 virtualenv -p python3 $(venv-name)
98 $(activate) && python -m pip install --upgrade pip
99 $(activate) && pip install --upgrade setuptools
Joey Armstrongf548adc2023-09-08 15:59:42 -0400100
Joey Armstrong62ae1392024-03-17 20:49:30 -0400101 $(HIDE)$(MAKE) --no-print-directory venv-requirements venv-version
102
103 $(call banner-leave,(virtualenv -t python))
Joey Armstrong0205d332023-04-11 17:29:23 -0400104
105## -----------------------------------------------------------------------
106## Intent: Explicit named installer target w/o dependencies.
107## Makefile targets should depend on venv-activate-script.
108## -----------------------------------------------------------------------
Joey Armstronge6a99912023-09-15 14:47:51 -0400109venv-activate-patched := $(venv-activate-script).patched
110venv-activate-patched : $(venv-activate-patched)
111$(venv-activate-patched) : $(venv-activate-script)
112 $(call banner-enter,Target $@)
Joey Armstronga5d86792024-04-22 15:49:29 -0400113 $(onf-mk-dir)/virtualenv/python_310_migration.sh
Joey Armstronge6a99912023-09-15 14:47:51 -0400114 touch $@
115 $(call banner-leave,Target $@)
116
117## -----------------------------------------------------------------------
118## Intent: Explicit named installer target w/o dependencies.
119## Makefile targets should depend on venv-activate-script.
120## -----------------------------------------------------------------------
Joey Armstrong62ae1392024-03-17 20:49:30 -0400121# venv : $(venv-activate-script)
Joey Armstrong0205d332023-04-11 17:29:23 -0400122
123## -----------------------------------------------------------------------
124## Intent: Revert installation to a clean checkout
125## -----------------------------------------------------------------------
126sterile :: clean
127 $(RM) -r "$(venv-abs-path)"
128
129## -----------------------------------------------------------------------
130## -----------------------------------------------------------------------
131help ::
Joey Armstrong62ae1392024-03-17 20:49:30 -0400132 @printf ' %-33.33s %s\n' 'venv' \
133 'Create a python virtual environment'
134 @printf ' %-33.33s %s\n' 'venv-help' \
135 'Extended target help'
136
137## -----------------------------------------------------------------------
138## -----------------------------------------------------------------------
139venv-help ::
140 @printf ' %-33.33s %s\n' 'venv-patched' \
141 'venv patched for v3.10.6+ use'
142
143 @printf ' %-33.33s %s\n' 'venv' \
144 'Create a python virtual environment'
145 @printf ' %-33.33s %s\n' ' venv-name' \
146 'virtualenv installation directory name'
147
Joey Armstrong3aabfdf2024-04-02 15:06:03 -0400148include $(onf-mk-dir)/virtualenv/todo.mk
Joey Armstrong0205d332023-04-11 17:29:23 -0400149
150$(if $(DEBUG),$(warning LEAVE))
151
152# [EOF]