summaryrefslogtreecommitdiff
path: root/core/src/xmake/binutils/extractlib.c
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-11 22:45:15 +0800
committerruki <[email protected]>2025-12-12 09:00:43 +0800
commitaf99153ccd7fbc82c7374eac23e6308e37fc061a (patch)
tree89004fc53742f316b4773ad564138c204c0b6b58 /core/src/xmake/binutils/extractlib.c
parenta6262f1fadbd0ae360dc336da298b86d4b9c1628 (diff)
improve extractlib
Diffstat (limited to 'core/src/xmake/binutils/extractlib.c')
-rw-r--r--core/src/xmake/binutils/extractlib.c42
1 files changed, 21 insertions, 21 deletions
diff --git a/core/src/xmake/binutils/extractlib.c b/core/src/xmake/binutils/extractlib.c
index 9385bc60f..e1d76c28e 100644
--- a/core/src/xmake/binutils/extractlib.c
+++ b/core/src/xmake/binutils/extractlib.c
@@ -50,15 +50,15 @@ extern tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t co
*/
tb_int_t xm_binutils_extractlib(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
-
+
// get the library file path
tb_char_t const *libraryfile = luaL_checkstring(lua, 1);
tb_check_return_val(libraryfile, 0);
-
+
// get the output directory
tb_char_t const *outputdir = luaL_checkstring(lua, 2);
tb_check_return_val(outputdir, 0);
-
+
// open library file
tb_stream_ref_t istream = tb_stream_init_from_file(libraryfile, TB_FILE_MODE_RO);
if (!istream) {
@@ -66,29 +66,27 @@ tb_int_t xm_binutils_extractlib(lua_State *lua) {
lua_pushfstring(lua, "extractlib: open %s failed", libraryfile);
return 2;
}
-
+
tb_bool_t ok = tb_false;
+ tb_char_t const* error_msg = tb_null;
do {
if (!tb_stream_open(istream)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "extractlib: open %s failed", libraryfile);
+ error_msg = "open failed";
break;
}
-
+
// detect format
tb_int_t format = xm_binutils_detect_format(istream);
if (format < 0) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "extractlib: cannot detect format of %s", libraryfile);
+ error_msg = "cannot detect format";
break;
}
-
+
// extract based on format
if (format == XM_BINUTILS_FORMAT_AR) {
// AR archive format (.a or .lib in AR format)
if (!xm_binutils_ar_extract(istream, outputdir)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "extractlib: extract AR archive %s failed", libraryfile);
+ error_msg = "extract AR archive failed";
break;
}
ok = tb_true;
@@ -99,30 +97,32 @@ tb_int_t xm_binutils_extractlib(lua_State *lua) {
// 1. Import libraries (different format)
// 2. Static libraries (COFF archive format, similar to AR but different)
if (!xm_binutils_mslib_extract(istream, outputdir)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "extractlib: extract MSVC lib %s failed", libraryfile);
+ error_msg = "extract MSVC lib failed";
break;
}
ok = tb_true;
} else {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "extractlib: unsupported format for %s (only AR and MSVC lib are supported)", libraryfile);
+ error_msg = "unsupported format (only AR and MSVC lib are supported)";
break;
}
-
+
} while (0);
-
+
if (istream) {
tb_stream_clos(istream);
tb_stream_exit(istream);
}
-
+
if (ok) {
lua_pushboolean(lua, tb_true);
return 1;
} else {
- // error message should already be pushed
+ lua_pushboolean(lua, tb_false);
+ if (error_msg) {
+ lua_pushfstring(lua, "extractlib: %s %s", error_msg, libraryfile);
+ } else {
+ lua_pushfstring(lua, "extractlib: unknown error for %s", libraryfile);
+ }
return 2;
}
}
-