Skip to content

Commit 034b27f

Browse files
committed
Resort sources to have .mc first
1 parent 42dfd06 commit 034b27f

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

distutils/compilers/C/msvc.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import subprocess
1919
import unittest.mock as mock
2020
import warnings
21-
from collections.abc import Iterable
21+
from collections.abc import Iterable, Sequence
2222

2323
with contextlib.suppress(ImportError):
2424
import winreg
@@ -371,17 +371,24 @@ def out_extensions(self) -> dict[str, str]:
371371

372372
def compile( # noqa: C901
373373
self,
374-
sources,
374+
sources: Sequence[str | os.PathLike[str]],
375375
output_dir=None,
376376
macros=None,
377377
include_dirs=None,
378378
debug=False,
379379
extra_preargs=None,
380380
extra_postargs=None,
381381
depends=None,
382-
):
382+
) -> list[str]:
383383
if not self.initialized:
384384
self.initialize()
385+
386+
# Re-sort the list of sources so that .mc files come first
387+
# See pypa/setuptools#4986
388+
sources = sorted(
389+
sources, key=lambda source: (not str(source).endswith(".mc"), source)
390+
)
391+
385392
compile_info = self._setup_compile(
386393
output_dir, macros, include_dirs, sources, depends, extra_postargs
387394
)

distutils/compilers/C/tests/test_msvc.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from distutils.errors import DistutilsPlatformError
77
from distutils.tests import support
88
from distutils.util import get_platform
9+
from pathlib import Path
910

1011
import pytest
1112

@@ -53,6 +54,19 @@ def _get_vcvars_spec(host_platform, platform):
5354
monkeypatch.setattr(msvc, '_get_vcvars_spec', _get_vcvars_spec)
5455
compiler.initialize(plat_name)
5556

57+
def test_sources_compilation_order(self, tmp_path: Path) -> None:
58+
test_sources = [tmp_path / "b.cpp", tmp_path / "z.mc", tmp_path / "a.c"]
59+
expected_objects = [
60+
os.path.splitdrive(tmp_path / obj)[1].removeprefix(os.path.sep)
61+
for obj in ("z.res", "a.obj", "b.obj")
62+
]
63+
for source in test_sources:
64+
source.write_text("")
65+
66+
compiler = msvc.Compiler()
67+
objects = compiler.compile(test_sources)
68+
assert objects == expected_objects
69+
5670
@needs_winreg
5771
def test_get_vc_env_unicode(self):
5872
test_var = 'ṰḖṤṪ┅ṼẨṜ'

0 commit comments

Comments
 (0)