Skip to content

Commit

Permalink
Fix iOS compilation.
Browse files Browse the repository at this point in the history
  • Loading branch information
l-kramer committed Sep 17, 2022
1 parent f493919 commit e059f51
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 82 deletions.
9 changes: 8 additions & 1 deletion source/eventcore/driver.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@
module eventcore.driver;

import core.time : Duration;
import std.process : StdProcessConfig = Config;
version (iOS) {
enum StdProcessConfig {
none,
detached,
newEnv,
suppressConsole,
}
} else import std.process : StdProcessConfig = Config;
import std.socket : Address;
import std.stdint : intptr_t;
import std.typecons : Tuple;
Expand Down
164 changes: 84 additions & 80 deletions source/eventcore/drivers/posix/processes.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,91 +64,95 @@ final class PosixEventDriverProcesses(Loop : PosixEventLoop) : EventDriverProces
string working_dir)
@trusted {
// Use std.process to spawn processes
import std.process : pipe, Pid, spawnProcess, StdProcessConfig = Config;
import std.stdio : File;
static import std.stdio;
version (iOS) {
return Process.init;
} else {
import std.process : pipe, Pid, spawnProcess, StdProcessConfig = Config;
import std.stdio : File;
static import std.stdio;

static File fdToFile(int fd, scope const(char)[] mode)
{
try {
File f;
f.fdopen(fd, mode);
return f;
} catch (Exception e) {
assert(0);
static File fdToFile(int fd, scope const(char)[] mode)
{
try {
File f;
f.fdopen(fd, mode);
return f;
} catch (Exception e) {
assert(0);
}
}
}

try {
Process process;
File stdinFile, stdoutFile, stderrFile;

stdinFile = stdin.visit!(
(int handle) => fdToFile(handle, "r"),
(ProcessRedirect redirect) {
final switch (redirect) {
case ProcessRedirect.inherit: return std.stdio.stdin;
case ProcessRedirect.none: return File.init;
case ProcessRedirect.pipe:
auto p = pipe();
process.stdin = m_driver.pipes.adopt(dup(p.writeEnd.fileno));
return p.readEnd;
}
});

stdoutFile = stdout.visit!(
(int handle) => fdToFile(handle, "w"),
(ProcessRedirect redirect) {
final switch (redirect) {
case ProcessRedirect.inherit: return std.stdio.stdout;
case ProcessRedirect.none: return File.init;
case ProcessRedirect.pipe:
auto p = pipe();
process.stdout = m_driver.pipes.adopt(dup(p.readEnd.fileno));
return p.writeEnd;
}
},
(_) => File.init);

stderrFile = stderr.visit!(
(int handle) => fdToFile(handle, "w"),
(ProcessRedirect redirect) {
final switch (redirect) {
case ProcessRedirect.inherit: return std.stdio.stderr;
case ProcessRedirect.none: return File.init;
case ProcessRedirect.pipe:
auto p = pipe();
process.stderr = m_driver.pipes.adopt(dup(p.readEnd.fileno));
return p.writeEnd;
}
},
(_) => File.init);

const redirectStdout = stdout.convertsTo!ProcessStdoutRedirect;
const redirectStderr = stderr.convertsTo!ProcessStderrRedirect;

if (redirectStdout) {
assert(!redirectStderr, "Can't redirect both stdout and stderr");
try {
Process process;
File stdinFile, stdoutFile, stderrFile;

stdinFile = stdin.visit!(
(int handle) => fdToFile(handle, "r"),
(ProcessRedirect redirect) {
final switch (redirect) {
case ProcessRedirect.inherit: return std.stdio.stdin;
case ProcessRedirect.none: return File.init;
case ProcessRedirect.pipe:
auto p = pipe();
process.stdin = m_driver.pipes.adopt(dup(p.writeEnd.fileno));
return p.readEnd;
}
});

stdoutFile = stdout.visit!(
(int handle) => fdToFile(handle, "w"),
(ProcessRedirect redirect) {
final switch (redirect) {
case ProcessRedirect.inherit: return std.stdio.stdout;
case ProcessRedirect.none: return File.init;
case ProcessRedirect.pipe:
auto p = pipe();
process.stdout = m_driver.pipes.adopt(dup(p.readEnd.fileno));
return p.writeEnd;
}
},
(_) => File.init);

stderrFile = stderr.visit!(
(int handle) => fdToFile(handle, "w"),
(ProcessRedirect redirect) {
final switch (redirect) {
case ProcessRedirect.inherit: return std.stdio.stderr;
case ProcessRedirect.none: return File.init;
case ProcessRedirect.pipe:
auto p = pipe();
process.stderr = m_driver.pipes.adopt(dup(p.readEnd.fileno));
return p.writeEnd;
}
},
(_) => File.init);

const redirectStdout = stdout.convertsTo!ProcessStdoutRedirect;
const redirectStderr = stderr.convertsTo!ProcessStderrRedirect;

if (redirectStdout) {
assert(!redirectStderr, "Can't redirect both stdout and stderr");

stdoutFile = stderrFile;
} else if (redirectStderr) {
stderrFile = stdoutFile;
}

stdoutFile = stderrFile;
} else if (redirectStderr) {
stderrFile = stdoutFile;
Pid stdPid = spawnProcess(
args,
stdinFile,
stdoutFile,
stderrFile,
env,
cast(StdProcessConfig)config,
working_dir);
process.pid = adopt(stdPid.osHandle);
stdPid.destroy();

return process;
} catch (Exception e) {
return Process.init;
}

Pid stdPid = spawnProcess(
args,
stdinFile,
stdoutFile,
stderrFile,
env,
cast(StdProcessConfig)config,
working_dir);
process.pid = adopt(stdPid.osHandle);
stdPid.destroy();

return process;
} catch (Exception e) {
return Process.init;
}
}

Expand Down
8 changes: 7 additions & 1 deletion source/eventcore/drivers/posix/sockets.d
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ version (Posix) {
else version (NetBSD) enum O_CLOEXEC = 0x400000;
else version (OpenBSD) enum O_CLOEXEC = 0x10000;
else version (OSX) enum O_CLOEXEC = 0x1000000;
else version (iOS) enum O_CLOEXEC = 0x1000000;
}
}
version (linux) {
Expand Down Expand Up @@ -60,7 +61,10 @@ version (linux) {
}
version (OSX) {
import core.sys.darwin.netinet.in_ : IP_ADD_MEMBERSHIP, IP_MULTICAST_LOOP;

static if (!is(typeof(ESHUTDOWN))) enum ESHUTDOWN = 58;
}
version (iOS) {
import core.sys.darwin.netinet.in_ : IP_ADD_MEMBERSHIP, IP_MULTICAST_LOOP;
static if (!is(typeof(ESHUTDOWN))) enum ESHUTDOWN = 58;
}
version (FreeBSD) {
Expand Down Expand Up @@ -104,6 +108,8 @@ version (Android) {
version (Posix) {
version (OSX) {
enum SEND_FLAGS = 0;
} else version (iOS) {
enum SEND_FLAGS = 0;
} else {
enum SEND_FLAGS = MSG_NOSIGNAL;
}
Expand Down

0 comments on commit e059f51

Please sign in to comment.