summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-07 11:17:46 +0800
committerruki <[email protected]>2025-12-07 17:15:59 +0800
commitcae5b0a17dc7d0eb5599c0d5a92fab69d39983d5 (patch)
treed459ac9eeb285bb4ba48d7ef5a5f9da375b53434
parent00f6b0ba403a86ad542a1fbecc142064aa641f0d (diff)
support for iphoneos in bin2macho
-rw-r--r--core/src/xmake/utils/bin2macho.c38
-rw-r--r--xmake/core/sandbox/modules/utils.lua4
-rw-r--r--xmake/modules/private/utils/bin2obj.lua19
-rw-r--r--xmake/rules/utils/bin2obj/xmake.lua6
4 files changed, 52 insertions, 15 deletions
diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c
index 4d69d0656..b5a97c21f 100644
--- a/core/src/xmake/utils/bin2macho.c
+++ b/core/src/xmake/utils/bin2macho.c
@@ -54,6 +54,11 @@
#define XM_MACHO_LC_SYMTAB 0x2
#define XM_MACHO_LC_BUILD_VERSION 0x32
+#define XM_MACHO_PLATFORM_MACOS 1
+#define XM_MACHO_PLATFORM_IOS 2
+#define XM_MACHO_PLATFORM_TVOS 3
+#define XM_MACHO_PLATFORM_WATCHOS 4
+
#define XM_MACHO_SECT_TYPE_REGULAR 0x0
#define XM_MACHO_SECT_ATTR_SOME_INITS 0x400
#define XM_MACHO_SECT_ATTR_PURE_INSTRUCTIONS 0x80000000
@@ -234,9 +239,26 @@ static tb_uint32_t xm_utils_bin2macho_align(tb_uint32_t value, tb_uint32_t align
return ((value + align - 1) & ~(align - 1));
}
+static tb_uint32_t xm_utils_bin2macho_get_platform(tb_char_t const *platform) {
+ if (!platform) {
+ return XM_MACHO_PLATFORM_MACOS;
+ }
+ if (tb_strcmp(platform, "macosx") == 0 || tb_strcmp(platform, "macos") == 0) {
+ return XM_MACHO_PLATFORM_MACOS;
+ } else if (tb_strcmp(platform, "iphoneos") == 0 || tb_strcmp(platform, "ios") == 0) {
+ return XM_MACHO_PLATFORM_IOS;
+ } else if (tb_strcmp(platform, "appletvos") == 0 || tb_strcmp(platform, "tvos") == 0) {
+ return XM_MACHO_PLATFORM_TVOS;
+ } else if (tb_strcmp(platform, "watchos") == 0) {
+ return XM_MACHO_PLATFORM_WATCHOS;
+ }
+ return XM_MACHO_PLATFORM_MACOS;
+}
+
static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream,
tb_stream_ref_t ostream,
tb_char_t const *symbol_prefix,
+ tb_char_t const *plat,
tb_char_t const *arch,
tb_char_t const *basename) {
tb_assert_and_check_return_val(istream && ostream, tb_false);
@@ -364,7 +386,7 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream,
tb_memset(&build_version, 0, sizeof(build_version));
build_version.cmd = XM_MACHO_LC_BUILD_VERSION;
build_version.cmdsize = build_version_cmd_size;
- build_version.platform = 1; // PLATFORM_MACOS
+ build_version.platform = xm_utils_bin2macho_get_platform(plat);
build_version.minos = 0x000a0000; // 10.0.0
build_version.sdk = 0x000a0000; // 10.0.0
build_version.ntools = 0;
@@ -473,10 +495,11 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream,
static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream,
tb_stream_ref_t ostream,
tb_char_t const *symbol_prefix,
+ tb_char_t const *plat,
tb_char_t const *arch,
tb_char_t const *basename) {
if (xm_utils_bin2macho_is_64bit(arch)) {
- return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, arch, basename);
+ return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename);
} else {
// 32-bit not implemented yet
return tb_false;
@@ -489,7 +512,7 @@ static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream,
/* generate Mach-O object file from binary file
*
- * local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, arch, basename)
+ * local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename)
*/
tb_int_t xm_utils_bin2macho(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
@@ -505,11 +528,14 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) {
// get symbol prefix (optional)
tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null;
+ // get plat (optional)
+ tb_char_t const *plat = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null;
+
// get arch (optional)
- tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null;
+ tb_char_t const *arch = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null;
// get basename (optional)
- tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null;
+ tb_char_t const *basename = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null;
// do dump
tb_bool_t ok = tb_false;
@@ -529,7 +555,7 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) {
break;
}
- if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, arch, basename)) {
+ if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, plat, arch, basename)) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "bin2macho: dump data failed");
break;
diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua
index 694447924..b6dfedd45 100644
--- a/xmake/core/sandbox/modules/utils.lua
+++ b/xmake/core/sandbox/modules/utils.lua
@@ -170,8 +170,8 @@ end
-- generate Mach-O object file from the binary file
if utils.bin2macho then
- function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, arch, basename)
- local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, arch, basename)
+ function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename)
+ local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename)
if not ok then
os.raise(errors)
end
diff --git a/xmake/modules/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua
index 2221adf3c..a27f5c5dc 100644
--- a/xmake/modules/private/utils/bin2obj.lua
+++ b/xmake/modules/private/utils/bin2obj.lua
@@ -26,15 +26,19 @@ local options = {
{'o', "outputpath", "kv", nil, "Set the output object file path."},
{'f', "format", "kv", nil, "Set the object file format (coff, elf, macho)."},
{nil, "symbol-prefix", "kv", nil, "Set the symbol prefix (default: _binary_)."},
- {'a', "arch", "kv", nil, "Set the target architecture."}
+ {'a', "arch", "kv", nil, "Set the target architecture."},
+ {'p', "plat", "kv", nil, "Set the target platform (macosx, iphoneos, etc.)."}
}
function _do_bin2obj_coff(binarypath, outputpath, opt)
-- get symbol prefix
local symbol_prefix = opt["symbol-prefix"] or "_binary_"
- -- get architecture
- local arch = opt.arch
+ -- get architecture (default: x86_64)
+ local arch = opt.arch or "x86_64"
+
+ -- get platform (not used for COFF, but passed for consistency)
+ local platform = opt.plat
-- get filename from binary path (with extension, dots replaced with underscores)
local filename = path.filename(binarypath)
@@ -63,8 +67,11 @@ function _do_bin2obj_macho(binarypath, outputpath, opt)
-- get symbol prefix
local symbol_prefix = opt["symbol-prefix"] or "_binary_"
- -- get architecture
- local arch = opt.arch
+ -- get platform (default: macosx)
+ local plat = opt.plat or "macosx"
+
+ -- get architecture (default: x86_64)
+ local arch = opt.arch or "x86_64"
-- get filename from binary path (with extension, dots replaced with underscores)
local filename = path.filename(binarypath)
@@ -76,7 +83,7 @@ function _do_bin2obj_macho(binarypath, outputpath, opt)
-- do dump
if utils.bin2macho then
- utils.bin2macho(binarypath, outputpath, symbol_prefix, arch, basename)
+ utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename)
else
raise("bin2obj: utils.bin2macho not available (C implementation not compiled)")
end
diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua
index 2bdd9c3a8..def3f56e8 100644
--- a/xmake/rules/utils/bin2obj/xmake.lua
+++ b/xmake/rules/utils/bin2obj/xmake.lua
@@ -50,12 +50,16 @@ rule("utils.bin2obj")
-- get architecture
local arch = target:arch()
+ -- get platform
+ local plat = target:plat()
+
-- convert binary file to object file
local argv = {
"-i", path(sourcefile_bin),
"-o", path(objectfile),
"-f", format,
- "-a", arch
+ "-a", arch,
+ "-p", plat
}
if symbol_prefix ~= "_binary_" then
table.insert(argv, "--symbol-prefix")