2019-05-28 12:00:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Compile the Cython libraries of Python-Solvespace."""
|
|
|
|
|
|
|
|
__author__ = "Yuan Chang"
|
|
|
|
__copyright__ = "Copyright (C) 2016-2019"
|
|
|
|
__license__ = "GPLv3+"
|
|
|
|
__email__ = "pyslvs@gmail.com"
|
|
|
|
|
2019-10-21 15:13:17 +00:00
|
|
|
import sys
|
2019-09-27 06:18:22 +00:00
|
|
|
from os import walk
|
2019-10-12 04:26:29 +00:00
|
|
|
from os.path import dirname, isdir, join as pth_join
|
2019-05-28 12:00:15 +00:00
|
|
|
import re
|
|
|
|
import codecs
|
|
|
|
from setuptools import setup, Extension, find_packages
|
2019-05-29 06:34:49 +00:00
|
|
|
from setuptools.command.build_ext import build_ext
|
2019-09-27 01:19:43 +00:00
|
|
|
from setuptools.command.sdist import sdist
|
2019-09-27 06:18:22 +00:00
|
|
|
from distutils import file_util, dir_util
|
2019-05-28 12:00:15 +00:00
|
|
|
from platform import system
|
|
|
|
|
2020-07-31 08:17:36 +00:00
|
|
|
m_path = 'python_solvespace'
|
|
|
|
include_path = pth_join(m_path, 'include')
|
|
|
|
src_path = pth_join(m_path, 'src')
|
2019-09-27 01:19:43 +00:00
|
|
|
platform_path = pth_join(src_path, 'platform')
|
2020-07-31 09:05:09 +00:00
|
|
|
extlib_path = pth_join(m_path, 'extlib')
|
|
|
|
mimalloc_path = pth_join(extlib_path, 'mimalloc')
|
2020-07-31 08:17:36 +00:00
|
|
|
mimalloc_include_path = pth_join(mimalloc_path, 'include')
|
|
|
|
mimalloc_src_path = pth_join(mimalloc_path, 'src')
|
2019-05-28 12:00:15 +00:00
|
|
|
|
|
|
|
|
2019-05-29 06:34:49 +00:00
|
|
|
def write(doc, *parts):
|
2019-10-06 10:08:46 +00:00
|
|
|
with codecs.open(pth_join(*parts), 'w') as f:
|
2019-05-29 06:34:49 +00:00
|
|
|
f.write(doc)
|
|
|
|
|
|
|
|
|
2019-05-28 12:00:15 +00:00
|
|
|
def read(*parts):
|
2019-10-06 10:08:46 +00:00
|
|
|
with codecs.open(pth_join(*parts), 'r') as f:
|
2019-05-28 12:00:15 +00:00
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
2019-09-23 02:30:20 +00:00
|
|
|
def find_version(*file_paths):
|
|
|
|
m = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", read(*file_paths), re.M)
|
|
|
|
if m:
|
|
|
|
return m.group(1)
|
2019-05-28 12:00:15 +00:00
|
|
|
raise RuntimeError("Unable to find version string.")
|
|
|
|
|
|
|
|
|
|
|
|
macros = [
|
2019-10-23 13:14:33 +00:00
|
|
|
('M_PI', 'PI'),
|
|
|
|
('_USE_MATH_DEFINES', None),
|
2019-05-28 12:00:15 +00:00
|
|
|
('ISOLATION_AWARE_ENABLED', None),
|
|
|
|
('LIBRARY', None),
|
|
|
|
('EXPORT_DLL', None),
|
|
|
|
('_CRT_SECURE_NO_WARNINGS', None),
|
|
|
|
]
|
|
|
|
compile_args = [
|
|
|
|
'-O3',
|
|
|
|
'-Wno-cpp',
|
|
|
|
'-g',
|
|
|
|
'-Wno-write-strings',
|
|
|
|
'-fpermissive',
|
|
|
|
'-fPIC',
|
2019-10-23 13:14:33 +00:00
|
|
|
'-std=c++17',
|
2019-05-28 12:00:15 +00:00
|
|
|
]
|
2020-07-07 06:55:03 +00:00
|
|
|
link_args = ['-static-libgcc', '-static-libstdc++',
|
|
|
|
'-Wl,-Bstatic,--whole-archive',
|
|
|
|
'-lwinpthread',
|
2020-07-31 09:05:09 +00:00
|
|
|
'-Wl,--no-whole-archive',
|
2020-07-31 08:17:36 +00:00
|
|
|
'-lbcrypt',
|
|
|
|
'-lpsapi',
|
2020-07-31 09:05:09 +00:00
|
|
|
'-Wl,-Bdynamic']
|
2019-05-28 12:00:15 +00:00
|
|
|
sources = [
|
2020-07-31 08:17:36 +00:00
|
|
|
pth_join(m_path, 'slvs.pyx'),
|
2019-09-27 01:19:43 +00:00
|
|
|
pth_join(src_path, 'util.cpp'),
|
|
|
|
pth_join(src_path, 'entity.cpp'),
|
|
|
|
pth_join(src_path, 'expr.cpp'),
|
|
|
|
pth_join(src_path, 'constraint.cpp'),
|
2020-07-31 08:17:36 +00:00
|
|
|
pth_join(src_path, 'constrainteq.cpp'),
|
2019-09-27 01:19:43 +00:00
|
|
|
pth_join(src_path, 'system.cpp'),
|
|
|
|
pth_join(src_path, 'lib.cpp'),
|
2020-07-08 06:48:06 +00:00
|
|
|
pth_join(platform_path, 'platform.cpp'),
|
2020-07-31 08:17:36 +00:00
|
|
|
# MiMalloc
|
|
|
|
pth_join(mimalloc_src_path, 'stats.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'random.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'os.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'arena.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'region.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'segment.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'page.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'alloc.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'alloc-aligned.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'alloc-posix.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'heap.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'options.c'),
|
|
|
|
pth_join(mimalloc_src_path, 'init.c'),
|
2019-05-28 12:00:15 +00:00
|
|
|
]
|
2019-10-21 15:13:17 +00:00
|
|
|
if {'sdist', 'bdist'} & set(sys.argv):
|
2020-07-08 06:48:06 +00:00
|
|
|
sources.append(pth_join(platform_path, 'platform.cpp'))
|
2019-10-21 15:13:17 +00:00
|
|
|
elif system() == 'Windows':
|
2019-05-28 12:00:15 +00:00
|
|
|
# Disable format warning
|
|
|
|
compile_args.append('-Wno-format')
|
|
|
|
# Solvespace arguments
|
|
|
|
macros.append(('WIN32', None))
|
2019-10-26 13:40:33 +00:00
|
|
|
if sys.version_info < (3, 7):
|
|
|
|
macros.append(('_hypot', 'hypot'))
|
2019-05-28 12:00:15 +00:00
|
|
|
else:
|
2020-07-08 06:48:06 +00:00
|
|
|
macros.append(('UNIX_DATADIR', '"solvespace"'))
|
2020-07-31 06:33:54 +00:00
|
|
|
compiler_directives = {'binding': True, 'cdivision': True}
|
2019-05-29 06:34:49 +00:00
|
|
|
|
|
|
|
|
2019-09-27 06:18:22 +00:00
|
|
|
def copy_source(dry_run):
|
|
|
|
dir_util.copy_tree(pth_join('..', 'include'), include_path, dry_run=dry_run)
|
2020-07-31 08:17:36 +00:00
|
|
|
dir_util.copy_tree(pth_join('..', 'extlib', 'mimalloc', 'include'), include_path, dry_run=dry_run)
|
2019-10-26 03:33:02 +00:00
|
|
|
dir_util.mkpath(src_path)
|
2020-07-31 08:17:36 +00:00
|
|
|
dir_util.mkpath(mimalloc_src_path)
|
|
|
|
for path in (pth_join('..', 'src'), pth_join('..', 'extlib', 'mimalloc', 'src')):
|
|
|
|
for root, _, files in walk(path):
|
|
|
|
for f in files:
|
|
|
|
if not (f.endswith('.h') or f.endswith('.c')):
|
|
|
|
continue
|
|
|
|
f = pth_join(root, f)
|
|
|
|
f_new = f.replace('..', m_path)
|
|
|
|
if not isdir(dirname(f_new)):
|
|
|
|
dir_util.mkpath(dirname(f_new))
|
|
|
|
file_util.copy_file(f, f_new, dry_run=dry_run)
|
2019-09-27 06:18:22 +00:00
|
|
|
for f in sources[1:]:
|
2020-07-31 08:17:36 +00:00
|
|
|
file_util.copy_file(f.replace(m_path, '..'), f, dry_run=dry_run)
|
|
|
|
# Create an empty header
|
2019-10-26 03:33:02 +00:00
|
|
|
open(pth_join(platform_path, 'config.h'), 'a').close()
|
2019-09-27 06:18:22 +00:00
|
|
|
|
|
|
|
|
2019-05-29 06:34:49 +00:00
|
|
|
class Build(build_ext):
|
2019-10-06 09:49:53 +00:00
|
|
|
def build_extensions(self):
|
2019-10-06 10:08:46 +00:00
|
|
|
compiler = self.compiler.compiler_type
|
2020-07-31 06:33:54 +00:00
|
|
|
for e in self.extensions:
|
|
|
|
e.cython_directives = compiler_directives
|
|
|
|
if compiler in {'mingw32', 'unix'}:
|
2019-10-06 09:49:53 +00:00
|
|
|
e.define_macros = macros
|
|
|
|
e.extra_compile_args = compile_args
|
2020-07-07 06:55:03 +00:00
|
|
|
if compiler == 'mingw32':
|
|
|
|
e.extra_link_args = link_args
|
2020-07-31 06:33:54 +00:00
|
|
|
elif compiler == 'msvc':
|
2019-10-26 13:40:33 +00:00
|
|
|
e.define_macros = macros[1:]
|
2019-10-06 09:49:53 +00:00
|
|
|
e.libraries = ['shell32']
|
2020-07-07 06:55:03 +00:00
|
|
|
e.extra_compile_args = ['/O2']
|
2019-10-06 09:49:53 +00:00
|
|
|
super(Build, self).build_extensions()
|
|
|
|
|
2019-05-29 06:34:49 +00:00
|
|
|
def run(self):
|
2020-07-31 09:05:09 +00:00
|
|
|
has_src = isdir(include_path) and isdir(src_path) and isdir(extlib_path)
|
2019-09-27 01:19:43 +00:00
|
|
|
if not has_src:
|
2019-09-27 06:18:22 +00:00
|
|
|
copy_source(self.dry_run)
|
2019-05-29 06:34:49 +00:00
|
|
|
super(Build, self).run()
|
2019-09-27 01:19:43 +00:00
|
|
|
if not has_src:
|
|
|
|
dir_util.remove_tree(include_path, dry_run=self.dry_run)
|
|
|
|
dir_util.remove_tree(src_path, dry_run=self.dry_run)
|
2020-07-31 09:05:09 +00:00
|
|
|
dir_util.remove_tree(extlib_path, dry_run=self.dry_run)
|
2019-09-27 01:19:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PackSource(sdist):
|
|
|
|
def run(self):
|
2019-09-27 06:18:22 +00:00
|
|
|
copy_source(self.dry_run)
|
2019-09-27 01:19:43 +00:00
|
|
|
super(PackSource, self).run()
|
|
|
|
if not self.keep_temp:
|
|
|
|
dir_util.remove_tree(include_path, dry_run=self.dry_run)
|
|
|
|
dir_util.remove_tree(src_path, dry_run=self.dry_run)
|
2020-07-31 09:05:09 +00:00
|
|
|
dir_util.remove_tree(extlib_path, dry_run=self.dry_run)
|
2019-05-29 06:34:49 +00:00
|
|
|
|
2019-05-28 12:00:15 +00:00
|
|
|
|
|
|
|
setup(
|
|
|
|
name="python_solvespace",
|
2020-07-31 08:17:36 +00:00
|
|
|
version=find_version(m_path, '__init__.py'),
|
2019-05-28 12:00:15 +00:00
|
|
|
author=__author__,
|
|
|
|
author_email=__email__,
|
2019-09-27 13:21:21 +00:00
|
|
|
description="Python library of Solvespace.",
|
2019-05-28 12:00:15 +00:00
|
|
|
long_description=read("README.md"),
|
2019-09-23 13:24:08 +00:00
|
|
|
long_description_content_type='text/markdown',
|
2019-09-25 10:32:27 +00:00
|
|
|
url="https://github.com/KmolYuan/solvespace",
|
2019-05-28 12:00:15 +00:00
|
|
|
packages=find_packages(exclude=('tests',)),
|
2019-10-12 04:26:29 +00:00
|
|
|
package_data={'': ["*.pyi", "*.pxd"], 'python_solvespace': ['py.typed']},
|
2019-05-28 12:00:15 +00:00
|
|
|
ext_modules=[Extension(
|
2019-05-29 06:34:49 +00:00
|
|
|
"python_solvespace.slvs",
|
2019-05-28 12:00:15 +00:00
|
|
|
sources,
|
|
|
|
language="c++",
|
2020-07-31 08:17:36 +00:00
|
|
|
include_dirs=[include_path, src_path, mimalloc_include_path, mimalloc_src_path]
|
2019-05-28 12:00:15 +00:00
|
|
|
)],
|
2019-09-27 01:19:43 +00:00
|
|
|
cmdclass={'build_ext': Build, 'sdist': PackSource},
|
2019-09-27 06:18:22 +00:00
|
|
|
zip_safe=False,
|
2019-05-28 12:00:15 +00:00
|
|
|
python_requires=">=3.6",
|
2019-09-13 06:22:10 +00:00
|
|
|
install_requires=read('requirements.txt').splitlines(),
|
2019-10-12 04:26:29 +00:00
|
|
|
test_suite='tests',
|
2019-05-28 12:00:15 +00:00
|
|
|
classifiers=[
|
2019-09-29 11:37:14 +00:00
|
|
|
"Programming Language :: Python :: 3.6",
|
|
|
|
"Programming Language :: Python :: 3.7",
|
|
|
|
"Programming Language :: Python :: 3.8",
|
2019-05-28 12:00:15 +00:00
|
|
|
"Programming Language :: Cython",
|
|
|
|
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
|
|
|
|
"Operating System :: OS Independent",
|
|
|
|
]
|
|
|
|
)
|