diff options
| author | ruki <[email protected]> | 2022-09-16 23:02:21 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-09-16 23:02:21 +0800 |
| commit | e09492c11bdfb481a7c013e10e00f2acc8522a96 (patch) | |
| tree | f392ea5705b92bfef6b3c62ac287ea3d8edbdab0 | |
| parent | 84fbc887377627a971dd60da0eeda15f0deca82e (diff) | |
support cross trybuild for meson
| -rw-r--r-- | xmake/modules/private/action/trybuild/meson.lua | 156 |
1 files changed, 153 insertions, 3 deletions
diff --git a/xmake/modules/private/action/trybuild/meson.lua b/xmake/modules/private/action/trybuild/meson.lua index f2baaff21..395084793 100644 --- a/xmake/modules/private/action/trybuild/meson.lua +++ b/xmake/modules/private/action/trybuild/meson.lua @@ -21,6 +21,7 @@ -- imports import("core.base.option") import("core.project.config") +import("core.platform.platform") import("lib.detect.find_file") import("lib.detect.find_tool") @@ -34,6 +35,153 @@ function _get_artifacts_dir() return path.absolute(path.join(_get_buildir(), "artifacts")) end +-- is cross compilation? +function _is_cross_compilation() + if not is_plat(os.subhost()) then + return true + end + if is_plat("macosx") and not is_arch(os.subarch()) then + return true + end + return false +end + +-- get pkg-config +function _get_pkgconfig() + if is_plat("windows") then + local pkgconf = find_tool("pkgconf") + if pkgconf then + return pkgconf.program + end + end + local pkgconfig = find_tool("pkg-config") + if pkgconfig then + return pkgconfig.program + end +end + +-- get the build environment +function _get_buildenv(key) + local value = config.get(key) + if value == nil then + value = platform.toolconfig(key, config.plat()) + end + if value == nil then + value = platform.tool(key, config.plat()) + end + return value +end + +-- get cross file +function _get_cross_file(buildir) + local crossfile = path.join(buildir, "cross_file.txt") + if not os.isfile(crossfile) then + local file = io.open(crossfile, "w") + -- binaries + file:print("[binaries]") + local cc = _get_buildenv("cc") + if cc then + file:print("c='%s'", cc) + end + local cxx = _get_buildenv("cxx") + if cxx then + file:print("cpp='%s'", cxx) + end + local ld = _get_buildenv("ld") + if ld then + file:print("ld='%s'", ld) + end + local ar = _get_buildenv("ar") + if ar then + file:print("ar='%s'", ar) + end + local strip = _get_buildenv("strip") + if strip then + file:print("strip='%s'", strip) + end + local ranlib = _get_buildenv("ranlib") + if ranlib then + file:print("ranlib='%s'", ranlib) + end + if is_plat("mingw") then + local mrc = _get_buildenv("mrc") + if mrc then + file:print("windres='%s'", mrc) + end + end + local cmake = find_tool("cmake") + if cmake then + file:print("cmake='%s'", cmake.program) + end + local pkgconfig = _get_pkgconfig() + if pkgconfig then + file:print("pkgconfig='%s'", pkgconfig) + end + file:print("") + + -- built-in options + file:print("[built-in options]") + local cflags = table.join(table.wrap(_get_buildenv("cxflags")), _get_buildenv("cflags")) + local cxxflags = table.join(table.wrap(_get_buildenv("cxflags")), _get_buildenv("cxxflags")) + local asflags = table.wrap(_get_buildenv("asflags")) + local arflags = table.wrap(_get_buildenv("arflags")) + local ldflags = table.wrap(_get_buildenv("ldflags")) + local shflags = table.wrap(_get_buildenv("shflags")) + if #cflags > 0 then + file:print("c_args=['%s']", table.concat(cflags, "', '")) + end + if #cxxflags > 0 then + file:print("cpp_args=['%s']", table.concat(cxxflags, "', '")) + end + local linkflags = table.join(ldflags or {}, shflags) + if #linkflags > 0 then + file:print("c_link_args=['%s']", table.concat(linkflags, "', '")) + file:print("cpp_link_args=['%s']", table.concat(linkflags, "', '")) + end + file:print("") + + -- host machine + file:print("[host_machine]") + if is_plat("iphoneos", "macosx") then + -- TODO + elseif is_plat("android") then + -- TODO + elseif is_plat("mingw") then + local cpu + local cpu_family + if is_arch("x64", "x86_64") then + cpu = "x86_64" + cpu_family = "x86_64" + elseif is_arch("x86", "i386") then + cpu = "i686" + cpu_family = "x86" + else + raise("unsupported arch(%s)", config.arch()) + end + file:print("system = 'windows'") + file:print("cpu_family = '%s'", cpu_family) + file:print("cpu = '%s'", cpu) + file:print("endian = 'little'") + elseif is_plat("cross") then + local cpu = config.arch() + if is_arch("arm64") then + cpu = "aarch64" + elseif is_arch("arm.*") then + cpu = "arm" + end + local cpu_family = cpu + file:print("system = '%s'", get_config("target_os") or "linux") + file:print("cpu_family = '%s'", cpu_family) + file:print("cpu = '%s'", cpu) + file:print("endian = 'little'") + end + file:print("") + file:close() + end + return crossfile +end + + -- get configs function _get_configs(artifacts_dir, buildir) @@ -51,6 +199,11 @@ function _get_configs(artifacts_dir, buildir) end end + -- add cross file + if _is_cross_compilation() then + table.insert(configs, "--cross-file=" .. _get_cross_file(buildir)) + end + -- add build directory table.insert(configs, buildir) return configs @@ -85,9 +238,6 @@ end -- do build function build() - -- only support the current subsystem host platform now! - assert(is_subhost(config.plat()), "meson: %s not supported!", config.plat()) - -- get artifacts directory local artifacts_dir = _get_artifacts_dir() if not os.isdir(artifacts_dir) then |
