2016-11-21 07:55:53 +03:00
|
|
|
#!/usr/bin/env python3
|
2016-10-31 12:15:49 +03:00
|
|
|
# vim:fileencoding=utf-8
|
|
|
|
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
|
|
|
2017-11-14 07:01:23 +03:00
|
|
|
import argparse
|
2018-03-12 05:20:54 +03:00
|
|
|
import glob
|
2017-11-14 07:01:23 +03:00
|
|
|
import json
|
2016-10-31 12:15:49 +03:00
|
|
|
import os
|
|
|
|
import re
|
2019-03-03 04:54:05 +03:00
|
|
|
import runpy
|
2016-10-31 12:15:49 +03:00
|
|
|
import shlex
|
2017-01-08 09:47:44 +03:00
|
|
|
import shutil
|
2016-10-31 12:15:49 +03:00
|
|
|
import subprocess
|
2017-11-14 07:01:23 +03:00
|
|
|
import sys
|
|
|
|
import sysconfig
|
2020-12-04 16:07:19 +03:00
|
|
|
import platform
|
2018-01-10 04:28:57 +03:00
|
|
|
import time
|
2019-06-28 09:30:12 +03:00
|
|
|
from contextlib import suppress
|
2020-03-06 04:37:18 +03:00
|
|
|
from functools import partial
|
2019-06-28 09:30:12 +03:00
|
|
|
from pathlib import Path
|
2020-03-06 05:29:55 +03:00
|
|
|
from typing import (
|
2020-03-14 12:07:11 +03:00
|
|
|
Callable, Dict, Iterable, Iterator, List, NamedTuple, Optional,
|
|
|
|
Sequence, Set, Tuple, Union
|
2020-03-06 05:29:55 +03:00
|
|
|
)
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
from glfw import glfw # noqa
|
|
|
|
|
2020-03-05 16:01:20 +03:00
|
|
|
if sys.version_info[:2] < (3, 6):
|
|
|
|
raise SystemExit('kitty requires python >= 3.6')
|
2016-10-31 12:15:49 +03:00
|
|
|
base = os.path.dirname(os.path.abspath(__file__))
|
2020-03-06 05:53:01 +03:00
|
|
|
sys.path.insert(0, base)
|
2017-11-19 20:54:36 +03:00
|
|
|
del sys.path[0]
|
2020-03-06 05:53:01 +03:00
|
|
|
|
|
|
|
verbose = False
|
2019-07-05 16:43:34 +03:00
|
|
|
build_dir = 'build'
|
2019-07-11 17:49:22 +03:00
|
|
|
constants = os.path.join('kitty', 'constants.py')
|
2016-10-31 12:15:49 +03:00
|
|
|
with open(constants, 'rb') as f:
|
|
|
|
constants = f.read().decode('utf-8')
|
2020-03-15 11:00:02 +03:00
|
|
|
appname = re.search(r"^appname: str = '([^']+)'", constants, re.MULTILINE).group(1) # type: ignore
|
2017-02-09 21:34:05 +03:00
|
|
|
version = tuple(
|
|
|
|
map(
|
|
|
|
int,
|
2020-03-05 15:53:45 +03:00
|
|
|
re.search( # type: ignore
|
2020-03-15 11:00:02 +03:00
|
|
|
r"^version: Version = Version\((\d+), (\d+), (\d+)\)", constants, re.MULTILINE
|
2020-03-05 15:53:45 +03:00
|
|
|
).group(1, 2, 3)
|
2017-02-09 21:34:05 +03:00
|
|
|
)
|
|
|
|
)
|
2017-01-07 10:31:32 +03:00
|
|
|
_plat = sys.platform.lower()
|
2017-11-21 03:49:46 +03:00
|
|
|
is_macos = 'darwin' in _plat
|
2020-05-21 09:13:55 +03:00
|
|
|
is_openbsd = 'openbsd' in _plat
|
2020-12-31 07:56:15 +03:00
|
|
|
is_freebsd = 'freebsd' in _plat
|
|
|
|
is_netbsd = 'netbsd' in _plat
|
|
|
|
is_dragonflybsd = 'dragonfly' in _plat
|
|
|
|
is_bsd = is_freebsd or is_netbsd or is_dragonflybsd or is_openbsd
|
2021-01-15 08:09:24 +03:00
|
|
|
is_arm = platform.processor() == 'arm' or platform.machine() == 'arm64'
|
2020-03-14 12:07:11 +03:00
|
|
|
Env = glfw.Env
|
|
|
|
env = Env()
|
|
|
|
PKGCONFIG = os.environ.get('PKGCONFIG_EXE', 'pkg-config')
|
2020-03-06 04:37:18 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
class Options(argparse.Namespace):
|
|
|
|
action: str = 'build'
|
|
|
|
debug: bool = False
|
|
|
|
verbose: int = 0
|
|
|
|
sanitize: bool = False
|
|
|
|
prefix: str = './linux-package'
|
|
|
|
incremental: bool = True
|
|
|
|
profile: bool = False
|
|
|
|
for_freeze: bool = False
|
|
|
|
libdir_name: str = 'lib'
|
|
|
|
extra_logging: List[str] = []
|
2020-10-28 21:24:01 +03:00
|
|
|
link_time_optimization: bool = 'KITTY_NO_LTO' not in os.environ
|
2020-03-14 12:07:11 +03:00
|
|
|
update_check_interval: float = 24
|
2020-10-28 21:24:01 +03:00
|
|
|
egl_library: Optional[str] = os.getenv('KITTY_EGL_LIBRARY')
|
|
|
|
startup_notification_library: Optional[str] = os.getenv('KITTY_STARTUP_NOTIFICATION_LIBRARY')
|
|
|
|
canberra_library: Optional[str] = os.getenv('KITTY_CANBERRA_LIBRARY')
|
2020-03-06 04:37:18 +03:00
|
|
|
|
2016-10-31 12:15:49 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
class CompileKey(NamedTuple):
|
|
|
|
src: str
|
|
|
|
dest: str
|
2020-03-06 04:37:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Command(NamedTuple):
|
|
|
|
desc: str
|
|
|
|
cmd: Sequence[str]
|
|
|
|
is_newer_func: Callable[[], bool]
|
|
|
|
on_success: Callable[[], None]
|
|
|
|
key: Optional[CompileKey]
|
|
|
|
keyfile: Optional[str]
|
2016-10-31 12:15:49 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def emphasis(text: str) -> str:
|
2017-11-21 05:02:48 +03:00
|
|
|
if sys.stdout.isatty():
|
|
|
|
text = '\033[32m' + text + '\033[39m'
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def error(text: str) -> str:
|
2017-11-21 05:02:48 +03:00
|
|
|
if sys.stdout.isatty():
|
|
|
|
text = '\033[91m' + text + '\033[39m'
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def pkg_config(pkg: str, *args: str) -> List[str]:
|
2017-11-21 02:28:35 +03:00
|
|
|
try:
|
|
|
|
return list(
|
|
|
|
filter(
|
|
|
|
None,
|
|
|
|
shlex.split(
|
|
|
|
subprocess.check_output([PKGCONFIG, pkg] + list(args))
|
|
|
|
.decode('utf-8')
|
|
|
|
)
|
2017-02-09 21:34:05 +03:00
|
|
|
)
|
|
|
|
)
|
2017-11-21 02:28:35 +03:00
|
|
|
except subprocess.CalledProcessError:
|
2017-11-21 05:02:48 +03:00
|
|
|
raise SystemExit('The package {} was not found on your system'.format(error(pkg)))
|
2016-11-02 18:57:20 +03:00
|
|
|
|
|
|
|
|
2021-01-17 04:52:21 +03:00
|
|
|
def pkg_version(package: str) -> Optional[Tuple[int, int]]:
|
|
|
|
ver = subprocess.check_output([
|
|
|
|
PKGCONFIG, package, '--modversion']).decode('utf-8').strip()
|
|
|
|
m = re.match(r'(\d+).(\d+)', ver)
|
|
|
|
if m is not None:
|
|
|
|
qmajor, qminor = map(int, m.groups())
|
|
|
|
return qmajor, qminor
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def at_least_version(package: str, major: int, minor: int = 0) -> None:
|
2017-11-05 06:52:15 +03:00
|
|
|
q = '{}.{}'.format(major, minor)
|
|
|
|
if subprocess.run([PKGCONFIG, package, '--atleast-version=' + q]
|
|
|
|
).returncode != 0:
|
2020-03-06 04:37:18 +03:00
|
|
|
qmajor = qminor = 0
|
2017-11-05 06:52:15 +03:00
|
|
|
try:
|
|
|
|
ver = subprocess.check_output([PKGCONFIG, package, '--modversion']
|
|
|
|
).decode('utf-8').strip()
|
2020-03-06 04:37:18 +03:00
|
|
|
m = re.match(r'(\d+).(\d+)', ver)
|
|
|
|
if m is not None:
|
|
|
|
qmajor, qminor = map(int, m.groups())
|
2017-11-05 06:52:15 +03:00
|
|
|
except Exception:
|
|
|
|
ver = 'not found'
|
|
|
|
if qmajor < major or (qmajor == major and qminor < minor):
|
|
|
|
raise SystemExit(
|
|
|
|
'{} >= {}.{} is required, found version: {}'.format(
|
2017-11-21 05:02:48 +03:00
|
|
|
error(package), major, minor, ver
|
2017-11-05 06:52:15 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-06 04:37:18 +03:00
|
|
|
def cc_version() -> Tuple[str, Tuple[int, int]]:
|
2019-01-30 06:35:42 +03:00
|
|
|
if 'CC' in os.environ:
|
|
|
|
cc = os.environ['CC']
|
|
|
|
else:
|
|
|
|
if is_macos:
|
|
|
|
cc = 'clang'
|
|
|
|
else:
|
|
|
|
if shutil.which('gcc'):
|
|
|
|
cc = 'gcc'
|
|
|
|
elif shutil.which('clang'):
|
|
|
|
cc = 'clang'
|
|
|
|
else:
|
|
|
|
cc = 'cc'
|
2016-11-12 06:23:27 +03:00
|
|
|
raw = subprocess.check_output([cc, '-dumpversion']).decode('utf-8')
|
2020-03-24 16:03:32 +03:00
|
|
|
ver_ = raw.strip().split('.')[:2]
|
2016-11-12 06:23:27 +03:00
|
|
|
try:
|
2020-03-24 16:03:32 +03:00
|
|
|
if len(ver_) == 1:
|
|
|
|
ver = int(ver_[0]), 0
|
|
|
|
else:
|
|
|
|
ver = int(ver_[0]), int(ver_[1])
|
2016-11-12 06:23:27 +03:00
|
|
|
except Exception:
|
|
|
|
ver = (0, 0)
|
2017-05-15 07:48:59 +03:00
|
|
|
return cc, ver
|
2016-11-12 06:23:27 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def get_python_include_paths() -> List[str]:
|
2019-05-19 06:47:52 +03:00
|
|
|
ans = []
|
|
|
|
for name in sysconfig.get_path_names():
|
|
|
|
if 'include' in name:
|
|
|
|
ans.append(name)
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def gp(x: str) -> Optional[str]:
|
|
|
|
return sysconfig.get_path(x)
|
|
|
|
|
|
|
|
return sorted(frozenset(filter(None, map(gp, sorted(ans)))))
|
2019-05-19 06:47:52 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
|
|
|
|
def get_python_flags(cflags: List[str]) -> List[str]:
|
2019-05-19 06:47:52 +03:00
|
|
|
cflags.extend('-I' + x for x in get_python_include_paths())
|
2020-03-06 04:37:18 +03:00
|
|
|
libs: List[str] = []
|
|
|
|
libs += (sysconfig.get_config_var('LIBS') or '').split()
|
|
|
|
libs += (sysconfig.get_config_var('SYSLIBS') or '').split()
|
2017-01-10 07:25:44 +03:00
|
|
|
fw = sysconfig.get_config_var('PYTHONFRAMEWORK')
|
|
|
|
if fw:
|
2017-01-10 10:36:48 +03:00
|
|
|
for var in 'data include stdlib'.split():
|
2017-01-10 07:15:33 +03:00
|
|
|
val = sysconfig.get_path(var)
|
2017-01-10 07:25:44 +03:00
|
|
|
if val and '/{}.framework'.format(fw) in val:
|
2017-01-10 10:15:33 +03:00
|
|
|
fdir = val[:val.index('/{}.framework'.format(fw))]
|
2017-02-09 21:34:05 +03:00
|
|
|
if os.path.isdir(
|
|
|
|
os.path.join(fdir, '{}.framework'.format(fw))
|
|
|
|
):
|
2017-01-20 20:08:58 +03:00
|
|
|
framework_dir = fdir
|
2017-01-10 10:23:23 +03:00
|
|
|
break
|
2017-01-20 20:08:58 +03:00
|
|
|
else:
|
|
|
|
raise SystemExit('Failed to find Python framework')
|
2020-03-06 04:37:18 +03:00
|
|
|
ldlib = sysconfig.get_config_var('LDLIBRARY')
|
|
|
|
if ldlib:
|
|
|
|
libs.append(os.path.join(framework_dir, ldlib))
|
2017-01-10 07:15:33 +03:00
|
|
|
else:
|
2020-03-06 04:37:18 +03:00
|
|
|
ldlib = sysconfig.get_config_var('LIBDIR')
|
|
|
|
if ldlib:
|
|
|
|
libs += ['-L' + ldlib]
|
|
|
|
ldlib = sysconfig.get_config_var('VERSION')
|
|
|
|
if ldlib:
|
|
|
|
libs += ['-lpython' + ldlib + sys.abiflags]
|
|
|
|
libs += (sysconfig.get_config_var('LINKFORSHARED') or '').split()
|
2017-01-10 07:15:33 +03:00
|
|
|
return libs
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def get_sanitize_args(cc: str, ccver: Tuple[int, int]) -> List[str]:
|
2017-11-01 12:05:57 +03:00
|
|
|
sanitize_args = ['-fsanitize=address']
|
2017-11-08 15:20:32 +03:00
|
|
|
if ccver >= (5, 0):
|
2017-11-01 12:05:57 +03:00
|
|
|
sanitize_args.append('-fsanitize=undefined')
|
2017-05-15 07:48:59 +03:00
|
|
|
# if cc == 'gcc' or (cc == 'clang' and ccver >= (4, 2)):
|
2017-11-01 12:05:57 +03:00
|
|
|
# sanitize_args.append('-fno-sanitize-recover=all')
|
|
|
|
sanitize_args.append('-fno-omit-frame-pointer')
|
2017-05-15 07:48:59 +03:00
|
|
|
return sanitize_args
|
|
|
|
|
|
|
|
|
2020-10-19 13:30:38 +03:00
|
|
|
def test_compile(cc: str, *cflags: str, src: Optional[str] = None, lang: str = 'c') -> bool:
|
2018-10-03 08:26:47 +03:00
|
|
|
src = src or 'int main(void) { return 0; }'
|
2020-10-19 14:04:03 +03:00
|
|
|
p = subprocess.Popen(
|
|
|
|
[cc] + list(cflags) + ['-x', lang, '-o', os.devnull, '-'],
|
|
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
stdin = p.stdin
|
|
|
|
assert stdin is not None
|
|
|
|
try:
|
|
|
|
stdin.write(src.encode('utf-8'))
|
|
|
|
stdin.close()
|
|
|
|
except BrokenPipeError:
|
|
|
|
return False
|
|
|
|
return p.wait() == 0
|
2018-04-20 18:02:16 +03:00
|
|
|
|
|
|
|
|
2020-10-19 13:30:38 +03:00
|
|
|
def first_successful_compile(cc: str, *cflags: str, src: Optional[str] = None, lang: str = 'c') -> str:
|
2018-04-20 18:26:29 +03:00
|
|
|
for x in cflags:
|
2020-10-19 00:08:19 +03:00
|
|
|
if test_compile(cc, *shlex.split(x), src=src, lang=lang):
|
2018-04-20 18:26:29 +03:00
|
|
|
return x
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
2017-11-05 06:52:15 +03:00
|
|
|
def init_env(
|
2020-03-14 12:07:11 +03:00
|
|
|
debug: bool = False,
|
|
|
|
sanitize: bool = False,
|
|
|
|
native_optimizations: bool = True,
|
2020-10-03 23:40:07 +03:00
|
|
|
link_time_optimization: bool = True,
|
2020-03-14 12:07:11 +03:00
|
|
|
profile: bool = False,
|
2020-04-28 19:08:05 +03:00
|
|
|
egl_library: Optional[str] = None,
|
|
|
|
startup_notification_library: Optional[str] = None,
|
|
|
|
canberra_library: Optional[str] = None,
|
2020-03-14 12:07:11 +03:00
|
|
|
extra_logging: Iterable[str] = ()
|
|
|
|
) -> Env:
|
2017-05-15 07:48:59 +03:00
|
|
|
native_optimizations = native_optimizations and not sanitize and not debug
|
2020-12-04 18:29:18 +03:00
|
|
|
if native_optimizations and is_macos and is_arm:
|
|
|
|
# see https://github.com/kovidgoyal/kitty/issues/3126
|
|
|
|
# -march=native is not supported when targeting Apple Silicon
|
|
|
|
native_optimizations = False
|
2017-05-15 07:48:59 +03:00
|
|
|
cc, ccver = cc_version()
|
|
|
|
print('CC:', cc, ccver)
|
2018-04-20 18:26:29 +03:00
|
|
|
stack_protector = first_successful_compile(cc, '-fstack-protector-strong', '-fstack-protector')
|
2016-11-12 06:34:17 +03:00
|
|
|
missing_braces = ''
|
2017-05-15 07:48:59 +03:00
|
|
|
if ccver < (5, 2) and cc == 'gcc':
|
2016-11-12 06:34:17 +03:00
|
|
|
missing_braces = '-Wno-missing-braces'
|
2017-11-08 13:43:21 +03:00
|
|
|
df = '-g3'
|
2019-11-27 06:57:23 +03:00
|
|
|
float_conversion = ''
|
2017-11-08 13:43:21 +03:00
|
|
|
if ccver >= (5, 0):
|
|
|
|
df += ' -Og'
|
2019-11-27 06:57:23 +03:00
|
|
|
float_conversion = '-Wfloat-conversion'
|
2020-02-13 08:50:57 +03:00
|
|
|
fortify_source = '-D_FORTIFY_SOURCE=2'
|
2017-11-08 13:43:21 +03:00
|
|
|
optimize = df if debug or sanitize else '-O3'
|
2017-05-15 07:48:59 +03:00
|
|
|
sanitize_args = get_sanitize_args(cc, ccver) if sanitize else set()
|
2020-03-06 04:37:18 +03:00
|
|
|
cppflags_ = os.environ.get(
|
2019-07-08 04:16:16 +03:00
|
|
|
'OVERRIDE_CPPFLAGS', '-D{}DEBUG'.format('' if debug else 'N'),
|
2018-04-19 06:07:01 +03:00
|
|
|
)
|
2020-03-06 04:37:18 +03:00
|
|
|
cppflags = shlex.split(cppflags_)
|
2019-04-24 13:46:40 +03:00
|
|
|
for el in extra_logging:
|
|
|
|
cppflags.append('-DDEBUG_{}'.format(el.upper().replace('-', '_')))
|
2020-03-06 04:37:18 +03:00
|
|
|
cflags_ = os.environ.get(
|
2017-02-09 21:34:05 +03:00
|
|
|
'OVERRIDE_CFLAGS', (
|
2020-08-24 09:35:39 +03:00
|
|
|
'-Wextra {} -Wno-missing-field-initializers -Wall -Wstrict-prototypes {}'
|
2020-02-13 08:09:57 +03:00
|
|
|
' -pedantic-errors -Werror {} {} -fwrapv {} {} -pipe {} -fvisibility=hidden {}'
|
2017-02-21 14:05:25 +03:00
|
|
|
).format(
|
2019-11-27 19:49:12 +03:00
|
|
|
float_conversion,
|
2020-08-24 09:35:39 +03:00
|
|
|
'' if is_openbsd else '-std=c11',
|
2017-11-05 06:52:15 +03:00
|
|
|
optimize,
|
|
|
|
' '.join(sanitize_args),
|
|
|
|
stack_protector,
|
|
|
|
missing_braces,
|
2017-08-26 07:54:12 +03:00
|
|
|
'-march=native' if native_optimizations else '',
|
2020-02-13 08:09:57 +03:00
|
|
|
fortify_source
|
2017-02-21 14:05:25 +03:00
|
|
|
)
|
2017-02-09 21:34:05 +03:00
|
|
|
)
|
2020-03-06 04:37:18 +03:00
|
|
|
cflags = shlex.split(cflags_) + shlex.split(
|
|
|
|
sysconfig.get_config_var('CCSHARED') or ''
|
2017-11-05 06:52:15 +03:00
|
|
|
)
|
2020-03-06 04:37:18 +03:00
|
|
|
ldflags_ = os.environ.get(
|
2017-11-05 06:52:15 +03:00
|
|
|
'OVERRIDE_LDFLAGS',
|
|
|
|
'-Wall ' + ' '.join(sanitize_args) + ('' if debug else ' -O3')
|
2017-02-09 21:34:05 +03:00
|
|
|
)
|
2020-03-06 04:37:18 +03:00
|
|
|
ldflags = shlex.split(ldflags_)
|
2017-11-19 20:54:36 +03:00
|
|
|
ldflags.append('-shared')
|
2018-04-19 06:07:01 +03:00
|
|
|
cppflags += shlex.split(os.environ.get('CPPFLAGS', ''))
|
2016-10-31 12:15:49 +03:00
|
|
|
cflags += shlex.split(os.environ.get('CFLAGS', ''))
|
|
|
|
ldflags += shlex.split(os.environ.get('LDFLAGS', ''))
|
2020-10-03 23:40:07 +03:00
|
|
|
if not debug and not sanitize and not is_openbsd and link_time_optimization:
|
2017-11-01 12:05:57 +03:00
|
|
|
# See https://github.com/google/sanitizers/issues/647
|
2020-03-06 04:37:18 +03:00
|
|
|
cflags.append('-flto')
|
|
|
|
ldflags.append('-flto')
|
2016-10-31 12:15:49 +03:00
|
|
|
|
2020-12-11 18:56:15 +03:00
|
|
|
if debug:
|
|
|
|
cflags.append('-DKITTY_DEBUG_BUILD')
|
|
|
|
|
2017-08-26 07:54:12 +03:00
|
|
|
if profile:
|
2018-04-19 06:07:01 +03:00
|
|
|
cppflags.append('-DWITH_PROFILER')
|
2017-11-01 12:05:57 +03:00
|
|
|
cflags.append('-g3')
|
2017-08-26 07:54:12 +03:00
|
|
|
ldflags.append('-lprofiler')
|
2020-04-28 19:08:05 +03:00
|
|
|
|
|
|
|
library_paths = {}
|
|
|
|
|
|
|
|
if egl_library is not None:
|
|
|
|
assert('"' not in egl_library)
|
|
|
|
library_paths['glfw/egl_context.c'] = ['_GLFW_EGL_LIBRARY="' + egl_library + '"']
|
|
|
|
|
|
|
|
desktop_libs = []
|
|
|
|
if startup_notification_library is not None:
|
|
|
|
assert('"' not in startup_notification_library)
|
|
|
|
desktop_libs = ['_KITTY_STARTUP_NOTIFICATION_LIBRARY="' + startup_notification_library + '"']
|
|
|
|
|
|
|
|
if canberra_library is not None:
|
|
|
|
assert('"' not in canberra_library)
|
|
|
|
desktop_libs += ['_KITTY_CANBERRA_LIBRARY="' + canberra_library + '"']
|
|
|
|
|
|
|
|
if desktop_libs != []:
|
|
|
|
library_paths['kitty/desktop.c'] = desktop_libs
|
|
|
|
|
|
|
|
return Env(cc, cppflags, cflags, ldflags, library_paths, ccver=ccver)
|
2017-11-19 10:24:02 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def kitty_env() -> Env:
|
2017-11-19 10:24:02 +03:00
|
|
|
ans = env.copy()
|
2017-11-19 20:54:36 +03:00
|
|
|
cflags = ans.cflags
|
2016-10-31 12:15:49 +03:00
|
|
|
cflags.append('-pthread')
|
2017-05-19 11:53:11 +03:00
|
|
|
# We add 4000 to the primary version because vim turns on SGR mouse mode
|
|
|
|
# automatically if this version is high enough
|
2018-04-19 06:07:01 +03:00
|
|
|
cppflags = ans.cppflags
|
|
|
|
cppflags.append('-DPRIMARY_VERSION={}'.format(version[0] + 4000))
|
|
|
|
cppflags.append('-DSECONDARY_VERSION={}'.format(version[1]))
|
2017-11-05 07:08:58 +03:00
|
|
|
at_least_version('harfbuzz', 1, 5)
|
2017-12-06 18:40:35 +03:00
|
|
|
cflags.extend(pkg_config('libpng', '--cflags-only-I'))
|
2020-08-12 05:18:47 +03:00
|
|
|
cflags.extend(pkg_config('lcms2', '--cflags-only-I'))
|
2017-11-21 03:49:46 +03:00
|
|
|
if is_macos:
|
2020-08-19 17:39:48 +03:00
|
|
|
platform_libs = [
|
|
|
|
'-framework', 'CoreText', '-framework', 'CoreGraphics',
|
|
|
|
]
|
2020-10-19 13:10:11 +03:00
|
|
|
test_program_src = '''#include <UserNotifications/UserNotifications.h>
|
|
|
|
int main(void) { return 0; }\n'''
|
2020-10-19 13:32:49 +03:00
|
|
|
user_notifications_framework = first_successful_compile(
|
|
|
|
ans.cc, '-framework UserNotifications', src=test_program_src, lang='objective-c')
|
2020-10-04 15:46:09 +03:00
|
|
|
if user_notifications_framework:
|
2020-09-28 02:33:40 +03:00
|
|
|
platform_libs.extend(shlex.split(user_notifications_framework))
|
|
|
|
else:
|
|
|
|
cppflags.append('-DKITTY_USE_DEPRECATED_MACOS_NOTIFICATION_API')
|
2018-07-29 17:05:43 +03:00
|
|
|
# Apple deprecated OpenGL in Mojave (10.14) silence the endless
|
|
|
|
# warnings about it
|
|
|
|
cppflags.append('-DGL_SILENCE_DEPRECATION')
|
2017-01-10 17:20:56 +03:00
|
|
|
else:
|
2017-02-08 19:22:10 +03:00
|
|
|
cflags.extend(pkg_config('fontconfig', '--cflags-only-I'))
|
2020-08-19 17:39:48 +03:00
|
|
|
platform_libs = pkg_config('fontconfig', '--libs')
|
2017-10-30 19:54:01 +03:00
|
|
|
cflags.extend(pkg_config('harfbuzz', '--cflags-only-I'))
|
2020-08-19 17:39:48 +03:00
|
|
|
platform_libs.extend(pkg_config('harfbuzz', '--libs'))
|
2017-01-10 07:15:33 +03:00
|
|
|
pylib = get_python_flags(cflags)
|
2017-11-21 03:49:46 +03:00
|
|
|
gl_libs = ['-framework', 'OpenGL'] if is_macos else pkg_config('gl', '--libs')
|
2017-09-27 15:01:11 +03:00
|
|
|
libpng = pkg_config('libpng', '--libs')
|
2020-08-12 05:18:47 +03:00
|
|
|
lcms2 = pkg_config('lcms2', '--libs')
|
2020-08-19 17:39:48 +03:00
|
|
|
ans.ldpaths += pylib + platform_libs + gl_libs + libpng + lcms2
|
2017-11-21 03:49:46 +03:00
|
|
|
if is_macos:
|
2017-11-20 11:39:13 +03:00
|
|
|
ans.ldpaths.extend('-framework Cocoa'.split())
|
2020-05-21 09:13:55 +03:00
|
|
|
elif not is_openbsd:
|
2017-11-19 10:24:02 +03:00
|
|
|
ans.ldpaths += ['-lrt']
|
|
|
|
if '-ldl' not in ans.ldpaths:
|
|
|
|
ans.ldpaths.append('-ldl')
|
|
|
|
if '-lz' not in ans.ldpaths:
|
|
|
|
ans.ldpaths.append('-lz')
|
2016-10-31 12:15:49 +03:00
|
|
|
|
2019-06-03 12:50:07 +03:00
|
|
|
with suppress(FileExistsError):
|
2016-10-31 12:15:49 +03:00
|
|
|
os.mkdir(build_dir)
|
2017-11-19 10:24:02 +03:00
|
|
|
return ans
|
2016-10-31 12:15:49 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def define(x: str) -> str:
|
2016-11-12 09:11:40 +03:00
|
|
|
return '-D' + x
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def run_tool(cmd: Union[str, List[str]], desc: Optional[str] = None) -> None:
|
2016-10-31 12:15:49 +03:00
|
|
|
if isinstance(cmd, str):
|
|
|
|
cmd = shlex.split(cmd[0])
|
2017-11-20 12:56:27 +03:00
|
|
|
if verbose:
|
|
|
|
desc = None
|
2017-11-19 09:08:35 +03:00
|
|
|
print(desc or ' '.join(cmd))
|
2016-10-31 12:15:49 +03:00
|
|
|
p = subprocess.Popen(cmd)
|
|
|
|
ret = p.wait()
|
|
|
|
if ret != 0:
|
2017-11-19 20:54:36 +03:00
|
|
|
if desc:
|
|
|
|
print(' '.join(cmd))
|
2016-10-31 12:15:49 +03:00
|
|
|
raise SystemExit(ret)
|
|
|
|
|
2016-11-28 17:36:42 +03:00
|
|
|
|
2020-04-28 19:08:05 +03:00
|
|
|
def get_vcs_rev_defines(env: Env, src: str) -> List[str]:
|
2019-06-22 16:24:58 +03:00
|
|
|
ans = []
|
|
|
|
if os.path.exists('.git'):
|
|
|
|
try:
|
2019-06-26 20:48:26 +03:00
|
|
|
rev = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('utf-8')
|
2019-06-22 16:24:58 +03:00
|
|
|
except FileNotFoundError:
|
2019-06-23 05:06:03 +03:00
|
|
|
try:
|
|
|
|
with open('.git/refs/heads/master') as f:
|
|
|
|
rev = f.read()
|
|
|
|
except NotADirectoryError:
|
2019-08-01 21:21:26 +03:00
|
|
|
with open('.git') as f:
|
|
|
|
gitloc = f.read()
|
2019-06-23 05:06:21 +03:00
|
|
|
with open(os.path.join(gitloc, 'refs/heads/master')) as f:
|
2019-06-23 05:06:03 +03:00
|
|
|
rev = f.read()
|
|
|
|
|
|
|
|
ans.append('KITTY_VCS_REV="{}"'.format(rev.strip()))
|
2019-06-22 16:24:58 +03:00
|
|
|
return ans
|
|
|
|
|
|
|
|
|
2020-04-28 19:08:05 +03:00
|
|
|
def get_library_defines(env: Env, src: str) -> Optional[List[str]]:
|
|
|
|
try:
|
|
|
|
return env.library_paths[src]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
SPECIAL_SOURCES: Dict[str, Tuple[str, Union[List[str], Callable[[Env, str], Union[Optional[List[str]], Iterator[str]]]]]] = {
|
|
|
|
'glfw/egl_context.c': ('glfw/egl_context.c', get_library_defines),
|
|
|
|
'kitty/desktop.c': ('kitty/desktop.c', get_library_defines),
|
2016-11-12 09:11:40 +03:00
|
|
|
'kitty/parser_dump.c': ('kitty/parser.c', ['DUMP_COMMANDS']),
|
2019-06-22 16:24:58 +03:00
|
|
|
'kitty/data-types.c': ('kitty/data-types.c', get_vcs_rev_defines),
|
2016-11-12 09:11:40 +03:00
|
|
|
}
|
|
|
|
|
2016-10-31 12:15:49 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def newer(dest: str, *sources: str) -> bool:
|
2017-01-11 02:15:27 +03:00
|
|
|
try:
|
|
|
|
dtime = os.path.getmtime(dest)
|
2020-01-09 18:59:41 +03:00
|
|
|
except OSError:
|
2017-01-11 02:15:27 +03:00
|
|
|
return True
|
|
|
|
for s in sources:
|
2020-01-09 18:21:56 +03:00
|
|
|
with suppress(FileNotFoundError):
|
2019-12-19 13:57:25 +03:00
|
|
|
if os.path.getmtime(s) >= dtime:
|
|
|
|
return True
|
2017-01-11 02:15:27 +03:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def dependecies_for(src: str, obj: str, all_headers: Iterable[str]) -> Iterable[str]:
|
2017-09-30 09:48:08 +03:00
|
|
|
dep_file = obj.rpartition('.')[0] + '.d'
|
|
|
|
try:
|
2019-08-01 21:21:26 +03:00
|
|
|
with open(dep_file) as f:
|
|
|
|
deps = f.read()
|
2017-09-30 09:48:08 +03:00
|
|
|
except FileNotFoundError:
|
|
|
|
yield src
|
|
|
|
yield from iter(all_headers)
|
|
|
|
else:
|
2017-11-05 06:52:15 +03:00
|
|
|
RE_INC = re.compile(
|
|
|
|
r'^(?P<target>.+?):\s+(?P<deps>.+?)$', re.MULTILINE
|
|
|
|
)
|
2017-09-30 09:55:03 +03:00
|
|
|
SPACE_TOK = '\x1B'
|
|
|
|
|
|
|
|
text = deps.replace('\\\n', ' ').replace('\\ ', SPACE_TOK)
|
|
|
|
for match in RE_INC.finditer(text):
|
2017-11-05 06:52:15 +03:00
|
|
|
files = (
|
|
|
|
f.replace(SPACE_TOK, ' ') for f in match.group('deps').split()
|
|
|
|
)
|
2017-09-30 09:55:03 +03:00
|
|
|
for path in files:
|
|
|
|
path = os.path.abspath(path)
|
|
|
|
if path.startswith(base):
|
|
|
|
yield path
|
2017-09-30 09:48:08 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def parallel_run(items: List[Command]) -> None:
|
2017-11-19 09:19:42 +03:00
|
|
|
try:
|
2020-03-06 04:37:18 +03:00
|
|
|
num_workers = max(2, os.cpu_count() or 1)
|
2017-11-19 09:19:42 +03:00
|
|
|
except Exception:
|
|
|
|
num_workers = 2
|
2019-07-05 15:43:21 +03:00
|
|
|
items = list(reversed(items))
|
2020-03-06 04:37:18 +03:00
|
|
|
workers: Dict[int, Tuple[Optional[Command], Optional[subprocess.Popen]]] = {}
|
2017-11-19 09:08:35 +03:00
|
|
|
failed = None
|
2019-07-05 16:04:01 +03:00
|
|
|
num, total = 0, len(items)
|
2017-11-19 09:08:35 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def wait() -> None:
|
2017-11-19 09:08:35 +03:00
|
|
|
nonlocal failed
|
|
|
|
if not workers:
|
|
|
|
return
|
|
|
|
pid, s = os.wait()
|
2019-07-05 15:43:21 +03:00
|
|
|
compile_cmd, w = workers.pop(pid, (None, None))
|
2020-03-06 04:37:18 +03:00
|
|
|
if compile_cmd is None:
|
|
|
|
return
|
|
|
|
if ((s & 0xff) != 0 or ((s >> 8) & 0xff) != 0):
|
2019-10-26 01:59:45 +03:00
|
|
|
if failed is None:
|
|
|
|
failed = compile_cmd
|
2019-07-05 15:43:21 +03:00
|
|
|
elif compile_cmd.on_success is not None:
|
|
|
|
compile_cmd.on_success()
|
2017-11-19 09:08:35 +03:00
|
|
|
|
2019-07-08 05:06:30 +03:00
|
|
|
printed = False
|
2020-10-04 13:20:39 +03:00
|
|
|
isatty = sys.stdout.isatty()
|
2017-11-19 09:08:35 +03:00
|
|
|
while items and failed is None:
|
|
|
|
while len(workers) < num_workers and items:
|
2019-07-05 15:43:21 +03:00
|
|
|
compile_cmd = items.pop()
|
2019-07-05 16:04:01 +03:00
|
|
|
num += 1
|
2017-11-20 12:56:27 +03:00
|
|
|
if verbose:
|
2019-07-05 15:43:21 +03:00
|
|
|
print(' '.join(compile_cmd.cmd))
|
2020-10-04 13:20:39 +03:00
|
|
|
elif isatty:
|
2019-07-05 16:04:01 +03:00
|
|
|
print('\r\x1b[K[{}/{}] {}'.format(num, total, compile_cmd.desc), end='')
|
2020-10-03 23:56:02 +03:00
|
|
|
else:
|
|
|
|
print('[{}/{}] {}'.format(num, total, compile_cmd.desc), flush=True)
|
2019-07-08 05:06:30 +03:00
|
|
|
printed = True
|
2019-07-05 15:43:21 +03:00
|
|
|
w = subprocess.Popen(compile_cmd.cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
|
|
|
workers[w.pid] = compile_cmd, w
|
2017-11-19 09:08:35 +03:00
|
|
|
wait()
|
|
|
|
while len(workers):
|
|
|
|
wait()
|
2019-07-08 05:06:30 +03:00
|
|
|
if not verbose and printed:
|
2019-07-05 18:27:56 +03:00
|
|
|
print(' done')
|
2017-11-19 09:08:35 +03:00
|
|
|
if failed:
|
2019-07-05 16:04:01 +03:00
|
|
|
print(failed.desc)
|
2020-03-14 12:07:11 +03:00
|
|
|
run_tool(list(failed.cmd))
|
2017-11-19 09:08:35 +03:00
|
|
|
|
|
|
|
|
2019-06-24 14:30:34 +03:00
|
|
|
class CompilationDatabase:
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def __init__(self, incremental: bool):
|
2019-07-05 15:43:21 +03:00
|
|
|
self.incremental = incremental
|
2020-03-14 12:07:11 +03:00
|
|
|
self.compile_commands: List[Command] = []
|
|
|
|
self.link_commands: List[Command] = []
|
|
|
|
|
|
|
|
def add_command(
|
|
|
|
self,
|
|
|
|
desc: str,
|
|
|
|
cmd: List[str],
|
|
|
|
is_newer_func: Callable,
|
|
|
|
key: Optional[CompileKey] = None,
|
|
|
|
on_success: Optional[Callable] = None,
|
|
|
|
keyfile: Optional[str] = None
|
|
|
|
) -> None:
|
|
|
|
def no_op() -> None:
|
|
|
|
pass
|
2019-07-05 15:43:21 +03:00
|
|
|
|
2019-07-05 16:39:10 +03:00
|
|
|
queue = self.link_commands if keyfile is None else self.compile_commands
|
2020-03-14 12:07:11 +03:00
|
|
|
queue.append(Command(desc, cmd, is_newer_func, on_success or no_op, key, keyfile))
|
2019-07-05 15:43:21 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def build_all(self) -> None:
|
|
|
|
def sort_key(compile_cmd: Command) -> int:
|
2019-07-05 15:43:21 +03:00
|
|
|
if compile_cmd.keyfile:
|
|
|
|
return os.path.getsize(compile_cmd.keyfile)
|
|
|
|
return 0
|
|
|
|
|
2019-07-05 15:57:13 +03:00
|
|
|
items = []
|
2019-07-05 15:43:21 +03:00
|
|
|
for compile_cmd in self.compile_commands:
|
|
|
|
if not self.incremental or self.cmd_changed(compile_cmd) or compile_cmd.is_newer_func():
|
|
|
|
items.append(compile_cmd)
|
|
|
|
items.sort(key=sort_key, reverse=True)
|
|
|
|
parallel_run(items)
|
|
|
|
|
|
|
|
items = []
|
|
|
|
for compile_cmd in self.link_commands:
|
|
|
|
if not self.incremental or compile_cmd.is_newer_func():
|
|
|
|
items.append(compile_cmd)
|
|
|
|
parallel_run(items)
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def cmd_changed(self, compile_cmd: Command) -> bool:
|
2019-07-05 15:43:21 +03:00
|
|
|
key, cmd = compile_cmd.key, compile_cmd.cmd
|
2020-03-14 12:07:11 +03:00
|
|
|
return bool(self.db.get(key) != cmd)
|
2019-06-24 14:30:34 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def __enter__(self) -> 'CompilationDatabase':
|
|
|
|
self.all_keys: Set[CompileKey] = set()
|
2019-06-25 05:44:23 +03:00
|
|
|
self.dbpath = os.path.abspath('compile_commands.json')
|
2019-07-05 16:39:10 +03:00
|
|
|
self.linkdbpath = os.path.join(os.path.dirname(self.dbpath), 'link_commands.json')
|
2019-06-24 14:30:34 +03:00
|
|
|
try:
|
2019-06-25 05:44:23 +03:00
|
|
|
with open(self.dbpath) as f:
|
2019-06-24 14:30:34 +03:00
|
|
|
compilation_database = json.load(f)
|
|
|
|
except FileNotFoundError:
|
|
|
|
compilation_database = []
|
2019-07-05 16:39:10 +03:00
|
|
|
try:
|
|
|
|
with open(self.linkdbpath) as f:
|
|
|
|
link_database = json.load(f)
|
|
|
|
except FileNotFoundError:
|
|
|
|
link_database = []
|
2019-06-24 14:30:34 +03:00
|
|
|
compilation_database = {
|
2019-06-26 15:54:50 +03:00
|
|
|
CompileKey(k['file'], k['output']): k['arguments'] for k in compilation_database
|
2019-06-24 14:30:34 +03:00
|
|
|
}
|
|
|
|
self.db = compilation_database
|
2020-03-14 12:08:56 +03:00
|
|
|
self.linkdb = {tuple(k['output']): k['arguments'] for k in link_database}
|
2019-06-24 14:30:34 +03:00
|
|
|
return self
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def __exit__(self, *a: object) -> None:
|
2019-06-24 14:30:34 +03:00
|
|
|
cdb = self.db
|
|
|
|
for key in set(cdb) - self.all_keys:
|
|
|
|
del cdb[key]
|
|
|
|
compilation_database = [
|
2020-03-14 12:07:11 +03:00
|
|
|
{'file': c.key.src, 'arguments': c.cmd, 'directory': base, 'output': c.key.dest} for c in self.compile_commands if c.key is not None
|
2019-06-24 14:30:34 +03:00
|
|
|
]
|
2019-06-25 05:44:23 +03:00
|
|
|
with open(self.dbpath, 'w') as f:
|
2019-06-24 14:30:34 +03:00
|
|
|
json.dump(compilation_database, f, indent=2, sort_keys=True)
|
2019-07-05 16:39:10 +03:00
|
|
|
with open(self.linkdbpath, 'w') as f:
|
|
|
|
json.dump([{'output': c.key, 'arguments': c.cmd, 'directory': base} for c in self.link_commands], f, indent=2, sort_keys=True)
|
2019-06-24 14:30:34 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def compile_c_extension(
|
|
|
|
kenv: Env,
|
|
|
|
module: str,
|
|
|
|
compilation_database: CompilationDatabase,
|
|
|
|
sources: List[str],
|
|
|
|
headers: List[str],
|
|
|
|
desc_prefix: str = ''
|
|
|
|
) -> None:
|
2016-10-31 12:15:49 +03:00
|
|
|
prefix = os.path.basename(module)
|
2017-02-09 21:34:05 +03:00
|
|
|
objects = [
|
|
|
|
os.path.join(build_dir, prefix + '-' + os.path.basename(src) + '.o')
|
|
|
|
for src in sources
|
|
|
|
]
|
2017-01-11 02:15:27 +03:00
|
|
|
|
2017-11-14 07:01:23 +03:00
|
|
|
for original_src, dest in zip(sources, objects):
|
|
|
|
src = original_src
|
2018-04-19 08:03:10 +03:00
|
|
|
cppflags = kenv.cppflags[:]
|
2017-11-14 07:01:23 +03:00
|
|
|
is_special = src in SPECIAL_SOURCES
|
|
|
|
if is_special:
|
2020-03-06 04:37:18 +03:00
|
|
|
src, defines_ = SPECIAL_SOURCES[src]
|
2020-04-28 19:08:05 +03:00
|
|
|
defines = defines_(kenv, src) if callable(defines_) else defines_
|
|
|
|
if defines is not None:
|
|
|
|
cppflags.extend(map(define, defines))
|
2016-11-12 09:11:40 +03:00
|
|
|
|
2018-04-19 08:03:10 +03:00
|
|
|
cmd = [kenv.cc, '-MMD'] + cppflags + kenv.cflags
|
2019-06-24 14:30:34 +03:00
|
|
|
cmd += ['-c', src] + ['-o', dest]
|
|
|
|
key = CompileKey(original_src, os.path.basename(dest))
|
2019-07-05 15:57:13 +03:00
|
|
|
desc = 'Compiling {} ...'.format(emphasis(desc_prefix + src))
|
2019-07-11 17:49:22 +03:00
|
|
|
compilation_database.add_command(desc, cmd, partial(newer, dest, *dependecies_for(src, dest, headers)), key=key, keyfile=src)
|
2019-07-01 14:26:03 +03:00
|
|
|
dest = os.path.join(build_dir, module + '.so')
|
2019-07-11 17:49:22 +03:00
|
|
|
real_dest = module + '.so'
|
2019-07-01 14:26:03 +03:00
|
|
|
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
2019-07-05 15:57:13 +03:00
|
|
|
desc = 'Linking {} ...'.format(emphasis(desc_prefix + module))
|
2019-07-05 15:43:21 +03:00
|
|
|
# Old versions of clang don't like -pthread being passed to the linker
|
|
|
|
# Don't treat linker warnings as errors (linker generates spurious
|
|
|
|
# warnings on some old systems)
|
|
|
|
unsafe = {'-pthread', '-Werror', '-pedantic-errors'}
|
|
|
|
linker_cflags = list(filter(lambda x: x not in unsafe, kenv.cflags))
|
|
|
|
cmd = [kenv.cc] + linker_cflags + kenv.ldflags + objects + kenv.ldpaths + ['-o', dest]
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def on_success() -> None:
|
2019-07-05 15:43:21 +03:00
|
|
|
os.rename(dest, real_dest)
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
compilation_database.add_command(desc, cmd, partial(newer, real_dest, *objects), on_success=on_success, key=CompileKey('', module + '.so'))
|
2016-10-31 12:15:49 +03:00
|
|
|
|
2016-11-07 10:53:03 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def find_c_files() -> Tuple[List[str], List[str]]:
|
2017-01-11 02:15:27 +03:00
|
|
|
ans, headers = [], []
|
2019-07-11 17:49:22 +03:00
|
|
|
d = 'kitty'
|
2018-07-24 08:37:53 +03:00
|
|
|
exclude = {'fontconfig.c', 'freetype.c', 'desktop.c'} if is_macos else {'core_text.m', 'cocoa_window.m', 'macos_process_info.c'}
|
2019-07-16 14:10:48 +03:00
|
|
|
for x in sorted(os.listdir(d)):
|
2017-01-11 02:15:27 +03:00
|
|
|
ext = os.path.splitext(x)[1]
|
|
|
|
if ext in ('.c', '.m') and os.path.basename(x) not in exclude:
|
2016-11-13 07:57:24 +03:00
|
|
|
ans.append(os.path.join('kitty', x))
|
2017-01-11 02:15:27 +03:00
|
|
|
elif ext == '.h':
|
|
|
|
headers.append(os.path.join('kitty', x))
|
2016-11-13 07:57:24 +03:00
|
|
|
ans.append('kitty/parser_dump.c')
|
2020-03-14 12:07:11 +03:00
|
|
|
return ans, headers
|
2016-11-11 19:41:40 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def compile_glfw(compilation_database: CompilationDatabase) -> None:
|
2017-11-21 03:49:46 +03:00
|
|
|
modules = 'cocoa' if is_macos else 'x11 wayland'
|
2017-11-19 20:54:36 +03:00
|
|
|
for module in modules.split():
|
2017-11-21 03:37:41 +03:00
|
|
|
try:
|
2021-01-17 04:52:21 +03:00
|
|
|
genv = glfw.init_env(env, pkg_config, pkg_version, at_least_version, test_compile, module)
|
2017-11-21 03:37:41 +03:00
|
|
|
except SystemExit as err:
|
|
|
|
if module != 'wayland':
|
|
|
|
raise
|
2017-11-21 04:37:53 +03:00
|
|
|
print(err, file=sys.stderr)
|
2017-11-21 05:02:48 +03:00
|
|
|
print(error('Disabling building of wayland backend'), file=sys.stderr)
|
2017-11-21 03:37:41 +03:00
|
|
|
continue
|
2017-11-19 20:54:36 +03:00
|
|
|
sources = [os.path.join('glfw', x) for x in genv.sources]
|
|
|
|
all_headers = [os.path.join('glfw', x) for x in genv.all_headers]
|
2017-11-21 03:37:41 +03:00
|
|
|
if module == 'wayland':
|
2017-12-27 03:52:36 +03:00
|
|
|
try:
|
2020-03-06 05:29:55 +03:00
|
|
|
glfw.build_wayland_protocols(genv, Command, parallel_run, emphasis, newer, 'glfw')
|
2017-12-27 03:52:36 +03:00
|
|
|
except SystemExit as err:
|
|
|
|
print(err, file=sys.stderr)
|
|
|
|
print(error('Disabling building of wayland backend'), file=sys.stderr)
|
|
|
|
continue
|
2019-07-05 15:57:13 +03:00
|
|
|
compile_c_extension(
|
|
|
|
genv, 'kitty/glfw-' + module, compilation_database,
|
|
|
|
sources, all_headers, desc_prefix='[{}] '.format(module))
|
2017-11-19 20:54:36 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def kittens_env() -> Env:
|
2018-02-09 17:26:25 +03:00
|
|
|
kenv = env.copy()
|
|
|
|
cflags = kenv.cflags
|
|
|
|
cflags.append('-pthread')
|
|
|
|
cflags.append('-Ikitty')
|
|
|
|
pylib = get_python_flags(cflags)
|
|
|
|
kenv.ldpaths += pylib
|
|
|
|
return kenv
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def compile_kittens(compilation_database: CompilationDatabase) -> None:
|
2018-05-03 17:45:05 +03:00
|
|
|
kenv = kittens_env()
|
2019-01-23 17:08:08 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def list_files(q: str) -> List[str]:
|
2019-07-16 04:52:31 +03:00
|
|
|
return sorted(glob.glob(q))
|
2019-01-23 17:08:08 +03:00
|
|
|
|
2020-03-06 05:18:41 +03:00
|
|
|
def files(
|
|
|
|
kitten: str,
|
|
|
|
output: str,
|
|
|
|
extra_headers: Sequence[str] = (),
|
|
|
|
extra_sources: Sequence[str] = (),
|
|
|
|
filter_sources: Optional[Callable[[str], bool]] = None
|
|
|
|
) -> Tuple[List[str], List[str], str]:
|
2019-01-23 17:08:08 +03:00
|
|
|
sources = list(filter(filter_sources, list(extra_sources) + list_files(os.path.join('kittens', kitten, '*.c'))))
|
|
|
|
headers = list_files(os.path.join('kittens', kitten, '*.h')) + list(extra_headers)
|
|
|
|
return (sources, headers, 'kittens/{}/{}'.format(kitten, output))
|
|
|
|
|
|
|
|
for sources, all_headers, dest in (
|
|
|
|
files('unicode_input', 'unicode_names'),
|
|
|
|
files('diff', 'diff_speedup'),
|
|
|
|
files(
|
|
|
|
'choose', 'subseq_matcher',
|
|
|
|
extra_headers=('kitty/charsets.h',),
|
|
|
|
extra_sources=('kitty/charsets.c',),
|
|
|
|
filter_sources=lambda x: 'windows_compat.c' not in x),
|
|
|
|
):
|
|
|
|
compile_c_extension(
|
2019-07-05 15:43:21 +03:00
|
|
|
kenv, dest, compilation_database, sources, all_headers + ['kitty/data-types.h'])
|
2018-02-09 17:26:25 +03:00
|
|
|
|
|
|
|
|
2020-07-29 12:41:35 +03:00
|
|
|
def init_env_from_args(args: Options, native_optimizations: bool = False) -> None:
|
2017-11-19 10:24:02 +03:00
|
|
|
global env
|
2020-04-28 19:08:05 +03:00
|
|
|
env = init_env(
|
2020-10-03 23:40:07 +03:00
|
|
|
args.debug, args.sanitize, native_optimizations, args.link_time_optimization, args.profile,
|
2020-04-28 19:08:05 +03:00
|
|
|
args.egl_library, args.startup_notification_library, args.canberra_library,
|
|
|
|
args.extra_logging
|
|
|
|
)
|
2020-07-29 12:41:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
def build(args: Options, native_optimizations: bool = True) -> None:
|
|
|
|
init_env_from_args(args, native_optimizations)
|
2020-03-14 12:07:11 +03:00
|
|
|
sources, headers = find_c_files()
|
2019-06-24 14:30:34 +03:00
|
|
|
compile_c_extension(
|
2020-03-14 12:07:11 +03:00
|
|
|
kitty_env(), 'kitty/fast_data_types', args.compilation_database, sources, headers
|
2019-06-24 14:30:34 +03:00
|
|
|
)
|
2019-07-05 15:43:21 +03:00
|
|
|
compile_glfw(args.compilation_database)
|
|
|
|
compile_kittens(args.compilation_database)
|
2017-01-08 09:47:44 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def safe_makedirs(path: str) -> None:
|
2017-06-08 21:25:34 +03:00
|
|
|
os.makedirs(path, exist_ok=True)
|
2017-01-09 08:07:10 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def build_launcher(args: Options, launcher_dir: str = '.', bundle_type: str = 'source') -> None:
|
2017-08-26 07:54:12 +03:00
|
|
|
cflags = '-Wall -Werror -fpie'.split()
|
2018-04-19 06:07:01 +03:00
|
|
|
cppflags = []
|
2020-03-06 05:18:41 +03:00
|
|
|
libs: List[str] = []
|
2019-06-24 14:30:34 +03:00
|
|
|
if args.profile or args.sanitize:
|
|
|
|
if args.sanitize:
|
|
|
|
cflags.append('-g3')
|
|
|
|
cflags.extend(get_sanitize_args(env.cc, env.ccver))
|
|
|
|
libs += ['-lasan'] if env.cc == 'gcc' and not is_macos else []
|
|
|
|
else:
|
|
|
|
cflags.append('-g')
|
|
|
|
if args.profile:
|
|
|
|
libs.append('-lprofiler')
|
2017-08-26 07:54:12 +03:00
|
|
|
else:
|
|
|
|
cflags.append('-O3')
|
2019-06-24 14:30:34 +03:00
|
|
|
if bundle_type.endswith('-freeze'):
|
2018-04-19 06:07:01 +03:00
|
|
|
cppflags.append('-DFOR_BUNDLE')
|
|
|
|
cppflags.append('-DPYVER="{}"'.format(sysconfig.get_python_version()))
|
2019-06-24 14:30:34 +03:00
|
|
|
cppflags.append('-DKITTY_LIB_DIR_NAME="{}"'.format(args.libdir_name))
|
|
|
|
elif bundle_type == 'source':
|
|
|
|
cppflags.append('-DFROM_SOURCE')
|
|
|
|
if bundle_type.startswith('macos-'):
|
2020-06-05 20:22:30 +03:00
|
|
|
klp = '../Resources/kitty'
|
2019-06-24 14:30:34 +03:00
|
|
|
elif bundle_type.startswith('linux-'):
|
|
|
|
klp = '../{}/kitty'.format(args.libdir_name.strip('/'))
|
|
|
|
elif bundle_type == 'source':
|
2019-06-28 08:48:07 +03:00
|
|
|
klp = os.path.relpath('.', launcher_dir)
|
2019-06-24 14:30:34 +03:00
|
|
|
else:
|
|
|
|
raise SystemExit('Unknown bundle type: {}'.format(bundle_type))
|
|
|
|
cppflags.append('-DKITTY_LIB_PATH="{}"'.format(klp))
|
2017-08-26 07:54:12 +03:00
|
|
|
pylib = get_python_flags(cflags)
|
2018-04-19 06:07:01 +03:00
|
|
|
cppflags += shlex.split(os.environ.get('CPPFLAGS', ''))
|
2018-03-11 05:42:17 +03:00
|
|
|
cflags += shlex.split(os.environ.get('CFLAGS', ''))
|
|
|
|
ldflags = shlex.split(os.environ.get('LDFLAGS', ''))
|
2019-06-24 14:30:34 +03:00
|
|
|
if bundle_type == 'linux-freeze':
|
2018-05-31 20:32:38 +03:00
|
|
|
ldflags += ['-Wl,-rpath,$ORIGIN/../lib']
|
2019-06-24 14:30:34 +03:00
|
|
|
os.makedirs(launcher_dir, exist_ok=True)
|
|
|
|
dest = os.path.join(launcher_dir, 'kitty')
|
|
|
|
src = 'launcher.c'
|
2018-04-19 06:07:01 +03:00
|
|
|
cmd = [env.cc] + cppflags + cflags + [
|
2019-06-24 14:30:34 +03:00
|
|
|
src, '-o', dest] + ldflags + libs + pylib
|
|
|
|
key = CompileKey('launcher.c', 'kitty')
|
2019-07-05 15:43:21 +03:00
|
|
|
desc = 'Building {}...'.format(emphasis('launcher'))
|
|
|
|
args.compilation_database.add_command(desc, cmd, partial(newer, dest, src), key=key, keyfile=src)
|
|
|
|
args.compilation_database.build_all()
|
2017-08-26 07:54:12 +03:00
|
|
|
|
|
|
|
|
2018-06-08 08:36:41 +03:00
|
|
|
# Packaging {{{
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def copy_man_pages(ddir: str) -> None:
|
2018-06-08 08:36:41 +03:00
|
|
|
mandir = os.path.join(ddir, 'share', 'man')
|
|
|
|
safe_makedirs(mandir)
|
2019-06-03 12:50:07 +03:00
|
|
|
with suppress(FileNotFoundError):
|
2018-06-08 08:36:41 +03:00
|
|
|
shutil.rmtree(os.path.join(mandir, 'man1'))
|
2019-07-11 17:49:22 +03:00
|
|
|
src = 'docs/_build/man'
|
2018-06-08 08:36:41 +03:00
|
|
|
if not os.path.exists(src):
|
|
|
|
raise SystemExit('''\
|
2018-06-12 06:44:16 +03:00
|
|
|
The kitty man page is missing. If you are building from git then run:
|
|
|
|
make && make docs
|
2018-06-08 08:36:41 +03:00
|
|
|
(needs the sphinx documentation system to be installed)
|
|
|
|
''')
|
|
|
|
shutil.copytree(src, os.path.join(mandir, 'man1'))
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def copy_html_docs(ddir: str) -> None:
|
2018-06-08 08:36:41 +03:00
|
|
|
htmldir = os.path.join(ddir, 'share', 'doc', appname, 'html')
|
|
|
|
safe_makedirs(os.path.dirname(htmldir))
|
2019-06-03 12:50:07 +03:00
|
|
|
with suppress(FileNotFoundError):
|
2018-06-08 08:36:41 +03:00
|
|
|
shutil.rmtree(htmldir)
|
2019-07-11 17:49:22 +03:00
|
|
|
src = 'docs/_build/html'
|
2018-06-08 08:36:41 +03:00
|
|
|
if not os.path.exists(src):
|
|
|
|
raise SystemExit('''\
|
2018-06-12 06:44:16 +03:00
|
|
|
The kitty html docs are missing. If you are building from git then run:
|
|
|
|
make && make docs
|
2018-06-08 08:36:41 +03:00
|
|
|
(needs the sphinx documentation system to be installed)
|
|
|
|
''')
|
|
|
|
shutil.copytree(src, htmldir)
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def compile_python(base_path: str) -> None:
|
2018-07-29 10:46:35 +03:00
|
|
|
import compileall
|
2018-11-20 06:12:08 +03:00
|
|
|
import py_compile
|
2018-07-29 10:46:35 +03:00
|
|
|
try:
|
2020-03-06 04:37:18 +03:00
|
|
|
num_workers = max(1, os.cpu_count() or 1)
|
2018-07-29 10:46:35 +03:00
|
|
|
except Exception:
|
|
|
|
num_workers = 1
|
|
|
|
for root, dirs, files in os.walk(base_path):
|
|
|
|
for f in files:
|
|
|
|
if f.rpartition('.')[-1] in ('pyc', 'pyo'):
|
|
|
|
os.remove(os.path.join(root, f))
|
2020-03-08 19:38:18 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def c(base_path: str, **kw: object) -> None:
|
2020-03-06 05:18:41 +03:00
|
|
|
try:
|
2020-03-08 19:38:18 +03:00
|
|
|
kw['invalidation_mode'] = py_compile.PycInvalidationMode.UNCHECKED_HASH
|
2020-03-06 05:18:41 +03:00
|
|
|
except AttributeError:
|
2020-03-08 19:38:18 +03:00
|
|
|
pass
|
2020-03-14 12:07:11 +03:00
|
|
|
compileall.compile_dir(base_path, **kw) # type: ignore
|
2020-03-08 19:38:18 +03:00
|
|
|
|
|
|
|
for optimize in (0, 1, 2):
|
|
|
|
c(base_path, ddir='', force=True, optimize=optimize, quiet=1, workers=num_workers)
|
2018-07-29 10:46:35 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def create_linux_bundle_gunk(ddir: str, libdir_name: str) -> None:
|
2019-07-11 17:49:22 +03:00
|
|
|
if not os.path.exists('docs/_build/html'):
|
2019-07-08 04:56:07 +03:00
|
|
|
run_tool(['make', 'docs'])
|
2019-06-25 05:44:23 +03:00
|
|
|
copy_man_pages(ddir)
|
|
|
|
copy_html_docs(ddir)
|
|
|
|
icdir = os.path.join(ddir, 'share', 'icons', 'hicolor', '256x256', 'apps')
|
|
|
|
safe_makedirs(icdir)
|
|
|
|
shutil.copy2('logo/kitty.png', icdir)
|
|
|
|
deskdir = os.path.join(ddir, 'share', 'applications')
|
|
|
|
safe_makedirs(deskdir)
|
|
|
|
with open(os.path.join(deskdir, 'kitty.desktop'), 'w') as f:
|
|
|
|
f.write(
|
|
|
|
'''\
|
2017-01-18 17:56:39 +03:00
|
|
|
[Desktop Entry]
|
|
|
|
Version=1.0
|
|
|
|
Type=Application
|
|
|
|
Name=kitty
|
|
|
|
GenericName=Terminal emulator
|
2021-01-12 17:31:44 +03:00
|
|
|
Comment=Fast, feature-rich, GPU based terminal
|
2017-01-18 17:56:39 +03:00
|
|
|
TryExec=kitty
|
|
|
|
Exec=kitty
|
|
|
|
Icon=kitty
|
2018-03-21 13:28:57 +03:00
|
|
|
Categories=System;TerminalEmulator;
|
2017-02-09 21:34:05 +03:00
|
|
|
'''
|
|
|
|
)
|
2020-03-14 12:07:11 +03:00
|
|
|
|
|
|
|
base = Path(ddir)
|
|
|
|
in_src_launcher = base / (libdir_name + '/kitty/kitty/launcher/kitty')
|
|
|
|
launcher = base / 'bin/kitty'
|
2019-06-28 09:30:12 +03:00
|
|
|
if os.path.exists(in_src_launcher):
|
|
|
|
os.remove(in_src_launcher)
|
|
|
|
os.makedirs(os.path.dirname(in_src_launcher), exist_ok=True)
|
|
|
|
os.symlink(os.path.relpath(launcher, os.path.dirname(in_src_launcher)), in_src_launcher)
|
2017-02-02 09:34:52 +03:00
|
|
|
|
2019-06-25 03:32:28 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def macos_info_plist() -> bytes:
|
2019-06-25 05:44:23 +03:00
|
|
|
import plistlib
|
2019-06-28 08:17:00 +03:00
|
|
|
VERSION = '.'.join(map(str, version))
|
2020-09-22 14:02:40 +03:00
|
|
|
|
|
|
|
def access(what: str, verb: str = 'would like to access') -> str:
|
|
|
|
return f'A program running inside kitty {verb} {what}'
|
|
|
|
|
2019-06-28 08:17:00 +03:00
|
|
|
pl = dict(
|
2020-06-18 17:46:36 +03:00
|
|
|
# see https://github.com/kovidgoyal/kitty/issues/1233
|
2019-06-28 08:17:00 +03:00
|
|
|
CFBundleDevelopmentRegion='English',
|
2020-06-18 17:46:36 +03:00
|
|
|
CFBundleAllowMixedLocalizations=True,
|
|
|
|
|
2019-06-28 08:17:00 +03:00
|
|
|
CFBundleDisplayName=appname,
|
|
|
|
CFBundleName=appname,
|
|
|
|
CFBundleIdentifier='net.kovidgoyal.' + appname,
|
|
|
|
CFBundleVersion=VERSION,
|
|
|
|
CFBundleShortVersionString=VERSION,
|
|
|
|
CFBundlePackageType='APPL',
|
|
|
|
CFBundleSignature='????',
|
|
|
|
CFBundleExecutable=appname,
|
|
|
|
LSMinimumSystemVersion='10.12.0',
|
|
|
|
LSRequiresNativeExecution=True,
|
|
|
|
NSAppleScriptEnabled=False,
|
|
|
|
# Needed for dark mode in Mojave when linking against older SDKs
|
|
|
|
NSRequiresAquaSystemAppearance='NO',
|
|
|
|
NSHumanReadableCopyright=time.strftime(
|
|
|
|
'Copyright %Y, Kovid Goyal'),
|
2019-11-26 19:23:21 +03:00
|
|
|
CFBundleGetInfoString='kitty, an OpenGL based terminal emulator https://sw.kovidgoyal.net/kitty/',
|
2019-06-28 08:17:00 +03:00
|
|
|
CFBundleIconFile=appname + '.icns',
|
|
|
|
NSHighResolutionCapable=True,
|
|
|
|
NSSupportsAutomaticGraphicsSwitching=True,
|
|
|
|
LSApplicationCategoryType='public.app-category.utilities',
|
|
|
|
LSEnvironment={'KITTY_LAUNCHED_BY_LAUNCH_SERVICES': '1'},
|
|
|
|
NSServices=[
|
|
|
|
{
|
|
|
|
'NSMenuItem': {'default': 'New ' + appname + ' Tab Here'},
|
|
|
|
'NSMessage': 'openTab',
|
|
|
|
'NSRequiredContext': {'NSTextContent': 'FilePath'},
|
|
|
|
'NSSendTypes': ['NSFilenamesPboardType', 'public.plain-text'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'NSMenuItem': {'default': 'New ' + appname + ' Window Here'},
|
|
|
|
'NSMessage': 'openOSWindow',
|
|
|
|
'NSRequiredContext': {'NSTextContent': 'FilePath'},
|
|
|
|
'NSSendTypes': ['NSFilenamesPboardType', 'public.plain-text'],
|
|
|
|
},
|
|
|
|
],
|
2020-09-22 14:02:40 +03:00
|
|
|
NSAppleEventsUsageDescription=access('AppleScript.'),
|
|
|
|
NSCalendarsUsageDescription=access('your calendar data.'),
|
|
|
|
NSCameraUsageDescription=access('the camera.'),
|
|
|
|
NSContactsUsageDescription=access('your contacts.'),
|
|
|
|
NSLocationAlwaysUsageDescription=access('your location information, even in the background.'),
|
|
|
|
NSLocationUsageDescription=access('your location information.'),
|
|
|
|
NSLocationWhenInUseUsageDescription=access('your location while active.'),
|
|
|
|
NSMicrophoneUsageDescription=access('your microphone.'),
|
|
|
|
NSRemindersUsageDescription=access('your reminders.'),
|
|
|
|
NSSystemAdministrationUsageDescription=access('elevated privileges.', 'requires'),
|
2019-06-28 08:17:00 +03:00
|
|
|
)
|
|
|
|
return plistlib.dumps(pl)
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def create_macos_app_icon(where: str = 'Resources') -> None:
|
2020-03-21 01:49:57 +03:00
|
|
|
iconset_dir = os.path.abspath(os.path.join('logo', appname + '.iconset'))
|
2020-03-21 01:59:58 +03:00
|
|
|
icns_dir = os.path.join(where, appname + '.icns')
|
|
|
|
try:
|
|
|
|
subprocess.check_call([
|
|
|
|
'iconutil', '-c', 'icns', iconset_dir, '-o', icns_dir
|
|
|
|
])
|
|
|
|
except FileNotFoundError:
|
|
|
|
print(error('iconutil not found') + ', using png2icns (without retina support) to convert the logo', file=sys.stderr)
|
|
|
|
subprocess.check_call([
|
|
|
|
'png2icns', icns_dir
|
|
|
|
] + [os.path.join(iconset_dir, logo) for logo in [
|
|
|
|
# png2icns does not support retina icons, so only pass the non-retina icons
|
|
|
|
'icon_16x16.png',
|
|
|
|
'icon_32x32.png',
|
|
|
|
'icon_128x128.png',
|
|
|
|
'icon_256x256.png',
|
|
|
|
'icon_512x512.png',
|
|
|
|
]])
|
2019-06-28 08:48:07 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def create_minimal_macos_bundle(args: Options, where: str) -> None:
|
2019-06-28 08:48:07 +03:00
|
|
|
if os.path.exists(where):
|
|
|
|
shutil.rmtree(where)
|
|
|
|
bin_dir = os.path.join(where, 'kitty.app/Contents/MacOS')
|
|
|
|
resources_dir = os.path.join(where, 'kitty.app/Contents/Resources')
|
2020-03-06 04:37:18 +03:00
|
|
|
os.makedirs(resources_dir)
|
|
|
|
os.makedirs(bin_dir)
|
2019-06-28 08:48:07 +03:00
|
|
|
with open(os.path.join(where, 'kitty.app/Contents/Info.plist'), 'wb') as f:
|
|
|
|
f.write(macos_info_plist())
|
|
|
|
build_launcher(args, bin_dir)
|
|
|
|
os.symlink(
|
|
|
|
os.path.join(os.path.relpath(bin_dir, where), appname),
|
|
|
|
os.path.join(where, appname))
|
|
|
|
create_macos_app_icon(resources_dir)
|
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def create_macos_bundle_gunk(dest: str) -> None:
|
|
|
|
ddir = Path(dest)
|
2019-06-28 09:30:12 +03:00
|
|
|
os.mkdir(ddir / 'Contents')
|
|
|
|
with open(ddir / 'Contents/Info.plist', 'wb') as fp:
|
|
|
|
fp.write(macos_info_plist())
|
|
|
|
os.rename(ddir / 'share', ddir / 'Contents/Resources')
|
|
|
|
os.rename(ddir / 'bin', ddir / 'Contents/MacOS')
|
|
|
|
os.rename(ddir / 'lib', ddir / 'Contents/Frameworks')
|
2020-06-05 20:22:30 +03:00
|
|
|
os.rename(ddir / 'Contents/Frameworks/kitty', ddir / 'Contents/Resources/kitty')
|
2019-06-28 09:30:12 +03:00
|
|
|
launcher = ddir / 'Contents/MacOS/kitty'
|
2020-06-05 20:22:30 +03:00
|
|
|
in_src_launcher = ddir / 'Contents/Resources/kitty/kitty/launcher/kitty'
|
2019-06-28 09:30:12 +03:00
|
|
|
if os.path.exists(in_src_launcher):
|
|
|
|
os.remove(in_src_launcher)
|
|
|
|
os.makedirs(os.path.dirname(in_src_launcher), exist_ok=True)
|
|
|
|
os.symlink(os.path.relpath(launcher, os.path.dirname(in_src_launcher)), in_src_launcher)
|
2019-06-28 08:48:07 +03:00
|
|
|
create_macos_app_icon(os.path.join(ddir, 'Contents', 'Resources'))
|
2019-06-25 03:32:28 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def package(args: Options, bundle_type: str) -> None:
|
2017-01-09 08:07:10 +03:00
|
|
|
ddir = args.prefix
|
2019-06-24 14:30:34 +03:00
|
|
|
if bundle_type == 'linux-freeze':
|
2018-03-12 05:40:53 +03:00
|
|
|
args.libdir_name = 'lib'
|
|
|
|
libdir = os.path.join(ddir, args.libdir_name.strip('/'), 'kitty')
|
2017-01-09 08:07:10 +03:00
|
|
|
if os.path.exists(libdir):
|
|
|
|
shutil.rmtree(libdir)
|
2019-07-08 04:16:16 +03:00
|
|
|
launcher_dir = os.path.join(ddir, 'bin')
|
|
|
|
safe_makedirs(launcher_dir)
|
|
|
|
build_launcher(args, launcher_dir, bundle_type)
|
2017-01-18 18:22:06 +03:00
|
|
|
os.makedirs(os.path.join(libdir, 'logo'))
|
2019-03-03 04:54:05 +03:00
|
|
|
build_terminfo = runpy.run_path('build-terminfo', run_name='import_build')
|
2017-01-23 06:59:23 +03:00
|
|
|
for x in (libdir, os.path.join(ddir, 'share')):
|
|
|
|
odir = os.path.join(x, 'terminfo')
|
|
|
|
safe_makedirs(odir)
|
2019-03-03 04:54:05 +03:00
|
|
|
build_terminfo['compile_terminfo'](odir)
|
2017-01-09 08:07:10 +03:00
|
|
|
shutil.copy2('__main__.py', libdir)
|
2017-01-18 18:22:06 +03:00
|
|
|
shutil.copy2('logo/kitty.rgba', os.path.join(libdir, 'logo'))
|
2019-01-29 15:41:48 +03:00
|
|
|
shutil.copy2('logo/kitty.png', os.path.join(libdir, 'logo'))
|
2018-07-07 08:09:30 +03:00
|
|
|
shutil.copy2('logo/beam-cursor.png', os.path.join(libdir, 'logo'))
|
|
|
|
shutil.copy2('logo/beam-cursor@2x.png', os.path.join(libdir, 'logo'))
|
2017-01-08 09:47:44 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def src_ignore(parent: str, entries: Iterable[str]) -> List[str]:
|
2017-02-09 21:34:05 +03:00
|
|
|
return [
|
|
|
|
x for x in entries
|
2017-11-05 06:52:15 +03:00
|
|
|
if '.' in x and x.rpartition('.')[2] not in
|
2018-06-05 06:47:31 +03:00
|
|
|
('py', 'so', 'glsl')
|
2017-02-09 21:34:05 +03:00
|
|
|
]
|
2017-01-08 09:47:44 +03:00
|
|
|
|
2017-01-09 08:07:10 +03:00
|
|
|
shutil.copytree('kitty', os.path.join(libdir, 'kitty'), ignore=src_ignore)
|
2018-02-09 14:29:15 +03:00
|
|
|
shutil.copytree('kittens', os.path.join(libdir, 'kittens'), ignore=src_ignore)
|
2019-06-04 12:02:37 +03:00
|
|
|
if args.update_check_interval != 24.0:
|
|
|
|
with open(os.path.join(libdir, 'kitty/config_data.py'), 'r+', encoding='utf-8') as f:
|
|
|
|
raw = f.read()
|
|
|
|
nraw = raw.replace("update_check_interval', 24", "update_check_interval', {}".format(args.update_check_interval), 1)
|
|
|
|
if nraw == raw:
|
|
|
|
raise SystemExit('Failed to change the value of update_check_interval')
|
|
|
|
f.seek(0), f.truncate(), f.write(nraw)
|
2018-07-29 10:46:35 +03:00
|
|
|
compile_python(libdir)
|
2018-02-16 00:46:51 +03:00
|
|
|
for root, dirs, files in os.walk(libdir):
|
2020-03-05 18:08:51 +03:00
|
|
|
for f_ in files:
|
|
|
|
path = os.path.join(root, f_)
|
|
|
|
os.chmod(path, 0o755 if f_.endswith('.so') else 0o644)
|
2019-06-25 05:44:23 +03:00
|
|
|
if not is_macos:
|
2019-06-25 16:17:09 +03:00
|
|
|
create_linux_bundle_gunk(ddir, args.libdir_name)
|
2019-06-25 05:44:23 +03:00
|
|
|
|
|
|
|
if bundle_type.startswith('macos-'):
|
2019-06-25 05:56:19 +03:00
|
|
|
create_macos_bundle_gunk(ddir)
|
2018-01-11 06:10:35 +03:00
|
|
|
# }}}
|
2017-01-08 09:47:44 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def clean() -> None:
|
2018-03-12 05:20:54 +03:00
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def safe_remove(*entries: str) -> None:
|
2018-03-12 05:20:54 +03:00
|
|
|
for x in entries:
|
|
|
|
if os.path.exists(x):
|
|
|
|
if os.path.isdir(x):
|
|
|
|
shutil.rmtree(x)
|
|
|
|
else:
|
|
|
|
os.unlink(x)
|
|
|
|
|
2019-07-07 02:22:24 +03:00
|
|
|
safe_remove(
|
|
|
|
'build', 'compile_commands.json', 'link_commands.json',
|
|
|
|
'linux-package', 'kitty.app', 'asan-launcher',
|
|
|
|
'kitty-profile', 'kitty/launcher')
|
2019-06-23 18:06:21 +03:00
|
|
|
exclude = ('.git',)
|
|
|
|
for root, dirs, files in os.walk('.', topdown=True):
|
|
|
|
dirs[:] = [d for d in dirs if d not in exclude]
|
2019-06-23 18:08:40 +03:00
|
|
|
remove_dirs = {d for d in dirs if d == '__pycache__' or d.endswith('.dSYM')}
|
2020-03-05 18:08:51 +03:00
|
|
|
for d in remove_dirs:
|
|
|
|
shutil.rmtree(os.path.join(root, d))
|
|
|
|
dirs.remove(d)
|
2018-03-12 05:20:54 +03:00
|
|
|
for f in files:
|
|
|
|
ext = f.rpartition('.')[-1]
|
|
|
|
if ext in ('so', 'dylib', 'pyc', 'pyo'):
|
|
|
|
os.unlink(os.path.join(root, f))
|
2018-03-12 05:22:08 +03:00
|
|
|
for x in glob.glob('glfw/wayland-*-protocol.[ch]'):
|
2018-03-12 05:20:54 +03:00
|
|
|
os.unlink(x)
|
2017-10-17 11:05:54 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def option_parser() -> argparse.ArgumentParser: # {{{
|
2017-11-21 02:50:10 +03:00
|
|
|
p = argparse.ArgumentParser()
|
|
|
|
p.add_argument(
|
|
|
|
'action',
|
|
|
|
nargs='?',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.action,
|
2020-08-12 05:17:18 +03:00
|
|
|
choices='build test linux-package kitty.app linux-freeze macos-freeze build-launcher clean export-ci-bundles'.split(),
|
2017-11-21 02:50:10 +03:00
|
|
|
help='Action to perform (default is build)'
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'--debug',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.debug,
|
2017-11-21 02:50:10 +03:00
|
|
|
action='store_true',
|
|
|
|
help='Build extension modules with debugging symbols'
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'-v', '--verbose',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.verbose,
|
2017-11-21 02:50:10 +03:00
|
|
|
action='count',
|
|
|
|
help='Be verbose'
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'--sanitize',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.sanitize,
|
2017-11-21 02:50:10 +03:00
|
|
|
action='store_true',
|
2019-06-24 14:30:34 +03:00
|
|
|
help='Turn on sanitization to detect memory access errors and undefined behavior. This is a big performance hit.'
|
2017-11-21 02:50:10 +03:00
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'--prefix',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.prefix,
|
2017-11-21 02:50:10 +03:00
|
|
|
help='Where to create the linux package'
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'--full',
|
|
|
|
dest='incremental',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.incremental,
|
2017-11-21 02:50:10 +03:00
|
|
|
action='store_false',
|
|
|
|
help='Do a full build, even for unchanged files'
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'--profile',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.profile,
|
2017-11-21 02:50:10 +03:00
|
|
|
action='store_true',
|
|
|
|
help='Use the -pg compile flag to add profiling information'
|
|
|
|
)
|
2018-05-31 20:32:38 +03:00
|
|
|
p.add_argument(
|
|
|
|
'--for-freeze',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.for_freeze,
|
2018-05-31 20:32:38 +03:00
|
|
|
action='store_true',
|
|
|
|
help='Internal use'
|
|
|
|
)
|
2018-03-12 05:40:53 +03:00
|
|
|
p.add_argument(
|
|
|
|
'--libdir-name',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.libdir_name,
|
2018-03-12 05:41:43 +03:00
|
|
|
help='The name of the directory inside --prefix in which to store compiled files. Defaults to "lib"'
|
2018-03-12 05:40:53 +03:00
|
|
|
)
|
2019-04-24 13:46:40 +03:00
|
|
|
p.add_argument(
|
|
|
|
'--extra-logging',
|
|
|
|
action='append',
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.extra_logging,
|
2019-04-24 13:46:40 +03:00
|
|
|
choices=('event-loop',),
|
|
|
|
help='Turn on extra logging for debugging in this build. Can be specified multiple times, to turn'
|
2019-06-04 11:57:28 +03:00
|
|
|
' on different types of logging.'
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'--update-check-interval',
|
|
|
|
type=float,
|
2020-03-14 12:07:11 +03:00
|
|
|
default=Options.update_check_interval,
|
2019-06-04 11:57:28 +03:00
|
|
|
help='When building a package, the default value for the update_check_interval setting will'
|
|
|
|
' be set to this number. Use zero to disable update checking.'
|
2019-04-24 13:46:40 +03:00
|
|
|
)
|
2020-04-28 19:08:05 +03:00
|
|
|
p.add_argument(
|
|
|
|
'--egl-library',
|
|
|
|
type=str,
|
|
|
|
default=Options.egl_library,
|
|
|
|
help='The filename argument passed to dlopen for libEGL.'
|
|
|
|
' This can be used to change the name of the loaded library or specify an absolute path.'
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'--startup-notification-library',
|
|
|
|
type=str,
|
|
|
|
default=Options.startup_notification_library,
|
|
|
|
help='The filename argument passed to dlopen for libstartup-notification-1.'
|
|
|
|
' This can be used to change the name of the loaded library or specify an absolute path.'
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
'--canberra-library',
|
|
|
|
type=str,
|
|
|
|
default=Options.canberra_library,
|
|
|
|
help='The filename argument passed to dlopen for libcanberra.'
|
|
|
|
' This can be used to change the name of the loaded library or specify an absolute path.'
|
|
|
|
)
|
2020-10-03 23:40:07 +03:00
|
|
|
p.add_argument(
|
|
|
|
'--disable-link-time-optimization',
|
|
|
|
dest='link_time_optimization',
|
|
|
|
default=Options.link_time_optimization,
|
|
|
|
action='store_false',
|
|
|
|
help='Turn off Link Time Optimization (LTO).'
|
|
|
|
)
|
2017-11-21 02:50:10 +03:00
|
|
|
return p
|
2018-05-31 20:32:38 +03:00
|
|
|
# }}}
|
2017-11-21 02:50:10 +03:00
|
|
|
|
|
|
|
|
2020-03-14 12:07:11 +03:00
|
|
|
def main() -> None:
|
2017-11-20 12:56:27 +03:00
|
|
|
global verbose
|
2020-03-14 12:07:11 +03:00
|
|
|
args = option_parser().parse_args(namespace=Options())
|
2017-11-20 12:56:27 +03:00
|
|
|
verbose = args.verbose > 0
|
2017-01-09 08:07:10 +03:00
|
|
|
args.prefix = os.path.abspath(args.prefix)
|
2019-07-11 19:18:45 +03:00
|
|
|
os.chdir(base)
|
2019-06-24 14:30:34 +03:00
|
|
|
if args.action == 'test':
|
2017-02-09 21:34:05 +03:00
|
|
|
os.execlp(
|
2019-07-11 17:49:22 +03:00
|
|
|
sys.executable, sys.executable, 'test.py'
|
2017-02-09 21:34:05 +03:00
|
|
|
)
|
2019-06-24 14:30:34 +03:00
|
|
|
if args.action == 'clean':
|
2017-10-17 11:05:54 +03:00
|
|
|
clean()
|
2019-06-24 14:30:34 +03:00
|
|
|
return
|
2020-06-21 12:07:36 +03:00
|
|
|
launcher_dir = 'kitty/launcher'
|
2019-06-24 14:30:34 +03:00
|
|
|
|
2019-07-05 15:43:21 +03:00
|
|
|
with CompilationDatabase(args.incremental) as cdb:
|
2019-06-24 14:30:34 +03:00
|
|
|
args.compilation_database = cdb
|
|
|
|
if args.action == 'build':
|
2020-12-04 18:29:18 +03:00
|
|
|
build(args)
|
2019-06-28 08:48:07 +03:00
|
|
|
if is_macos:
|
|
|
|
create_minimal_macos_bundle(args, launcher_dir)
|
|
|
|
else:
|
|
|
|
build_launcher(args, launcher_dir=launcher_dir)
|
2020-07-29 12:41:35 +03:00
|
|
|
elif args.action == 'build-launcher':
|
|
|
|
init_env_from_args(args, False)
|
|
|
|
build_launcher(args, launcher_dir=launcher_dir)
|
2019-06-24 14:30:34 +03:00
|
|
|
elif args.action == 'linux-package':
|
|
|
|
build(args, native_optimizations=False)
|
|
|
|
package(args, bundle_type='linux-package')
|
|
|
|
elif args.action == 'linux-freeze':
|
|
|
|
build(args, native_optimizations=False)
|
|
|
|
package(args, bundle_type='linux-freeze')
|
|
|
|
elif args.action == 'macos-freeze':
|
|
|
|
build(args, native_optimizations=False)
|
2020-06-21 12:07:36 +03:00
|
|
|
build_launcher(args, launcher_dir=launcher_dir)
|
2019-06-24 14:30:34 +03:00
|
|
|
package(args, bundle_type='macos-freeze')
|
|
|
|
elif args.action == 'kitty.app':
|
|
|
|
args.prefix = 'kitty.app'
|
|
|
|
if os.path.exists(args.prefix):
|
|
|
|
shutil.rmtree(args.prefix)
|
2020-12-04 18:29:18 +03:00
|
|
|
build(args)
|
2019-06-24 14:30:34 +03:00
|
|
|
package(args, bundle_type='macos-package')
|
|
|
|
print('kitty.app successfully built!')
|
2020-08-12 05:17:18 +03:00
|
|
|
elif args.action == 'export-ci-bundles':
|
|
|
|
cmd = [sys.executable, '../bypy', 'export']
|
|
|
|
dest = ['download.calibre-ebook.com:/srv/download/ci/kitty']
|
|
|
|
subprocess.check_call(cmd + ['linux'] + dest)
|
|
|
|
subprocess.check_call(cmd + ['macos'] + dest)
|
|
|
|
subprocess.check_call(cmd + ['linux', '32'] + dest)
|
2016-11-07 10:53:03 +03:00
|
|
|
|
2016-11-28 17:36:42 +03:00
|
|
|
|
2016-11-07 10:53:03 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|