This commit is contained in:
liang.he 2025-05-08 01:06:57 +08:00 committed by GitHub
commit f84d905a97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 91 additions and 40 deletions

View File

@ -4195,13 +4195,13 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
bh_print_time("Begin to emit object file"); bh_print_time("Begin to emit object file");
if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) { if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
char cmd[1024];
int ret; int ret;
if (comp_ctx->external_llc_compiler) { if (comp_ctx->external_llc_compiler) {
const char *stack_usage_flag = ""; char *stack_usage_flag = "";
char bc_file_name[64]; char bc_file_name[64] = { 0 };
char su_file_name[65]; /* See the comment below */ char su_file_name[65] = { 0 };
char *argv[10] = { 0 };
if (comp_ctx->stack_usage_file != NULL) { if (comp_ctx->stack_usage_file != NULL) {
/* /*
@ -4229,14 +4229,16 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
return false; return false;
} }
snprintf(cmd, sizeof(cmd), "%s%s %s -o %s %s", argv[0] = stack_usage_flag;
comp_ctx->external_llc_compiler, stack_usage_flag, argv[1] = comp_ctx->llc_compiler_flags
comp_ctx->llc_compiler_flags ? comp_ctx->llc_compiler_flags ? (char *)comp_ctx->llc_compiler_flags
: "-O3 -c", : "-O3 -c";
file_name, bc_file_name); argv[2] = "-o";
LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd); argv[3] = file_name;
argv[4] = bc_file_name;
argv[5] = NULL;
ret = bh_system(cmd); ret = bh_execve(comp_ctx->external_llc_compiler, argv, 6);
/* remove temp bitcode file */ /* remove temp bitcode file */
unlink(bc_file_name); unlink(bc_file_name);
@ -4263,7 +4265,8 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
} }
} }
else if (comp_ctx->external_asm_compiler) { else if (comp_ctx->external_asm_compiler) {
char asm_file_name[64]; char asm_file_name[64] = { 0 };
char *argv[10] = { 0 };
if (!aot_generate_tempfile_name("wamrc-asm", "s", asm_file_name, if (!aot_generate_tempfile_name("wamrc-asm", "s", asm_file_name,
sizeof(asm_file_name))) { sizeof(asm_file_name))) {
@ -4282,14 +4285,15 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
return false; return false;
} }
snprintf(cmd, sizeof(cmd), "%s %s -o %s %s", argv[0] = comp_ctx->asm_compiler_flags
comp_ctx->external_asm_compiler, ? (char *)comp_ctx->asm_compiler_flags
comp_ctx->asm_compiler_flags ? comp_ctx->asm_compiler_flags : "-O3 -c";
: "-O3 -c", argv[1] = "-o";
file_name, asm_file_name); argv[2] = file_name;
LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd); argv[3] = asm_file_name;
argv[4] = NULL;
ret = bh_system(cmd); ret = bh_execve(comp_ctx->external_asm_compiler, argv, 5);
/* remove temp assembly file */ /* remove temp assembly file */
unlink(asm_file_name); unlink(asm_file_name);

View File

@ -4352,18 +4352,23 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) { else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) {
/* Emit to assembly file instead for arc target /* Emit to assembly file instead for arc target
as it cannot emit to object file */ as it cannot emit to object file */
char file_name[] = "wasm-XXXXXX", buf[128]; char file_name[] = "wasm-XXXXXX";
char assembly_file_name[64] = { 0 };
char object_file_name[64] = { 0 };
int ret; int ret;
char *argv[] = { "-mcpu=arcem", "-o", object_file_name, "-c",
assembly_file_name, NULL };
if (!bh_mkstemp(file_name, sizeof(file_name))) { if (!bh_mkstemp(file_name, sizeof(file_name))) {
aot_set_last_error("make temp file failed."); aot_set_last_error("make temp file failed.");
goto fail; goto fail;
} }
snprintf(buf, sizeof(buf), "%s%s", file_name, ".s"); snprintf(assembly_file_name, sizeof(assembly_file_name) - 1, "%s.s",
file_name);
if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine, if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
comp_ctx->module, buf, LLVMAssemblyFile, comp_ctx->module, assembly_file_name,
&err) LLVMAssemblyFile, &err)
!= 0) { != 0) {
if (err) { if (err) {
LLVMDisposeMessage(err); LLVMDisposeMessage(err);
@ -4376,14 +4381,14 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
/* call arc gcc to compile assembly file to object file */ /* call arc gcc to compile assembly file to object file */
/* TODO: get arc gcc from environment variable firstly /* TODO: get arc gcc from environment variable firstly
and check whether the toolchain exists actually */ and check whether the toolchain exists actually */
snprintf(buf, sizeof(buf), "%s%s%s%s%s%s", snprintf(object_file_name, sizeof(object_file_name) - 1, "%s.o",
"/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ", file_name);
"-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s");
/* TODO: use try..catch to handle possible exceptions */ /* TODO: use try..catch to handle possible exceptions */
ret = bh_system(buf); /* TODO: use ZEPHYR_SDK_INSTALL_DIR to construct the path */
ret = bh_execve("/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc",
argv, 6);
/* remove temp assembly file */ /* remove temp assembly file */
snprintf(buf, sizeof(buf), "%s%s", file_name, ".s"); unlink(assembly_file_name);
unlink(buf);
if (ret != 0) { if (ret != 0) {
aot_set_last_error("failed to compile asm file to obj file " aot_set_last_error("failed to compile asm file to obj file "
@ -4392,12 +4397,10 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
} }
/* create memory buffer from object file */ /* create memory buffer from object file */
snprintf(buf, sizeof(buf), "%s%s", file_name, ".o"); ret = LLVMCreateMemoryBufferWithContentsOfFile(
ret = LLVMCreateMemoryBufferWithContentsOfFile(buf, &obj_data->mem_buf, object_file_name, &obj_data->mem_buf, &err);
&err);
/* remove temp object file */ /* remove temp object file */
snprintf(buf, sizeof(buf), "%s%s", file_name, ".o"); unlink(object_file_name);
unlink(buf);
if (ret != 0) { if (ret != 0) {
if (err) { if (err) {

View File

@ -167,15 +167,39 @@ wa_strdup(const char *s)
} }
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
/* need to make sure that The `argv[]` must be terminated by a NULL pointer. */
int int
bh_system(const char *cmd) bh_execve(const char *pathname, char *const argv[], int argc)
{ {
int ret; int ret;
/* no environment variables */
char *const envp[] = { NULL };
if (pathname == NULL) {
return -1;
}
if (argc > 0) {
if (argv == NULL) {
return -1;
}
/* The `argv[]` must be terminated by a NULL pointer. */
if (argv[argc - 1] != NULL) {
return -1;
}
}
#if !(defined(_WIN32) || defined(_WIN32_)) #if !(defined(_WIN32) || defined(_WIN32_))
ret = system(cmd); ret = execve(pathname, argv, envp);
#ifndef NDEBUG
if (ret == -1) {
LOG_WARNING("execute \"%s\" failed because of \"%s\"", pathname,
strerror(errno));
}
#endif
#else #else
ret = _spawnlp(_P_WAIT, "cmd.exe", "/c", cmd, NULL); ret = _execve(pathname, argv, envp);
#endif #endif
return ret; return ret;

View File

@ -67,9 +67,19 @@ char *
wa_strdup(const char *s); wa_strdup(const char *s);
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
/* Executes a system command in bash/cmd.exe */ /*
* Executes a program referred to by cmd in bash/cmd.exe
* Always be sure that argv[argc-1] == NULL
*
* @param pathname The program to execute. need to be absolute path.
* @param argv The command line arguments.
* @param argc The number of command line arguments.
*
* like to execute "ls -l /tmp":
* bh_execve("/bin/ls", (char *const []){ "-l", "/tmp", NULL }, 3);
*/
int int
bh_system(const char *cmd); bh_execve(const char *pathname, char *const argv[], int argc);
/* Tests whether can create a temporary file with the given name */ /* Tests whether can create a temporary file with the given name */
bool bool

View File

@ -25,6 +25,6 @@ set (unit_test_sources
add_executable (shared_utils_test ${unit_test_sources}) add_executable (shared_utils_test ${unit_test_sources})
target_link_libraries (shared_utils_test gtest_main) target_link_libraries (shared_utils_test gtest_main ${LLVM_AVAILABLE_LIBS})
gtest_discover_tests(shared_utils_test) gtest_discover_tests(shared_utils_test)

View File

@ -94,3 +94,13 @@ TEST_F(bh_common_test_suite, b_memcpy_s)
EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), nullptr, sizeof(STR_TEST))); EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), nullptr, sizeof(STR_TEST)));
EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), STR_TEST, sizeof(dest) + 1)); EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), STR_TEST, sizeof(dest) + 1));
} }
TEST_F(bh_common_test_suite, bh_execve)
{
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
char *const argv[] = { "-n", "\"hello\"", "world", "\n", nullptr };
EXPECT_EQ(0, bh_execve("/usr/bin/echo", argv, 5));
#else
GTEST_SKIP() << "bh_execve() is not supported";
#endif
}