Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c98d6fb

Browse files
author
David Joyce
committed
Initial coreclr and build tooling
Can load .NET Core into python Pass clr PyObject back to Python
1 parent 32ec24c commit c98d6fb

File tree

9 files changed

+1028
-2
lines changed

9 files changed

+1028
-2
lines changed

setup.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ def build_extension(self, ext):
265265
subprocess.check_call(" ".join(cmd + ["/t:Build"]), shell=use_shell)
266266
if DEVTOOLS == "MsDev15" or DEVTOOLS == "dotnet":
267267
subprocess.check_call(" ".join(cmd + ['"/t:Console_15:publish;Python_EmbeddingTest_15:publish"', "/p:TargetFramework=netcoreapp2.0"]), shell=use_shell)
268-
if DEVTOOLS == "Mono" or DEVTOOLS == "dotnet":
268+
if DEVTOOLS == "Mono":
269269
self._build_monoclr()
270+
if DEVTOOLS == "dotnet":
271+
self._build_coreclr()
270272

271273
def _get_manifest(self, build_dir):
272274
if DEVTOOLS != "MsDev" and DEVTOOLS != "MsDev15":
@@ -306,6 +308,19 @@ def _build_monoclr(self):
306308

307309
build_ext.build_ext.build_extension(self, clr_ext)
308310

311+
def _build_coreclr(self):
312+
# build the clr python module
313+
clr_ext = Extension(
314+
"clr",
315+
sources=[
316+
"src/coreclr/pynetinit.c",
317+
"src/coreclr/clrmod.c",
318+
"src/coreclr/coreutils.c",
319+
],
320+
)
321+
322+
build_ext.build_ext.build_extension(self, clr_ext)
323+
309324
def _install_packages(self):
310325
"""install packages using nuget"""
311326
use_shell = DEVTOOLS == "Mono" or DEVTOOLS == "dotnet"

src/coreclr/clrmod.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "pynetclr.h"
2+
3+
/* List of functions defined in the module */
4+
static PyMethodDef clr_methods[] = {
5+
{NULL, NULL, 0, NULL} /* Sentinel */
6+
};
7+
8+
PyDoc_STRVAR(clr_module_doc,
9+
"clr facade module to initialize the CLR. It's later "
10+
"replaced by the real clr module. This module has a facade "
11+
"attribute to make it distinguishable from the real clr module."
12+
);
13+
14+
static PyNet_Args *pn_args;
15+
char **environ = NULL;
16+
17+
#if PY_MAJOR_VERSION >= 3
18+
static struct PyModuleDef clrdef = {
19+
PyModuleDef_HEAD_INIT,
20+
"clr", /* m_name */
21+
clr_module_doc, /* m_doc */
22+
-1, /* m_size */
23+
clr_methods, /* m_methods */
24+
NULL, /* m_reload */
25+
NULL, /* m_traverse */
26+
NULL, /* m_clear */
27+
NULL, /* m_free */
28+
};
29+
#endif
30+
31+
static PyObject *_initclr(void)
32+
{
33+
PyObject *m;
34+
35+
/* Create the module and add the functions */
36+
#if PY_MAJOR_VERSION >= 3
37+
m = PyModule_Create(&clrdef);
38+
#else
39+
m = Py_InitModule3("clr", clr_methods, clr_module_doc);
40+
#endif
41+
if (m == NULL)
42+
return NULL;
43+
PyModule_AddObject(m, "facade", Py_True);
44+
Py_INCREF(Py_True);
45+
46+
pn_args = PyNet_Init(1);
47+
if (pn_args->error)
48+
{
49+
return NULL;
50+
}
51+
52+
if (NULL != pn_args->module)
53+
return pn_args->module;
54+
55+
return m;
56+
}
57+
58+
#if PY_MAJOR_VERSION >= 3
59+
PyMODINIT_FUNC
60+
PyInit_clr(void)
61+
{
62+
return _initclr();
63+
}
64+
#else
65+
PyMODINIT_FUNC
66+
initclr(void)
67+
{
68+
_initclr();
69+
}
70+
#endif

src/coreclr/coreclrhost.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
//
6+
// APIs for hosting CoreCLR
7+
//
8+
9+
#ifndef __CORECLR_HOST_H__
10+
#define __CORECLR_HOST_H__
11+
12+
// For each hosting API, we define a function prototype and a function pointer
13+
// The prototype is useful for implicit linking against the dynamic coreclr
14+
// library and the pointer for explicit dynamic loading (dlopen, LoadLibrary)
15+
#define CORECLR_HOSTING_API(function, ...) \
16+
int function(__VA_ARGS__); \
17+
typedef int (*function##_ptr)(__VA_ARGS__)
18+
19+
CORECLR_HOSTING_API(coreclr_initialize,
20+
const char* exePath,
21+
const char* appDomainFriendlyName,
22+
int propertyCount,
23+
const char** propertyKeys,
24+
const char** propertyValues,
25+
void** hostHandle,
26+
unsigned int* domainId);
27+
28+
CORECLR_HOSTING_API(coreclr_shutdown,
29+
void* hostHandle,
30+
unsigned int domainId);
31+
32+
CORECLR_HOSTING_API(coreclr_shutdown_2,
33+
void* hostHandle,
34+
unsigned int domainId,
35+
int* latchedExitCode);
36+
37+
CORECLR_HOSTING_API(coreclr_create_delegate,
38+
void* hostHandle,
39+
unsigned int domainId,
40+
const char* entryPointAssemblyName,
41+
const char* entryPointTypeName,
42+
const char* entryPointMethodName,
43+
void** delegate);
44+
45+
CORECLR_HOSTING_API(coreclr_execute_assembly,
46+
void* hostHandle,
47+
unsigned int domainId,
48+
int argc,
49+
const char** argv,
50+
const char* managedAssemblyPath,
51+
unsigned int* exitCode);
52+
53+
#undef CORECLR_HOSTING_API
54+
55+
#endif // __CORECLR_HOST_H__

0 commit comments

Comments
 (0)