Skip to content

Commit a9d42b6

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

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

distutils/compilers/C/msvc.py

+8-3
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,22 @@ 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+
# Move .mc files to the start of the list, otherwise keep the same order
387+
# See pypa/setuptools#4986
388+
sources = sorted(sources, key=lambda source: not str(source).endswith(".mc"))
389+
385390
compile_info = self._setup_compile(
386391
output_dir, macros, include_dirs, sources, depends, extra_postargs
387392
)

distutils/compilers/C/tests/test_msvc.py

+18
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,23 @@ def _get_vcvars_spec(host_platform, platform):
5354
monkeypatch.setattr(msvc, '_get_vcvars_spec', _get_vcvars_spec)
5455
compiler.initialize(plat_name)
5556

57+
@pytest.mark.skipif(
58+
not sysconfig.get_platform().startswith("win"),
59+
reason="Only run test for non-mingw Windows platforms",
60+
)
61+
def test_sources_compilation_order(self, tmp_path: Path) -> None:
62+
# We expect the the mc files to be compiled first, but otherwise keep the same order
63+
test_sources = [tmp_path / file for file in ("b.cpp", "y.mc", "a.c", "z.mc")]
64+
expected_objects = [
65+
str(tmp_path / obj) for obj in ("y.res", "z.res", "b.obj", "a.obj")
66+
]
67+
for source in test_sources:
68+
source.write_text("")
69+
70+
compiler = msvc.Compiler()
71+
objects = compiler.compile(test_sources)
72+
assert objects == expected_objects
73+
5674
@needs_winreg
5775
def test_get_vc_env_unicode(self):
5876
test_var = 'ṰḖṤṪ┅ṼẨṜ'

0 commit comments

Comments
 (0)