Skip to content

Commit

Permalink
Switched from clang to libclang following pybind#32
Browse files Browse the repository at this point in the history
  • Loading branch information
jeguzzi committed Sep 23, 2024
1 parent cb0cef0 commit 4626c7f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
84 changes: 76 additions & 8 deletions pybind11_mkdoc/mkdoc_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ def read_args(args):
sdk_dir = dev_path + 'Platforms/MacOSX.platform/Developer/SDKs'
libclang = lib_dir + 'libclang.dylib'

if os.path.exists(libclang):
# if os.path.exists(libclang):
if cindex.Config.library_path is None and os.path.exists(libclang):
cindex.Config.set_library_path(os.path.dirname(libclang))

if os.path.exists(sdk_dir):
Expand All @@ -297,7 +298,8 @@ def read_args(args):
else:
raise FileNotFoundError("Failed to find libclang.dll! "
"Set the LIBCLANG_PATH environment variable to provide a path to it.")
else:
# else:
elif cindex.Config.library_path is None:
library_file = ctypes.util.find_library('libclang.dll')
if library_file is not None:
cindex.Config.set_library_file(library_file)
Expand All @@ -318,7 +320,8 @@ def folder_version(d):
# Ability to override LLVM/libclang paths
if 'LLVM_DIR_PATH' in os.environ:
llvm_dir = os.environ['LLVM_DIR_PATH']
elif llvm_dir is None:
elif llvm_dir is None and cindex.Config.library_path is None:
# elif llvm_dir is None:
raise FileNotFoundError(
"Failed to find a LLVM installation providing the file "
"/usr/lib{32,64}/llvm-{VER}/lib/libclang.so.1. Make sure that "
Expand All @@ -331,11 +334,16 @@ def folder_version(d):
"variables.")

if 'LIBCLANG_PATH' in os.environ:
libclang_dir = os.environ['LIBCLANG_PATH']
else:
libclang_dir = os.path.join(llvm_dir, 'lib', 'libclang.so.1')
# libclang_dir = os.environ['LIBCLANG_PATH']
# else:
# libclang_dir = os.path.join(llvm_dir, 'lib', 'libclang.so.1')
cindex.Config.set_library_file(os.environ['LIBCLANG_PATH'])
elif cindex.Config.library_path is None:
cindex.Config.set_library_file(os.path.join(llvm_dir, 'lib',
'libclang.so.1'))


cindex.Config.set_library_file(libclang_dir)
# cindex.Config.set_library_file(libclang_dir)
cpp_dirs = [ ]

if '-stdlib=libc++' not in args:
Expand All @@ -347,11 +355,17 @@ def folder_version(d):
glob('/usr/include/%s-linux-gnu/c++/*' % platform.machine()
), default=None, key=folder_version))
else:
if llvm_dir is None:
raise FileNotFoundError(
"-stdlib=libc++ has been specified, but no LLVM "
"installation have been found on the system.")

cpp_dirs.append(os.path.join(llvm_dir, 'include', 'c++', 'v1'))

if 'CLANG_INCLUDE_DIR' in os.environ:
cpp_dirs.append(os.environ['CLANG_INCLUDE_DIR'])
else:
# else:
elif llvm_dir is not None:
cpp_dirs.append(max(
glob(os.path.join(llvm_dir, 'lib', 'clang', '*', 'include')
), default=None, key=folder_version))
Expand Down Expand Up @@ -441,6 +455,60 @@ def write_header(comments, out_file=sys.stdout):
''', file=out_file)


def write_header2(comments, out_file=sys.stdout):
print('''/*
This file contains docstrings for use in the Python bindings.
Do not edit! They were automatically extracted by pybind11_mkdoc.
*/
#define stringify_literal( x ) # x
#define QUOTE( x ) stringify_literal( x )
#define __EXPAND(x) x
#define __COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
#define __VA_SIZE(...) __EXPAND(__COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
#define __CAT1(a, b) a ## b
#define __CAT2(a, b) __CAT1(a, b)
#define __DOC1(n1) __doc_##n1
#define __DOC2(n1, n2) __doc_##n1##_##n2
#define __DOC3(n1, n2, n3) __doc_##n1##_##n2##_##n3
#define __DOC4(n1, n2, n3, n4) __doc_##n1##_##n2##_##n3##_##n4
#define __DOC5(n1, n2, n3, n4, n5) __doc_##n1##_##n2##_##n3##_##n4##_##n5
#define __DOC6(n1, n2, n3, n4, n5, n6) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6
#define __DOC7(n1, n2, n3, n4, n5, n6, n7) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
#define DOC(...) get_docs(QUOTE(__EXPAND(__EXPAND(__CAT2(__DOC, __VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))))
#if defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#include <map>
#include <string>
const char * get_docs(const std::string & key) {
static std::map<const std::string, const char *> docs{
''', file=out_file)

name_ctr = 1
name_prev = None
for name, _, comment in list(sorted(comments, key=lambda x: (x[0], x[1]))):
if name == name_prev:
name_ctr += 1
name = name + "_%i" % name_ctr
else:
name_prev = name
name_ctr = 1
# print('\n{"%s", R"doc(%s)doc"},' % (name, comment), file=out_file)
print('''
};
return docs[key];
}
#if defined(__GNUG__)
#pragma GCC diagnostic pop
#endif
''', file=out_file)


def mkdoc(args, width, output=None):
if width != None:
global docstring_width
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ classifiers = [
"Operating System :: POSIX",
"Operating System :: MacOS"
]
requires = ["clang"]
requires = [
# "clang"
"libclang"
]
requires-python = ">=3.6"

[tool.flit.scripts]
Expand Down

0 comments on commit 4626c7f

Please sign in to comment.