Skip to content

Commit

Permalink
python is working
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkie committed Nov 21, 2019
1 parent b456c59 commit 847ac17
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 45 deletions.
79 changes: 79 additions & 0 deletions openwrt/feeds/packages/lang/python/python/files/python-config.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!@EXENAME@

import sys
import os
import getopt
from distutils import sysconfig

# start changes
host_prefix = sysconfig.PREFIX

target_prefix = '@TARGET_PREFIX@'

target_data_dir = os.path.join(target_prefix, 'lib', 'python' + sysconfig.get_config_var('VERSION') + '-openwrt')
sys.path.append(target_data_dir)

try:
from _sysconfigdatatarget import build_time_vars
sysconfig._config_vars = {}
sysconfig._config_vars.update(build_time_vars)
except ImportError:
print >>sys.stderr, "Could not import target data from %s" % (target_data_dir)
sys.exit(1)
# end changes
# plus .replace(host_prefix, target_prefix) below

valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags',
'ldflags', 'help']

def exit_with_usage(code=1):
print >>sys.stderr, "Usage: %s [%s]" % (sys.argv[0],
'|'.join('--'+opt for opt in valid_opts))
sys.exit(code)

try:
opts, args = getopt.getopt(sys.argv[1:], '', valid_opts)
except getopt.error:
exit_with_usage()

if not opts:
exit_with_usage()

pyver = sysconfig.get_config_var('VERSION')
getvar = sysconfig.get_config_var

opt_flags = [flag for (flag, val) in opts]

if '--help' in opt_flags:
exit_with_usage(code=0)

for opt in opt_flags:
if opt == '--prefix':
#print sysconfig.PREFIX
print target_prefix

elif opt == '--exec-prefix':
#print sysconfig.EXEC_PREFIX
print target_prefix

elif opt in ('--includes', '--cflags'):
flags = ['-I' + sysconfig.get_python_inc(),
'-I' + sysconfig.get_python_inc(plat_specific=True)]
if opt == '--cflags':
flags.extend(getvar('CFLAGS').split())
#print ' '.join(flags)
print ' '.join(flags).replace(host_prefix, target_prefix)

elif opt in ('--libs', '--ldflags'):
libs = ['-lpython' + pyver]
libs += getvar('LIBS').split()
libs += getvar('SYSLIBS').split()
# add the prefix/lib/pythonX.Y/config dir, but only if there is no
# shared library in prefix/lib/.
if opt == '--ldflags':
if not getvar('Py_ENABLE_SHARED'):
libs.insert(0, '-L' + getvar('LIBPL'))
if not getvar('PYTHONFRAMEWORK'):
libs.extend(getvar('LINKFORSHARED').split())
#print ' '.join(libs)
print ' '.join(libs).replace(host_prefix, target_prefix)
48 changes: 28 additions & 20 deletions openwrt/feeds/packages/lang/python/python/files/python-host.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
# See /LICENSE for more information.
#

# Note: include this after `include $(TOPDIR)/rules.mk in your package Makefile
# if `python-package.mk` is included, this will already be included

ifneq ($(__python_host_mk_inc),1)
__python_host_mk_inc=1

# For PYTHON_VERSION
$(call include_mk, python-version.mk)
python_mk_path:=$(dir $(lastword $(MAKEFILE_LIST)))
include $(python_mk_path)python-version.mk

HOST_PYTHON_DIR:=$(STAGING_DIR_HOSTPKG)
HOST_PYTHON_INC_DIR:=$(HOST_PYTHON_DIR)/include/python$(PYTHON_VERSION)
Expand Down Expand Up @@ -37,21 +41,26 @@ define HostPython
$(HOST_PYTHON_BIN) $(2);
endef

define host_python_settings
ARCH="$(HOST_ARCH)" \
CC="$(HOSTCC)" \
CCSHARED="$(HOSTCC) $(HOST_FPIC)" \
CXX="$(HOSTCXX)" \
LD="$(HOSTCC)" \
LDSHARED="$(HOSTCC) -shared" \
CFLAGS="$(HOST_CFLAGS)" \
CPPFLAGS="$(HOST_CPPFLAGS) -I$(HOST_PYTHON_INC_DIR)" \
LDFLAGS="$(HOST_LDFLAGS) -lpython$(PYTHON_VERSION) -Wl$(comma)-rpath=$(STAGING_DIR_HOSTPKG)/lib" \
_PYTHON_HOST_PLATFORM=linux2
endef

# $(1) => commands to execute before running pythons script
# $(2) => python script and its arguments
# $(3) => additional variables
define Build/Compile/HostPyRunHost
$(call HostPython, \
$(if $(1),$(1);) \
CC="$(HOSTCC)" \
CCSHARED="$(HOSTCC) $(HOST_FPIC)" \
CXX="$(HOSTCXX)" \
LD="$(HOSTCC)" \
LDSHARED="$(HOSTCC) -shared" \
CFLAGS="$(HOST_CFLAGS)" \
CPPFLAGS="$(HOST_CPPFLAGS) -I$(HOST_PYTHON_INC_DIR)" \
LDFLAGS="$(HOST_LDFLAGS) -lpython$(PYTHON_VERSION) -Wl$(comma)-rpath=$(STAGING_DIR_HOSTPKG)/lib" \
_PYTHON_HOST_PLATFORM=linux2 \
$(call host_python_settings) \
$(3) \
, \
$(2) \
Expand All @@ -62,16 +71,15 @@ endef

# Note: I shamelessly copied this from Yousong's logic (from python-packages);
HOST_PYTHON_PIP:=$(STAGING_DIR_HOSTPKG)/bin/pip$(PYTHON_VERSION)
define host_python_pip_install
$(HOST_PYTHON_PIP) install \
--root=$(1) \
--prefix=$(2) \
--ignore-installed \
$(3)
endef

define host_python_pip_install_host
$(call host_python_pip_install,$(STAGING_DIR_HOSTPKG),"",$(1))
# $(1) => packages to install
define Build/Compile/HostPyPipInstall
$(call host_python_settings) \
$(HOST_PYTHON_PIP) \
--disable-pip-version-check \
--cache-dir "$(DL_DIR)/pip-cache" \
install \
$(1)
endef

# $(1) => build subdir
Expand All @@ -84,4 +92,4 @@ define Build/Compile/HostPyMod
$(3))
endef

endif # __python_host_mk_inc
endif # __python_host_mk_inc
59 changes: 34 additions & 25 deletions openwrt/feeds/packages/lang/python/python/files/python-package.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
# See /LICENSE for more information.
#

$(call include_mk, python-version.mk)
# Note: include this after `include $(TOPDIR)/rules.mk in your package Makefile

python_mk_path:=$(dir $(lastword $(MAKEFILE_LIST)))
include $(python_mk_path)python-host.mk

PYTHON_DIR:=$(STAGING_DIR)/usr
PYTHON_BIN_DIR:=$(PYTHON_DIR)/bin
Expand All @@ -32,12 +35,21 @@ ifdef CONFIG_USE_MIPS16
TARGET_CFLAGS += -mno-mips16 -mno-interlink-mips16
endif

define PyShebang
$(SED) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python2," -i --follow-symlinks $(1)
endef

define PyPackage

define Package/$(1)-src
$(call Package/$(1))
DEPENDS:=
CONFLICTS:=
PROVIDES:=
EXTRA_DEPENDS:=
TITLE+= (sources)
DEPENDS:=$$$$(foreach dep,$$$$(filter +python-%,$$$$(DEPENDS)),$$$$(dep)-src)
USERID:=
MENU:=
endef

define Package/$(1)-src/description
Expand Down Expand Up @@ -65,21 +77,13 @@ define PyPackage
$(call shexport,PyPackage/$(1)/filespec)

define Package/$(1)/install
$(call PyPackage/$(1)/install,$$(1))
find $(PKG_INSTALL_DIR) -name "*\.exe" | xargs rm -f
if [ -e files/python-package-install.sh ] ; then \
$(SHELL) files/python-package-install.sh \
"$(PKG_INSTALL_DIR)" "$$(1)" \
"$(HOST_PYTHON_BIN)" "$$(2)" \
"$$$$$$$$$$(call shvar,PyPackage/$(1)/filespec)" ; \
elif [ -e $(STAGING_DIR)/mk/python-package-install.sh ] ; then \
$(SHELL) $(STAGING_DIR)/mk/python-package-install.sh \
"$(PKG_INSTALL_DIR)" "$$(1)" \
"$(HOST_PYTHON_BIN)" "$$(2)" \
"$$$$$$$$$$(call shvar,PyPackage/$(1)/filespec)" ; \
else \
echo "No 'python-package-install.sh' script found" ; \
exit 1 ; \
$$(call PyPackage/$(1)/install,$$(1))
$(SHELL) $(python_mk_path)python-package-install.sh "2" \
"$(PKG_INSTALL_DIR)" "$$(1)" \
"$(HOST_PYTHON_BIN)" "$$(2)" \
"$$$$$$$$$$(call shvar,PyPackage/$(1)/filespec)" && \
if [ -d "$$(1)/usr/bin" ]; then \
$(call PyShebang,$$(1)/usr/bin/*) ; \
fi
endef

Expand All @@ -89,8 +93,6 @@ define PyPackage
endif # Package/$(1)/install
endef

$(call include_mk, python-host.mk)

# $(1) => commands to execute before running pythons script
# $(2) => python script and its arguments
# $(3) => additional variables
Expand Down Expand Up @@ -122,16 +124,23 @@ define Build/Compile/PyMod
cd $(PKG_BUILD_DIR)/$(strip $(1)), \
./setup.py $(2), \
$(3))
find $(PKG_INSTALL_DIR) -name "*\.exe" | xargs rm -f
endef

PYTHON_PKG_SETUP_DIR ?=
PYTHON_PKG_SETUP_GLOBAL_ARGS ?=
PYTHON_PKG_SETUP_ARGS ?= --single-version-externally-managed
PYTHON_PKG_SETUP_VARS ?=

define PyBuild/Compile/Default
$(foreach pkg,$(HOST_PYTHON_PACKAGE_BUILD_DEPENDS),
$(call host_python_pip_install_host,$(pkg))
$(if $(HOST_PYTHON_PACKAGE_BUILD_DEPENDS),
$(call Build/Compile/HostPyPipInstall,$(HOST_PYTHON_PACKAGE_BUILD_DEPENDS))
)
$(call Build/Compile/PyMod,, \
$(call Build/Compile/PyMod, \
$(PYTHON_PKG_SETUP_DIR), \
$(PYTHON_PKG_SETUP_GLOBAL_ARGS) \
install --prefix="/usr" --root="$(PKG_INSTALL_DIR)" \
--single-version-externally-managed \
$(PYTHON_PKG_SETUP_ARGS), \
$(PYTHON_PKG_SETUP_VARS) \
)
endef

Expand All @@ -141,4 +150,4 @@ ifeq ($(BUILD_VARIANT),python)
define Build/Compile
$(call PyBuild/Compile)
endef
endif # python
endif # python

0 comments on commit 847ac17

Please sign in to comment.