blob: 2ce04d2b4fbac23c59c17a48cb71cbd10cc89906 [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# -----------------------------------------------------------------------
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.
Joey Armstronge6a99912023-09-15 14:47:51 -040022# -----------------------------------------------------------------------
23# Usage:
Joey Armstrongc86d0352023-11-09 10:31:10 -050024# include makefiles/virtualenv/include.mk
Joey Armstronge6a99912023-09-15 14:47:51 -040025#
Joey Armstrongc86d0352023-11-09 10:31:10 -050026# 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
Joey Armstronge98239c2023-05-08 17:10:07 -040041# -----------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040042
43$(if $(DEBUG),$(warning ENTER))
44
45##-------------------##
46##---] GLOBALS [---##
47##-------------------##
Joey Armstronge6a99912023-09-15 14:47:51 -040048.PHONY: venv venv-patched
Joey Armstrong0205d332023-04-11 17:29:23 -040049
50##------------------##
51##---] LOCALS [---##
52##------------------##
53venv-name ?= .venv# # default install directory
Joey Armstronge6a99912023-09-15 14:47:51 -040054venv-abs-path := $(PWD)/$(venv-name)# #
55venv-activate-bin := $(venv-name)/bin# # no whitespace
Joey Armstrongf548adc2023-09-08 15:59:42 -040056venv-activate-script := $(venv-activate-bin)/activate# # dependency
Joey Armstrong0205d332023-04-11 17:29:23 -040057
Joey Armstrongc86d0352023-11-09 10:31:10 -050058# ------------------------------------------------------------------------
59# Intent: Define macro activate= to access virtualenv activation script.
60# Usage:
61# - $(activate) && python # Syntax inlined within a target
62# - PYTHON := $(activate) && python # Define a named command macro
63# ------------------------------------------------------------------------
Joey Armstrong0205d332023-04-11 17:29:23 -040064activate ?= set +u && source $(venv-activate-script) && set -u
65
66## -----------------------------------------------------------------------
67## Intent: Activate script path dependency
68## Usage:
69## o place on the right side of colon as a target dependency
70## o When the script does not exist install the virtual env and display.
71## -----------------------------------------------------------------------
72$(venv-activate-script):
73 @echo
Joey Armstronge6a99912023-09-15 14:47:51 -040074 @echo '============================='
75 @echo 'Installing python virtual env'
76 @echo '============================='
Joey Armstrong0205d332023-04-11 17:29:23 -040077 virtualenv -p python3 $(venv-name)
78 $(activate) && python -m pip install --upgrade pip
79 $(activate) && pip install --upgrade setuptools
Joey Armstrongf548adc2023-09-08 15:59:42 -040080 $(activate) && [[ -r requirements.txt ]] \
81 && { python -m pip install -r requirements.txt; } \
82 || { /bin/true; }
83
Joey Armstrong0205d332023-04-11 17:29:23 -040084 $(activate) && python --version
85
86## -----------------------------------------------------------------------
87## Intent: Explicit named installer target w/o dependencies.
88## Makefile targets should depend on venv-activate-script.
89## -----------------------------------------------------------------------
Joey Armstronge6a99912023-09-15 14:47:51 -040090venv-activate-patched := $(venv-activate-script).patched
91venv-activate-patched : $(venv-activate-patched)
92$(venv-activate-patched) : $(venv-activate-script)
93 $(call banner-enter,Target $@)
94 $(ONF_MAKEDIR)/virtualenv/python_310_migration.sh
95 touch $@
96 $(call banner-leave,Target $@)
97
98## -----------------------------------------------------------------------
99## Intent: Explicit named installer target w/o dependencies.
100## Makefile targets should depend on venv-activate-script.
101## -----------------------------------------------------------------------
102venv : $(venv-activate-script)
103venv-patched : $(venv-activate-patched)
Joey Armstrong0205d332023-04-11 17:29:23 -0400104
105## -----------------------------------------------------------------------
106## Intent: Revert installation to a clean checkout
107## -----------------------------------------------------------------------
108sterile :: clean
109 $(RM) -r "$(venv-abs-path)"
110
111## -----------------------------------------------------------------------
112## -----------------------------------------------------------------------
113help ::
114 @echo
115 @echo '[VIRTUAL ENV]'
116 @echo ' venv Create a python virtual environment'
117 @echo ' venv-name= Subdir name for virtualenv install'
118 @echo ' venv-activate-script make macro name'
119 @echo ' $$(target) dependency install python virtualenv'
120 @echo ' source $$(macro) && cmd configure env and run cmd'
121
122$(if $(DEBUG),$(warning LEAVE))
123
124# [EOF]