Skip to content

Instantly share code, notes, and snippets.

@uranusjr
Last active January 17, 2019 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uranusjr/0821140136c4de900640f70d976af2ca to your computer and use it in GitHub Desktop.
Save uranusjr/0821140136c4de900640f70d976af2ca to your computer and use it in GitHub Desktop.
Trying to set up a minimal CPython extension build environment
FROM python:3.7-windowsservercore
ENV VS_BUILDTOOLS '\
https://download.visualstudio.microsoft.com/download/pr/11503713\
/e64d79b40219aea618ce2fe10ebd5f0d/vs_BuildTools.exe'
COPY install-vs-tools.ps1 .
RUN .\install-vs-tools.ps1
COPY hello.c .
COPY setup.py .
RUN python setup.py install
RUN python -c "import hello; hello.say('Here is some stuff...')"
#include <stdio.h>
#include <Python.h>
static PyObject *hello_say(PyObject *self, PyObject *args)
{
const char *msg;
if (!PyArg_ParseTuple(args, "s", &msg))
return NULL;
fprintf(stdout, "Hello! %s\n", msg);
return Py_None;
}
static PyMethodDef HelloMethods[] = {
{"say", hello_say, METH_VARARGS, "Say stuff."},
{NULL, NULL, 0, NULL},
};
static struct PyModuleDef hellomodule = {
PyModuleDef_HEAD_INIT,
"hello",
NULL,
-1,
HelloMethods,
};
PyMODINIT_FUNC
PyInit_hello(void)
{
PyObject *m = PyModule_Create(&hellomodule);
if (!m)
return NULL;
return m;
}
Set-PSDebug -Trace 1
Invoke-WebRequest -Uri "$env:VS_BUILDTOOLS" -OutFile 'vs_BuildTools.exe'
Start-Process 'vs_BuildTools.exe' -Wait -ArgumentList @(
'--add', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
'--add', 'Microsoft.VisualStudio.Component.VC.CoreBuildTools',
'--norestart',
'--quiet')
from distutils.core import setup, Extension
setup(
name="hello",
version="1.0",
description="Hello!",
ext_modules=[Extension("hello", sources=["hello.c"])],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment