From 7750a461aa55eacbe2eccad2651abb14dcbf5f6a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 3 Oct 2018 10:56:47 +0530 Subject: [PATCH] Fix compilation on systems that dont have the memfd_create syscall --- glfw/memfd.h | 2 +- glfw/wl_window.c | 2 +- setup.py | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/glfw/memfd.h b/glfw/memfd.h index 06686314a..bdaa29808 100644 --- a/glfw/memfd.h +++ b/glfw/memfd.h @@ -6,7 +6,7 @@ #pragma once -#if defined(__linux__) +#ifdef HAS_MEMFD_CREATE #define _GNU_SOURCE #include diff --git a/glfw/wl_window.c b/glfw/wl_window.c index 525b3d959..62314a528 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -140,7 +140,7 @@ static int createAnonymousFile(off_t size) { int ret; -#if defined(__linux__) +#ifdef HAS_MEMFD_CREATE int fd = memfd_create("glfw-shared", MFD_CLOEXEC | MFD_ALLOW_SEALING); if (fd < 0) return -1; // We can add this seal before calling posix_fallocate(), as the file diff --git a/setup.py b/setup.py index 820fdc560..982a3b8b5 100755 --- a/setup.py +++ b/setup.py @@ -13,7 +13,6 @@ import shutil import subprocess import sys import sysconfig -import tempfile import time base = os.path.dirname(os.path.abspath(__file__)) @@ -141,11 +140,10 @@ def get_sanitize_args(cc, ccver): def test_compile(cc, *cflags, src=None): - with tempfile.NamedTemporaryFile(suffix='.c') as f: - src = src or 'int main(void) { return 0; }' - f.write(src.encode('utf-8')) - f.flush() - return subprocess.Popen([cc] + list(cflags) + [f.name, '-o', os.devnull]).wait() == 0 + src = src or 'int main(void) { return 0; }' + p = subprocess.Popen([cc] + list(cflags) + ['-x', 'c', '-o', os.devnull, '-'], stdin=subprocess.PIPE) + p.stdin.write(src.encode('utf-8')), p.stdin.close() + return p.wait() == 0 def first_successful_compile(cc, *cflags, src=None): @@ -171,6 +169,12 @@ def init_env( cc, ccver = cc_version() print('CC:', cc, ccver) stack_protector = first_successful_compile(cc, '-fstack-protector-strong', '-fstack-protector') + has_memfd_create = test_compile(cc, '-Werror', src='''#define _GNU_SOURCE +#include +#include +int main(void) { + return syscall(__NR_memfd_create, "test", 0); +}''') missing_braces = '' if ccver < (5, 2) and cc == 'gcc': missing_braces = '-Wno-missing-braces' @@ -220,6 +224,8 @@ def init_env( cflags.append('-g3') ldflags.append('-lprofiler') ldpaths = [] + if has_memfd_create: + cppflags.append('-DHAS_MEMFD_CREATE') return Env(cc, cppflags, cflags, ldflags, ldpaths=ldpaths)