Add oc_run_cmd

This commit is contained in:
Ben Visness 2023-09-27 15:53:15 -05:00
parent 4f0738fe64
commit d502f35da4
15 changed files with 673 additions and 135 deletions

View File

@ -26,7 +26,6 @@ BraceWrapping:
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterObjCDeclaration: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true

View File

@ -19,7 +19,7 @@ set STDLIB_DIR=%ORCA_DIR%\src\libc-shim
set wasmFlags=--target=wasm32^
--no-standard-libraries ^
-mbulk-memory ^
-g -O2 ^
-g ^
-D__ORCA__ ^
-Wl,--no-entry ^
-Wl,--export-dynamic ^

View File

@ -0,0 +1,74 @@
[
{
"directory": "{{ORCAROOT}}",
"file": "src/orca.c",
"arguments": [
"src/orca.c",
"-g",
"-D",
"OC_DEBUG",
"-D",
"OC_LOG_COMPILE_DEBUG",
"-c",
"-std=c11",
"-I",
"src",
"-I",
"src/util",
"-I",
"src/platform",
"-I",
"src/ext",
"-I",
"src/ext/angle/include"
]
},
{
"directory": "{{ORCAROOT}}",
"file": "src/orca.m",
"arguments": [
"-xobjective-c",
"src/orca.m",
"-g",
"-D",
"OC_DEBUG",
"-D",
"OC_LOG_COMPILE_DEBUG",
"-c",
"-I",
"src",
"-I",
"src/util",
"-I",
"src/platform",
"-I",
"src/ext",
"-I",
"src/ext/angle/include"
]
},
{
"directory": "{{ORCAROOT}}",
"file": "src/runtime.c",
"arguments": [
"src/runtime.c",
"-g",
"-D",
"OC_DEBUG",
"-D",
"OC_LOG_COMPILE_DEBUG",
"-I",
"src",
"-I",
"src/ext",
"-I",
"src/ext/angle/include",
"-I",
"src/ext/wasm3/source",
"-Lbuild/bin",
"-Lbuild/lib",
"-lorca",
"-lwasm3"
]
}
]

View File

@ -1,7 +1,7 @@
import glob
import json
import os
import platform
import re
import urllib.request
import shutil
import subprocess
@ -63,6 +63,8 @@ def build_runtime(args):
ensure_programs()
ensure_angle()
plonk_compile_commands()
build_platform_layer("lib", args.release)
build_wasm3(args.release)
build_orca(args.release)
@ -90,6 +92,14 @@ def clean(args):
yeetdir("scripts/__pycache__")
def plonk_compile_commands():
os.makedirs("build", exist_ok=True)
with open("scripts/compile_commands.template.json") as infile:
out = infile.read().replace(r'"{{ORCAROOT}}"', json.dumps(os.getcwd()))
with open("build/compile_commands.json", "w") as outfile:
outfile.write(out)
def build_platform_layer(target, release):
print("Building Orca platform layer...")
@ -344,7 +354,7 @@ def build_orca_mac(release):
"-Isrc/ext/wasm3/source"
]
libs = ["-Lbuild/bin", "-Lbuild/lib", "-lorca", "-lwasm3"]
debug_flags = ["-O2"] if release else ["-g", "-DOC_DEBUG -DOC_LOG_COMPILE_DEBUG"]
debug_flags = ["-O2"] if release else ["-g", "-DOC_DEBUG", "-DOC_LOG_COMPILE_DEBUG"]
flags = [
*debug_flags,
"-mmacos-version-min=10.15.4"]
@ -376,22 +386,18 @@ def gen_all_bindings():
"src/wasmbind/gles_api.json",
"src/graphics/orca_gl31.h"
)
bindgen("gles", "src/wasmbind/gles_api.json",
wasm3_bindings="src/wasmbind/gles_api_bind_gen.c",
)
bindgen("core", "src/wasmbind/core_api.json",
guest_stubs="src/wasmbind/core_api_stubs.c",
wasm3_bindings="src/wasmbind/core_api_bind_gen.c",
)
bindgen("surface", "src/wasmbind/surface_api.json",
guest_stubs="src/graphics/orca_surface_stubs.c",
guest_include="graphics/graphics.h",
wasm3_bindings="src/wasmbind/surface_api_bind_gen.c",
)
bindgen("clock", "src/wasmbind/clock_api.json",
guest_include="platform/platform_clock.h",
wasm3_bindings="src/wasmbind/clock_api_bind_gen.c",
@ -400,6 +406,10 @@ def gen_all_bindings():
guest_stubs="src/platform/orca_io_stubs.c",
wasm3_bindings="src/wasmbind/io_api_bind_gen.c",
)
bindgen("subprocess", "src/wasmbind/subprocess_api.json",
guest_stubs="src/wasmbind/subprocess_api_stubs.c",
wasm3_bindings="src/wasmbind/subprocess_api_bind_gen.c",
)
def ensure_programs():

View File

@ -20,6 +20,7 @@
#include "platform/win32_io.c"
#include "platform/win32_thread.c"
#include "platform/win32_platform.c"
#include "platform/win32_subprocess.c"
//TODO
#elif OC_PLATFORM_MACOS
#include "platform/native_debug.c"
@ -53,6 +54,7 @@
#include "platform/orca_io_stubs.c"
#include "platform/orca_platform.c"
#include "platform/platform_path.c"
#include "platform/orca_subprocess.c"
#else
#error "Unsupported platform"
#endif
@ -98,6 +100,7 @@
#elif OC_PLATFORM_ORCA
#include "app/orca_app.c"
#include "wasmbind/core_api_stubs.c"
#include "wasmbind/subprocess_api_stubs.c"
#include "graphics/graphics_common.c"
#include "graphics/orca_surface_stubs.c"
#else

View File

@ -23,6 +23,7 @@
#include "platform/platform_io.h"
#include "platform/platform_io_dialog.h"
#include "platform/platform_path.h"
#include "platform/platform_subprocess.h"
#if !defined(OC_PLATFORM_ORCA) || !(OC_PLATFORM_ORCA)
#include "platform/platform_thread.h"

View File

@ -0,0 +1,11 @@
/*************************************************************************
*
* Orca
* Copyright 2023 Martin Fouilleul and the Orca project contributors
* See LICENSE.txt for licensing information
*
**************************************************************************/
#include "platform_subprocess.h"
#include "util/typedefs.h"
oc_str8 ORCA_IMPORT(oc_run_cmd)(oc_arena* arena, oc_str8 cmd);

View File

@ -0,0 +1,24 @@
/*************************************************************************
*
* Orca
* Copyright 2023 Martin Fouilleul and the Orca project contributors
* See LICENSE.txt for licensing information
*
**************************************************************************/
#ifndef __PLATFORM_SUBPROCESS_H
#define __PLATFORM_SUBPROCESS_H
#include "platform.h"
#include "util/strings.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
ORCA_API oc_str8 oc_run_cmd(oc_arena* arena, oc_str8 cmd);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // __PLATFORM_SUBPROCESS_H

View File

@ -0,0 +1,135 @@
/*************************************************************************
*
* Orca
* Copyright 2023 Martin Fouilleul and the Orca project contributors
* See LICENSE.txt for licensing information
*
**************************************************************************/
#include "platform_subprocess.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
oc_str8 oc_run_cmd(oc_arena* arena, oc_str8 cmd)
{
oc_arena_scope scratch = oc_scratch_begin();
oc_str8 out = { 0 };
HANDLE childStdinRd = NULL;
HANDLE childStdinWr = NULL;
HANDLE childStdoutRd = NULL;
HANDLE childStdoutWr = NULL;
// Create child process's stdin and stdout pipes
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.lpSecurityDescriptor = NULL;
saAttr.bInheritHandle = TRUE;
if(!CreatePipe(&childStdinRd, &childStdinWr,
&saAttr, 0)
|| !SetHandleInformation(childStdinWr, HANDLE_FLAG_INHERIT, 0))
{
oc_log_error("failed to create child stdin\n");
goto error;
}
if(!CreatePipe(&childStdoutRd, &childStdoutWr,
&saAttr, 0)
|| !SetHandleInformation(childStdoutRd, HANDLE_FLAG_INHERIT, 0))
{
oc_log_error("failed to create child stdout\n");
goto error;
}
// Create child process
char* cmdStr = oc_str8_to_cstring(scratch.arena, cmd);
PROCESS_INFORMATION procInfo;
STARTUPINFO startInfo;
memset(&procInfo, 0, sizeof(PROCESS_INFORMATION));
memset(&startInfo, 0, sizeof(STARTUPINFO));
startInfo.cb = sizeof(STARTUPINFO);
startInfo.hStdInput = childStdinRd;
startInfo.hStdOutput = childStdoutWr;
startInfo.hStdError = childStdoutWr;
startInfo.dwFlags |= STARTF_USESTDHANDLES;
BOOL success = CreateProcess(
NULL,
cmdStr,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&startInfo,
&procInfo);
if(!success)
{
oc_log_error("failed to create child process\n");
goto error;
}
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
CloseHandle(childStdinRd); // the parent does not need these ends of the pipes
CloseHandle(childStdoutWr);
// TODO: Optionally write to the child's stdin
if(!CloseHandle(childStdinWr))
{
oc_log_error("failed to close child's stdin");
goto error;
}
// Read child's combined stdout/stderr
u64 outputLen = 0;
char* outputBuf = NULL;
while(true)
{
const int chunkSize = 1024;
char* chunk = oc_arena_push(arena, chunkSize);
if(outputBuf == NULL)
{
// Initialize output buffer to first allocated chunk
outputBuf = chunk;
}
DWORD nRead;
BOOL success = ReadFile(childStdoutRd, chunk, chunkSize, &nRead, NULL);
if(!success || nRead == 0)
{
break;
}
outputLen += nRead;
}
out = oc_str8_from_buffer(outputLen, outputBuf);
CloseHandle(childStdinWr);
CloseHandle(childStdoutRd);
goto cleanup;
error:
LPVOID msgBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&msgBuf,
0, NULL);
oc_log_error("%s\n", msgBuf);
LocalFree(msgBuf);
cleanup:
oc_scratch_end(scratch);
return out;
}

View File

@ -15,6 +15,7 @@
#include "runtime.h"
#include "runtime_clipboard.c"
#include "runtime_subprocess.c"
#include "runtime_io.c"
#include "runtime_memory.c"
@ -216,6 +217,12 @@ void oc_bridge_request_quit(void)
__orcaApp.quit = true;
}
// TODO: wait why do I have this bridge
oc_wasm_str8 oc_bridge_run_cmd(oc_wasm_addr wasmArena, oc_wasm_str8 cmd)
{
return oc_runtime_run_cmd(wasmArena, cmd);
}
typedef struct orca_surface_create_data
{
oc_window window;
@ -403,6 +410,7 @@ void oc_wasm_env_init(oc_wasm_env* runtime)
#include "wasmbind/io_api_bind_gen.c"
#include "wasmbind/surface_api_bind_manual.c"
#include "wasmbind/surface_api_bind_gen.c"
#include "wasmbind/subprocess_api_bind_gen.c"
i32 orca_runloop(void* user)
{
@ -461,6 +469,7 @@ i32 orca_runloop(void* user)
err |= bindgen_link_clock_api(app->env.m3Module);
err |= bindgen_link_io_api(app->env.m3Module);
err |= bindgen_link_gles_api(app->env.m3Module);
err |= bindgen_link_subprocess_api(app->env.m3Module);
err |= manual_link_gles_api(app->env.m3Module);
if(err)

View File

@ -11,6 +11,7 @@
#include "platform/platform_io_internal.h"
#include "runtime_memory.h"
#include "runtime_clipboard.h"
#include "runtime_subprocess.h"
#include "m3_compile.h"
#include "m3_env.h"

29
src/runtime_subprocess.c Normal file
View File

@ -0,0 +1,29 @@
/*************************************************************************
*
* Orca
* Copyright 2023 Martin Fouilleul and the Orca project contributors
* See LICENSE.txt for licensing information
*
**************************************************************************/
#include "runtime_subprocess.h"
// #include "memory.h"
oc_arena cmdArena;
oc_wasm_str8 oc_runtime_run_cmd(oc_wasm_addr wasmArena, oc_wasm_str8 cmd)
{
if(!cmdArena.currentChunk)
{
oc_arena_init(&cmdArena);
}
oc_arena_clear(&cmdArena);
oc_str8 nativeCmd = oc_wasm_str8_to_native(cmd);
oc_str8 out = oc_run_cmd(&cmdArena, nativeCmd);
oc_wasm_addr outAddr = oc_wasm_arena_push(wasmArena, out.len);
char* outPtr = (char*)oc_wasm_address_to_ptr(outAddr, out.len);
memcpy(outPtr, out.ptr, out.len);
return (oc_wasm_str8){ .ptr = outAddr,
.len = out.len };
}

15
src/runtime_subprocess.h Normal file
View File

@ -0,0 +1,15 @@
/*************************************************************************
*
* Orca
* Copyright 2023 Martin Fouilleul and the Orca project contributors
* See LICENSE.txt for licensing information
*
**************************************************************************/
#ifndef __RUNTIME_SUBPROCESS_H_
#define __RUNTIME_SUBPROCESS_H_
#include "runtime_memory.h"
oc_wasm_str8 oc_runtime_run_cmd(oc_wasm_addr wasmArena, oc_wasm_str8 cmd);
#endif // __RUNTIME_SUBPROCESS_H

View File

@ -1,128 +1,326 @@
[
{
"name": "oc_bridge_log",
"cname": "oc_bridge_log",
"ret": {"name": "void", "tag": "v"},
"args": [ {"name": "level",
"type": {"name": "oc_log_level", "tag": "i"}},
{"name": "functionLen",
"type": {"name": "int", "tag": "i"}},
{"name": "function",
"type": {"name": "char*", "tag": "p"},
"len": {"count": "functionLen"}},
{"name": "fileLen",
"type": {"name": "int", "tag": "i"}},
{"name": "file",
"type": {"name": "char*", "tag": "p"},
"len": {"count": "fileLen"}},
{"name": "line",
"type": {"name": "int", "tag": "i"}},
{"name": "msgLen",
"type": {"name": "int", "tag": "i"}},
{"name": "msg",
"type": {"name": "char*", "tag": "p"},
"len": {"count": "msgLen"}}
]
},
{
"name": "oc_mem_grow",
"cname": "oc_mem_grow",
"ret": {"name": "int", "tag": "i"},
"args": [ {"name": "size",
"type": {"name": "u64", "tag": "I"}}]
},
{
"name": "oc_bridge_assert_fail",
"cname": "oc_assert_fail",
"ret": {"name": "void", "tag": "v"},
"args": [ {"name": "file",
"type": {"name": "const char*", "tag": "p"},
"len": {"proc": "orca_check_cstring", "args": ["file"]}},
{"name": "function",
"type": {"name": "const char*", "tag": "p"},
"len": {"proc": "orca_check_cstring", "args": ["function"]}},
{"name": "line",
"type": {"name": "int", "tag": "i"}},
{"name": "src",
"type": {"name": "const char*", "tag": "p"},
"len": {"proc": "orca_check_cstring", "args": ["src"]}},
{"name": "note",
"type": {"name": "const char*", "tag": "p"},
"len": {"proc": "orca_check_cstring", "args": ["note"]}}
]
},
{
"name": "oc_bridge_abort_ext",
"cname": "oc_abort_ext",
"ret": {"name": "void", "tag": "v"},
"args": [ {"name": "file",
"type": {"name": "const char*", "tag": "p"},
"len": {"proc": "orca_check_cstring", "args": ["file"]}},
{"name": "function",
"type": {"name": "const char*", "tag": "p"},
"len": {"proc": "orca_check_cstring", "args": ["function"]}},
{"name": "line",
"type": {"name": "int", "tag": "i"}},
{"name": "note",
"type": {"name": "const char*", "tag": "p"},
"len": {"proc": "orca_check_cstring", "args": ["note"]}}
]
},
{
"name": "oc_get_host_platform",
"cname": "oc_get_host_platform",
"ret": {"name": "int", "tag": "i"},
"args": []
},
{
"name": "oc_request_quit",
"cname": "oc_bridge_request_quit",
"ret": {"name": "void", "tag": "v"},
"args": []
},
{
"name": "oc_window_set_title",
"cname": "oc_bridge_window_set_title",
"ret": {"name": "void", "tag": "v"},
"args": [
{ "name": "title",
"type": {"name": "oc_str8", "cname": "oc_wasm_str8", "tag": "S"}}
]
},
{
"name": "oc_window_set_size",
"cname": "oc_bridge_window_set_size",
"ret": {"name": "void", "tag": "v"},
"args": [
{ "name": "size",
"type": {"name": "oc_vec2", "tag": "S"}}
]
},
{
"name": "oc_scancode_to_keycode",
"cname": "oc_scancode_to_keycode",
"ret": { "name": "oc_key_code", "tag": "i"},
"args": [
{ "name": "scanCode",
"type": {"name": "oc_scan_code", "tag": "i"}}
]
},
{
"name": "oc_clipboard_get_string",
"cname": "oc_bridge_clipboard_get_string",
"ret": { "name": "oc_str8", "cname": "oc_wasm_str8", "tag": "S"},
"args": [
{"name": "arena",
"type": {"name": "oc_arena*", "cname": "i32", "tag": "i"}}
]
},
{
"name": "oc_clipboard_set_string",
"cname": "oc_bridge_clipboard_set_string",
"ret": {"name": "void", "tag": "v"},
"args": [
{ "name": "value",
"type": {"name": "oc_str8", "cname": "oc_wasm_str8", "tag": "S"}}
]
}
]
{
"name": "oc_bridge_log",
"cname": "oc_bridge_log",
"ret": {
"name": "void",
"tag": "v"
},
"args": [
{
"name": "level",
"type": {
"name": "oc_log_level",
"tag": "i"
}
},
{
"name": "functionLen",
"type": {
"name": "int",
"tag": "i"
}
},
{
"name": "function",
"type": {
"name": "char*",
"tag": "p"
},
"len": {
"count": "functionLen"
}
},
{
"name": "fileLen",
"type": {
"name": "int",
"tag": "i"
}
},
{
"name": "file",
"type": {
"name": "char*",
"tag": "p"
},
"len": {
"count": "fileLen"
}
},
{
"name": "line",
"type": {
"name": "int",
"tag": "i"
}
},
{
"name": "msgLen",
"type": {
"name": "int",
"tag": "i"
}
},
{
"name": "msg",
"type": {
"name": "char*",
"tag": "p"
},
"len": {
"count": "msgLen"
}
}
]
},
{
"name": "oc_mem_grow",
"cname": "oc_mem_grow",
"ret": {
"name": "int",
"tag": "i"
},
"args": [
{
"name": "size",
"type": {
"name": "u64",
"tag": "I"
}
}
]
},
{
"name": "oc_bridge_assert_fail",
"cname": "oc_assert_fail",
"ret": {
"name": "void",
"tag": "v"
},
"args": [
{
"name": "file",
"type": {
"name": "const char*",
"tag": "p"
},
"len": {
"proc": "orca_check_cstring",
"args": [
"file"
]
}
},
{
"name": "function",
"type": {
"name": "const char*",
"tag": "p"
},
"len": {
"proc": "orca_check_cstring",
"args": [
"function"
]
}
},
{
"name": "line",
"type": {
"name": "int",
"tag": "i"
}
},
{
"name": "src",
"type": {
"name": "const char*",
"tag": "p"
},
"len": {
"proc": "orca_check_cstring",
"args": [
"src"
]
}
},
{
"name": "note",
"type": {
"name": "const char*",
"tag": "p"
},
"len": {
"proc": "orca_check_cstring",
"args": [
"note"
]
}
}
]
},
{
"name": "oc_bridge_abort_ext",
"cname": "oc_abort_ext",
"ret": {
"name": "void",
"tag": "v"
},
"args": [
{
"name": "file",
"type": {
"name": "const char*",
"tag": "p"
},
"len": {
"proc": "orca_check_cstring",
"args": [
"file"
]
}
},
{
"name": "function",
"type": {
"name": "const char*",
"tag": "p"
},
"len": {
"proc": "orca_check_cstring",
"args": [
"function"
]
}
},
{
"name": "line",
"type": {
"name": "int",
"tag": "i"
}
},
{
"name": "note",
"type": {
"name": "const char*",
"tag": "p"
},
"len": {
"proc": "orca_check_cstring",
"args": [
"note"
]
}
}
]
},
{
"name": "oc_get_host_platform",
"cname": "oc_get_host_platform",
"ret": {
"name": "int",
"tag": "i"
},
"args": []
},
{
"name": "oc_request_quit",
"cname": "oc_bridge_request_quit",
"ret": {
"name": "void",
"tag": "v"
},
"args": []
},
{
"name": "oc_window_set_title",
"cname": "oc_bridge_window_set_title",
"ret": {
"name": "void",
"tag": "v"
},
"args": [
{
"name": "title",
"type": {
"name": "oc_str8",
"cname": "oc_wasm_str8",
"tag": "S"
}
}
]
},
{
"name": "oc_window_set_size",
"cname": "oc_bridge_window_set_size",
"ret": {
"name": "void",
"tag": "v"
},
"args": [
{
"name": "size",
"type": {
"name": "oc_vec2",
"tag": "S"
}
}
]
},
{
"name": "oc_scancode_to_keycode",
"cname": "oc_scancode_to_keycode",
"ret": {
"name": "oc_key_code",
"tag": "i"
},
"args": [
{
"name": "scanCode",
"type": {
"name": "oc_scan_code",
"tag": "i"
}
}
]
},
{
"name": "oc_clipboard_get_string",
"cname": "oc_bridge_clipboard_get_string",
"ret": {
"name": "oc_str8",
"cname": "oc_wasm_str8",
"tag": "S"
},
"args": [
{
"name": "arena",
"type": {
"name": "oc_arena*",
"cname": "i32",
"tag": "i"
}
}
]
},
{
"name": "oc_clipboard_set_string",
"cname": "oc_bridge_clipboard_set_string",
"ret": {
"name": "void",
"tag": "v"
},
"args": [
{
"name": "value",
"type": {
"name": "oc_str8",
"cname": "oc_wasm_str8",
"tag": "S"
}
}
]
}
]

View File

@ -0,0 +1,29 @@
[
{
"name": "oc_run_cmd",
"cname": "oc_bridge_run_cmd",
"ret": {
"name": "oc_str8",
"cname": "oc_wasm_str8",
"tag": "S"
},
"args": [
{
"name": "arena",
"type": {
"name": "oc_arena*",
"cname": "i32",
"tag": "i"
}
},
{
"name": "cmd",
"type": {
"name": "oc_str8",
"cname": "oc_wasm_str8",
"tag": "S"
}
}
]
}
]