From e2a115b35e151ece7fefd0e095a73d478e2268f6 Mon Sep 17 00:00:00 2001 From: Daniel Garcia Moreno Date: Mon, 14 Nov 2022 12:56:32 +0100 Subject: [PATCH] Make clib build reproducible by sorting sources Fix https://github.com/pypa/setuptools/issues/3678 --- changelog.d/3678.change.rst | 1 + setuptools/_distutils/command/build_clib.py | 2 +- setuptools/command/build_clib.py | 2 +- setuptools/tests/test_build_clib.py | 28 +++++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 changelog.d/3678.change.rst diff --git a/changelog.d/3678.change.rst b/changelog.d/3678.change.rst new file mode 100644 index 00000000000..9893f99fc84 --- /dev/null +++ b/changelog.d/3678.change.rst @@ -0,0 +1 @@ +Make clib builds reproducible by sorting sources -- by :user:`danigm` diff --git a/setuptools/_distutils/command/build_clib.py b/setuptools/_distutils/command/build_clib.py index 50bb9bbabb7..7d5f8c51349 100644 --- a/setuptools/_distutils/command/build_clib.py +++ b/setuptools/_distutils/command/build_clib.py @@ -183,7 +183,7 @@ def build_libraries(self, libraries): "'sources' must be present and must be " "a list of source filenames" % lib_name ) - sources = list(sources) + sources = sorted(list(sources)) log.info("building '%s' library", lib_name) diff --git a/setuptools/command/build_clib.py b/setuptools/command/build_clib.py index 67ce2444ea6..09483e69e5b 100644 --- a/setuptools/command/build_clib.py +++ b/setuptools/command/build_clib.py @@ -28,7 +28,7 @@ def build_libraries(self, libraries): "in 'libraries' option (library '%s'), " "'sources' must be present and must be " "a list of source filenames" % lib_name) - sources = list(sources) + sources = sorted(list(sources)) log.info("building '%s' library", lib_name) diff --git a/setuptools/tests/test_build_clib.py b/setuptools/tests/test_build_clib.py index af9e7c6dc39..2d9273cdcab 100644 --- a/setuptools/tests/test_build_clib.py +++ b/setuptools/tests/test_build_clib.py @@ -2,6 +2,7 @@ import pytest +import random from distutils.errors import DistutilsSetupError from setuptools.command.build_clib import build_clib from setuptools.dist import Distribution @@ -56,3 +57,30 @@ def test_build_libraries(self, mock_newer): cmd.build_libraries(libs) assert cmd.compiler.compile.call_count == 1 assert cmd.compiler.create_static_lib.call_count == 1 + + @mock.patch( + 'setuptools.command.build_clib.newer_pairwise_group') + def test_build_libraries_reproducible(self, mock_newer): + dist = Distribution() + cmd = build_clib(dist) + + # with that out of the way, let's see if the crude dependency + # system works + cmd.compiler = mock.MagicMock(spec=cmd.compiler) + mock_newer.return_value = ([], []) + + original_sources = ['a-example.c', 'example.c'] + sources = original_sources + + obj_deps = {'': ('global.h',), 'example.c': ('example.h',)} + libs = [('example', {'sources': sources, 'obj_deps': obj_deps})] + + cmd.build_libraries(libs) + computed_call_args = mock_newer.call_args[0] + + while sources == original_sources: + sources = random.sample(original_sources, len(original_sources)) + libs = [('example', {'sources': sources, 'obj_deps': obj_deps})] + + cmd.build_libraries(libs) + assert computed_call_args == mock_newer.call_args[0]