Skip to content
This repository has been archived by the owner on Feb 15, 2025. It is now read-only.

Commit

Permalink
Revert "Do not use cmake"
Browse files Browse the repository at this point in the history
This reverts commit ab3845e
  • Loading branch information
bakatrouble committed Mar 29, 2019
1 parent 1ec8a6d commit b51afbe
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 96 deletions.
1 change: 1 addition & 0 deletions 3rdparty/pybind11
Submodule pybind11 added at 25abf7
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# PytgVoIP - Telegram VoIP Library for Python
# Copyright (C) 2019 bakatrouble <https://github.com/bakatrouble>
#
# This file is part of PytgVoIP.
#
# PytgVoIP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PytgVoIP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PytgVoIP. If not, see <http://www.gnu.org/licenses/>.


cmake_minimum_required(VERSION 2.8.12)
project(pylibtgvoip)
set(CMAKE_CXX_STANDARD 14)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src")

find_package(TGVoIP)

add_definitions(-DTGVOIP_USE_CALLBACK_AUDIO_IO -DWEBRTC_APM_DEBUG_DUMP=0 -DWEBRTC_NS_FLOAT -DTGVOIP_USE_DESKTOP_DSP)
add_subdirectory(3rdparty/pybind11)
include_directories(${TGVOIP_INCLUDE_DIR})

list(APPEND SOURCES
src/_tgvoip.cpp
src/_tgvoip_module.cpp
)

link_libraries(${TGVOIP_LIBRARY})
pybind11_add_module(_tgvoip ${SOURCES})
5 changes: 3 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include COPYING COPYING.lesser NOTICE pyproject.toml
recursive-include src *.py *.pyi *.cpp *.h *.hpp
include COPYING COPYING.lesser NOTICE CMakeLists.txt
recursive-include 3rdparty/pybind11 *
recursive-include src *.py *.pyi *.cpp *.h *.hpp *.cmake
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 a C++11 compatible compiler, Python headers and ``libtgvoip`` installed:
On Linux and macOS to install this library you must have ``cmake``, C++11 compatible compiler, Python headers and ``libtgvoip`` installed:

- Debian-based distributions

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

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

.. code-block:: bash
$ brew install gcc g++ python3
$ brew install cmake 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 libtool autoconf automake gcc g++ openssl libssl-dev libopus0 libopus-dev
$ apt install make autoconf automake gcc g++ openssl libssl-dev libopus0 libopus-dev
- Archlinux-based distributions

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

.. code-block:: bash
$ brew install make libtool autoconf automake gcc g++ openssl opus
$ brew install make autoconf automake gcc g++ openssl opus
Build and install
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml

This file was deleted.

137 changes: 53 additions & 84 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
# 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 @@ -36,96 +37,64 @@ def check_libraries():
match = re.findall(r'cannot find -l(\w+)', out)
if match:
raise RuntimeError(
'libtgvoip was not found.\nFor guide on compiling it please refer to '
'https://pytgvoip.readthedocs.io/en/latest/guides/libtgvoip.html'
'Following libraries are not installed: {}\nFor guide on installing libtgvoip refer '
'https://github.com/bakatrouble/pytgvoip/blob/master/docs/libtgvoip.md'.format(', '.join(match))
)


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; }')
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):
try:
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':
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':
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:
ext.extra_compile_args = opts
build_ext.build_extensions(self)
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)


def get_version():
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:
with open('src/tgvoip/__init__.py', 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 @@ -161,12 +130,12 @@ def get_data_files():
'Source': 'https://github.com/bakatrouble/pytgvoip',
},
python_required='~=3.4',
ext_modules=ext_modules,
ext_modules=[CMakeExtension('_tgvoip')],
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': BuildExt},
cmdclass={'build_ext': CMakeBuild},
zip_safe=False,
classifiers=[
'Development Status :: 3 - Alpha',
Expand Down
67 changes: 67 additions & 0 deletions src/FindTGVoIP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# PytgVoIP - Telegram VoIP Library for Python
# Copyright (C) 2019 bakatrouble <https://github.com/bakatrouble>
#
# This file is part of PytgVoIP.
#
# PytgVoIP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PytgVoIP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PytgVoIP. If not, see <http://www.gnu.org/licenses/>.


# - Find tgvoip
# Find the native tgvoip includes and libraries
#
# TGVOIP_INCLUDE_DIR - where to find VoIPController.h, etc.
# TGVOIP_LIBRARIES - List of libraries when using tgvoip.
# TGVOIP_FOUND - True if tgvoip found.

if(TGVOIP_INCLUDE_DIR)
set(TGVOIP_FIND_QUIETLY TRUE)
endif(TGVOIP_INCLUDE_DIR)

set(FIND_TGVOIP_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt
$ENV{TGVOIP_LIBRARY_ROOT}
$ENV{TGVOIP_INCLUDE_ROOT}
)

find_path(TGVOIP_INCLUDE_DIR
VoIPController.h
PATH_SUFFIXES tgvoip libtgvoip
PATHS ${FIND_TGVOIP_PATHS}
)
find_library(TGVOIP_LIBRARY
NAMES tgvoip tgvoip_static libtgvoip libtgvoip_static libtgvoip.dll
PATHS ${FIND_TGVOIP_PATHS}
)

message(STATUS ${TGVOIP_INCLUDE_DIR})
message(STATUS ${TGVOIP_LIBRARY})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(TGVOIP DEFAULT_MSG TGVOIP_INCLUDE_DIR TGVOIP_LIBRARY)

if(TGVOIP_FOUND)
set(TGVOIP_LIBRARIES ${TGVOIP_LIBRARY})
else(TGVOIP_FOUND)
set(TGVOIP_LIBRARIES)
endif(TGVOIP_FOUND)

mark_as_advanced(TGVOIP_INCLUDE_DIR)
mark_as_advanced(TGVOIP_LIBRARY)
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.3'
__version__ = '0.0.2.1'

0 comments on commit b51afbe

Please sign in to comment.