{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Headers processing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filtering header content\n", "\n", "A C/C++ header can contains different zone, some of which are parts of the public API, and some of which may correspond to specific low-level options.\n", "\n", "litgen (and srcmlcpp) can filter a header based on preprocessor `#ifdef / #ifndef` occurrences.\n", "\n", "Let's look at an example header: its code is defined in the `cpp_code` variable below." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "cpp_code = \"\"\"\n", "#ifndef MY_HEADER_H // This is an inclusion guard, it should not be filtered out\n", "\n", "void Foo() {}\n", "\n", "#ifdef ARCANE_OPTION\n", " // We are entering a zone that handle arcane options that should not be included in the bindings\n", " void Foo2() {}\n", "#else\n", " // this should also not be included in the bindings\n", " void Foo3() {}\n", "#endif\n", "\n", "#ifdef COMMON_OPTION\n", " // We are entering a zone for which we would like to publish bindings\n", " void Foo4();\n", "#endif\n", "\n", "#endif // #ifndef MY_HEADER_H\n", "\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's try to generate bindings for it:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
#ifndef MY_HEADER_H // This is an inclusion guard, it should not be filtered out\n",
"\n",
"void Foo() {}\n",
"\n",
"#ifdef ARCANE_OPTION\n",
" // We are entering a zone that handle arcane options that should not be included in the bindings\n",
" void Foo2() {}\n",
"#else\n",
" // this should also not be included in the bindings\n",
" void Foo3() {}\n",
"#endif\n",
"\n",
"#ifdef COMMON_OPTION\n",
" // We are entering a zone for which we would like to publish bindings\n",
" void Foo4();\n",
"#endif\n",
"\n",
"#endif // #ifndef MY_HEADER_H\n",
"
# #ifndef MY_HEADER_H \n",
"\n",
"def foo() -> None:\n",
" pass\n",
"\n",
"\n",
"\n",
"# #endif \n",
"
// #ifndef MY_HEADER_H \n",
"\n",
"m.def("foo",\n",
" Foo);\n",
"// #endif \n",
"
#ifndef MY_HEADER_H // This is an inclusion guard, it should not be filtered out\n",
"\n",
"void Foo() {}\n",
"\n",
"#ifdef ARCANE_OPTION\n",
" // We are entering a zone that handle arcane options that should not be included in the bindings\n",
" void Foo2() {}\n",
"#else\n",
" // this should also not be included in the bindings\n",
" void Foo3() {}\n",
"#endif\n",
"\n",
"#ifdef COMMON_OPTION\n",
" // We are entering a zone for which we would like to publish bindings\n",
" void Foo4();\n",
"#endif\n",
"\n",
"#endif // #ifndef MY_HEADER_H\n",
"
# #ifndef MY_HEADER_H \n",
"\n",
"def foo() -> None:\n",
" pass\n",
"\n",
"\n",
"# #ifdef COMMON_OPTION\n",
"# \n",
"def foo4() -> None:\n",
" """ We are entering a zone for which we would like to publish bindings"""\n",
" pass\n",
"# #endif\n",
"# \n",
"\n",
"# #endif \n",
"
// #ifndef MY_HEADER_H \n",
"\n",
"m.def("foo",\n",
" Foo);\n",
"// #ifdef COMMON_OPTION\n",
"// \n",
"\n",
"m.def("foo4",\n",
" Foo4, "We are entering a zone for which we would like to publish bindings");\n",
"// #endif\n",
"// \n",
"// #endif \n",
"
// This function has an API marker and is exported in a shared library\n",
"MY_API int add(int, int b);\n",
"\n",
"// This function does not have an API marker, and is probably private\n",
"int mul(int a, int b);\n",
"
def add(param_0: int, b: int) -> int:\n",
" """ This function has an API marker and is exported in a shared library"""\n",
" pass\n",
"
m.def("add",\n",
" add, \n",
" py::arg("param_0"), py::arg("b"), \n",
" "This function has an API marker and is exported in a shared library");\n",
"
// This function has an API marker and is exported in a shared library\n",
"MY_API int add(int, int b);\n",
"\n",
"// This function does not have an API marker, and is probably private\n",
"int mul(int a, int b);\n",
"
def add(param_0: int, b: int) -> int:\n",
" """ This function has an API marker and is exported in a shared library"""\n",
" pass\n",
"\n",
"def mul(a: int, b: int) -> int:\n",
" """ This function does not have an API marker, and is probably private\n",
" Private API!\n",
" """\n",
" pass\n",
"
m.def("add",\n",
" add, \n",
" py::arg("param_0"), py::arg("b"), \n",
" "This function has an API marker and is exported in a shared library");\n",
"\n",
"m.def("mul",\n",
" mul, \n",
" py::arg("a"), py::arg("b"), \n",
" " This function does not have an API marker, and is probably private\\nPrivate API!");\n",
"
def add(param_0: Int32, b: Int32) -> Int32:\n",
" pass\n",
"