From 92a705c79ea74f2460434e14a15636fba7652715 Mon Sep 17 00:00:00 2001 From: Luflosi Date: Sun, 18 Oct 2020 23:08:19 +0200 Subject: [PATCH 1/2] Add language parameter to first_successful_compile() and `test_compile()` This allows compiling Objective-C files. The temporary directory is needed because clang would try to write to `/dev/null.d`, which would obviously fail. --- setup.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/setup.py b/setup.py index fc84bc7bd..a0a2613f0 100755 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ import shutil import subprocess import sys import sysconfig +import tempfile import time from contextlib import suppress from functools import partial @@ -211,25 +212,27 @@ def get_sanitize_args(cc: str, ccver: Tuple[int, int]) -> List[str]: return sanitize_args -def test_compile(cc: str, *cflags: str, src: Optional[str] = None) -> bool: +def test_compile(cc: str, *cflags: str, src: Optional[str] = None, lang: Optional[str] = None) -> bool: + lang = lang or 'c' src = src or 'int main(void) { return 0; }' - p = subprocess.Popen( - [cc] + list(cflags) + ['-x', 'c', '-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 + with tempfile.TemporaryDirectory() as tdir: + p = subprocess.Popen( + [cc] + list(cflags) + ['-x', lang, '-o', tdir + '/result', '-'], + 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 -def first_successful_compile(cc: str, *cflags: str, src: Optional[str] = None) -> str: +def first_successful_compile(cc: str, *cflags: str, src: Optional[str] = None, lang: Optional[str] = None) -> str: for x in cflags: - if test_compile(cc, *shlex.split(x), src=src): + if test_compile(cc, *shlex.split(x), src=src, lang=lang): return x return '' From 46ef2f313bbbc576715dbe0e4f2fd8e59fe89a6d Mon Sep 17 00:00:00 2001 From: Luflosi Date: Mon, 19 Oct 2020 12:10:11 +0200 Subject: [PATCH 2/2] macOS: Fix detection for notification API For some reason it can happen that the `UserNotifications` framework exists but the `UserNotifications/UserNotifications.h` header doesn't. Closes https://github.com/kovidgoyal/kitty/issues/3042. --- setup.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a0a2613f0..a78849695 100755 --- a/setup.py +++ b/setup.py @@ -343,7 +343,10 @@ def kitty_env() -> Env: platform_libs = [ '-framework', 'CoreText', '-framework', 'CoreGraphics', ] - user_notifications_framework = first_successful_compile(ans.cc, '-framework UserNotifications') + test_program_src = '''#include + int main(void) { return 0; }\n''' + user_notifications_framework = first_successful_compile(ans.cc, ' -framework UserNotifications', + src=test_program_src, lang='objective-c') if user_notifications_framework: platform_libs.extend(shlex.split(user_notifications_framework)) else: