Skip to content

Commit

Permalink
wheel.mk: Additional fixes mainly around PATH handling
Browse files Browse the repository at this point in the history
  • Loading branch information
th0ma7 committed Jan 9, 2025
1 parent cb680b4 commit 2d31802
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
30 changes: 29 additions & 1 deletion mk/spksrc.common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,36 @@ RED=$$(tput setaf 1)
GREEN=$$(tput setaf 2)
NC=$$(tput sgr0)

# Version Comparison
# Macro: Version Comparison
version_le = $(shell if printf '%s\n' "$(1)" "$(2)" | sort -VC ; then echo 1; fi)
version_ge = $(shell if printf '%s\n' "$(1)" "$(2)" | sort -VCr ; then echo 1; fi)
version_lt = $(shell if [ "$(1)" != "$(2)" ] && printf "%s\n" "$(1)" "$(2)" | sort -VC ; then echo 1; fi)
version_gt = $(shell if [ "$(1)" != "$(2)" ] && printf "%s\n" "$(1)" "$(2)" | sort -VCr ; then echo 1; fi)

# Macro: dedup
# removes duplicate entries from a specified delimiter,
# preserving the order of unique elements.
dedup = $(shell /bin/bash -c '\
input="$$(echo "$1" | xargs)"; \
delimiter="$$(echo "$2" | xargs)"; \
echo "$$input" | \
tr "$$delimiter" "\n" | \
awk '\''!seen[$$0]++ {print $$0}'\'' | \
tr "\n" "$$delimiter" | \
sed "s/$$delimiter$$//" \
')

# Macro: merge
# merges multiple environment variable values from a given input string,
# inverting their order and separating them with a specified delimiter
merge = $(shell /bin/bash -c '\
input="$$(echo "$1" | xargs)"; \
var_name="$$(echo "$2" | xargs)"; \
delimiter="$$(echo "$3" | xargs)"; \
echo "$$input" | \
grep -o "$$var_name=[^ ]*" | \
tac | \
sed "s/^$$var_name=//" | \
tr "\n" "$$delimiter" | \
sed "s/$$delimiter$$//" \
')
5 changes: 4 additions & 1 deletion mk/spksrc.crossenv.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ PYTHON_PKG_NAME = python$(subst .,,$(PYTHON_PKG_VERS_MAJOR_MINOR))
PYTHON_PKG_DIR = Python-$(PYTHON_PKG_VERS)
#
HOSTPYTHON_LIB_NATIVE = $(abspath $(WORK_DIR)/../../../native/$(PYTHON_PKG_NAME)/work-native/$(PYTHON_PKG_DIR)/build/lib.linux-$(shell uname -m)-$(PYTHON_PKG_VERS_MAJOR_MINOR))
PYTHON_NATIVE = $(abspath $(WORK_DIR)/../../../native/$(PYTHON_PKG_NAME)/work-native/install/usr/local/bin/python3)
PYTHON_NATIVE_PATH = $(abspath $(WORK_DIR)/../../../native/$(PYTHON_PKG_NAME)/work-native/install/usr/local/bin)
PYTHON_NATIVE = $(PYTHON_NATIVE_PATH)/python3
PYTHON_LIB_NATIVE = $(abspath $(PYTHON_WORK_DIR)/$(PYTHON_PKG_DIR)/build/lib.linux-$(shell uname -m)-$(PYTHON_PKG_VERS_MAJOR_MINOR))
PYTHON_LIB_CROSS = $(abspath $(PYTHON_WORK_DIR)/$(PYTHON_PKG_DIR)/build/lib.linux-$(shell expr "$(TC_TARGET)" : '\([^-]*\)' )-$(PYTHON_PKG_VERS_MAJOR_MINOR))

Expand Down Expand Up @@ -233,6 +234,8 @@ $(CROSSENV_PATH)/build/python-cc.mk:
@echo CROSSENV=$(CROSSENV_PATH)/bin/activate >> $@
@echo HOSTPYTHON=$(abspath $(PYTHON_WORK_DIR)/$(PYTHON_PKG_DIR)/hostpython) >> $@
@echo HOSTPYTHON_LIB_NATIVE=$(HOSTPYTHON_LIB_NATIVE) >> $@
@echo PYTHON_NATIVE=$(PYTHON_NATIVE) >> $@
@echo PYTHON_NATIVE_PATH=$(PYTHON_NATIVE_PATH) >> $@
@echo PYTHON_LIB_NATIVE=$(PYTHON_LIB_NATIVE) >> $@
@echo PYTHON_LIB_CROSS=$(PYTHON_LIB_CROSS) >> $@
@echo PYTHON_SITE_PACKAGES_NATIVE=$(abspath $(WORK_DIR)/../../../native/$(PYTHON_PKG_NAME)/work-native/install/usr/local/lib/python$(PYTHON_PKG_VERS_MAJOR_MINOR)/site-packages) >> $@
Expand Down
37 changes: 17 additions & 20 deletions mk/spksrc.wheel.mk
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,23 @@ else
@$(MSG) "[SKIP] Building pure-python"
endif

##
## crossenv PATH environment requires a combination of:
## 1) unique PATH variable from $(ENV) -> using merge + dedup macros
## Note: Multiple declarations of ENV += PATH=bla creates confusion in its interpretation.
## Solution implemented fetches all PATH from ENV and combine them in reversed order.
## 2) access to maturin from native/python<version>/.../bin -> ${PYTHON_NATIVE_PATH}/bin
## 3) access to crossenv/bin/cross* tools, mainly cross-pip -> ${CROSSENV_PATH}/bin
##
cross-compile-wheel-%: SHELL:=/bin/bash
cross-compile-wheel-%:
@for crossenv in $(WORK_DIR)/crossenv-$(WHEEL_NAME)-$(WHEEL_VERSION) $(WORK_DIR)/crossenv-$(WHEEL_NAME) $(WORK_DIR)/crossenv ; do \
[ -d $${crossenv} ] && . $${crossenv}/build/python-cc.mk && break ; \
done ; \
if [ -s "$${CROSSENV}" ] ; then \
. $${CROSSENV} ; \
$(MSG) "crossenv: [$${CROSSENV}]" ; \
$(MSG) "PATH = [$${PATH}]" ; \
$(MSG) "pip: [$$(which pip)]" ; \
if [ -d "$${CROSSENV_PATH}" ] ; then \
PATH=$(call dedup, $(call merge, $(ENV), PATH, :), :):$${PYTHON_NATIVE_PATH}:$${CROSSENV_PATH}/bin:$${PATH} ; \
$(MSG) "crossenv: [$${CROSSENV_PATH}]" ; \
$(MSG) "pip: [$$(which cross-pip)]" ; \
$(MSG) "maturin: [$$(which maturin)]" ; \
else \
echo "ERROR: crossenv not found!" ; \
Expand All @@ -226,33 +233,23 @@ cross-compile-wheel-%:
pip_global_option=$$(echo $(PIP_GLOBAL_OPTION) | sed 's/=\([^ ]*\)/="\1"/g; s/[^ ]*/--global-option=&/g') ; \
pip_global_option=$${pip_global_option}" --no-use-pep517" ; \
fi ; \
$(MSG) \
$(RUN) $(MSG) \
_PYTHON_HOST_PLATFORM=\"$(TC_TARGET)\" \
PATH=$(subst PATH=,,$(subst $(PATH),,$(filter PATH=%,$(ENV))))$${PATH} \
CFLAGS=\"$(CFLAGS)\" \
CPPFLAGS=\"$(CPPFLAGS)\" \
CXXFLAGS=\"$(CXXFLAGS)\" \
LDFLAGS=\"$(LDFLAGS)\" \
LDFLAGS=\"$(LDFLAGS)\" \
PATH=$${PATH} \
CMAKE_TOOLCHAIN_FILE=$${CMAKE_TOOLCHAIN_FILE} \
MESON_CROSS_FILE=$${MESON_CROSS_FILE} \
$$(which pip) \
cross-pip \
$(PIP_WHEEL_ARGS_CROSSENV) \
$${pip_global_option} \
--no-build-isolation \
$(ABI3) \
$(REQUIREMENT) ; \
$(RUN) \
_PYTHON_HOST_PLATFORM="$(TC_TARGET)" \
PATH=$(subst PATH=,,$(subst $(PATH),,$(filter PATH=%,$(ENV))))$${PATH} \
CFLAGS="$(CFLAGS)" \
CPPFLAGS="$(CPPFLAGS)" \
CXXFLAGS="$(CXXFLAGS)" \
LDFLAGS="$(LDFLAGS)" \
LDFLAGS="$(LDFLAGS)" \
PATH=$${PATH} \
CMAKE_TOOLCHAIN_FILE=$${CMAKE_TOOLCHAIN_FILE} \
MESON_CROSS_FILE=$${MESON_CROSS_FILE} \
$$(which pip) \
cross-pip \
$(PIP_WHEEL_ARGS_CROSSENV) \
$${pip_global_option} \
--no-build-isolation \
Expand Down

0 comments on commit 2d31802

Please sign in to comment.