Skip to content

Commit

Permalink
Merge pull request grpc#5064 from nicolasnoble/win32-python
Browse files Browse the repository at this point in the history
Win32 python
  • Loading branch information
ctiller committed Feb 4, 2016
2 parents 00c3947 + 221c9c7 commit 411da52
Show file tree
Hide file tree
Showing 14 changed files with 1,722 additions and 37 deletions.
2 changes: 1 addition & 1 deletion include/grpc/impl/codegen/compression_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#ifndef GRPC_IMPL_CODEGEN_COMPRESSION_TYPES_H
#define GRPC_IMPL_CODEGEN_COMPRESSION_TYPES_H

#include <stdint.h>
#include <grpc/support/port_platform.h>

#ifdef __cplusplus
extern "C" {
Expand Down
17 changes: 15 additions & 2 deletions include/grpc/impl/codegen/port_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,22 @@
#define GPR_FORBID_UNREACHABLE_CODE 1
#endif

/* For a common case, assume that the platform has a C99-like stdint.h */

#ifdef _MSC_VER
#if _MSC_VER < 1700
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif /* _MSC_VER < 1700 */
#else
#include <stdint.h>
#endif /* _MSC_VER */

/* Cache line alignment */
#ifndef GPR_CACHELINE_SIZE_LOG
Expand Down
2 changes: 1 addition & 1 deletion include/grpc/impl/codegen/propagation_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#ifndef GRPC_IMPL_CODEGEN_H
#define GRPC_IMPL_CODEGEN_H

#include <stdint.h>
#include <grpc/support/port_platform.h>

#ifdef __cplusplus
extern "C" {
Expand Down
37 changes: 27 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,25 @@

CYTHON_EXTENSION_MODULE_NAMES = ('grpc._cython.cygrpc',)

CYTHON_HELPER_C_FILES = (
os.path.join(PYTHON_STEM, 'grpc/_cython/loader.c'),
os.path.join(PYTHON_STEM, 'grpc/_cython/imports.generated.c'),
)

CORE_C_FILES = ()
if not "win32" in sys.platform:
CORE_C_FILES += tuple(grpc_core_dependencies.CORE_SOURCE_FILES)

EXTENSION_INCLUDE_DIRECTORIES = (
(PYTHON_STEM,) + CORE_INCLUDE + BORINGSSL_INCLUDE + ZLIB_INCLUDE)

EXTENSION_LIBRARIES = ('m',)
if not "darwin" in sys.platform:
EXTENSION_LIBRARIES = ()
if "linux" in sys.platform:
EXTENSION_LIBRARIES += ('rt',)
if not "win32" in sys.platform:
EXTENSION_LIBRARIES += ('m',)

DEFINE_MACROS = (('OPENSSL_NO_ASM', 1),)
DEFINE_MACROS = (('OPENSSL_NO_ASM', 1), ('_WIN32_WINNT', 0x600))

CFLAGS = ()
LDFLAGS = ()
Expand All @@ -93,8 +104,8 @@
DEFINE_MACROS += (('PyMODINIT_FUNC', '__attribute__((visibility ("default"))) void'),)


def cython_extensions(package_names, module_names, include_dirs, libraries,
define_macros, build_with_cython=False):
def cython_extensions(package_names, module_names, extra_sources, include_dirs,
libraries, define_macros, build_with_cython=False):
if ENABLE_CYTHON_TRACING:
define_macros = define_macros + [('CYTHON_TRACE_NOGIL', 1)]
file_extension = 'pyx' if build_with_cython else 'c'
Expand All @@ -104,7 +115,7 @@ def cython_extensions(package_names, module_names, include_dirs, libraries,
extensions = [
_extension.Extension(
name=module_name,
sources=[module_file] + grpc_core_dependencies.CORE_SOURCE_FILES,
sources=[module_file] + extra_sources,
include_dirs=include_dirs, libraries=libraries,
define_macros=define_macros,
) for (module_name, module_file) in zip(module_names, module_files)
Expand All @@ -120,6 +131,7 @@ def cython_extensions(package_names, module_names, include_dirs, libraries,

CYTHON_EXTENSION_MODULES = cython_extensions(
list(CYTHON_EXTENSION_PACKAGE_NAMES), list(CYTHON_EXTENSION_MODULE_NAMES),
list(CYTHON_HELPER_C_FILES) + list(CORE_C_FILES),
list(EXTENSION_INCLUDE_DIRECTORIES), list(EXTENSION_LIBRARIES),
list(DEFINE_MACROS), bool(BUILD_WITH_CYTHON))

Expand Down Expand Up @@ -174,9 +186,6 @@ def cython_extensions(package_names, module_names, include_dirs, libraries,
'credentials/server1.key',
'credentials/server1.pem',
],
'grpc._adapter': [
'credentials/roots.pem'
],
}

TESTS_REQUIRE = (
Expand All @@ -189,7 +198,15 @@ def cython_extensions(package_names, module_names, include_dirs, libraries,
TEST_LOADER = 'tests:Loader'
TEST_RUNNER = 'tests:Runner'

PACKAGE_DATA = {}
PACKAGE_DATA = {
'grpc._adapter': [
'credentials/roots.pem'
],
'grpc._cython': [
'_windows/grpc_c.32.python',
'_windows/grpc_c.64.python',
],
}
if INSTALL_TESTS:
PACKAGE_DATA = dict(PACKAGE_DATA, **TEST_PACKAGE_DATA)
PACKAGES = setuptools.find_packages(PYTHON_STEM)
Expand Down
3 changes: 1 addition & 2 deletions src/python/grpcio/grpc/_cython/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
*.h
*.c
cygrpc.c
*.a
*.so
*.dll
Expand Down
26 changes: 8 additions & 18 deletions src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cimport libc.time
from libc.stdint cimport int64_t, uint32_t, int32_t


cdef extern from "grpc/support/alloc.h":
cdef extern from "grpc/_cython/loader.h":

ctypedef int int32_t
ctypedef unsigned uint32_t
ctypedef long int64_t

int pygrpc_load_core(const char*)

void *gpr_malloc(size_t size)
void gpr_free(void *ptr)
void *gpr_realloc(void *p, size_t size)

cdef extern from "grpc/support/slice.h":
ctypedef struct gpr_slice:
# don't worry about writing out the members of gpr_slice; we never access
# them directly.
Expand All @@ -55,9 +60,6 @@ cdef extern from "grpc/support/slice.h":
void *gpr_slice_start_ptr "GPR_SLICE_START_PTR" (gpr_slice s)
size_t gpr_slice_length "GPR_SLICE_LENGTH" (gpr_slice s)


cdef extern from "grpc/support/time.h":

ctypedef enum gpr_clock_type:
GPR_CLOCK_MONOTONIC
GPR_CLOCK_REALTIME
Expand All @@ -78,8 +80,6 @@ cdef extern from "grpc/support/time.h":
gpr_timespec gpr_convert_clock_type(gpr_timespec t,
gpr_clock_type target_clock)


cdef extern from "grpc/status.h":
ctypedef enum grpc_status_code:
GRPC_STATUS_OK
GRPC_STATUS_CANCELLED
Expand All @@ -100,14 +100,10 @@ cdef extern from "grpc/status.h":
GRPC_STATUS_DATA_LOSS
GRPC_STATUS__DO_NOT_USE


cdef extern from "grpc/byte_buffer_reader.h":
struct grpc_byte_buffer_reader:
# We don't care about the internals
pass


cdef extern from "grpc/byte_buffer.h":
ctypedef struct grpc_byte_buffer:
# We don't care about the internals.
pass
Expand All @@ -123,9 +119,6 @@ cdef extern from "grpc/byte_buffer.h":
gpr_slice *slice)
void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader)


cdef extern from "grpc/grpc.h":

const char *GRPC_ARG_PRIMARY_USER_AGENT_STRING
const char *GRPC_ARG_ENABLE_CENSUS
const char *GRPC_ARG_MAX_CONCURRENT_STREAMS
Expand Down Expand Up @@ -333,9 +326,6 @@ cdef extern from "grpc/grpc.h":
void grpc_server_cancel_all_calls(grpc_server *server)
void grpc_server_destroy(grpc_server *server)


cdef extern from "grpc/grpc_security.h":

ctypedef struct grpc_ssl_pem_key_cert_pair:
const char *private_key
const char *certificate_chain "cert_chain"
Expand Down
9 changes: 7 additions & 2 deletions src/python/grpcio/grpc/_cython/_cygrpc/records.pyx.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ cdef class Timespec:
gpr_convert_clock_type(self.c_time, GPR_CLOCK_REALTIME))
return <double>real_time.seconds + <double>real_time.nanoseconds / 1e9

infinite_future = Timespec(float("+inf"))
infinite_past = Timespec(float("-inf"))
@staticmethod
def infinite_future():
return Timespec(float("+inf"))

@staticmethod
def infinite_past():
return Timespec(float("-inf"))


cdef class CallDetails:
Expand Down
13 changes: 12 additions & 1 deletion src/python/grpcio/grpc/_cython/cygrpc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

cimport cpython

import pkg_resources
import os.path

# TODO(atash): figure out why the coverage tool gets confused about the Cython
# coverage plugin when the following files don't have a '.pxi' suffix.
include "grpc/_cython/_cygrpc/call.pyx.pxi"
Expand All @@ -44,11 +47,19 @@ include "grpc/_cython/_cygrpc/server.pyx.pxi"

cdef class _ModuleState:

cdef bint is_loaded

def __cinit__(self):
filename = pkg_resources.resource_filename(
'grpc._cython', '_windows/grpc_c.64.python')
if not pygrpc_load_core(filename):
raise ImportError('failed to load core gRPC library')
grpc_init()
self.is_loaded = True

def __dealloc__(self):
grpc_shutdown()
if self.is_loaded:
grpc_shutdown()

_module_state = _ModuleState()

Loading

0 comments on commit 411da52

Please sign in to comment.