python3: Move functionality into python3-package.mk
authorJeffery To <jeffery.to@gmail.com>
Tue, 14 Apr 2020 13:14:49 +0000 (21:14 +0800)
committerJeffery To <jeffery.to@gmail.com>
Sat, 18 Apr 2020 17:56:23 +0000 (01:56 +0800)
This moves functionality from python-package-install.sh into
python3-package.mk, so that they can be reused separate from filespec
processing.

Signed-off-by: Jeffery To <jeffery.to@gmail.com>
lang/python/python-package-install.sh
lang/python/python3-package.mk

index 337727af1f9294389067bf4582a477fc5d53d588..fa42f6f4a80f361a63ff0f68235f284c480d5bed 100644 (file)
@@ -1,11 +1,6 @@
 #!/bin/sh
 set -e
 
-[ -z "$SOURCE_DATE_EPOCH" ] || {
-       PYTHONHASHSEED="$SOURCE_DATE_EPOCH"
-       export PYTHONHASHSEED
-}
-
 process_filespec() {
        local src_dir="$1"
        local dst_dir="$2"
@@ -40,56 +35,11 @@ process_filespec() {
        )
 }
 
-delete_empty_dirs() {
-       local dst_dir="$1"
-       if [ -d "$dst_dir/usr" ] ; then
-               find "$dst_dir/usr" -empty -type d -delete
-       fi
-}
-
-ver="$1"
-src_dir="$2"
-dst_dir="$3"
-python="$4"
-mode="$5"
-filespec="$6"
-
-find "$src_dir" -name "*.exe" -delete
+src_dir="$1"
+dst_dir="$2"
+filespec="$3"
 
 process_filespec "$src_dir" "$dst_dir" "$filespec" || {
        echo "process filespec error-ed"
        exit 1
 }
-
-if [ "$mode" == "sources" ] ; then
-       # Copy only python source files
-       find "$dst_dir" -not -type d -not -name "*.py" -delete
-
-       delete_empty_dirs "$dst_dir"
-       exit 0
-fi
-
-if [ "$ver" == "3" ] ; then
-       legacy="-b"
-fi
-# default max recursion is 10
-max_recursion_level=20
-
-# XXX [So that you won't goof as I did]
-# Note: Yes, I tried to use the -O & -OO flags here.
-#       However the generated byte-codes were not portable.
-#       So, we just stuck to un-optimized byte-codes,
-#       which is still way better/faster than running
-#       Python sources all the time.
-$python -m compileall -r "$max_recursion_level" $legacy -d '/' "$dst_dir" || {
-       echo "python -m compileall err-ed"
-       exit 1
-}
-
-# Delete source files and pyc [ un-optimized bytecode files ]
-# We may want to make this optimization thing configurable later, but not sure atm
-find "$dst_dir" -type f -name "*.py" -delete
-
-delete_empty_dirs "$dst_dir"
-
-exit 0
index 81e7cd77786d536e4cef7af8e2ee4df330f12f27..6fab0f5cdbbe244538e801ed69227c375396dc4b 100644 (file)
@@ -75,7 +75,47 @@ define Python3/ModSetup
 endef
 
 define Python3/FixShebang
-$(SED) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python3," -i --follow-symlinks $(1)
+       $(SED) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python3," -i --follow-symlinks $(1)
+endef
+
+# default max recursion is 10
+PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL:=20
+
+# $(1) => directory of python source files to compile
+#
+# XXX [So that you won't goof as I did]
+# Note: Yes, I tried to use the -O & -OO flags here.
+#       However the generated byte-codes were not portable.
+#       So, we just stuck to un-optimized byte-codes,
+#       which is still way better/faster than running
+#       Python sources all the time.
+#
+# Setting a fixed hash seed value is less secure than using
+# random seed values, but is necessary for reproducible builds
+# (for now).
+#
+# Should revisit this when https://bugs.python.org/issue37596
+# (and other related reproducibility issues) are fixed.
+define Python3/CompileAll
+       $(call Python3/Run,, \
+               -m compileall -r "$(PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL)" -b -d '/' $(1),
+               $(if $(SOURCE_DATE_EPOCH),PYTHONHASHSEED="$(SOURCE_DATE_EPOCH)")
+       )
+endef
+
+# $(1) => target directory
+define Python3/DeleteSourceFiles
+       $(FIND) $(1) -type f -name '*.py' -delete
+endef
+
+# $(1) => target directory
+define Python3/DeleteNonSourceFiles
+       $(FIND) $(1) -not -type d -not -name '*.py' -delete
+endef
+
+# $(1) => target directory
+define Python3/DeleteEmptyDirs
+       $(FIND) $(1) -mindepth 1 -empty -type d -not -path '$(1)/CONTROL' -not -path '$(1)/CONTROL/*' -delete
 endef
 
 
@@ -122,20 +162,28 @@ define Py3Package
   ifndef Package/$(1)/install
   $(call shexport,Py3Package/$(1)/filespec)
 
-  define Package/$(1)/install
+    define Package/$(1)/install
        $$(call Py3Package/$(1)/install,$$(1))
-       $(SHELL) $(python3_mk_path)python-package-install.sh "3" \
+       $(SHELL) $(python3_mk_path)python-package-install.sh \
                "$(PKG_INSTALL_DIR)" "$$(1)" \
-               "$(HOST_PYTHON3_BIN)" "$$(2)" \
-               "$$$$$$$$$$(call shvar,Py3Package/$(1)/filespec)" && \
-       if [ -d "$$(1)/usr/bin" ]; then \
-               $(call Python3/FixShebang,$$(1)/usr/bin/*) ; \
+               "$$$$$$$$$$(call shvar,Py3Package/$(1)/filespec)"
+       $(FIND) $$(1) -name '*.exe' -delete
+       $$(call Python3/CompileAll,$$(1))
+       $$(call Python3/DeleteSourceFiles,$$(1))
+       $$(call Python3/DeleteEmptyDirs,$$(1))
+       if [ -d "$$(1)/usr/bin" ]; then \
+               $$(call Python3/FixShebang,$$(1)/usr/bin/*) ; \
        fi
- endef
   endef
 
-  define Package/$(1)-src/install
-       $$(call Package/$(1)/install,$$(1),sources)
-  endef
+    define Package/$(1)-src/install
+       $$(call Py3Package/$(1)/install,$$(1))
+       $(SHELL) $(python3_mk_path)python-package-install.sh \
+               "$(PKG_INSTALL_DIR)" "$$(1)" \
+               "$$$$$$$$$$(call shvar,Py3Package/$(1)/filespec)"
+       $$(call Python3/DeleteNonSourceFiles,$$(1))
+       $$(call Python3/DeleteEmptyDirs,$$(1))
+    endef
   endif # Package/$(1)/install
 endef