solvespace/cython/setup.py

179 lines
5.9 KiB
Python
Raw Normal View History

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
2020-10-07 02:25:50 +00:00
from os.path import dirname, isdir, join
2021-02-12 06:21:07 +00:00
from setuptools import setup, Extension
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'
2020-10-05 06:29:02 +00:00
include_path = join(m_path, 'include')
src_path = join(m_path, 'src')
platform_path = join(src_path, 'platform')
extlib_path = join(m_path, 'extlib')
mimalloc_path = join(extlib_path, 'mimalloc')
mimalloc_include_path = join(mimalloc_path, 'include')
mimalloc_src_path = join(mimalloc_path, 'src')
2020-10-07 02:25:50 +00:00
build_dir = 'build'
2019-05-28 12:00:15 +00:00
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',
2020-10-05 06:29:02 +00:00
'-std=c++17',
]
compile_args_msvc = [
'/O2',
'/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-10-05 06:29:02 +00:00
join(m_path, 'slvs.pyx'),
join(src_path, 'util.cpp'),
join(src_path, 'entity.cpp'),
join(src_path, 'expr.cpp'),
join(src_path, 'constraint.cpp'),
join(src_path, 'constrainteq.cpp'),
join(src_path, 'system.cpp'),
join(src_path, 'lib.cpp'),
join(platform_path, 'platform.cpp'),
]
mimalloc_sources = [
2020-07-31 08:17:36 +00:00
# MiMalloc
2020-10-05 06:29:02 +00:00
join(mimalloc_src_path, 'stats.c'),
join(mimalloc_src_path, 'random.c'),
join(mimalloc_src_path, 'os.c'),
2021-02-12 06:21:07 +00:00
join(mimalloc_src_path, 'bitmap.c'),
2020-10-05 06:29:02 +00:00
join(mimalloc_src_path, 'arena.c'),
2021-02-12 06:21:07 +00:00
join(mimalloc_src_path, 'segment-cache.c'),
2020-10-05 06:29:02 +00:00
join(mimalloc_src_path, 'segment.c'),
join(mimalloc_src_path, 'page.c'),
join(mimalloc_src_path, 'alloc.c'),
join(mimalloc_src_path, 'alloc-aligned.c'),
join(mimalloc_src_path, 'alloc-posix.c'),
join(mimalloc_src_path, 'heap.c'),
join(mimalloc_src_path, 'options.c'),
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-10-05 06:29:02 +00:00
sources.append(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):
2020-10-05 06:29:02 +00:00
dir_util.copy_tree(join('..', 'include'), include_path, dry_run=dry_run)
dir_util.copy_tree(join('..', 'extlib', 'mimalloc', 'include'),
mimalloc_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)
2020-10-05 06:29:02 +00:00
for path in (join('..', 'src'), join('..', 'extlib', 'mimalloc', 'src')):
2020-07-31 08:17:36 +00:00
for root, _, files in walk(path):
for f in files:
if not (f.endswith('.h') or f.endswith('.c')):
continue
2020-10-05 06:29:02 +00:00
f = join(root, f)
2020-07-31 08:17:36 +00:00
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)
2020-10-05 06:29:02 +00:00
for f in sources[1:] + mimalloc_sources:
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
2020-10-05 06:29:02 +00:00
open(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):
2021-02-12 06:21:07 +00:00
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
2020-10-05 06:29:02 +00:00
e.libraries = ['mimalloc']
e.library_dirs = [build_dir]
2020-07-31 06:33:54 +00:00
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:]
2020-10-05 06:29:02 +00:00
e.libraries.extend(['shell32', 'advapi32', 'Ws2_32'])
e.extra_compile_args = compile_args_msvc
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)
2020-10-05 06:29:02 +00:00
# Pre-build MiMalloc
if compiler in {'mingw32', 'unix'}:
args = ['-fPIC']
else:
args = []
objects = self.compiler.compile(
mimalloc_sources,
extra_postargs=args,
include_dirs=[mimalloc_include_path, mimalloc_src_path]
)
2020-10-07 02:25:50 +00:00
dir_util.mkpath(build_dir)
2020-10-05 06:29:02 +00:00
self.compiler.create_static_lib(objects, 'mimalloc', target_lang='c',
output_dir=build_dir)
super(Build, self).build_extensions()
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):
2021-02-12 06:21:07 +00:00
2019-09-27 01:19:43 +00:00
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
2021-02-12 06:21:07 +00:00
setup(ext_modules=[Extension(
"python_solvespace.slvs",
sources,
language="c++",
include_dirs=[include_path, src_path, mimalloc_include_path,
mimalloc_src_path]
)], cmdclass={'build_ext': Build, 'sdist': PackSource})