Commit 28e16123 authored by qsr's avatar qsr Committed by Commit bot

Core mojo API for python.

Review URL: https://codereview.chromium.org/377293002

Cr-Commit-Position: refs/heads/master@{#291701}
parent 0aff6f3f
...@@ -13,6 +13,12 @@ group("mojo") { ...@@ -13,6 +13,12 @@ group("mojo") {
"//mojo/services", "//mojo/services",
"//mojo/shell:mojo_shell", "//mojo/shell:mojo_shell",
] ]
if (is_linux) {
deps += [
"//mojo/python",
]
}
} }
group("tests") { group("tests") {
......
...@@ -87,6 +87,13 @@ ...@@ -87,6 +87,13 @@
'mojo_dbus_echo_service', 'mojo_dbus_echo_service',
], ],
}], }],
['component != "shared_library" and OS == "linux"', {
'dependencies': [
'mojo_python_embedder',
'mojo_python_system',
'mojo_python',
],
}],
] ]
}, },
{ {
...@@ -531,5 +538,77 @@ ...@@ -531,5 +538,77 @@
}, },
], ],
}], }],
['component!="shared_library" and OS=="linux"', {
'targets': [
{
'target_name': 'mojo_python_system',
'variables': {
'python_base_module': 'mojo',
'python_cython_module': 'system',
},
'sources': [
'public/python/mojo/c_core.pxd',
'public/python/mojo/system.pyx',
],
'dependencies': [
'mojo_base.gyp:mojo_system',
],
'includes': [ '../third_party/cython/cython_compiler.gypi' ],
},
{
'target_name': 'mojo_python_embedder',
'type': 'loadable_module',
'variables': {
'python_base_module': 'mojo',
'python_cython_module': 'embedder',
},
'sources': [
'python/system/mojo/embedder.pyx',
],
'dependencies': [
'mojo_base.gyp:mojo_system_impl',
],
'includes': [ '../third_party/cython/cython_compiler.gypi' ],
},
{
'target_name': 'mojo_python',
'type': 'none',
'variables': {
'python_base_module': 'mojo',
},
'sources': [
'public/python/mojo/__init__.py',
],
'dependencies': [
'mojo_python_embedder',
'mojo_python_system',
],
'includes': [ '../third_party/cython/python_module.gypi' ],
},
{
'target_name': 'mojo_python_unittests',
'type': 'none',
'actions': [
{
'action_name': 'run_mojo_python_unittests',
'inputs': [
'python/tests/test_core.py',
'<(SHARED_INTERMEDIATE_DIR)/mojo_python_py_module.stamp',
'<(PRODUCT_DIR)/python/mojo/__init__.py',
],
'outputs': [
'none',
],
'action': [
'python', '<@(_inputs)', '<(PRODUCT_DIR)/python',
],
},
],
'dependency': [
'mojo_python',
],
},
],
}],
], ],
} }
...@@ -14,6 +14,12 @@ group("public") { ...@@ -14,6 +14,12 @@ group("public") {
"//mojo/public/js/bindings", "//mojo/public/js/bindings",
] ]
if (is_linux) {
deps += [
"//mojo/public/python",
]
}
if (is_android) { if (is_android) {
deps += [ deps += [
"//mojo/public/java:system", "//mojo/public/java:system",
......
# 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("//third_party/cython/rules.gni")
group("python") {
deps = [
":base",
":system",
]
}
python_binary_module("system") {
python_base_module = "mojo"
sources = [
"mojo/c_core.pxd",
"mojo/system.pyx",
]
deps = [
"//mojo/public/c/system",
":base",
]
}
copy("base") {
sources = [
"mojo/__init__.py",
]
outputs = [
"$root_out_dir/python/mojo/__init__.py",
]
}
# 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.
# 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.
# distutils: language = c++
from cpython.buffer cimport PyBUF_CONTIG
from cpython.buffer cimport PyBUF_CONTIG_RO
from cpython.buffer cimport Py_buffer
from cpython.buffer cimport PyBuffer_FillInfo
from cpython.buffer cimport PyBuffer_Release
from cpython.buffer cimport PyObject_GetBuffer
from cpython.mem cimport PyMem_Malloc, PyMem_Free
from libc.stdint cimport int32_t, int64_t, uint32_t, uint64_t, uintptr_t
cdef extern from "third_party/cython/python_export.h":
pass
cdef extern from "mojo/public/platform/native/system_thunks.h" nogil:
cdef struct MojoSystemThunks:
pass
cdef extern size_t MojoSetSystemThunks(const MojoSystemThunks* system_thunks)
cdef extern from "mojo/public/c/system/core.h" nogil:
# types.h
ctypedef int64_t MojoTimeTicks
ctypedef uint32_t MojoHandle
const MojoHandle MOJO_HANDLE_INVALID
ctypedef int32_t MojoResult
const MojoResult MOJO_RESULT_OK
const MojoResult MOJO_RESULT_CANCELLED
const MojoResult MOJO_RESULT_UNKNOWN
const MojoResult MOJO_RESULT_INVALID_ARGUMENT
const MojoResult MOJO_RESULT_DEADLINE_EXCEEDED
const MojoResult MOJO_RESULT_NOT_FOUND
const MojoResult MOJO_RESULT_ALREADY_EXISTS
const MojoResult MOJO_RESULT_PERMISSION_DENIED
const MojoResult MOJO_RESULT_RESOURCE_EXHAUSTED
const MojoResult MOJO_RESULT_FAILED_PRECONDITION
const MojoResult MOJO_RESULT_ABORTED
const MojoResult MOJO_RESULT_OUT_OF_RANGE
const MojoResult MOJO_RESULT_UNIMPLEMENTED
const MojoResult MOJO_RESULT_INTERNAL
const MojoResult MOJO_RESULT_UNAVAILABLE
const MojoResult MOJO_RESULT_DATA_LOSS
const MojoResult MOJO_RESULT_BUSY
const MojoResult MOJO_RESULT_SHOULD_WAIT
ctypedef uint64_t MojoDeadline
const MojoDeadline MOJO_DEADLINE_INDEFINITE
ctypedef uint32_t MojoHandleSignals
const MojoHandleSignals MOJO_HANDLE_SIGNAL_NONE
const MojoHandleSignals MOJO_HANDLE_SIGNAL_READABLE
const MojoHandleSignals MOJO_HANDLE_SIGNAL_WRITABLE
# functions.h
MojoTimeTicks MojoGetTimeTicksNow()
MojoResult MojoClose(MojoHandle handle)
MojoResult MojoWait(MojoHandle handle,
MojoHandleSignals signals,
MojoDeadline deadline)
MojoResult MojoWaitMany(const MojoHandle* handles,
const MojoHandleSignals* signals,
uint32_t num_handles,
MojoDeadline deadline)
# message_pipe.h
ctypedef uint32_t MojoCreateMessagePipeOptionsFlags
const MojoCreateMessagePipeOptionsFlags MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE
ctypedef uint32_t MojoWriteMessageFlags
const MojoWriteMessageFlags MOJO_WRITE_MESSAGE_FLAG_NONE
ctypedef uint32_t MojoReadMessageFlags
const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_NONE
const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_MAY_DISCARD
cdef struct MojoCreateMessagePipeOptions:
uint32_t struct_size
MojoCreateMessagePipeOptionsFlags flags
MojoResult MojoCreateMessagePipe(
const MojoCreateMessagePipeOptions* options,
MojoHandle* message_pipe_handle0,
MojoHandle* message_pipe_handle1)
MojoResult MojoWriteMessage(
MojoHandle message_pipe_handle,
const void* bytes,
uint32_t num_bytes,
const MojoHandle* handles,
uint32_t num_handles,
MojoWriteMessageFlags flags)
MojoResult MojoReadMessage(
MojoHandle message_pipe_handle,
void* bytes,
uint32_t* num_bytes,
MojoHandle* handles,
uint32_t* num_handles,
MojoReadMessageFlags flags)
# data_pipe.h
ctypedef uint32_t MojoCreateDataPipeOptionsFlags
const MojoCreateDataPipeOptionsFlags MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE
const MojoCreateDataPipeOptionsFlags MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD
cdef struct MojoCreateDataPipeOptions:
uint32_t struct_size
MojoCreateDataPipeOptionsFlags flags
uint32_t element_num_bytes
uint32_t capacity_num_bytes
ctypedef uint32_t MojoWriteDataFlags
const MojoWriteDataFlags MOJO_WRITE_DATA_FLAG_NONE
const MojoWriteDataFlags MOJO_WRITE_DATA_FLAG_ALL_OR_NONE
ctypedef uint32_t MojoReadDataFlags
const MojoReadDataFlags MOJO_READ_DATA_FLAG_NONE
const MojoReadDataFlags MOJO_READ_DATA_FLAG_ALL_OR_NONE
const MojoReadDataFlags MOJO_READ_DATA_FLAG_DISCARD
const MojoReadDataFlags MOJO_READ_DATA_FLAG_QUERY
MojoResult MojoCreateDataPipe(
const MojoCreateDataPipeOptions* options,
MojoHandle* data_pipe_producer_handle,
MojoHandle* data_pipe_consumer_handle)
MojoResult MojoWriteData(
MojoHandle data_pipe_producer_handle,
const void* elements,
uint32_t* num_bytes,
MojoWriteDataFlags flags)
MojoResult MojoBeginWriteData(
MojoHandle data_pipe_producer_handle,
void** buffer,
uint32_t* buffer_num_bytes,
MojoWriteDataFlags flags)
MojoResult MojoEndWriteData(
MojoHandle data_pipe_producer_handle,
uint32_t num_bytes_written)
MojoResult MojoReadData(
MojoHandle data_pipe_consumer_handle,
void* elements,
uint32_t* num_bytes,
MojoReadDataFlags flags)
MojoResult MojoBeginReadData(
MojoHandle data_pipe_consumer_handle,
const void** buffer,
uint32_t* buffer_num_bytes,
MojoReadDataFlags flags)
MojoResult MojoEndReadData(
MojoHandle data_pipe_consumer_handle,
uint32_t num_bytes_read)
# buffer.h
ctypedef uint32_t MojoCreateSharedBufferOptionsFlags
const MojoCreateSharedBufferOptionsFlags MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE
cdef struct MojoCreateSharedBufferOptions:
uint32_t struct_size
MojoCreateSharedBufferOptionsFlags flags
ctypedef uint32_t MojoDuplicateBufferHandleOptionsFlags
const MojoDuplicateBufferHandleOptionsFlags MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE
cdef struct MojoDuplicateBufferHandleOptions:
uint32_t struct_size
MojoDuplicateBufferHandleOptionsFlags flags
ctypedef uint32_t MojoMapBufferFlags
const MojoMapBufferFlags MOJO_MAP_BUFFER_FLAG_NONE
MojoResult MojoCreateSharedBuffer(
const MojoCreateSharedBufferOptions* options,
uint64_t num_bytes,
MojoHandle* shared_buffer_handle)
MojoResult MojoDuplicateBufferHandle(
MojoHandle buffer_handle,
const MojoDuplicateBufferHandleOptions* options,
MojoHandle* new_buffer_handle)
MojoResult MojoMapBuffer(MojoHandle buffer_handle,
uint64_t offset,
uint64_t num_bytes,
void** buffer,
MojoMapBufferFlags flags)
MojoResult MojoUnmapBuffer(void* buffer)
This diff is collapsed.
# 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("//third_party/cython/rules.gni")
group("python") {
deps = [
":embedder",
]
}
group("tests") {
deps = [
"//mojo/python/tests",
]
}
python_binary_module("embedder") {
python_base_module = "mojo"
sources = [
"system/mojo/embedder.pyx",
]
deps = [
"//mojo/system",
]
datadeps = [
"//mojo/public/python:system",
]
}
# 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.
# distutils: language = c++
from libc.stdint cimport uintptr_t
from mojo import system
cdef extern from "third_party/cython/python_export.h":
pass
cdef extern from "base/memory/scoped_ptr.h":
cdef cppclass scoped_ptr[T]:
scoped_ptr(T*)
cdef extern from "mojo/embedder/platform_support.h" \
namespace "mojo::embedder" nogil:
cdef cppclass PlatformSupport:
pass
cdef extern from "mojo/embedder/simple_platform_support.h" \
namespace "mojo::embedder" nogil:
cdef cppclass SimplePlatformSupport(PlatformSupport):
SimplePlatformSupport()
cdef extern from "mojo/embedder/embedder.h" nogil:
cdef void InitCEmbedder "mojo::embedder::Init"(
scoped_ptr[PlatformSupport] platform_support)
cdef extern from "mojo/public/platform/native/system_thunks.h" nogil:
cdef struct MojoSystemThunks:
pass
cdef MojoSystemThunks MojoMakeSystemThunks()
def init():
InitCEmbedder(scoped_ptr[PlatformSupport](
new SimplePlatformSupport()))
cdef MojoSystemThunks thunks = MojoMakeSystemThunks()
system.set_system_thunks(<uintptr_t>(&thunks))
# 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("//third_party/cython/rules.gni")
group("tests") {
deps = [
":test_core",
]
}
action("test_core") {
script = "test_core.py"
outputs = [
"$root_out_dir/no_file",
]
inputs = []
deps = [
"//mojo/public/python:system",
"//mojo/python:embedder",
]
args = [ rebase_path("$root_out_dir/python", root_build_dir) ]
}
This diff is collapsed.
# 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()
# 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,
}
// 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
# 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()
# 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,
}
# 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"
]
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment