Skip to content

Commit

Permalink
Find libclang so that Python3 works, remove PY2 annotations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cblichmann authored and copybara-github committed Jun 24, 2019
1 parent 4bcea59 commit 97b5f07
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ echo "deb http://storage.googleapis.com/bazel-apt stable jdk1.8" | \
sudo tee /etc/apt/sources.list.d/bazel.list
wget -qO - https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
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
sudo apt-get install -qy build-essential linux-libc-dev bazel python3 \
python3-pip libclang-7-dev
pip3 install clang
```

Clone and run the build:
Expand Down
2 changes: 0 additions & 2 deletions sandboxed_api/tools/generator2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ py_test(
"code_test.py",
"code_test_util.py",
],
python_version = "PY2",
deps = [
":code",
"@com_google_absl_py//absl/testing:absltest",
Expand All @@ -40,7 +39,6 @@ py_test(
py_binary(
name = "sapi_generator",
srcs = ["sapi_generator.py"],
python_version = "PY2",
visibility = ["//visibility:public"],
deps = [
":code",
Expand Down
21 changes: 20 additions & 1 deletion sandboxed_api/tools/generator2/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ctypes import util
import itertools
import os
from clang import cindex
Expand All @@ -32,6 +33,23 @@
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):
# type: (Text) -> Text
"""Generates header guard string from path."""
Expand Down Expand Up @@ -610,6 +628,7 @@ class Analyzer(object):
@staticmethod
def process_files(input_paths, compile_flags):
# type: (Text, List[Text]) -> List[_TranslationUnit]
_init_libclang()
return [Analyzer._analyze_file_for_tu(path, compile_flags=compile_flags)
for path in input_paths]

Expand Down Expand Up @@ -663,9 +682,9 @@ def __init__(self, translation_units):
facultative. If not given, then one is computed for each element of
input_paths
"""

self.translation_units = translation_units
self.functions = None
_init_libclang()

def generate(self, name, function_names, namespace=None, output_file=None,
embed_dir=None, embed_name=None):
Expand Down

0 comments on commit 97b5f07

Please sign in to comment.