Skip to content

Commit

Permalink
Improve clib builds reproducibility (#3679)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Nov 23, 2022
2 parents 8c58148 + 3ec0769 commit d805c53
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/3678.change.rst
@@ -0,0 +1 @@
Improve clib builds reproducibility by sorting sources -- by :user:`danigm`
2 changes: 1 addition & 1 deletion setuptools/command/build_clib.py
Expand Up @@ -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)

Expand Down
28 changes: 28 additions & 0 deletions setuptools/tests/test_build_clib.py
Expand Up @@ -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
Expand Down Expand Up @@ -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]

0 comments on commit d805c53

Please sign in to comment.