Find libclang so that Python3 works, remove PY2 annotations

We now require that Debian users install the `python3` and `python3-pip`
packages. This change lets the Python code search for `libclang.so`, which can
be located in different directories, depending on version, and is not found by
default otherwise.

Fixes #28

PiperOrigin-RevId: 254745872
Change-Id: Ia77680da2a3235c0a9518125676aa8a460e38e76
This commit is contained in:
Christian Blichmann 2019-06-24 06:30:10 -07:00 committed by Copybara-Service
parent 4bcea59309
commit 97b5f0767a
3 changed files with 23 additions and 5 deletions

View File

@ -68,8 +68,9 @@ echo "deb http://storage.googleapis.com/bazel-apt stable jdk1.8" | \
sudo tee /etc/apt/sources.list.d/bazel.list sudo tee /etc/apt/sources.list.d/bazel.list
wget -qO - https://bazel.build/bazel-release.pub.gpg | sudo apt-key add - wget -qO - https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update sudo apt-get update
sudo apt-get install -qy python-typing python-clang-7 libclang-7-dev sudo apt-get install -qy build-essential linux-libc-dev bazel python3 \
sudo apt-get install -qy build-essential linux-libc-dev bazel python3-pip libclang-7-dev
pip3 install clang
``` ```
Clone and run the build: Clone and run the build:

View File

@ -29,7 +29,6 @@ py_test(
"code_test.py", "code_test.py",
"code_test_util.py", "code_test_util.py",
], ],
python_version = "PY2",
deps = [ deps = [
":code", ":code",
"@com_google_absl_py//absl/testing:absltest", "@com_google_absl_py//absl/testing:absltest",
@ -40,7 +39,6 @@ py_test(
py_binary( py_binary(
name = "sapi_generator", name = "sapi_generator",
srcs = ["sapi_generator.py"], srcs = ["sapi_generator.py"],
python_version = "PY2",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
":code", ":code",

View File

@ -17,6 +17,7 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from ctypes import util
import itertools import itertools
import os import os
from clang import cindex from clang import cindex
@ -32,6 +33,23 @@ _PARSE_OPTIONS = (cindex.TranslationUnit.PARSE_SKIP_FUNCTION_BODIES |
cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD) cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)
def _init_libclang():
"""Finds and initializes the libclang library."""
if cindex.Config.loaded:
return
# Try to find libclang in the standard location and a few versioned paths
# that are used on Debian (and others). If LD_LIBRARY_PATH is set, it is
# used as well.
for lib in [
'clang', 'clang-9', 'clang-8', 'clang-7', 'clang-6.0', 'clang-5.0',
'clang-4.0'
]:
libclang = util.find_library(lib)
if libclang:
cindex.Config.set_library_file(libclang)
break
def get_header_guard(path): def get_header_guard(path):
# type: (Text) -> Text # type: (Text) -> Text
"""Generates header guard string from path.""" """Generates header guard string from path."""
@ -610,6 +628,7 @@ class Analyzer(object):
@staticmethod @staticmethod
def process_files(input_paths, compile_flags): def process_files(input_paths, compile_flags):
# type: (Text, List[Text]) -> List[_TranslationUnit] # type: (Text, List[Text]) -> List[_TranslationUnit]
_init_libclang()
return [Analyzer._analyze_file_for_tu(path, compile_flags=compile_flags) return [Analyzer._analyze_file_for_tu(path, compile_flags=compile_flags)
for path in input_paths] for path in input_paths]
@ -663,9 +682,9 @@ class Generator(object):
facultative. If not given, then one is computed for each element of facultative. If not given, then one is computed for each element of
input_paths input_paths
""" """
self.translation_units = translation_units self.translation_units = translation_units
self.functions = None self.functions = None
_init_libclang()
def generate(self, name, function_names, namespace=None, output_file=None, def generate(self, name, function_names, namespace=None, output_file=None,
embed_dir=None, embed_name=None): embed_dir=None, embed_name=None):