Skip to content

Commit 4a1b95f

Browse files
committed
Patch dds' memory detection.
1 parent 2ff1ea9 commit 4a1b95f

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

README.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ Redeal runs under Python 3.6 or higher. See the ``examples/`` directory for
1919
some example simulations.
2020

2121
A double-dummy solver function is also available through Bo Haglund's DDS
22-
2.9.0, which is distributed with Redeal as a git submodule. Note that on
23-
Linux, DDS requires the ``procps-ng`` package. You can also download the
24-
compiled shared objects from `Bo Haglund's website`__. For Windows, the DDS
25-
DLLs are distributed together with Redeal. In any case, if you cannot compile
26-
the DDS library, Redeal will work fine but the ``dd_tricks``, ``dd_score`` and
27-
``dd_all_tricks`` methods will be unavailable.
22+
2.9.0 (slightly patched at build time), which is distributed with Redeal as
23+
a git submodule. Note that this requires the ``libgomp`` package. You can
24+
also download the compiled shared objects from `Bo Haglund's website`__.
25+
For Windows, the DDS DLLs are distributed together with Redeal. In any
26+
case, if you cannot compile the DDS library, Redeal will work fine but the
27+
``dd_tricks``, ``dd_score`` and ``dd_all_tricks`` methods will be unavailable.
2828

2929
__ http://privat.bahnhof.se/wb758135/bridge/dll.html
3030

setup.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
" https://pypi.python.org/pypi/setuptools")
66

77
import contextlib
8+
from contextlib import ExitStack
89
import os
910
from pathlib import Path
1011
import shutil
@@ -24,15 +25,31 @@
2425

2526

2627
@contextlib.contextmanager
27-
def patched_path(path, *pairs):
28-
orig_contents = contents = path.read_text()
29-
for old, new in pairs:
30-
contents = contents.replace(old, new)
28+
def patched_path(path, old, new):
29+
contents = path.read_text()
30+
if old not in contents:
31+
raise Exception(f"Invalid patch: {old}")
3132
try:
32-
path.write_text(contents)
33+
path.write_text(contents.replace(old, new))
3334
yield
3435
finally:
35-
path.write_text(orig_contents)
36+
path.write_text(contents)
37+
38+
39+
patches = [
40+
("Makefiles/Makefile_linux_shared",
41+
"$(COMPILE_FLAGS)", "$(COMPILE_FLAGS) $(CFLAGS)"),
42+
("System.cpp", # redeal issue 19.
43+
"free -k | tail -n+3 | head -n1 | awk '{print $NF}'",
44+
r"grep -Po 'MemAvailable:\\s*\\K[0-9]*' /proc/meminfo || "
45+
r"grep -Po 'MemFree:\\s*\\K[0-9]*' /proc/meminfo"),
46+
("dds.cpp", # dds issue #91.
47+
"FreeMemory();", ""),
48+
("Makefiles/Makefile_Mac_clang_shared",
49+
"$(COMPILE_FLAGS)", "$(COMPILE_FLAGS) $(CFLAGS)"),
50+
("Makefiles/Makefile_Mac_clang_shared",
51+
"$(LINK_FLAGS)", "$(LINK_FLAGS) -lc++"),
52+
]
3653

3754

3855
class build_ext(build_ext):
@@ -54,20 +71,14 @@ def build_extensions(self):
5471
git submodule init && git submodule update
5572
5673
On a Unix system, do not use the zip archives from github.""")
57-
if sys.platform.startswith("linux"):
58-
# Patch dds issue #91.
59-
with patched_path(
60-
dds_src / "Makefiles/Makefile_linux_shared",
61-
("$(COMPILE_FLAGS)", "$(COMPILE_FLAGS) $(CFLAGS)")), \
62-
patched_path(dds_src / "dds.cpp", ("FreeMemory();", "")):
74+
with ExitStack() as stack:
75+
for name, old, new in patches:
76+
stack.enter_context(patched_path(dds_src / name, old, new))
77+
if sys.platform.startswith("linux"):
6378
subprocess.check_call(
6479
["make", "-f", "Makefiles/Makefile_linux_shared",
6580
"THREADING=", "THREAD_LINK="], cwd=dds_src)
66-
elif sys.platform == "darwin":
67-
with patched_path(
68-
dds_src / "Makefiles/Makefile_Mac_clang_shared",
69-
("$(COMPILE_FLAGS)", "$(COMPILE_FLAGS) $(CFLAGS)"),
70-
("$(LINK_FLAGS)", "$(LINK_FLAGS) -lc++")):
81+
elif sys.platform == "darwin":
7182
subprocess.check_call(
7283
["make", "-f", "Makefiles/Makefile_Mac_clang_shared",
7384
"CC=gcc", "THREADING=", "THREAD_LINK="], cwd=dds_src)

0 commit comments

Comments
 (0)