Skip to content

Commit f2b87d7

Browse files
Support external toolchain on Windows for aot compiler (#3911)
allowing custom ARC toolchain on Windows
1 parent 2975e2f commit f2b87d7

File tree

7 files changed

+97
-68
lines changed

7 files changed

+97
-68
lines changed

build-scripts/build_llvm.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,27 @@ def build_llvm(llvm_dir, platform, backends, projects, use_clang=False, extra_fl
102102
"default": [],
103103
}
104104

105+
experimental_backends = ["ARC", "Xtensa"]
106+
normal_backends = [s for s in backends if s not in experimental_backends]
107+
105108
LLVM_TARGETS_TO_BUILD = [
106-
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(backends) + '"'
107-
if backends
109+
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(normal_backends) + '"'
110+
if normal_backends
108111
else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;X86"'
109112
]
110113

114+
# if not on ARC platform, but want to add expeirmental backend ARC as target
115+
if platform != "ARC" and "ARC" in backends:
116+
LLVM_TARGETS_TO_BUILD.extend(
117+
LLVM_EXTRA_COMPILE_OPTIONS["arc"]
118+
)
119+
120+
if platform != "Xtensa" and "Xtensa" in backends:
121+
print(
122+
"Currently it's not supported to build Xtensa backend on non-Xtensa platform"
123+
)
124+
return None
125+
111126
LLVM_PROJECTS_TO_BUILD = [
112127
'-DLLVM_ENABLE_PROJECTS:STRING="' + ";".join(projects) + '"' if projects else ""
113128
]
@@ -240,6 +255,7 @@ def main():
240255
"X86",
241256
"Xtensa",
242257
],
258+
default=[],
243259
help="identify LLVM supported backends, separate by space, like '--arch ARM Mips X86'",
244260
)
245261
parser.add_argument(

core/iwasm/compilation/aot_compiler.c

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,39 +4093,6 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
40934093
return true;
40944094
}
40954095

4096-
#if !(defined(_WIN32) || defined(_WIN32_))
4097-
char *
4098-
aot_generate_tempfile_name(const char *prefix, const char *extension,
4099-
char *buffer, uint32 len)
4100-
{
4101-
int fd, name_len;
4102-
4103-
name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);
4104-
4105-
if ((fd = mkstemp(buffer)) <= 0) {
4106-
aot_set_last_error("make temp file failed.");
4107-
return NULL;
4108-
}
4109-
4110-
/* close and remove temp file */
4111-
close(fd);
4112-
unlink(buffer);
4113-
4114-
/* Check if buffer length is enough */
4115-
/* name_len + '.' + extension + '\0' */
4116-
if (name_len + 1 + strlen(extension) + 1 > len) {
4117-
aot_set_last_error("temp file name too long.");
4118-
return NULL;
4119-
}
4120-
4121-
snprintf(buffer + name_len, len - name_len, ".%s", extension);
4122-
return buffer;
4123-
}
4124-
#else
4125-
4126-
errno_t
4127-
_mktemp_s(char *nameTemplate, size_t sizeInChars);
4128-
41294096
char *
41304097
aot_generate_tempfile_name(const char *prefix, const char *extension,
41314098
char *buffer, uint32 len)
@@ -4134,7 +4101,8 @@ aot_generate_tempfile_name(const char *prefix, const char *extension,
41344101

41354102
name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);
41364103

4137-
if (_mktemp_s(buffer, name_len + 1) != 0) {
4104+
if (!bh_mkstemp(buffer, name_len + 1)) {
4105+
aot_set_last_error("make temp file failed.");
41384106
return NULL;
41394107
}
41404108

@@ -4148,7 +4116,6 @@ aot_generate_tempfile_name(const char *prefix, const char *extension,
41484116
snprintf(buffer + name_len, len - name_len, ".%s", extension);
41494117
return buffer;
41504118
}
4151-
#endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */
41524119

41534120
bool
41544121
aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name)
@@ -4227,7 +4194,6 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
42274194

42284195
bh_print_time("Begin to emit object file");
42294196

4230-
#if !(defined(_WIN32) || defined(_WIN32_))
42314197
if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
42324198
char cmd[1024];
42334199
int ret;
@@ -4270,7 +4236,7 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
42704236
file_name, bc_file_name);
42714237
LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd);
42724238

4273-
ret = system(cmd);
4239+
ret = bh_system(cmd);
42744240
/* remove temp bitcode file */
42754241
unlink(bc_file_name);
42764242

@@ -4323,7 +4289,7 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
43234289
file_name, asm_file_name);
43244290
LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd);
43254291

4326-
ret = system(cmd);
4292+
ret = bh_system(cmd);
43274293
/* remove temp assembly file */
43284294
unlink(asm_file_name);
43294295

@@ -4336,7 +4302,6 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
43364302

43374303
return true;
43384304
}
4339-
#endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */
43404305

43414306
if (!strncmp(LLVMGetTargetName(target), "arc", 3))
43424307
/* Emit to assembly file instead for arc target

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4292,10 +4292,6 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
42924292

42934293
bh_print_time("Begin to emit object file");
42944294
if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
4295-
#if defined(_WIN32) || defined(_WIN32_)
4296-
aot_set_last_error("external toolchain not supported on Windows");
4297-
goto fail;
4298-
#else
42994295
/* Generate a temp file name */
43004296
int ret;
43014297
char obj_file_name[64];
@@ -4323,27 +4319,18 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
43234319
aot_set_last_error("create mem buffer with file failed.");
43244320
goto fail;
43254321
}
4326-
#endif /* end of defined(_WIN32) || defined(_WIN32_) */
43274322
}
43284323
else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) {
4329-
#if defined(_WIN32) || defined(_WIN32_)
4330-
aot_set_last_error("emit object file on Windows is unsupported.");
4331-
goto fail;
4332-
#else
43334324
/* Emit to assembly file instead for arc target
43344325
as it cannot emit to object file */
43354326
char file_name[] = "wasm-XXXXXX", buf[128];
4336-
int fd, ret;
4327+
int ret;
43374328

4338-
if ((fd = mkstemp(file_name)) <= 0) {
4329+
if (!bh_mkstemp(file_name, sizeof(file_name))) {
43394330
aot_set_last_error("make temp file failed.");
43404331
goto fail;
43414332
}
43424333

4343-
/* close and remove temp file */
4344-
close(fd);
4345-
unlink(file_name);
4346-
43474334
snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
43484335
if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
43494336
comp_ctx->module, buf, LLVMAssemblyFile,
@@ -4364,7 +4351,7 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
43644351
"/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ",
43654352
"-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s");
43664353
/* TODO: use try..catch to handle possible exceptions */
4367-
ret = system(buf);
4354+
ret = bh_system(buf);
43684355
/* remove temp assembly file */
43694356
snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
43704357
unlink(buf);
@@ -4391,7 +4378,6 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
43914378
aot_set_last_error("create mem buffer with file failed.");
43924379
goto fail;
43934380
}
4394-
#endif /* end of defined(_WIN32) || defined(_WIN32_) */
43954381
}
43964382
else {
43974383
if (LLVMTargetMachineEmitToMemoryBuffer(

core/iwasm/compilation/aot_llvm.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,10 +2746,6 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
27462746
/* verify external llc compiler */
27472747
comp_ctx->external_llc_compiler = getenv("WAMRC_LLC_COMPILER");
27482748
if (comp_ctx->external_llc_compiler) {
2749-
#if defined(_WIN32) || defined(_WIN32_)
2750-
comp_ctx->external_llc_compiler = NULL;
2751-
LOG_WARNING("External LLC compiler not supported on Windows.");
2752-
#else
27532749
if (access(comp_ctx->external_llc_compiler, X_OK) != 0) {
27542750
LOG_WARNING("WAMRC_LLC_COMPILER [%s] not found, fallback to "
27552751
"default pipeline",
@@ -2761,17 +2757,12 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
27612757
LOG_VERBOSE("Using external LLC compiler [%s]",
27622758
comp_ctx->external_llc_compiler);
27632759
}
2764-
#endif
27652760
}
27662761

27672762
/* verify external asm compiler */
27682763
if (!comp_ctx->external_llc_compiler) {
27692764
comp_ctx->external_asm_compiler = getenv("WAMRC_ASM_COMPILER");
27702765
if (comp_ctx->external_asm_compiler) {
2771-
#if defined(_WIN32) || defined(_WIN32_)
2772-
comp_ctx->external_asm_compiler = NULL;
2773-
LOG_WARNING("External ASM compiler not supported on Windows.");
2774-
#else
27752766
if (access(comp_ctx->external_asm_compiler, X_OK) != 0) {
27762767
LOG_WARNING(
27772768
"WAMRC_ASM_COMPILER [%s] not found, fallback to "
@@ -2784,7 +2775,6 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
27842775
LOG_VERBOSE("Using external ASM compiler [%s]",
27852776
comp_ctx->external_asm_compiler);
27862777
}
2787-
#endif
27882778
}
27892779
}
27902780

core/iwasm/compilation/aot_llvm.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@
3737
#include "aot_orc_extra.h"
3838
#include "aot_comp_option.h"
3939

40+
#if defined(_WIN32) || defined(_WIN32_)
41+
#include <io.h>
42+
#define access _access
43+
/* On windows there is no X_OK flag to check for executablity, only check for
44+
* existence */
45+
#ifdef X_OK
46+
#undef X_OK
47+
#endif
48+
#define X_OK 00
49+
#define unlink _unlink
50+
#endif
51+
4052
#ifdef __cplusplus
4153
extern "C" {
4254
#endif

core/shared/utils/bh_common.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,53 @@ wa_strdup(const char *s)
165165
}
166166
return s1;
167167
}
168+
169+
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
170+
int
171+
bh_system(const char *cmd)
172+
{
173+
int ret;
174+
175+
#if !(defined(_WIN32) || defined(_WIN32_))
176+
ret = system(cmd);
177+
#else
178+
ret = _spawnlp(_P_WAIT, "cmd.exe", "/c", cmd, NULL);
179+
#endif
180+
181+
return ret;
182+
}
183+
184+
#if defined(_WIN32) || defined(_WIN32_)
185+
errno_t
186+
_mktemp_s(char *nameTemplate, size_t sizeInChars);
187+
#endif
188+
189+
bool
190+
bh_mkstemp(char *file_name, size_t name_len)
191+
{
192+
int fd;
193+
194+
#if !(defined(_WIN32) || defined(_WIN32_))
195+
(void)name_len;
196+
/* On Linux, it generates a unique temporary filename from template, creates
197+
* and opens the file, and returns an open file descriptor for the file. */
198+
if ((fd = mkstemp(file_name)) <= 0) {
199+
goto fail;
200+
}
201+
202+
/* close and remove temp file */
203+
close(fd);
204+
unlink(file_name);
205+
#else
206+
/* On Windows, it generates a unique temporary file name but does not create
207+
* or open the file */
208+
if (_mktemp_s(file_name, name_len) != 0) {
209+
goto fail;
210+
}
211+
#endif
212+
213+
return true;
214+
fail:
215+
return false;
216+
}
217+
#endif /* End of WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 */

core/shared/utils/bh_common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ bh_strdup(const char *s);
6666
char *
6767
wa_strdup(const char *s);
6868

69+
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
70+
/* Executes a system command in bash/cmd.exe */
71+
int
72+
bh_system(const char *cmd);
73+
74+
/* Tests whether can create a temporary file with the given name */
75+
bool
76+
bh_mkstemp(char *filename, size_t name_len);
77+
#endif
78+
6979
#ifdef __cplusplus
7080
}
7181
#endif

0 commit comments

Comments
 (0)