Skip to content

Commit

Permalink
Allow using pre-installed dependencies (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdenCullen authored Jan 2, 2019
1 parent a66021b commit 6c7032b
Showing 1 changed file with 39 additions and 71 deletions.
110 changes: 39 additions & 71 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import platform
from os import path
import sys

def is_64bit():
if sys.maxsize > 2**32:
return True
Expand All @@ -22,9 +22,9 @@ def determine_cross_compile_string():
host_arch = platform.machine()
if (host_arch == 'AMD64' or host_arch == 'x86_64') and is_32bit() and sys.platform != 'win32':
return 'CMAKE_C_FLAGS=-m32'
return ''

return ''

def determine_generator_string():
if sys.platform == 'win32':
vs_version = None
Expand All @@ -48,115 +48,84 @@ def determine_generator_string():
vswhere_output = subprocess.check_output(vswhere_args, shell=True)
except CalledProcessError as ex:
print('No version of MSVC compiler could be found!')
exit(1)
exit(1)

if vswhere_output != None:
for out in vswhere_output.split():
vs_version = out.decode('utf-8')
else:
print('No MSVC compiler could be found!')
exit(1)

vs_major_version = vs_version.split('.')[0]

cmake_list_gen_args = ['cmake', '--help']
cmake_help_output = subprocess.check_output(cmake_list_gen_args)

vs_version_gen_str = None
for out in cmake_help_output.splitlines():
trimmed_out = out.decode('utf-8').strip()
if 'Visual Studio' in trimmed_out and vs_major_version in trimmed_out:
print('selecting generator {}'.format(trimmed_out))
vs_version_gen_str = trimmed_out.split('[')[0].strip()
vs_version_gen_str = trimmed_out.split('[')[0].strip()
break

if vs_version_gen_str == None:
print('CMake does not recognize an installed version of visual studio on your system.')
exit(1)

if is_64bit():
print('64bit version of python detected, using win64 builds')
vs_version_gen_str = vs_version_gen_str + ' Win64'

vs_version_gen_str = '-G' + vs_version_gen_str
print('Succesfully determined generator as \"{}\"'.format(vs_version_gen_str))
vs_version_gen_str = '-G' + vs_version_gen_str
print('Succesfully determined generator as \"{}\"'.format(vs_version_gen_str))
return vs_version_gen_str
return ''
return ''

generator_string = determine_generator_string()
cross_compile_string = determine_cross_compile_string()
current_dir = os.path.dirname(os.path.realpath(__file__))
shell = sys.platform.startswith('win')

build_dir = os.path.join(current_dir, 'deps_build')

if not os.path.exists(build_dir):
os.mkdir(build_dir)

os.chdir(build_dir)

dep_install_path = os.path.join(os.getcwd(), 'install')

dep_install_path = os.path.join(build_dir, 'install')
if 'AWS_C_INSTALL' in os.environ:
dep_install_path = os.getenv('AWS_C_INSTALL')

common_dir = os.path.join(current_dir, 'aws-c-common')
s2n_dir = os.path.join(current_dir, 's2n')
io_dir = os.path.join(current_dir, 'aws-c-io')
mqtt_dir = os.path.join(current_dir, 'aws-c-mqtt')
common_cmake_args = ['cmake', generator_string, cross_compile_string, '-DVERSION_LIBS=OFF', '-DCMAKE_INSTALL_PREFIX={}'.format(dep_install_path), '-DBUILD_SHARED_LIBS=OFF', common_dir]
s2n_cmake_args = ['cmake', generator_string, cross_compile_string, '-DCMAKE_INSTALL_PREFIX={}'.format(dep_install_path), '-DBUILD_SHARED_LIBS=OFF', s2n_dir]
io_cmake_args = ['cmake', generator_string, cross_compile_string, '-DVERSION_LIBS=OFF', '-DCMAKE_INSTALL_PREFIX={}'.format(dep_install_path), '-DBUILD_SHARED_LIBS=OFF', io_dir]
mqtt_cmake_args = ['cmake', generator_string, cross_compile_string, '-DVERSION_LIBS=OFF', '-DCMAKE_INSTALL_PREFIX={}'.format(dep_install_path), '-DBUILD_SHARED_LIBS=OFF', mqtt_dir]
build_cmd = ['cmake', '--build', './', '--config', 'release', '--target', 'install']

common_build_dir = os.path.join(build_dir, 'aws-c-common')
s2n_build_dir = os.path.join(build_dir, 's2n')
io_build_dir = os.path.join(build_dir, 'aws-c-io')
mqtt_build_dir = os.path.join(build_dir, 'aws-c-mqtt')

if sys.platform.startswith('win'):
shell = True
else:
shell = False

if not os.path.exists(common_build_dir):
os.mkdir(common_build_dir)
os.chdir(common_build_dir)

ret_code = subprocess.check_call(common_cmake_args, stderr=subprocess.STDOUT, shell=shell)
ret_code = subprocess.check_call(build_cmd, stderr=subprocess.STDOUT, shell=shell)
def build_dependency(lib_name, pass_dversion_libs=True):
lib_source_dir = os.path.join(current_dir, lib_name)

os.chdir(build_dir)
# Skip library if it wasn't pulled
if not os.path.exists(os.path.join(lib_source_dir, 'CMakeLists.txt')):
return

if not os.path.exists(s2n_build_dir):
os.mkdir(s2n_build_dir)
lib_build_dir = os.path.join(build_dir, lib_name)
if not os.path.exists(lib_build_dir):
os.mkdir(lib_build_dir)
os.chdir(lib_build_dir)

os.chdir(s2n_build_dir)
cmake_args = ['cmake', generator_string, cross_compile_string, '-DCMAKE_INSTALL_PREFIX={}'.format(dep_install_path), '-DBUILD_SHARED_LIBS=OFF']
if pass_dversion_libs:
cmake_args.append('-DVERSION_LIBS=OFF')
cmake_args.append(lib_source_dir)
build_cmd = ['cmake', '--build', './', '--config', 'release', '--target', 'install']

if sys.platform != 'darwin' and sys.platform != 'win32':
ret_code = subprocess.check_call(s2n_cmake_args, stderr=subprocess.STDOUT, shell=shell)
ret_code = subprocess.check_call(cmake_args, stderr=subprocess.STDOUT, shell=shell)
ret_code = subprocess.check_call(build_cmd, stderr=subprocess.STDOUT, shell=shell)

os.chdir(build_dir)

if not os.path.exists(io_build_dir):
os.mkdir(io_build_dir)
os.chdir(build_dir)

os.chdir(io_build_dir)
if sys.platform != 'darwin' and sys.platform != 'win32':
build_dependency('s2n', pass_dversion_libs=False)
build_dependency('aws-c-common')
build_dependency('aws-c-io')
build_dependency('aws-c-mqtt')

ret_code = subprocess.check_call(io_cmake_args, stderr=subprocess.STDOUT, shell=shell)
ret_code = subprocess.check_call(build_cmd, stderr=subprocess.STDOUT, shell=shell)

os.chdir(build_dir)

if not os.path.exists(mqtt_build_dir):
os.mkdir(mqtt_build_dir)

os.chdir(mqtt_build_dir)

ret_code = subprocess.check_call(mqtt_cmake_args, stderr=subprocess.STDOUT, shell=shell)
ret_code = subprocess.check_call(build_cmd, stderr=subprocess.STDOUT, shell=shell)

os.chdir(current_dir)

from distutils.ccompiler import get_default_compiler
Expand Down Expand Up @@ -192,8 +161,7 @@ def determine_generator_string():
ldflags += ['-framework Security']
include_dirs = ['/usr/local/include'] + include_dirs
library_dirs = ['/usr/local/lib'] + library_dirs
extra_objects = [
'{}/lib/lib{}.a'.format(dep_install_path, lib) for lib in aws_c_libs]
extra_objects = ['{}/lib/lib{}.a'.format(dep_install_path, lib) for lib in aws_c_libs]
else:
include_dirs = ['/usr/local/include'] + include_dirs
library_dirs = ['/usr/local/lib'] + library_dirs
Expand Down Expand Up @@ -246,4 +214,4 @@ def determine_generator_string():
],
ext_modules = [_aws_crt_python],
)

0 comments on commit 6c7032b

Please sign in to comment.