Files
cibuildwheel/test/test_cpp_standards.py
T

132 lines
3.5 KiB
Python
Raw Normal View History

2020-05-02 16:54:19 +01:00
import os
2020-05-02 22:50:52 +01:00
import jinja2
2020-05-02 16:54:19 +01:00
import pytest
from . import utils
from .test_projects import TestProject
2020-05-02 16:54:19 +01:00
cpp_test_project = TestProject()
2020-05-03 10:30:44 +01:00
2021-05-03 11:45:43 -04:00
setup_py_template = r"""
2020-05-03 10:30:44 +01:00
from setuptools import Extension, setup
setup(
name="spam",
ext_modules=[Extension('spam', sources=['spam.cpp'], language="c++", extra_compile_args={{ extra_compile_args }})],
version="0.1.0",
2020-05-02 16:54:19 +01:00
)
2021-05-03 11:45:43 -04:00
"""
2020-05-02 22:50:52 +01:00
2021-05-03 11:45:43 -04:00
spam_cpp_template = r"""
2020-05-02 16:54:19 +01:00
#include <Python.h>
2020-05-02 22:42:35 +01:00
{{ spam_cpp_top_level_add }}
2020-05-02 16:54:19 +01:00
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;
if (!PyArg_ParseTuple(args, "s", &command))
return NULL;
sts = system(command);
return PyLong_FromLong(sts);
}
/* Module initialization */
static PyMethodDef module_methods[] = {
{"system", (PyCFunction)spam_system, METH_VARARGS,
"Execute a shell command."},
{NULL} /* Sentinel */
};
2021-02-14 20:44:20 +01:00
PyMODINIT_FUNC PyInit_spam(void)
2020-05-02 16:54:19 +01:00
{
2021-02-14 20:44:20 +01:00
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT, "spam", "Example module", -1, module_methods,
};
return PyModule_Create(&moduledef);
2020-05-02 16:54:19 +01:00
}
2021-05-03 11:45:43 -04:00
"""
2020-05-02 16:54:19 +01:00
2021-05-03 11:45:43 -04:00
cpp_test_project.files["setup.py"] = jinja2.Template(setup_py_template)
cpp_test_project.files["spam.cpp"] = jinja2.Template(spam_cpp_template)
2020-05-02 16:54:19 +01:00
cpp11_project = cpp_test_project.copy()
2021-05-03 11:45:43 -04:00
cpp11_project.template_context["extra_compile_args"] = (
["/std:c++11"] if utils.platform == "windows" else ["-std=c++11"]
2020-05-15 12:19:38 +01:00
)
2021-05-03 11:45:43 -04:00
cpp11_project.template_context["spam_cpp_top_level_add"] = "#include <array>"
2020-05-15 12:19:38 +01:00
2020-05-03 14:08:57 +01:00
def test_cpp11(tmp_path):
2020-05-02 16:54:19 +01:00
# This test checks that the C++11 standard is supported
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
2020-05-02 16:54:19 +01:00
2020-05-15 12:19:38 +01:00
cpp11_project.generate(project_dir)
2020-05-02 16:54:19 +01:00
2021-02-14 20:44:20 +01:00
actual_wheels = utils.cibuildwheel_run(project_dir)
expected_wheels = [w for w in utils.expected_wheels("spam", "0.1.0")]
2020-05-02 16:54:19 +01:00
assert set(actual_wheels) == set(expected_wheels)
cpp14_project = cpp_test_project.copy()
2021-05-03 11:45:43 -04:00
cpp14_project.template_context["extra_compile_args"] = (
["/std:c++14"] if utils.platform == "windows" else ["-std=c++14"]
2020-05-15 12:19:38 +01:00
)
2021-05-03 11:45:43 -04:00
cpp14_project.template_context["spam_cpp_top_level_add"] = "int a = 100'000;"
2020-05-15 12:19:38 +01:00
2020-05-03 14:08:57 +01:00
def test_cpp14(tmp_path):
2020-05-02 16:54:19 +01:00
# This test checks that the C++14 standard is supported
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
2020-05-02 16:54:19 +01:00
2020-05-15 12:19:38 +01:00
cpp14_project.generate(project_dir)
2020-05-02 16:54:19 +01:00
2021-02-14 20:44:20 +01:00
actual_wheels = utils.cibuildwheel_run(project_dir)
expected_wheels = [w for w in utils.expected_wheels("spam", "0.1.0")]
2020-05-02 16:54:19 +01:00
assert set(actual_wheels) == set(expected_wheels)
cpp17_project = cpp_test_project.copy()
2021-02-14 20:44:20 +01:00
cpp17_project.template_context["extra_compile_args"] = [
"/std:c++17" if utils.platform == "windows" else "-std=c++17"
]
2021-04-30 17:56:34 -04:00
cpp17_project.template_context[
2021-05-03 11:45:43 -04:00
"spam_cpp_top_level_add"
] = r"""
2020-05-05 21:56:34 +01:00
#include <utility>
auto a = std::pair(5.0, false);
2021-05-03 11:45:43 -04:00
"""
2020-05-05 21:56:34 +01:00
2020-05-03 14:08:57 +01:00
def test_cpp17(tmp_path):
2020-05-02 16:54:19 +01:00
# This test checks that the C++17 standard is supported
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
2020-05-02 16:54:19 +01:00
2020-05-05 21:56:34 +01:00
cpp17_project.generate(project_dir)
2020-05-02 16:54:19 +01:00
2021-05-03 11:45:43 -04:00
if os.environ.get("APPVEYOR_BUILD_WORKER_IMAGE", "") == "Visual Studio 2015":
pytest.skip("Visual Studio 2015 does not support C++17")
2020-05-02 16:54:19 +01:00
# Pypy's distutils sets the default compiler to 'msvc9compiler', which
# is too old to support cpp17.
2021-02-14 20:44:20 +01:00
add_env = {"CIBW_SKIP": "pp??-*"}
2020-05-02 16:54:19 +01:00
2021-05-03 11:45:43 -04:00
if utils.platform == "macos":
add_env["MACOSX_DEPLOYMENT_TARGET"] = "10.13"
2020-05-02 16:54:19 +01:00
actual_wheels = utils.cibuildwheel_run(project_dir, add_env=add_env)
2021-04-30 17:56:34 -04:00
expected_wheels = [
w
2021-05-03 11:45:43 -04:00
for w in utils.expected_wheels("spam", "0.1.0", macosx_deployment_target="10.13")
2021-02-14 20:44:20 +01:00
if "-pp" not in w
2021-04-30 17:56:34 -04:00
]
2020-05-02 16:54:19 +01:00
assert set(actual_wheels) == set(expected_wheels)