Skip to content

Commit

Permalink
Do not use cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
bakatrouble committed Mar 29, 2019
1 parent 237f482 commit ab3845e
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 170 deletions.
1 change: 0 additions & 1 deletion 3rdparty/pybind11
Submodule pybind11 deleted from 25abf7
38 changes: 0 additions & 38 deletions CMakeLists.txt

This file was deleted.

5 changes: 2 additions & 3 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
include COPYING COPYING.lesser NOTICE CMakeLists.txt
recursive-include 3rdparty/pybind11 *
recursive-include src *.py *.pyi *.cpp *.h *.hpp *.cmake
include COPYING COPYING.lesser NOTICE pyproject.toml
recursive-include src *.py *.pyi *.cpp *.h *.hpp
8 changes: 4 additions & 4 deletions docs/guides/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ Installation

Requirements
------------
On Linux and macOS to install this library you must have ``cmake``, C++11 compatible compiler, Python headers and ``libtgvoip`` installed:
On Linux and macOS to install this library you must have a C++11 compatible compiler, Python headers and ``libtgvoip`` installed:

- Debian-based distributions

.. code-block:: bash
$ apt install cmake gcc g++ python3-dev
$ apt install gcc g++ python3-dev
- Archlinux-based distributions

.. code-block:: bash
$ pacman -S cmake gcc python3
$ pacman -S gcc python3
- macOS

.. code-block:: bash
$ brew install cmake gcc g++ python3
$ brew install gcc g++ python3
For instructions on building ``libtgvoip`` refer to the corresponding docs section: :ref:`libtgvoip`

Expand Down
6 changes: 3 additions & 3 deletions docs/guides/libtgvoip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ Requirements

.. code-block:: bash
$ apt install make autoconf automake gcc g++ openssl libssl-dev libopus0 libopus-dev
$ apt install make libtool autoconf automake gcc g++ openssl libssl-dev libopus0 libopus-dev
- Archlinux-based distributions

.. code-block:: bash
$ pacman -S make autoconf automake gcc openssl opus
$ pacman -S make libtool autoconf automake gcc openssl opus
- macOS

.. code-block:: bash
$ brew install make autoconf automake gcc g++ openssl opus
$ brew install make libtool autoconf automake gcc g++ openssl opus
Build and install
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools", "wheel", "pybind11>=2.2"]
137 changes: 84 additions & 53 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
# along with PytgVoIP. If not, see <http://www.gnu.org/licenses/>.


import multiprocessing
import os
import re
import sys
import platform
import subprocess

import setuptools
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.version import LooseVersion


def check_libraries():
Expand All @@ -37,64 +36,96 @@ def check_libraries():
match = re.findall(r'cannot find -l(\w+)', out)
if match:
raise RuntimeError(
'Following libraries are not installed: {}\nFor guide on installing libtgvoip refer '
'https://github.com/bakatrouble/pytgvoip/blob/master/docs/libtgvoip.md'.format(', '.join(match))
'libtgvoip was not found.\nFor guide on compiling it please refer to '
'https://pytgvoip.readthedocs.io/en/latest/guides/libtgvoip.html'
)


class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)


class CMakeBuild(build_ext):
def run(self):
class get_pybind_include(object):
"""Helper class to determine the pybind11 include path
The purpose of this class is to postpone importing pybind11
until it is actually installed, so that the ``get_include()``
method can be invoked. """

def __init__(self, user=False):
self.user = user

def __str__(self):
import pybind11
return pybind11.get_include(self.user)


ext_modules = [
Extension(
'_tgvoip',
[
'src/_tgvoip.cpp',
'src/_tgvoip_module.cpp',
],
include_dirs=[
# Path to pybind11 headers
get_pybind_include(),
get_pybind_include(user=True)
],
language='c++'
),
]


def has_flag(compiler, flagname):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f:
f.write('int main (int argc, char **argv) { return 0; }')
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))

if platform.system() == "Windows":
cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
if cmake_version < '3.1.0':
raise RuntimeError("CMake >= 3.1.0 is required on Windows")

if platform.system() != 'Windows':
compiler.compile([f.name], extra_postargs=[flagname])
except setuptools.distutils.errors.CompileError:
return False
return True


def cpp_flag(compiler):
"""Return the -std=c++[11/14] compiler flag.
The c++14 is prefered over c++11 (when it is available).
"""
if has_flag(compiler, '-std=c++14'):
return '-std=c++14'
elif has_flag(compiler, '-std=c++11'):
return '-std=c++11'
else:
raise RuntimeError('Unsupported compiler -- at least C++11 support is needed!')


class BuildExt(build_ext):
c_opts = {
'msvc': ['/EHsc'],
'unix': [],
}

if sys.platform == 'darwin':
c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']

def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
if ct == 'unix':
check_libraries()

opts.append('-DVERSION_INFO="{}"'.format(get_version()))
opts.append(cpp_flag(self.compiler))
if has_flag(self.compiler, '-fvisibility=hidden'):
opts.append('-fvisibility=hidden')
elif ct == 'msvc':
opts.append(r'/DVERSION_INFO=\"{}\"'.format(get_version()))
for ext in self.extensions:
self.build_extension(ext)

def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable]

cfg = 'Release'
build_args = ['--config', cfg]

if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m:{}'.format(multiprocessing.cpu_count() + 1)]
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
build_args += ['--', '-j{}'.format(multiprocessing.cpu_count() + 1)]

env = os.environ.copy()
env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
ext.extra_compile_args = opts
build_ext.build_extensions(self)


def get_version():
with open('src/tgvoip/__init__.py', encoding='utf-8') as f:
init_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'src', 'tgvoip', '__init__.py')
with open(init_path, encoding='utf-8') as f:
version = re.findall(r"__version__ = '(.+)'", f.read())[0]
if os.environ.get('BUILD') is None:
version += '.develop'
Expand Down Expand Up @@ -130,12 +161,12 @@ def get_data_files():
'Source': 'https://github.com/bakatrouble/pytgvoip',
},
python_required='~=3.4',
ext_modules=[CMakeExtension('_tgvoip')],
ext_modules=ext_modules,
packages=['tgvoip'],
package_dir={'tgvoip': os.path.join('src', 'tgvoip')},
package_data={'': [os.path.join('src', '_tgvoip.pyi')]},
data_files=get_data_files(),
cmdclass={'build_ext': CMakeBuild},
cmdclass={'build_ext': BuildExt},
zip_safe=False,
classifiers=[
'Development Status :: 3 - Alpha',
Expand Down
67 changes: 0 additions & 67 deletions src/FindTGVoIP.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion src/tgvoip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
from tgvoip.tgvoip import *
from tgvoip.tgvoip import __all__

__version__ = '0.0.2.1'
__version__ = '0.0.3'

0 comments on commit ab3845e

Please sign in to comment.