From 97b5f0767a41bb29bb4dc79668ae1229db63bae1 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Mon, 24 Jun 2019 06:30:10 -0700 Subject: [PATCH] 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 --- README.md | 5 +++-- sandboxed_api/tools/generator2/BUILD | 2 -- sandboxed_api/tools/generator2/code.py | 21 ++++++++++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ea93935..45ef2b2 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/sandboxed_api/tools/generator2/BUILD b/sandboxed_api/tools/generator2/BUILD index 26bdb05..dde3466 100644 --- a/sandboxed_api/tools/generator2/BUILD +++ b/sandboxed_api/tools/generator2/BUILD @@ -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", @@ -40,7 +39,6 @@ py_test( py_binary( name = "sapi_generator", srcs = ["sapi_generator.py"], - python_version = "PY2", visibility = ["//visibility:public"], deps = [ ":code", diff --git a/sandboxed_api/tools/generator2/code.py b/sandboxed_api/tools/generator2/code.py index 47f9692..726a5d9 100644 --- a/sandboxed_api/tools/generator2/code.py +++ b/sandboxed_api/tools/generator2/code.py @@ -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 @@ -32,6 +33,23 @@ _PARSE_OPTIONS = (cindex.TranslationUnit.PARSE_SKIP_FUNCTION_BODIES | 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.""" @@ -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] @@ -663,9 +682,9 @@ class Generator(object): 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):