2020-09-16 21:13:41 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2021-08-13 16:37:05 +00:00
|
|
|
import sys
|
2020-09-16 21:13:41 +00:00
|
|
|
from textwrap import dedent
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
DIR = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
|
2021-11-08 21:27:32 +00:00
|
|
|
WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin")
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
|
2020-10-02 14:03:35 +00:00
|
|
|
@pytest.mark.parametrize("parallel", [False, True])
|
2020-09-16 21:13:41 +00:00
|
|
|
@pytest.mark.parametrize("std", [11, 0])
|
2020-10-02 14:03:35 +00:00
|
|
|
def test_simple_setup_py(monkeypatch, tmpdir, parallel, std):
|
2020-09-16 21:13:41 +00:00
|
|
|
monkeypatch.chdir(tmpdir)
|
|
|
|
monkeypatch.syspath_prepend(MAIN_DIR)
|
|
|
|
|
|
|
|
(tmpdir / "setup.py").write_text(
|
|
|
|
dedent(
|
2022-02-12 00:06:16 +00:00
|
|
|
f"""\
|
2020-09-16 21:13:41 +00:00
|
|
|
import sys
|
|
|
|
sys.path.append({MAIN_DIR!r})
|
|
|
|
|
|
|
|
from setuptools import setup, Extension
|
|
|
|
from pybind11.setup_helpers import build_ext, Pybind11Extension
|
|
|
|
|
|
|
|
std = {std}
|
|
|
|
|
|
|
|
ext_modules = [
|
|
|
|
Pybind11Extension(
|
|
|
|
"simple_setup",
|
|
|
|
sorted(["main.cpp"]),
|
|
|
|
cxx_std=std,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
cmdclass = dict()
|
|
|
|
if std == 0:
|
|
|
|
cmdclass["build_ext"] = build_ext
|
|
|
|
|
|
|
|
|
2020-10-02 14:03:35 +00:00
|
|
|
parallel = {parallel}
|
|
|
|
if parallel:
|
|
|
|
from pybind11.setup_helpers import ParallelCompile
|
|
|
|
ParallelCompile().install()
|
|
|
|
|
2020-09-16 21:13:41 +00:00
|
|
|
setup(
|
|
|
|
name="simple_setup_package",
|
|
|
|
cmdclass=cmdclass,
|
|
|
|
ext_modules=ext_modules,
|
|
|
|
)
|
|
|
|
"""
|
2022-02-12 00:06:16 +00:00
|
|
|
),
|
2020-09-16 21:13:41 +00:00
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
|
|
|
|
(tmpdir / "main.cpp").write_text(
|
|
|
|
dedent(
|
2022-02-11 02:28:08 +00:00
|
|
|
"""\
|
2020-09-16 21:13:41 +00:00
|
|
|
#include <pybind11/pybind11.h>
|
|
|
|
|
|
|
|
int f(int x) {
|
|
|
|
return x * 3;
|
|
|
|
}
|
|
|
|
PYBIND11_MODULE(simple_setup, m) {
|
|
|
|
m.def("f", &f);
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
|
2021-11-08 21:27:32 +00:00
|
|
|
out = subprocess.check_output(
|
2020-09-16 21:13:41 +00:00
|
|
|
[sys.executable, "setup.py", "build_ext", "--inplace"],
|
|
|
|
)
|
2021-11-08 21:27:32 +00:00
|
|
|
if not WIN:
|
|
|
|
assert b"-g0" in out
|
|
|
|
out = subprocess.check_output(
|
|
|
|
[sys.executable, "setup.py", "build_ext", "--inplace", "--force"],
|
|
|
|
env=dict(os.environ, CFLAGS="-g"),
|
|
|
|
)
|
|
|
|
if not WIN:
|
|
|
|
assert b"-g0" not in out
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
# Debug helper printout, normally hidden
|
2021-11-08 21:27:32 +00:00
|
|
|
print(out)
|
2020-09-16 21:13:41 +00:00
|
|
|
for item in tmpdir.listdir():
|
|
|
|
print(item.basename)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
len([f for f in tmpdir.listdir() if f.basename.startswith("simple_setup")]) == 1
|
|
|
|
)
|
|
|
|
assert len(list(tmpdir.listdir())) == 4 # two files + output + build_dir
|
|
|
|
|
|
|
|
(tmpdir / "test.py").write_text(
|
|
|
|
dedent(
|
2022-02-11 02:28:08 +00:00
|
|
|
"""\
|
2020-09-16 21:13:41 +00:00
|
|
|
import simple_setup
|
|
|
|
assert simple_setup.f(3) == 9
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
|
|
|
|
subprocess.check_call(
|
|
|
|
[sys.executable, "test.py"], stdout=sys.stdout, stderr=sys.stderr
|
|
|
|
)
|
2021-07-13 21:21:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_intree_extensions(monkeypatch, tmpdir):
|
|
|
|
monkeypatch.syspath_prepend(MAIN_DIR)
|
|
|
|
|
|
|
|
from pybind11.setup_helpers import intree_extensions
|
|
|
|
|
|
|
|
monkeypatch.chdir(tmpdir)
|
|
|
|
root = tmpdir
|
|
|
|
root.ensure_dir()
|
|
|
|
subdir = root / "dir"
|
|
|
|
subdir.ensure_dir()
|
|
|
|
src = subdir / "ext.cpp"
|
|
|
|
src.ensure()
|
2022-02-11 02:28:08 +00:00
|
|
|
relpath = src.relto(tmpdir)
|
|
|
|
(ext,) = intree_extensions([relpath])
|
2021-07-13 21:21:55 +00:00
|
|
|
assert ext.name == "ext"
|
|
|
|
subdir.ensure("__init__.py")
|
2022-02-11 02:28:08 +00:00
|
|
|
(ext,) = intree_extensions([relpath])
|
2021-07-13 21:21:55 +00:00
|
|
|
assert ext.name == "dir.ext"
|
|
|
|
|
|
|
|
|
|
|
|
def test_intree_extensions_package_dir(monkeypatch, tmpdir):
|
|
|
|
monkeypatch.syspath_prepend(MAIN_DIR)
|
|
|
|
|
|
|
|
from pybind11.setup_helpers import intree_extensions
|
|
|
|
|
|
|
|
monkeypatch.chdir(tmpdir)
|
|
|
|
root = tmpdir / "src"
|
|
|
|
root.ensure_dir()
|
|
|
|
subdir = root / "dir"
|
|
|
|
subdir.ensure_dir()
|
|
|
|
src = subdir / "ext.cpp"
|
|
|
|
src.ensure()
|
|
|
|
(ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
|
|
|
|
assert ext.name == "dir.ext"
|
|
|
|
(ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
|
|
|
|
assert ext.name == "foo.dir.ext"
|
|
|
|
subdir.ensure("__init__.py")
|
|
|
|
(ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
|
|
|
|
assert ext.name == "dir.ext"
|
|
|
|
(ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
|
|
|
|
assert ext.name == "foo.dir.ext"
|