Skip to content

Commit cb51dbb

Browse files
authored
Fix some issues on MacOS platform (#937)
Fix some issues on MacOS platform - Enable libc-wasi by default - Set target abi to "gnu" if it is not set for wamrc to avoid generating object file of unsupported Mach-O format - Set `<vendor>-<sys>` info according to target abi for wamrc to support generating AOT file for other OSs but not current host - Set cpu name if arch/abi/cpu are not set to avoid checking SIMD capability failed - Set size level to 1 for MacOS/Windows platform to avoid relocation type unsupported warning - Clear posix_memmap.c compiling warning - Fix spec case test script issues, enable test spec cases on MacOS Signed-off-by: Wenyong Huang <[email protected]>
1 parent 308d31c commit cb51dbb

File tree

6 files changed

+106
-10
lines changed

6 files changed

+106
-10
lines changed

core/iwasm/compilation/aot_llvm.c

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,74 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
16101610
abi = "ilp32d";
16111611
}
16121612

1613-
if (arch) {
1613+
#if defined(__APPLE__) || defined(__MACH__)
1614+
if (!abi) {
1615+
/* On MacOS platform, set abi to "gnu" to avoid generating
1616+
object file of Mach-O binary format which is unsupported */
1617+
abi = "gnu";
1618+
if (!arch && !cpu && !features) {
1619+
/* Get CPU name of the host machine to avoid checking
1620+
SIMD capability failed */
1621+
if (!(cpu = cpu_new = LLVMGetHostCPUName())) {
1622+
aot_set_last_error("llvm get host cpu name failed.");
1623+
goto fail;
1624+
}
1625+
}
1626+
}
1627+
#endif
1628+
1629+
if (abi) {
1630+
/* Construct target triple: <arch>-<vendor>-<sys>-<abi> */
1631+
const char *vendor_sys;
1632+
char *arch1 = arch, default_arch[32] = { 0 };
1633+
1634+
if (!arch1) {
1635+
char *default_triple = LLVMGetDefaultTargetTriple();
1636+
1637+
if (!default_triple) {
1638+
aot_set_last_error(
1639+
"llvm get default target triple failed.");
1640+
goto fail;
1641+
}
1642+
1643+
vendor_sys = strstr(default_triple, "-");
1644+
bh_assert(vendor_sys);
1645+
bh_memcpy_s(default_arch, sizeof(default_arch), default_triple,
1646+
vendor_sys - default_triple);
1647+
arch1 = default_arch;
1648+
1649+
LLVMDisposeMessage(default_triple);
1650+
}
1651+
1652+
/**
1653+
* Set <vendor>-<sys> according to abi to generate the object file
1654+
* with the correct file format which might be different from the
1655+
* default object file format of the host, e.g., generating AOT file
1656+
* for Windows/MacOS under Linux host, or generating AOT file for
1657+
* Linux/MacOS under Windows host.
1658+
*/
1659+
if (!strcmp(abi, "msvc")) {
1660+
if (!strcmp(arch1, "i386"))
1661+
vendor_sys = "-pc-win32-";
1662+
else
1663+
vendor_sys = "-pc-windows-";
1664+
}
1665+
else {
1666+
vendor_sys = "-pc-linux-";
1667+
}
1668+
1669+
bh_assert(strlen(arch1) + strlen(vendor_sys) + strlen(abi)
1670+
< sizeof(triple_buf));
1671+
bh_memcpy_s(triple_buf, sizeof(triple_buf), arch1, strlen(arch1));
1672+
bh_memcpy_s(triple_buf + strlen(arch1),
1673+
sizeof(triple_buf) - strlen(arch1), vendor_sys,
1674+
strlen(vendor_sys));
1675+
bh_memcpy_s(triple_buf + strlen(arch1) + strlen(vendor_sys),
1676+
sizeof(triple_buf) - strlen(arch1) - strlen(vendor_sys),
1677+
abi, strlen(abi));
1678+
triple = triple_buf;
1679+
}
1680+
else if (arch) {
16141681
/* Construct target triple: <arch>-<vendor>-<sys>-<abi> */
16151682
const char *vendor_sys;
16161683
char *default_triple = LLVMGetDefaultTargetTriple();
@@ -1640,10 +1707,13 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
16401707

16411708
bh_assert(strlen(arch) + strlen(vendor_sys) + strlen(abi)
16421709
< sizeof(triple_buf));
1643-
memcpy(triple_buf, arch, strlen(arch));
1644-
memcpy(triple_buf + strlen(arch), vendor_sys, strlen(vendor_sys));
1645-
memcpy(triple_buf + strlen(arch) + strlen(vendor_sys), abi,
1646-
strlen(abi));
1710+
bh_memcpy_s(triple_buf, sizeof(triple_buf), arch, strlen(arch));
1711+
bh_memcpy_s(triple_buf + strlen(arch),
1712+
sizeof(triple_buf) - strlen(arch), vendor_sys,
1713+
strlen(vendor_sys));
1714+
bh_memcpy_s(triple_buf + strlen(arch) + strlen(vendor_sys),
1715+
sizeof(triple_buf) - strlen(arch) - strlen(vendor_sys),
1716+
abi, strlen(abi));
16471717
triple = triple_buf;
16481718
}
16491719

core/shared/platform/common/posix/posix_memmap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static size_t total_size_munmapped = 0;
1616

1717
#define HUGE_PAGE_SIZE (2 * 1024 * 1024)
1818

19+
#if !defined(__APPLE__) && !defined(__NuttX__)
1920
static inline uintptr_t
2021
round_up(uintptr_t v, uintptr_t b)
2122
{
@@ -29,6 +30,7 @@ round_down(uintptr_t v, uintptr_t b)
2930
uintptr_t m = b - 1;
3031
return v & ~m;
3132
}
33+
#endif
3234

3335
void *
3436
os_mmap(void *hint, size_t size, int prot, int flags)

product-mini/platforms/darwin/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
5555
endif ()
5656

5757
if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
58-
# Disable libc wasi support by default
59-
set (WAMR_BUILD_LIBC_WASI 0)
58+
# Enable libc wasi support by default
59+
set (WAMR_BUILD_LIBC_WASI 1)
6060
endif ()
6161

6262
if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
@@ -65,7 +65,7 @@ if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
6565
endif ()
6666

6767
if (NOT DEFINED WAMR_BUILD_MULTI_MODULE)
68-
# Enable multiple modules
68+
# Disable multiple module by default
6969
set (WAMR_BUILD_MULTI_MODULE 0)
7070
endif ()
7171

tests/wamr-test-suites/spec-test-script/all.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
The script itself has to be put under the same directory with the "spec".
2222
"""
2323

24-
IWASM_CMD = "../../../product-mini/platforms/linux/build/iwasm"
24+
PLATFORM_NAME = os.uname().sysname.lower()
25+
IWASM_CMD = "../../../product-mini/platforms/" + PLATFORM_NAME + "/build/iwasm"
2526
IWASM_SGX_CMD = "../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm"
2627
SPEC_TEST_DIR = "spec/test/core"
2728
WAST2WASM_CMD = "./wabt/out/gcc/Release/wat2wasm"

tests/wamr-test-suites/spec-test-script/runtest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ def cleanup(self):
141141
os.killpg(self.p.pid, signal.SIGTERM)
142142
except OSError:
143143
pass
144+
except IOError:
145+
pass
144146
self.p = None
145147
self.stdin.close()
146148
if self.stdin != self.stdout:

wamr-compiler/main.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ main(int argc, char *argv[])
7979
AOTCompOption option = { 0 };
8080
char error_buf[128];
8181
int log_verbose_level = 2;
82-
bool sgx_mode = false;
82+
bool sgx_mode = false, size_level_set = false;
8383
int exit_status = EXIT_FAILURE;
8484

8585
option.opt_level = 3;
@@ -133,6 +133,7 @@ main(int argc, char *argv[])
133133
option.size_level = (uint32)atoi(argv[0] + 13);
134134
if (option.size_level > 3)
135135
option.size_level = 3;
136+
size_level_set = true;
136137
}
137138
else if (!strcmp(argv[0], "-sgx")) {
138139
sgx_mode = true;
@@ -207,6 +208,26 @@ main(int argc, char *argv[])
207208
if (argc == 0 || !out_file_name)
208209
return print_help();
209210

211+
if (!size_level_set) {
212+
/**
213+
* Set opt level to 1 by default for Windows and MacOS as
214+
* they can not memory map out 0-2GB memory and might not
215+
* be able to meet the requirements of some AOT relocation
216+
* operations.
217+
*/
218+
if (option.target_abi && !strcmp(option.target_abi, "msvc")) {
219+
LOG_VERBOSE("Set size level to 1 for Windows AOT file");
220+
option.size_level = 1;
221+
}
222+
#if defined(_WIN32) || defined(_WIN32_) || defined(__APPLE__) \
223+
|| defined(__MACH__)
224+
if (!option.target_abi) {
225+
LOG_VERBOSE("Set size level to 1 for Windows or MacOS AOT file");
226+
option.size_level = 1;
227+
}
228+
#endif
229+
}
230+
210231
if (sgx_mode) {
211232
option.size_level = 1;
212233
option.is_sgx_platform = true;

0 commit comments

Comments
 (0)