jw-pkg/make/py-defs.mk
Jan Lindemann f05e1ee0e3 make/[Makefile|*.mk): Improve variable caching
This commit aims at improving speed by using better caching.

  - Makefile, cache.mk: Split .cache.mk up

    To allow caching of runtime path variables which are
    project-specific, split .cache.mk up in .cache-project.mk and
    .cache-projects.mk

  - ldlibpath.mk: Cache ldlibpath, exepath and pythonpath

    Place the output of $(call proj_query ldlibpath), $(call
    proj_query, exepath) and $(call proj_query pythonpath) in
    JW_PKG_LD_LIBRARY_PATH, JW_PKG_EXE_PATH, and JW_PKG_PYTHON_PATH
    respectively, and cache the variables in make/.project-cache.mk.

  - cache.mk: Use = instead of :=

    Recursively expanded variables are nearly as fast as := variables
    if the assigned value is a fixed string. And sometimes it's not,
    rightly so, because variables get assigned below, as with
    JW_PKG_XXX for instance.

  - cache.mk: Use $(TOPDIR) as variable values

    Replace absolute references to project's topdir by $(TOPDIR) with
    sed. As soon as the project queries produce absolute paths, they
    will be transformed into relative paths which allow the code base
    to be moved to a different location and still remain functional
    without a rebuild.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-01-26 17:17:23 +01:00

119 lines
4.2 KiB
Makefile

include $(JWBDIR)/make/py-version.mk
ifeq ($(DEVELOPMENT),true)
PY_SITE_PACKAGES_PATH := $(PREFIX)/python$(PYTHON_VERSION)/site-packages
else
PY_SITE_PACKAGES_PATH := $(shell $(PYTHON) -c "import site; print([d for d in site.getsitepackages() if d.find('/local/') == -1][0])")
endif
ifndef PY_PREREQ_BUILD
PY_PREREQ_BUILD := $(shell $(proj_query_cmd) pkg-requires --subsections jw -d ' ' -p --no-version build $(PROJECT))
endif
ifndef PY_PREREQ_BUILD_DIRS
PY_PREREQ_BUILD_DIRS := $(shell $(proj_query_cmd) proj-dir $(PY_PREREQ_BUILD))
endif
PY_MYPY ?= mypy --ignore-missing-imports --no-namespace-packages
PY_SRC_PY ?= $(wildcard *.py)
PY_ALL_PY = $(PY_SRC_PY)
ifneq ($(PYTHON_MAJOR),3)
PY_PYC = $(patsubst %.py,%.pyc,$(PY_ALL_PY))
else
PY_CPYTHON_PREFIX := $(shell $(PYTHON) -c "import sys; print('cpython-{}{}'.format(sys.version_info[0],sys.version_info[1]))")
PY_PYC = $(patsubst %.py,__pycache__/%.$(PY_CPYTHON_PREFIX).pyc,$(PY_ALL_PY))
endif
ifneq ($(PY_INSTALL_DIR),)
PY_INSTALL_DIR_PY ?= $(PY_INSTALL_DIR)
endif
# deduce PY_INSTALL_DIR_PY from working directory below .. python/
ifeq ($(PY_INSTALL_DIR_PY),)
PY_INSTALL_PKG_MOD ?= $(shell $(ECHO) $(CWD) | $(SED) 's%.*/python/%%; s%/.*%%')
PY_INSTALL_SUB_MOD ?= $(shell $(ECHO) $(CWD) | $(SED) "s%.*/$(PY_INSTALL_PKG_MOD)\(/\|$$\)%%")
ifneq ($(PY_INSTALL_SUB_MOD),)
PY_INSTALL_MOD ?= $(PY_INSTALL_PKG_MOD)/$(PY_INSTALL_SUB_MOD)
else
PY_INSTALL_MOD ?= $(PY_INSTALL_PKG_MOD)
endif
PY_MOD ?= $(subst /,.,$(PY_INSTALL_MOD))
PY_ALL_PY += __init__.py
PY_INSTALL_DIR_PY ?= $(ENV_PREFIX)$(PY_SITE_PACKAGES_PATH)/$(patsubst .,/,$(PY_INSTALL_MOD))
else
ifeq ($(PY_INSTALL_DIR_PY),)
$(error PY_INSTALL_DIR_PY is undefined)
endif
endif
ifeq ($(PYTHON_MAJOR),3)
PY_INSTALL_DIR_PYC ?= $(PY_INSTALL_DIR_PY)/__pycache__
else
PY_INSTALL_DIR_PYC ?= $(PY_INSTALL_DIR_PY)
endif
PY_UPDATE_INIT_PY ?= true
PY_INSTALL ?= true
PY_INSTALL_REG ?= true
ifneq ($(PY_INSTALL),true)
PY_INSTALL_REG = false
endif
PY_INSTALL_PYC ?= true
ifneq ($(PY_INSTALL_REG),true)
PY_INSTALL_PYC = false
endif
PY_INSTALL_DIRS ?= $(sort $(PY_INSTALL_DIR_PY) $(PY_INSTALL_DIR_PYC))
PY_INSTALLED_PY = $(addprefix $(PY_INSTALL_DIR_PY)/,$(sort $(PY_ALL_PY)))
ifeq ($(PY_INSTALL_PYC),true)
PY_INSTALLED_PYC = $(addprefix $(PY_INSTALL_DIR_PY)/,$(PY_PYC))
endif
ifeq ($(PY_INSTALL_REG),true)
PY_INSTALLED_REG = $(PY_INSTALLED_PY) $(PY_INSTALLED_PYC)
endif
ifeq ($(origin PYTHONPATH),undefined)
PYTHONPATH := $(JW_PKG_PYTHON_PATH)
endif
#PYTHONPATH = $(subst $(space),,$(ENV_PYTHONPATH)$(foreach d,$(PY_MODULE_DIRS),:$(d)))
MYPYPATH = $(PYTHONPATH)
export PYTHONPATH MYPYPATH
# not used anywhere
#PY_PROJ_MODULE_DIRS ?= $(wildcard $(foreach subdir,/tools/python /src/python,$(addsuffix $(subdir),$(TOPDIR) $(PY_PREREQ_BUILD_DIRS))))
#PY_MODULE_DIRS ?= $(PY_PROJ_MODULE_DIRS)
#PY_MODULES ?= $(PY_PROJ_MODULES)
#PY_PROJ_MODULES += $(dir $(wildcard $(PY_MODULE_DIRS)/*/__init__.py))
ifneq ($(PY_DEFS_MK_INCLUDED),true)
all:
clean: pyc.clean
echo-py:
@echo "PYTHON = $(PYTHON)"
@echo "PY_INSTALL_DIR_PY = $(PY_INSTALL_DIR_PY)"
@echo "PROJECT = $(PROJECT)"
@echo "PY_SITE_PACKAGES_PATH = $(PY_SITE_PACKAGES_PATH)"
@echo "PY_MODULES = $(PY_MODULES)"
@echo "PYTHONPATH = $(PYTHONPATH)"
@echo "MYPYPATH = $(MYPYPATH)"
@echo "PY_INSTALL_MOD = $(PY_INSTALL_MOD)"
@echo "PY_INSTALL_SUB_MOD = $(PY_INSTALL_SUB_MOD)"
# not used anywhere"
#@echo "PY_PROJ_MODULE_DIRS = $(PY_PROJ_MODULE_DIRS)"
#@echo "PY_MODULE_DIRS = $(PY_MODULE_DIRS)"
pyc.clean:
$(RM) -rf $(wildcard *.pyc) __pycache__
libpath: py-libpath
py-libpath:
@echo export PYTHONPATH=$(PYTHONPATH)
@echo export MYPYPATH=$(MYPYPATH)
endif
PY_DEFS_MK_INCLUDED := true
include $(JWBDIR)/make/ldlibpath.mk