Skip to content

Commit

Permalink
Merge pull request #17 from Falldog/support_windows
Browse files Browse the repository at this point in the history
Support windows
  • Loading branch information
Falldog authored Sep 19, 2017
2 parents 9513844 + 80e1d25 commit 9b0a538
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ $ TEST_PYE_PERFORMANCE_COUNT=1 ./pyconcrete-admin.py test

Building on Windows
--------------
#### Python 2.7 - Visual Studio 2008
https://www.microsoft.com/en-us/download/details.aspx?id=44266

* Open VS2008 Command Prompt
* `set DISTUTILS_USE_SDK=1`
* `set SET MSSdk=1`
* create `distutils.cfg` and put inside
```text
[build]
compiler=msvc
```

#### Python 3.5, 3.6 - Visual Studio 2015

[MSVC 2015 Build Tools](http://landinghub.visualstudio.com/visual-cpp-build-tools)

[Document](https://matthew-brett.github.io/pydagogue/python_msvc.html#python-3-5-3-6)

* make sure setuptools >= 24.0
```sh
python -c 'import setuptools; print(setuptools.__version__)'
```

* Open VS2015 Build Tools Command Prompt
* `set DISTUTILS_USE_SDK=1 `
* `setenv /x64 /release` or `setenv /x86 /release`


#### Reference
https://matthew-brett.github.io/pydagogue/python_msvc.html
https://github.com/cython/cython/wiki/CythonExtensionsOnWindows

Expand Down
66 changes: 47 additions & 19 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def is_mingw():
return False


def is_msvc():
return sys.platform == 'win32' and not is_mingw()


def hash_key(key):
if PY2:
factor = sum([ord(s) for s in key])
Expand Down Expand Up @@ -256,9 +260,14 @@ def create_pth(self):
print('creating %s' % filename)

def install_exe(self):
# install `pyconcrete` to /usr/local/bin
self.copy_file(os.path.join(self.build_scripts, 'pyconcrete'),
os.path.join(self.install_scripts, 'pyconcrete'))
if sys.platform == 'win32':
# install `pyconcrete.exe` to %PYTHON%/Scripts
exe_name = 'pyconcrete.exe'
else:
# install `pyconcrete` to /usr/local/bin
exe_name = 'pyconcrete'
self.copy_file(os.path.join(self.build_scripts, exe_name),
os.path.join(self.install_scripts, exe_name))

# ================================================= test command ================================================= #

Expand All @@ -281,28 +290,47 @@ def run(self):

# ================================================= extension ================================================= #

openaes_include_dirs = [
join(EXT_SRC_DIR),
join(EXT_SRC_DIR, 'openaes', 'inc'),
]
if sys.platform == 'win32' and not is_mingw():
openaes_include_dirs.append(join(EXT_SRC_DIR, 'include_win'))
def get_include_dirs():
openaes_include_dirs = [
join(EXT_SRC_DIR),
join(EXT_SRC_DIR, 'openaes', 'inc'),
]
if is_msvc() and PY2: # Only Python 2.7 & 3.2 Need VisualStudio 2008 (without stdint.h)
openaes_include_dirs.append(join(EXT_SRC_DIR, 'include_win'))
return openaes_include_dirs


def get_libraries(include_python_lib=False):
libraries = []
if sys.version_info[0] == 3 and sys.version_info[1] >= 5 and is_msvc():
# https://stackoverflow.com/questions/32418766/c-unresolved-external-symbol-sprintf-and-sscanf-in-visual-studio-2015
libraries = ['legacy_stdio_definitions']

if is_msvc():
link_py = 'python{0}{1}'.format(*sys.version_info[0:2])
else:
if PY2:
link_py_fmt = 'python{version}'
else:
link_py_fmt = 'python{version}m'
link_py = link_py_fmt.format(version=sysconfig.get_python_version())

if include_python_lib:
libraries.append(link_py)

return libraries


include_dirs = get_include_dirs()
openaes_sources = [
join(EXT_SRC_DIR, 'openaes', 'src', 'oaes_base64.c'),
join(EXT_SRC_DIR, 'openaes', 'src', 'oaes_lib.c'),
]

if PY2:
link_py = 'python{version}'
else:
link_py = 'python{version}m'
link_py = link_py.format(version=sysconfig.get_python_version())


ext_module = Extension(
'pyconcrete._pyconcrete',
include_dirs=openaes_include_dirs,
include_dirs=include_dirs,
libraries=get_libraries(include_python_lib=False),
sources=[
join(EXT_SRC_DIR, 'pyconcrete.c'),
join(EXT_SRC_DIR, 'pyconcrete_module.c'),
Expand All @@ -311,8 +339,8 @@ def run(self):

exe_module = Extension(
'pyconcrete',
include_dirs=openaes_include_dirs,
extra_link_args=['-l'+link_py],
include_dirs=include_dirs,
libraries=get_libraries(include_python_lib=True),
sources=[
join(EXE_SRC_DIR, 'pyconcrete_exe.c'),
join(EXT_SRC_DIR, 'pyconcrete.c'),
Expand Down
3 changes: 2 additions & 1 deletion test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def build_tmp_pyconcrete(passphrase):

copy_pyconcrete_ext(tmp_dir)

tmp_pyconcrete_exe = join(tmp_dir, 'scripts', 'pyconcrete')
exe_name = 'pyconcrete.exe' if sys.platform == 'win32' else 'pyconcrete'
tmp_pyconcrete_exe = join(tmp_dir, 'scripts', exe_name)
tmp_pyconcrete_dir = tmp_dir
print('build tmp pyconcrete at "%s"' % tmp_dir)

Expand Down
1 change: 1 addition & 0 deletions test/test_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def test_sys_path(self):

output = subprocess.check_output([self._pyconcrete_exe, pye])
output = output.decode('utf8')
output = output.replace('\r\n', '\n')
paths = output.split('\n')

self.assertTrue(pye_dir in paths, "pye dir(%s) not in output paths %s" % (pye_dir, paths))
Expand Down

0 comments on commit 9b0a538

Please sign in to comment.