Skip to content

Add ability to patch header files too #158

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

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions robotpy_build/maven.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def convert_maven_to_downloads(
# headers
dl_header["incdir"] = ""
dl_header["url"] = _get_artifact_url(mcfg, "headers")
dl_header["header_patches"] = mcfg.header_patches

# Construct downloads and return it
downloads = []
Expand Down
7 changes: 7 additions & 0 deletions robotpy_build/pyproject_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class MavenLibDownload(Model):
#: must be in unified diff format.
patches: Optional[List[PatchInfo]] = None

#: Patches to downloaded header files. Patches must be in unified diff format.
header_patches: Optional[List[PatchInfo]] = None


class Download(Model):
"""
Expand Down Expand Up @@ -147,6 +150,10 @@ class Download(Model):
#: must be in unified diff format.
patches: Optional[List[PatchInfo]] = None

#: Patches to downloaded header files in incdir. Patches must be in unified
#: diff format.
header_patches: Optional[List[PatchInfo]] = None

def _update_with_platform(self, platform):
for n in ("url", "incdir", "libdir"):
v = getattr(self, n, None)
Expand Down
30 changes: 18 additions & 12 deletions robotpy_build/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from .devcfg import get_dev_config
from .download import download_and_extract_zip
from .pyproject_configs import WrapperConfig, Download
from .pyproject_configs import PatchInfo, WrapperConfig, Download
from .generator_data import MissingReporter
from .hooks import Hooks
from .hooks_datacfg import HooksDataYaml
Expand Down Expand Up @@ -333,6 +333,19 @@ def on_build_dl(self, cache: str, srcdir: str):
self._write_libinit_py(libnames_full)
self._write_pkgcfg_py(pkgcfgpy, libnames_full)

def _apply_patches(self, patches: List[PatchInfo], root: str):
import patch

for p in patches:
patch_path = join(self.setup_root, normpath(p.patch))
ps = patch.PatchSet()
with open(patch_path, "rb") as fp:
if not ps.parse(fp):
raise ValueError(f"Error parsing patch '{patch_path}'")

if not ps.apply(strip=p.strip, root=root):
raise ValueError(f"Error applying patch '{patch_path}' to '{root}'")

def _clean_and_download(
self, downloads: List[Download], cache: str, srcdir: str
) -> List[str]:
Expand All @@ -358,17 +371,7 @@ def _clean_and_download(
sources = [join(srcdir, normpath(s)) for s in dl.sources]
self.extension.sources.extend(sources)
if dl.patches:
import patch

for p in dl.patches:
patch_path = join(self.setup_root, normpath(p.patch))
ps = patch.PatchSet()
with open(patch_path, "rb") as fp:
if not ps.parse(fp):
raise ValueError(f"Error parsing patch '{patch_path}'")

if not ps.apply(strip=p.strip, root=srcdir):
raise ValueError(f"Error applying patch '{patch_path}")
self._apply_patches(dl.patches, srcdir)
elif dl.sources is not None:
raise ValueError("sources must be None if use_sources is False!")
elif dl.patches is not None:
Expand Down Expand Up @@ -413,6 +416,9 @@ def _clean_and_download(

download_and_extract_zip(dl.url, to, cache)

if dl.header_patches:
self._apply_patches(dl.header_patches, incdir)

if add_incdir:
for f in glob.glob(join(glob.escape(incdir), "**"), recursive=True):
self._add_generated_file(f)
Expand Down
4 changes: 4 additions & 0 deletions tests/cpp/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ sources = [
"downloaded.cpp"
]

[[tool.robotpy-build.wrappers."rpytest.dl".maven_lib_download.header_patches]]
patch = "rpytest/dl/dl.patch"
strip = 0

[tool.robotpy-build.wrappers."rpytest.dl"]
name = "rpytest_dl"

Expand Down
4 changes: 2 additions & 2 deletions tests/cpp/rpytest/dl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . import _init_rpytest_dl

# autogenerated by 'robotpy-build create-imports rpytest.dl rpytest.dl._rpytest_dl'
from ._rpytest_dl import downloaded_fn
from ._rpytest_dl import downloaded_fn, extra_content

__all__ = ["downloaded_fn"]
__all__ = ["downloaded_fn", "extra_content"]
10 changes: 10 additions & 0 deletions tests/cpp/rpytest/dl/dl.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- downloaded.h.old 2022-02-23 23:09:51.053000000 -0500
+++ downloaded.h 2022-02-23 23:10:13.861277275 -0500
@@ -1,4 +1,6 @@

-#pragma once
+#pragma once
+
+inline bool extra_content() { return true; }

int downloaded_fn(int val);
4 changes: 4 additions & 0 deletions tests/test_downloaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@

def test_downloaded_fn():
assert rpytest.dl.downloaded_fn(3) == 0x42 + 3


def test_extra_content():
assert rpytest.dl.extra_content() == True