summaryrefslogtreecommitdiffstats
path: root/third_party/cython
diff options
context:
space:
mode:
authorqsr <qsr@chromium.org>2014-08-25 10:00:45 -0700
committerCommit bot <commit-bot@chromium.org>2014-08-25 17:01:40 +0000
commit28e16123231497d889ef08ebd917e80d5008ffbf (patch)
tree8e7a961631240f7792423918d84730926d77f4d2 /third_party/cython
parent0aff6f3f6d49226f59a6b5adce44b1cd0b30ffbd (diff)
downloadchromium_src-28e16123231497d889ef08ebd917e80d5008ffbf.zip
chromium_src-28e16123231497d889ef08ebd917e80d5008ffbf.tar.gz
chromium_src-28e16123231497d889ef08ebd917e80d5008ffbf.tar.bz2
Core mojo API for python.
Review URL: https://codereview.chromium.org/377293002 Cr-Commit-Position: refs/heads/master@{#291701}
Diffstat (limited to 'third_party/cython')
-rw-r--r--third_party/cython/cp_python_binary_modules.py42
-rw-r--r--third_party/cython/cython_compiler.gypi71
-rw-r--r--third_party/cython/python_export.h13
-rw-r--r--third_party/cython/python_flags.py53
-rw-r--r--third_party/cython/python_module.gypi51
-rw-r--r--third_party/cython/rules.gni84
6 files changed, 314 insertions, 0 deletions
diff --git a/third_party/cython/cp_python_binary_modules.py b/third_party/cython/cp_python_binary_modules.py
new file mode 100644
index 0000000..dd38e64
--- /dev/null
+++ b/third_party/cython/cp_python_binary_modules.py
@@ -0,0 +1,42 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import argparse
+import os
+import shutil
+import sys
+
+def touch(fname):
+ if os.path.exists(fname):
+ os.utime(fname, None)
+ else:
+ open(fname, 'a').close()
+
+def main():
+ """Command line utility to copy python binary modules to the correct package
+ hierarchy.
+ """
+ parser = argparse.ArgumentParser(
+ description='Copy python binary modules to the correct package '
+ 'hierarchy.')
+ parser.add_argument('timestamp', help='The timetsamp file.')
+ parser.add_argument('lib_dir', help='The directory containing the modules')
+ parser.add_argument('destination_dir',
+ help='The destination directory of the module')
+ parser.add_argument('mappings', nargs='+',
+ help='The mapping from module to library.')
+ opts = parser.parse_args()
+
+ if not os.path.exists(opts.destination_dir):
+ os.makedirs(opts.destination_dir)
+
+ for mapping in opts.mappings:
+ [module, library] = mapping.split('=')
+ shutil.copy(os.path.join(opts.lib_dir, library),
+ os.path.join(opts.destination_dir, module))
+
+ touch(opts.timestamp)
+
+if __name__ == '__main__':
+ main()
diff --git a/third_party/cython/cython_compiler.gypi b/third_party/cython/cython_compiler.gypi
new file mode 100644
index 0000000..39d8318
--- /dev/null
+++ b/third_party/cython/cython_compiler.gypi
@@ -0,0 +1,71 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+{
+ 'variables': {
+ 'python_flags': '<(DEPTH)/third_party/cython/python_flags.py',
+ },
+ 'conditions': [
+ ['OS=="mac"', {
+ 'variables': {
+ 'module_prefix': '',
+ 'module_suffix': '.so',
+ },
+ }, {
+ 'variables': {
+ 'module_prefix': '<(SHARED_LIB_PREFIX)',
+ 'module_suffix': '<(SHARED_LIB_SUFFIX)',
+ },
+ }],
+ ],
+ 'type': 'loadable_module',
+ 'rules': [
+ {
+ 'rule_name': '<(_target_name)_cython_compiler',
+ 'extension': 'pyx',
+ 'variables': {
+ 'cython_compiler': '<(DEPTH)/third_party/cython/src/cython.py',
+ },
+ 'inputs': [
+ '<(cython_compiler)',
+ ],
+ 'outputs': [
+ '<(SHARED_INTERMEDIATE_DIR)/cython/<(python_base_module)/<(RULE_INPUT_ROOT).cc',
+ ],
+ 'action': [
+ 'python', '<(cython_compiler)',
+ '--cplus',
+ '-I<(DEPTH)',
+ '-o', '<@(_outputs)',
+ '<(RULE_INPUT_PATH)',
+ ],
+ 'message': 'Generating C++ source from <(RULE_INPUT_PATH)',
+ 'process_outputs_as_sources': 1,
+ }
+ ],
+ 'include_dirs': [
+ '<!@(python <(python_flags) --includes)',
+ '<(DEPTH)',
+ ],
+ 'libraries': [
+ '<!@(python <(python_flags) --libraries)',
+ ],
+ 'cflags': [
+ '-Wno-unused-function',
+ ],
+ 'xcode_settings': {
+ 'WARNING_CFLAGS': [ '-Wno-unused-function' ],
+ },
+ 'library_dirs': [
+ '<!@(python <(python_flags) --library_dirs)',
+ ],
+ 'direct_dependent_settings': {
+ 'variables': {
+ 'python_binary_modules': [
+ '<(python_cython_module)<(module_suffix)=<(module_prefix)<(_target_name)<(module_suffix)',
+ ],
+ },
+ },
+ 'hard_dependency': 1,
+}
diff --git a/third_party/cython/python_export.h b/third_party/cython/python_export.h
new file mode 100644
index 0000000..e943399
--- /dev/null
+++ b/third_party/cython/python_export.h
@@ -0,0 +1,13 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#if defined(PyMODINIT_FUNC)
+#undef PyMODINIT_FUNC
+#endif
+
+#if defined(WIN32)
+#define PyMODINIT_FUNC extern "C" __declspec(dllexport) void
+#else
+#define PyMODINIT_FUNC extern "C" __attribute__((visibility("default"))) void
+#endif
diff --git a/third_party/cython/python_flags.py b/third_party/cython/python_flags.py
new file mode 100644
index 0000000..03c4ad7
--- /dev/null
+++ b/third_party/cython/python_flags.py
@@ -0,0 +1,53 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import argparse
+import os
+import sys
+
+from distutils import sysconfig
+from distutils.command import build_ext
+from distutils.dist import Distribution
+from distutils.extension import Extension
+
+def main():
+ """Command line utility to retrieve compilation options for python modules'
+ """
+ parser = argparse.ArgumentParser(
+ description='Retrieves compilation options for python modules.')
+ parser.add_argument('--gn',
+ help='Returns all values in a format suitable for gn',
+ action='store_true')
+ parser.add_argument('--libraries', help='Returns libraries',
+ action='store_true')
+ parser.add_argument('--includes', help='Returns includes',
+ action='store_true')
+ parser.add_argument('--library_dirs', help='Returns library_dirs',
+ action='store_true')
+ opts = parser.parse_args()
+
+ ext = Extension('Dummy', [])
+ b = build_ext.build_ext(Distribution())
+ b.initialize_options()
+ b.finalize_options()
+ result = []
+ if opts.libraries:
+ libraries = b.get_libraries(ext)
+ if sys.platform == 'darwin':
+ libraries += [ '-lpython%s' % sys.version[:3] ]
+ result = result + libraries
+ if opts.includes:
+ result = result + b.include_dirs
+ if opts.library_dirs:
+ if sys.platform == 'darwin':
+ result.append('%s/lib' % sysconfig.get_config_vars('prefix')[0])
+
+ if opts.gn:
+ for x in result:
+ print x
+ else:
+ print ''.join(['"%s"' % x for x in result])
+
+if __name__ == '__main__':
+ main()
diff --git a/third_party/cython/python_module.gypi b/third_party/cython/python_module.gypi
new file mode 100644
index 0000000..a61ca2d
--- /dev/null
+++ b/third_party/cython/python_module.gypi
@@ -0,0 +1,51 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+{
+ 'variables': {
+ 'python_binary_modules%': [],
+ 'python_module_destination': '<(PRODUCT_DIR)/python/<(python_base_module)',
+ 'cp': '<(DEPTH)/third_party/cython/cp_python_binary_modules.py',
+ 'timestamp': '<(SHARED_INTERMEDIATE_DIR)/<(_target_name)_py_module.stamp',
+ },
+ 'rules': [
+ {
+ 'rule_name': '<(_target_name)_cp_python',
+ 'extension': 'py',
+ 'inputs': [
+ '<(DEPTH)/build/cp.py',
+ ],
+ 'outputs': [
+ '<(python_module_destination)/<(RULE_INPUT_NAME)',
+ ],
+ 'action': [
+ 'python',
+ '<@(_inputs)',
+ '<(RULE_INPUT_PATH)',
+ '<@(_outputs)',
+ ],
+ 'message': 'Moving <(RULE_INPUT_PATH) to its destination',
+ }
+ ],
+ 'actions': [
+ {
+ 'action_name': '<(_target_name)_move_to_python_modules',
+ 'inputs': [
+ '<(cp)',
+ ],
+ 'outputs': [
+ '<(timestamp)',
+ ],
+ 'action': [
+ 'python',
+ '<(cp)',
+ '<(timestamp)',
+ '<(PRODUCT_DIR)',
+ '<(python_module_destination)',
+ '>@(python_binary_modules)',
+ ],
+ },
+ ],
+ 'hard_dependency': 1,
+}
diff --git a/third_party/cython/rules.gni b/third_party/cython/rules.gni
new file mode 100644
index 0000000..22a56c3
--- /dev/null
+++ b/third_party/cython/rules.gni
@@ -0,0 +1,84 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+template("python_binary_module") {
+ # Only available on linux for now.
+ assert(is_linux)
+ assert(defined(invoker.sources))
+ assert(defined(invoker.python_base_module))
+
+ cython_root = "//third_party/cython"
+ cython_script = "$cython_root/src/cython.py"
+ cython_output = "${target_out_dir}/${target_name}.cc"
+
+ generator_target_name = target_name + "_cython_compiler"
+ shared_library_name = target_name + "_shared_library"
+ config_name = target_name + "_python_config"
+
+ if (is_linux) {
+ shared_library_prefix = "lib"
+ shared_library_suffix = ".so"
+ python_module_suffix = ".so"
+ }
+
+ target_visibility = [
+ ":$generator_target_name",
+ ":$shared_library_name",
+ ":$target_name",
+ ]
+
+ action(generator_target_name) {
+ visibility = target_visibility
+ script = cython_script
+ sources = invoker.sources
+ outputs = [ cython_output ]
+ args = [
+ "--cplus",
+ "-I", rebase_path("//", root_build_dir),
+ "-o", rebase_path(cython_output, root_build_dir),
+ ] + rebase_path(sources, root_build_dir)
+ }
+
+ config(config_name) {
+ visibility = target_visibility
+ python_flags = "//third_party/cython/python_flags.py"
+ include_dirs = exec_script(python_flags,
+ [ "--gn", "--includes" ],
+ "list lines")
+ libs = exec_script(python_flags,
+ [ "--gn", "--libraries" ],
+ "list lines")
+ lib_dirs = exec_script(python_flags,
+ [ "--gn", "--library_dirs" ],
+ "list lines")
+ }
+
+ shared_library(shared_library_name) {
+ visibility = target_visibility
+ deps = [
+ ":$generator_target_name",
+ ]
+ if (defined(invoker.deps)) {
+ deps += invoker.deps
+ }
+ if (defined(invoker.datadeps)) {
+ datadeps = invoker.datadeps
+ }
+ sources = [ cython_output ]
+ configs += [ ":$config_name" ]
+ }
+
+ copy(target_name) {
+ python_base_module = invoker.python_base_module
+ sources = [
+ "$root_out_dir/${shared_library_prefix}${shared_library_name}${shared_library_suffix}"
+ ]
+ outputs = [
+ "$root_out_dir/python/$python_base_module/${target_name}${python_module_suffix}"
+ ]
+ deps = [
+ ":$shared_library_name"
+ ]
+ }
+}