| 303 | } |
| 304 | |
| 305 | Expect<void> outputWasmLibrary(LLVM::Context LLContext, |
| 306 | const std::filesystem::path &OutputPath, |
| 307 | Span<const Byte> Data, |
| 308 | const LLVM::MemoryBuffer &OSVec) noexcept { |
| 309 | std::filesystem::path SharedObjectName; |
| 310 | { |
| 311 | // tempfile |
| 312 | std::filesystem::path SOPath(OutputPath); |
| 313 | SOPath.replace_extension("%%%%%%%%%%" WASMEDGE_LIB_EXTENSION); |
| 314 | SharedObjectName = createTemp(SOPath); |
| 315 | if (SharedObjectName.empty()) { |
| 316 | // TODO:return error |
| 317 | spdlog::error("so file creation failed:{}"sv, SOPath.u8string()); |
| 318 | return Unexpect(ErrCode::Value::IllegalPath); |
| 319 | } |
| 320 | std::ofstream OS(SharedObjectName, std::ios_base::binary); |
| 321 | OS.write(OSVec.data(), static_cast<std::streamsize>(OSVec.size())); |
| 322 | OS.close(); |
| 323 | } |
| 324 | |
| 325 | EXPECTED_TRY(outputNativeLibrary(SharedObjectName, OSVec)); |
| 326 | |
| 327 | LLVM::MemoryBuffer SOFile; |
| 328 | if (auto [Res, ErrorMessage] = |
| 329 | LLVM::MemoryBuffer::getFile(SharedObjectName.u8string().c_str()); |
| 330 | unlikely(ErrorMessage)) { |
| 331 | spdlog::error("object file open error:{}"sv, ErrorMessage.string_view()); |
| 332 | return Unexpect(ErrCode::Value::IllegalPath); |
| 333 | } else { |
| 334 | SOFile = std::move(Res); |
| 335 | } |
| 336 | |
| 337 | LLVM::Binary ObjFile; |
| 338 | if (auto [Res, ErrorMessage] = LLVM::Binary::create(SOFile, LLContext); |
| 339 | unlikely(ErrorMessage)) { |
| 340 | spdlog::error("object file parse error:{}"sv, ErrorMessage.string_view()); |
| 341 | return Unexpect(ErrCode::Value::IllegalPath); |
| 342 | } else { |
| 343 | ObjFile = std::move(Res); |
| 344 | } |
| 345 | |
| 346 | std::string OSCustomSecVec; |
| 347 | { |
| 348 | std::ostringstream OS; |
| 349 | WriteName(OS, "wasmedge"sv); |
| 350 | WriteU32(OS, AOT::kBinaryVersion); |
| 351 | |
| 352 | #if WASMEDGE_OS_LINUX |
| 353 | WriteByte(OS, UINT8_C(1)); |
| 354 | #elif WASMEDGE_OS_MACOS |
| 355 | WriteByte(OS, UINT8_C(2)); |
| 356 | #elif WASMEDGE_OS_WINDOWS |
| 357 | WriteByte(OS, UINT8_C(3)); |
| 358 | #else |
| 359 | #error Unsupported operating system! |
| 360 | #endif |
| 361 | |
| 362 | #if defined(__x86_64__) |
no test coverage detected