discord-rpc/build.py

126 lines
3.6 KiB
Python
Raw Normal View History

2017-09-14 17:59:32 +02:00
#!/usr/bin/env python
import os
import subprocess
import sys
import shutil
import zipfile
from contextlib import contextmanager
2017-11-23 01:12:37 +01:00
import click
SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
2017-11-23 01:12:37 +01:00
def platform():
""" a name for the platform """
if sys.platform.startswith('win'):
return 'win'
elif sys.platform == 'darwin':
return 'osx'
elif sys.platform.startswith('linux'):
return 'linux'
raise Exception('Unsupported platform ' + sys.platform)
@contextmanager
def cd(new_dir):
""" Temporarily change current directory """
if new_dir:
old_dir = os.getcwd()
os.chdir(new_dir)
yield
if new_dir:
os.chdir(old_dir)
def mkdir_p(path):
""" mkdir -p """
if not os.path.isdir(path):
2017-11-23 01:12:37 +01:00
click.secho('Making ' + path, fg='yellow')
os.makedirs(path)
2017-11-23 01:12:37 +01:00
@click.group(invoke_without_command=True)
@click.pass_context
@click.option('--clean', is_flag=True)
def cli(ctx, clean):
if ctx.invoked_subcommand is None:
ctx.invoke(libs, clean=clean)
ctx.invoke(archive)
@cli.command()
def unity():
pass
@cli.command()
def unreal():
pass
def build_lib(build_name, generator, options):
2017-11-23 01:12:37 +01:00
""" Create a dir under builds, run build and install in it """
build_path = os.path.join(SCRIPT_PATH, 'builds', build_name)
install_path = os.path.join(SCRIPT_PATH, 'builds', 'install', build_name)
mkdir_p(build_path)
mkdir_p(install_path)
with cd(build_path):
2017-11-23 01:12:37 +01:00
initial_cmake = [
'cmake',
SCRIPT_PATH,
'-DCMAKE_INSTALL_PREFIX=%s' % os.path.join('..', 'install', build_name)
]
if generator:
initial_cmake.extend(['-G', generator])
for key in options:
val = 'ON' if options[key] else 'OFF'
2017-11-23 01:12:37 +01:00
initial_cmake.append('-D%s=%s' % (key, val))
subprocess.check_call(initial_cmake)
subprocess.check_call(['cmake', '--build', '.', '--config', 'Debug'])
subprocess.check_call(['cmake', '--build', '.', '--config', 'Release', '--target', 'install'])
2017-11-23 01:12:37 +01:00
@cli.command()
def archive():
archive_file_path = os.path.join(SCRIPT_PATH, 'builds', 'discord-rpc-%s.zip' % platform())
archive_file = zipfile.ZipFile(archive_file_path, 'w', zipfile.ZIP_DEFLATED)
archive_src_base_path = os.path.join(SCRIPT_PATH, 'builds', 'install')
archive_dst_base_path = 'discord-rpc'
with cd(archive_src_base_path):
for path, subdirs, filenames in os.walk('.'):
for fname in filenames:
fpath = os.path.join(path, fname)
archive_file.write(fpath, os.path.normpath(os.path.join(archive_dst_base_path, fpath)))
2017-11-23 01:12:37 +01:00
@cli.command()
@click.option('--clean', is_flag=True)
2017-11-23 01:12:37 +01:00
def libs(clean):
os.chdir(SCRIPT_PATH)
if clean:
shutil.rmtree('builds', ignore_errors=True)
2017-09-14 17:59:32 +02:00
mkdir_p('builds')
2017-11-23 01:12:37 +01:00
plat = platform()
if plat == 'win':
generator32 = 'Visual Studio 14 2015'
generator64 = 'Visual Studio 14 2015 Win64'
build_lib('win32-static', generator32, {})
2017-11-13 18:22:45 +01:00
build_lib('win32-dynamic', generator32, {'BUILD_SHARED_LIBS': True, 'USE_STATIC_CRT': True})
build_lib('win64-static', generator64, {})
2017-11-13 18:22:45 +01:00
build_lib('win64-dynamic', generator64, {'BUILD_SHARED_LIBS': True, 'USE_STATIC_CRT': True})
2017-11-23 01:12:37 +01:00
elif plat == 'osx':
2017-09-14 17:59:32 +02:00
build_lib('osx-static', None, {})
build_lib('osx-dynamic', None, {'BUILD_SHARED_LIBS': True})
2017-11-23 01:12:37 +01:00
elif plat == 'linux':
build_lib('linux-static', None, {})
build_lib('linux-dynamic', None, {'BUILD_SHARED_LIBS': True})
if __name__ == '__main__':
2017-11-23 01:12:37 +01:00
sys.exit(cli())