xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
9.87k stars 776 forks source link

Package based on xmake doesn't work when changing output naming #5252

Closed MobSlicer152 closed 2 months ago

MobSlicer152 commented 3 months ago

Xmake Version

v2.9.2+master.38c2ebc6f

Operating System Version and Architecture

Windows 10.0.19045.4529 Pro 22H2

Describe Bug

In my project, I use custom platforms which use MSVC, meaning the names have to follow the exact convention or they don't work. I get around this by setting the prefix and extension like this:

    if is_plat("gdk", "gdkx") then
--      set_prefixname("")
--      set_extension(".lib")
    end

If the two lines there aren't commented out, when I build the package, no libraries are detected, and using add_packages only finds header files, even though the library output is still copied.

Expected Behavior

When I change the extension of a target used in a package, it should still be detected as a library and included in the list for the package, so I can link to it.

Project Configuration

package("mimalloc_local")
    set_homepage("https://github.com/microsoft/mimalloc")
    set_description("mimalloc (pronounced 'me-malloc') is a general purpose allocator with excellent performance characteristics.")
    set_license("MIT")

    add_includedirs("include")
    add_links("mimalloc_local")

    set_urls("https://github.com/microsoft/mimalloc/archive/v$(version).zip")
    add_versions("2.1.7", "fa61cf01e3dd869b35275bfd8be95bfde77f0b65dfa7e34012c09a66e1ea463f")

    if is_plat("android") then
        add_syslinks("atomic")
    end

    on_install("macosx", "gdk", "gdkx", "windows", "linux", "android", "mingw", function (package)
        os.cp(path.join(package:scriptdir(), "port", "xmake.lua"), "xmake.lua")
        import("package.tools.xmake").install(package, configs)
    end)
package_end()

Additional Information and Error Logs

With the default cross name:

finding mimalloc_local from xmake ..
checking for xmake::mimalloc_local ... mimalloc_local 2.1.7
{
  libfiles = {
    "C:\Users\MobSlicer152\AppData\Local\.xmake\packages\m\mimalloc_local\2.1.7\ececb808b51846568c9918195610170e\lib\libmimalloc_local.a"
  },
  links = {
    "mimalloc_local"
  },
  linkdirs = {
    "C:\Users\MobSlicer152\AppData\Local\.xmake\packages\m\mimalloc_local\2.1.7\ececb808b51846568c9918195610170e\lib"
  },
  static = true,
  sysincludedirs = {
    "C:\Users\MobSlicer152\AppData\Local\.xmake\packages\m\mimalloc_local\2.1.7\ececb808b51846568c9918195610170e\include"
  },
  license = "MIT",
  version = "2.1.7"
}

With the custom name:

finding mimalloc_local from xmake ..
checking for xmake::mimalloc_local ... mimalloc_local 2.1.7
{
  sysincludedirs = {
    "C:\Users\MobSlicer152\AppData\Local\.xmake\packages\m\mimalloc_local\2.1.7\ececb808b51846568c9918195610170e\include"
  },
  static = true,
  license = "MIT",
  version = "2.1.7"
}
SirLynix commented 3 months ago

I'm curious, which compiler are you using? You said you use MSVC but MSVC doesn't have a prefix and already uses .lib extension.

As for this issue, xmake searches library based on the toolchain know lib extension, I think the best here is not to make a custom package but to make a custom toolchain (if msvc doesn't suit you)

MobSlicer152 commented 3 months ago

I'm targetting the GDK, and I use a custom platform name with the msvc toolchain from xmake, so basically the cross platfrom, with msvc. The cross platform defaults to Linux naming, which MSVC doesn't like. I don't know why (I've dug through the code for installing packages and stuff) it doesn't link to the library, or why it only detects it with its default naming. Sorry, didn't see the second paragraph. There's no way to define a real custom platform that doesn't just get treated as cross, or change the extension from the toolchain. I had previously been using custom targets within my code, but my old structure was messy and I was hoping I'd be able to get packages to work. I think if a way to define custom platforms was added, it would work fine. MSVC does work, it's just the code that detects libraries goes by extension of the file or something, and if you change it, then it's not detected, and there's not necessarily a good way to fix that for cross.

MobSlicer152 commented 3 months ago

This is how I do the gdk platform I have:

-- This is in my project
set_allowedplats(
    "gdk", "gdkx", "xbox360", "windows",
    "linux",
    "switch", "switchhb"
)
set_allowedarchs(
    "gdk|x64", "gdkx|x64", "xbox360|powerpc64", "windows|x86",
    "switch|arm64", "switchhb|arm64"
)

-- This is in a separate lua file that gets included and then called in a function I use for on_load
function fix_name(target)
    if is_plat("gdk", "gdkx", "xbox360") then
        target:set("prefixname", "")
        if target:kind() == "binary" then
            target:set("extension", ".exe")
        elseif target:kind() == "static" then
            target:set("extension", ".lib")
        elseif target:kind() == "shared" then
            target:set("extension", ".dll")
        end
    elseif is_plat("switch") then
        if target:kind() == "binary" then
            target:set("prefixname", "")
            target:set("extension", ".nss")
        elseif target:kind() == "static" then
            target:set("prefixname", "lib")
            target:set("extension", ".a")
        elseif target:kind() == "shared" then
            target:set("prefixname", "lib")
            target:set("extension", ".nrs")
        end
    elseif is_plat("switchhb", "psp", "ps3") then
        if target:kind() == "binary" then
            target:set("prefixname", "")
            target:set("extension", ".elf")
        elseif target:kind() == "static" then
            target:set("prefixname", "lib")
            target:set("extension", ".a")
        elseif target:kind() == "shared" then
            target:set("prefixname", "lib")
            target:set("extension", ".elf")
        end
    end
end

I also have a similar if in the xmake.lua for my mimalloc package, and it builds mimalloc_local.lib fine, but it just doesn't know it's a library, so all it does is install it, but it's not available as a library when I do add_packages("mimalloc_local"), and it doesn't even have an error, so it's basically like if I didn't link to the library.

waruqi commented 3 months ago

The cross platform is only for cross-compilation toolchain, but msvc is not. If you still use msvc, please use windows platform.

And package only supports some builtin platforms now.

You can define a custom option to set your custom platform. e.g. option("myplat"), xmake f --myplat=gdk.