Skip to content

Commit 7fe2c33

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

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

distutils/compilers/C/msvc.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
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
22+
from pathlib import Path
2223

2324
with contextlib.suppress(ImportError):
2425
import winreg
@@ -371,17 +372,24 @@ def out_extensions(self) -> dict[str, str]:
371372

372373
def compile( # noqa: C901
373374
self,
374-
sources,
375+
sources: Sequence[str | os.PathLike[str]],
375376
output_dir=None,
376377
macros=None,
377378
include_dirs=None,
378379
debug=False,
379380
extra_preargs=None,
380381
extra_postargs=None,
381382
depends=None,
382-
):
383+
) -> list[str]:
383384
if not self.initialized:
384385
self.initialize()
386+
387+
# Move .mc files to the start of the list, otherwise keep the same order
388+
# See pypa/setuptools#4986
389+
sources = sorted(
390+
sources, key=lambda source: Path(source).suffix not in self._mc_extensions
391+
)
392+
385393
compile_info = self._setup_compile(
386394
output_dir, macros, include_dirs, sources, depends, extra_postargs
387395
)

distutils/compilers/C/tests/test_msvc.py

Lines changed: 18 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,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)