diff mbox series

[V2] python3-cython: make generated _cyutility.c be reproducible

Message ID 20260519124943.497200-1-zhixiong.chi@windriver.com
State New
Headers show
Series [V2] python3-cython: make generated _cyutility.c be reproducible | expand

Commit Message

Chi, Zhixiong May 19, 2026, 12:49 p.m. UTC
While python3 module use cython to build shared moudle utility library,
the generated source file _cyutility.c is not stable at each build and
made the generated library not be reproducible.

This commit use the fixed relative buildpath instead of the original
unpredictable tmpdir in the generated source file to assure the file
_cyutility.c should be reproducible.

Signed-off-by: Zhixiong Chi <zhixiong.chi@windriver.com>
---
 ...ve-dstdir-instead-of-the-unpredictab.patch | 93 +++++++++++++++++++
 .../python/python3-cython_3.2.4.bb            |  1 +
 2 files changed, 94 insertions(+)
 create mode 100644 meta/recipes-devtools/python/python3-cython/0001-Use-fixed-relative-dstdir-instead-of-the-unpredictab.patch
diff mbox series

Patch

diff --git a/meta/recipes-devtools/python/python3-cython/0001-Use-fixed-relative-dstdir-instead-of-the-unpredictab.patch b/meta/recipes-devtools/python/python3-cython/0001-Use-fixed-relative-dstdir-instead-of-the-unpredictab.patch
new file mode 100644
index 0000000000..68c21d82f9
--- /dev/null
+++ b/meta/recipes-devtools/python/python3-cython/0001-Use-fixed-relative-dstdir-instead-of-the-unpredictab.patch
@@ -0,0 +1,93 @@ 
+From 1a97d877e9fa9856c55db73bb5fbdcd3e86cada1 Mon Sep 17 00:00:00 2001
+From: Zhixiong Chi <zhixiong.chi@windriver.com>
+Date: Thu, 23 Apr 2026 00:19:59 -0700
+Subject: [PATCH] Use fixed relative dstdir instead of the unpredictable tmpdir
+ for sharedmodule
+
+When the option "--generated-shared" is used to generated __cyutility as the
+link https://github.com/scikit-learn/scikit-learn/pull/31151/files, the path
+for filename_table in the generated pyx/c file contains tmp dir which is not
+predictable though it has been updated to the relative path, and it caused
+the generated output file is not stable at each build and made the generated
+library is not reproducible [1] between builds.
+
+example as python3_pandas:
+
+vim build/_cyutility.c
+......
+/* #### Code section: filename_table ### */
+
+static const char* const __pyx_f[] = {
+  "../../../../../../../../../../../../tmp/tmpXXXXXX/_cyutility.pyx",
+  "<stringsource>",
+};
+
+Replace with the fixed path based dest directory provided by the option
+"--generated-shard", and don't use the tmpdir anymore.
+
+After applied this commit, vim build/_cyutility.c
+......
+/* #### Code section: filename_table ### */
+
+static const char* const __pyx_f[] = {
+  "shared_buildpath/_cyutility.pyx",
+  "<stringsource>",
+};
+
+[1] https://reproducible-builds.org/
+
+Upstream-Status: Submitted [https://github.com/cython/cython/pull/7634]
+
+Signed-off-by: Zhixiong Chi <zhixiong.chi@windriver.com>
+---
+ Cython/Build/SharedModule.py | 30 +++++++++++++++++-------------
+ 1 file changed, 17 insertions(+), 13 deletions(-)
+
+diff --git a/Cython/Build/SharedModule.py b/Cython/Build/SharedModule.py
+index b466e57ed..171f8180f 100644
+--- a/Cython/Build/SharedModule.py
++++ b/Cython/Build/SharedModule.py
+@@ -72,23 +72,27 @@ def generate_shared_module(options):
+     Errors.open_listing_file(None)
+ 
+     dest_c_file = options.shared_c_file_path
++    dest_c_dir = os.path.dirname(dest_c_file)
++    dest_tmp_buildpath = os.path.join(dest_c_dir, "shared_buildpath")
+     module_name = os.path.splitext(os.path.basename(dest_c_file))[0]
+ 
+     context = Main.Context.from_options(options)
+     scope = Symtab.ModuleScope('MemoryView', parent_module = None, context = context, is_package=False)
+ 
+-    with tempfile.TemporaryDirectory() as tmpdirname:
+-        pyx_file = os.path.join(tmpdirname, f'{module_name}.pyx')
+-        c_file = os.path.join(tmpdirname, f'{module_name}.c')
+-        with open(pyx_file, 'w'):
+-            pass
+-        source_desc = FileSourceDescriptor(pyx_file)
+-        comp_src = Main.CompilationSource(source_desc, EncodedString(module_name), os.getcwd())
+-        result = Main.create_default_resultobj(comp_src, options)
+-
+-        pipeline = create_shared_library_pipeline(context, scope, options, result)
+-        err, enddata = Pipeline.run_pipeline(pipeline, comp_src)
+-        if err is None:
+-            shutil.copy(c_file, dest_c_file)
++    os.makedirs(dest_tmp_buildpath, exist_ok=True)
++    pyx_file = os.path.join(dest_tmp_buildpath, f'{module_name}.pyx')
++    c_file = os.path.join(dest_tmp_buildpath, f'{module_name}.c')
++    with open(pyx_file, 'w'):
++        pass
++    source_desc = FileSourceDescriptor(pyx_file)
++    comp_src = Main.CompilationSource(source_desc, EncodedString(module_name), os.getcwd())
++    result = Main.create_default_resultobj(comp_src, options)
++
++    pipeline = create_shared_library_pipeline(context, scope, options, result)
++    err, enddata = Pipeline.run_pipeline(pipeline, comp_src)
++    if err is None:
++        shutil.copy(c_file, dest_c_file)
++
++    shutil.rmtree(dest_tmp_buildpath)
+ 
+     return err, enddata
+-- 
+2.49.0
+
diff --git a/meta/recipes-devtools/python/python3-cython_3.2.4.bb b/meta/recipes-devtools/python/python3-cython_3.2.4.bb
index 3e889857e1..d380489b78 100644
--- a/meta/recipes-devtools/python/python3-cython_3.2.4.bb
+++ b/meta/recipes-devtools/python/python3-cython_3.2.4.bb
@@ -13,6 +13,7 @@  inherit pypi setuptools3 cython
 
 SRC_URI += " \
     file://0001-Replace-not-predictable-build-path-prefix-with-hardc.patch \
+    file://0001-Use-fixed-relative-dstdir-instead-of-the-unpredictab.patch \
 "
 
 # No need to depend on self