-
Notifications
You must be signed in to change notification settings - Fork 9
Add Support for -MD, -MMD, -gccdep #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hohle
wants to merge
1
commit into
mkst:main
Choose a base branch
from
hohle:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| from pathlib import Path | ||
| import re | ||
|
|
||
|
|
||
| class MakeRule: | ||
| target: str | ||
| source: str | None | ||
| includes: list[str] | ||
|
|
||
| def __init__(self, data: bytes, use_wibo=False): | ||
| if use_wibo: | ||
| encoding = "iso-8859-1" | ||
| else: | ||
| encoding = "utf-8" | ||
|
|
||
| rule = data.decode(encoding) | ||
| rule = re.sub(r"\\[\r\n]+", " ", rule) | ||
| (target, remaining) = re.split(": ", rule) | ||
| files = remaining.split() | ||
| files.insert(0, target) | ||
|
|
||
| if use_wibo: | ||
| files = [path_from_wibo(p) for p in files] | ||
|
|
||
| # the first file is the target, the second is the | ||
| self.target = files.pop(0) | ||
| self.source = files.pop(0) | ||
| self.includes = files | ||
|
|
||
| def as_str(self): | ||
| rule = f"{self.target}: " | ||
| if self.source is not None: | ||
| rule += f"{self.source} " | ||
| for file in self.includes: | ||
| rule += f"\\\n\t{file} " | ||
| rule += "\n" | ||
|
|
||
| return rule | ||
|
|
||
|
|
||
| # an implementation of the wibo translation from "windows" | ||
| # path to a unix path | ||
| def path_from_wibo(path_str: str) -> Path: | ||
| path_str = path_str.replace("\\", "/") | ||
|
|
||
| # remove the extended path prefix | ||
| if path_str.startswith("//?/"): | ||
| path_str = path_str[4:] | ||
|
|
||
| # remove the drive letter | ||
| if path_str.lower().startswith("z:/"): | ||
| path_str = path_str[2:] | ||
|
|
||
| # if it exists, we're done | ||
| path = Path(path_str) | ||
| if path.is_file(): | ||
| return path | ||
|
|
||
| # otherwise try to find a case insensitive match | ||
| new_path = Path(".") | ||
| for part in path.parts: | ||
| candidate = new_path / part | ||
| if new_path.is_dir(): | ||
| for entry in new_path.iterdir(): | ||
| if entry.name.lower() == part.lower(): | ||
| candidate = new_path / entry.name | ||
| break | ||
| new_path = candidate | ||
|
|
||
| return new_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import tempfile | ||
| import unittest | ||
| import os | ||
| import re | ||
| import shutil | ||
|
|
||
| from pathlib import Path | ||
| from mwccgap.compiler import Compiler | ||
| from mwccgap.makerule import MakeRule | ||
|
|
||
| mwcc = os.getenv("MWCC") | ||
| if mwcc is None: | ||
| mwcc = "mwccpsp.exe" | ||
|
|
||
|
|
||
| def has_wibo_and_mwcc(): | ||
| wibo = shutil.which("wibo") | ||
| mwcc_exe = shutil.which(mwcc) | ||
| return wibo is not None and mwcc_exe is not None | ||
|
|
||
|
|
||
| class TestDependencies(unittest.TestCase): | ||
| def __init__(self, x): | ||
| super().__init__(x) | ||
| self.wibo = shutil.which("wibo") | ||
| self.mwcc = shutil.which(mwcc) | ||
|
|
||
| def has_dependencies(self): | ||
| return self.wibo is not None and self.mwcc is not None | ||
|
|
||
| def _compile(self, c_flags: list[str], program: str): | ||
| compiler = Compiler(c_flags, self.mwcc, True, self.wibo) | ||
|
|
||
| test_path = os.path.abspath(__file__) | ||
| test_dir = os.path.dirname(test_path) | ||
| with tempfile.NamedTemporaryFile(suffix=".c", dir=test_dir) as c_file: | ||
| c_file.write(program.encode("utf-8")) | ||
| c_file.flush() | ||
| compiler.compile_file(Path(c_file.name)) | ||
|
|
||
| return compiler | ||
|
|
||
| @unittest.skipUnless(has_wibo_and_mwcc(), "requires wibo and mwcc") | ||
| def test_dependencies_gcc_behavior(self): | ||
| compiler = self._compile( | ||
| ["-MD", "-gccdep"], | ||
| """ | ||
| int add(int a, int b) { | ||
| return a + b; | ||
| } | ||
| """, | ||
| ) | ||
|
|
||
| rule = compiler.make_rule | ||
|
|
||
| self.assertTrue( | ||
| str(rule.target).startswith("/tmp"), | ||
| f"target: {rule.target} should start with /tmp", | ||
| ) | ||
| self.assertTrue( | ||
| str(rule.target).endswith("result.o"), | ||
| f"target: {rule.target} should end with result.o", | ||
| ) | ||
| self.assertTrue(str(rule.source).endswith(".c")) | ||
| self.assertEqual(0, len(rule.includes)) | ||
|
|
||
| @unittest.skipUnless(has_wibo_and_mwcc(), "requires wibo and mwcc") | ||
| def test_no_dependencies_mw_behavior(self): | ||
| compiler = self._compile( | ||
| ["-MD"], | ||
| """int add(int a, int b) { | ||
| return a + b; | ||
| } | ||
| """, | ||
| ) | ||
|
|
||
| rule = compiler.make_rule | ||
|
|
||
| self.assertTrue( | ||
| str(rule.target).startswith("/tmp"), | ||
| f"target: {rule.target} should start with /tmp", | ||
| ) | ||
| self.assertTrue( | ||
| str(rule.target).endswith("result.o"), | ||
| f"target: {rule.target} should end with result.o", | ||
| ) | ||
| self.assertTrue(str(rule.source).endswith(".c")) | ||
| self.assertEqual(0, len(rule.includes)) | ||
|
|
||
| @unittest.skipUnless(has_wibo_and_mwcc(), "requires wibo and mwcc") | ||
| def test_no_depencies(self): | ||
| compiler = self._compile( | ||
| [], | ||
| """int add(int a, int b) { | ||
| return a + b; | ||
| } | ||
| """, | ||
| ) | ||
|
|
||
| rule = compiler.make_rule | ||
|
|
||
| self.assertIsNone(rule) | ||
|
|
||
| def test_make_rule_simple(self): | ||
| wibo_make_rule = "Z:\\tmp\\tmpfmuzt8mz\\result.o: test.c \r\n".encode("ascii") | ||
|
|
||
| rule = MakeRule(wibo_make_rule, True) | ||
|
|
||
| self.assertEqual(Path("/tmp/tmpfmuzt8mz/result.o"), rule.target) | ||
| self.assertEqual(Path("test.c"), rule.source) | ||
| self.assertEqual([], rule.includes) | ||
| self.assertEqual("/tmp/tmpfmuzt8mz/result.o: test.c \n", rule.as_str()) | ||
|
|
||
| def test_make_rule_with_includes(self): | ||
| wibo_make_rule = ( | ||
| "Z:\\tmp\\tmpfkcxmvnu\\result.o: test2.c \\\r\n" | ||
| "\tZ:\\home\\user\\Projects\\mwccgap\\decl.h \\\r\n" | ||
| "\t\\\\?\\Z:\\home\\user\\Projects\\mwccgap\\lib.h \r\n" | ||
| ).encode("ascii") | ||
|
|
||
| rule = MakeRule(wibo_make_rule, True) | ||
|
|
||
| expected_rule = ( | ||
| "/tmp/tmpfkcxmvnu/result.o: test2.c \\\n" | ||
| "\t/home/user/Projects/mwccgap/decl.h \\\n" | ||
| "\t/home/user/Projects/mwccgap/lib.h \n" | ||
| ) | ||
| self.assertEqual(expected_rule, rule.as_str()) | ||
|
|
||
| def test_unix_deps(self): | ||
| make_rule = ( | ||
| "/tmp/tmpfkcxmvnu/result.o: test.c \\\n" "\tdecl.h \\\n" "\tlib.h \n" | ||
| ).encode("utf-8") | ||
|
|
||
| rule = MakeRule(make_rule, False) | ||
|
|
||
| self.assertEqual("/tmp/tmpfkcxmvnu/result.o", rule.target) | ||
| self.assertEqual("test.c", rule.source) | ||
| self.assertEqual(["decl.h", "lib.h"], rule.includes) | ||
| self.assertEqual(make_rule.decode("utf-8"), rule.as_str()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it simpler to make this
b""rather thanNone?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few cases that I didn't add support for yet, but was planning on coming back to for completeness. The
-M, and-MMoptions as well as-make,-P,-Soptions don't emit object code and would not haveobj_bytes. Some of those need similar post-processing. It would be fair to saymwccgapshouldn't be used for those cases, but I was considering it as a generalizedmwccwrapper rather than only for cases where ASM rewriting is necessary.