Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warning when adding generated include dirs and files from dependency target #838

Closed
caizongchao opened this issue Jun 12, 2020 · 23 comments
Closed

Comments

@caizongchao
Copy link

caizongchao commented Jun 12, 2020

xmake 2.3.4

sample xmake.lua:

target('generate_files')
   on_build(function (target)
     -- generate header files and a .obj file in output dir
     os.runv('gen', {'--output', output_dir})

     -- i wish there're apis to add output_dir and generated files into target('foo') like:
     target('foo):add_includedirs(output_dir)
     target('foo):add_files(output_dir/generated.obj)
     -- but after searching docs and source code there seems no such api
   end)

target('foo')
    set_policy("build.across_targets_in_parallel", false)
    add_deps('generate_files')

    set_kind('static')

    -- here I get waring: target(foo).add_includedirs(...) path not found
    add_includedirs(output_dir_of_generated_files)

    -- here I get warning: cannot match target(foo).add_files(...)
    -- and the generated object file is not inserted into the final .lib file
    add_files(output_dir_of_generated_files/generated.obj)

So what's the correct way to add include dirs and files generated from dependency target?

thanks

@waruqi
Copy link
Member

waruqi commented Jun 12, 2020

Please add/set target infos, e.g. includedirs in on_load() stage.

target('generate_files')

   -- @see https://xmake.io/#/manual/project_target?id=targetadd_includedirs
   add_includedirs("xxx", {public = true})

   on_load(function (target)

     -- same as add_includedirs("", {public = true})
     target:add("includedirs", output_dir, {public = true})

     target:data_add("myobjs", "xxx.obj")
   end)

target('foo')
    add_deps('generate_files')
    on_load(function (target)
        local dep = target:dep("generate_files")
        table.join2(target:objectfiles(), dep:data("myobjs"))
    end)

@waruqi
Copy link
Member

waruqi commented Jun 12, 2020

And please uses rule('generate_files')/on_load/on_build and add_rules("generate_files") instead of target("generate_files")/add_deps

https://xmake.io/#/manual/custom_rule?id=rule

rule('generate_files')
   on_load(function (target)
     target:add("includedirs", output_dir)
     target:add("files", "xxx.obj")
   end)

target('foo')
    add_rules('generate_files')

@caizongchao
Copy link
Author

Thanks for the solution. add_includedirs works fine now, but I still have problem with adding object files. I cannot find a proper event (on_load, before_build, on_build, etc) to insert the generated object file.

Since the generator is also a dependency target, so I cannot use on_load/before_build because the generator has not been built at that time.

The on_build event cannot be used because it overrides the default build process, although I can insert object file here but there's no build action.

I can insert the object file in the generator's on_build event, but it's ignored by the target and does not appear in the final library.

What I want to do is to build luajit with xmake, below is the code of xmake.lua, any suggestions are appreciated:

local function array_append(a, v, ...)
    if v ~= nil then
        if type(v) ~= 'table' then
            a[#a + 1] = v
        else
            array_append(a, unpack(v))
        end
        
        array_append(a, ...)
    end
end

local function array(...)
    local r = {}; array_append(r, ...); return r
end

option('disable-jit')
    set_default(false)
    set_showmenu(true)
    add_defines('LUAJIT_DISABLE_JIT', 'LUAJIT_DISABLE_FFI')

target('luajit-minilua')
    set_policy("build.across_targets_in_parallel", false)
    set_kind('binary')
    add_files('deps/LuaJIT/src/host/minilua.c')

target('luajit-buildvm-arch')
    set_policy("build.across_targets_in_parallel", false)
    add_deps('luajit-minilua')

    on_build(function(target)
        import("core.base.option")
        import("core.project.depend")
        import("core.project.project")

        local sourcefile = 'deps/LuaJIT/src/vm_x64.dasc'
        local outputdir = target:objectdir()
        local outputfile = path.join(outputdir, 'buildvm_arch.h')

        target:add("includedirs", outputdir, {public = true})

        local dependfile = target:dependfile(outputfile)
        local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {})

        if not depend.is_changed(dependinfo, {lastmtime = os.mtime(outputfile)}) then
            return
        end

        local hasjit = not project.option('disable-jit'):enabled()

        local minilua = target:deps()['luajit-minilua']:targetfile()
        local dasm = 'deps/LuaJIT/dynasm/dynasm.lua'
        local flags = {'-D', 'WIN', '-D', 'P64'}; if hasjit then
            flags = {'-D', 'JIT', '-D', 'FFI', unpack(flags)}
        end
        local args = array(dasm, '-LN', flags, '-o', outputfile, sourcefile)

        if not os.isdir(outputdir) then
            os.mkdir(outputdir)
        end

        print('compiling', sourcefile)
        os.runv(minilua, args)

        dependinfo.files = {sourcefile}
        depend.save(dependinfo, dependfile)
    end)

target('luajit-buildvm')
    set_policy("build.across_targets_in_parallel", false)
    add_deps('luajit-buildvm-arch')

    set_kind('binary')

    add_options('disable-jit')

    add_includedirs('deps/LuaJIT/src')

    add_files('deps/LuaJIT/src/host/buildvm*.c')

target('luajit-vm')
    set_policy("build.across_targets_in_parallel", false)
    add_deps('luajit-buildvm')

    on_build(function (target)
        import("core.base.option")
        import("core.project.project")

        local outputdir = target:objectdir()
        local lj_vm_h = path.join(outputdir, 'lj_vmdef.h')
        local lj_vm_obj = path.join(outputdir, 'lj_vm.obj')

        target:add("includedirs", outputdir, {public = true})

        local target_luajit = project.target('luajit')
        -- object is inserted here but it's not inserted in the final library
        target_luajit:add("files", lj_vm_obj)

        if os.exists(lj_vm_obj) and (not option.get("rebuild")) then return end

        local buildvm = target:deps()['luajit-buildvm']:targetfile()
        local all_lib = {
            'deps/LuaJIT/src/lib_base.c',
            'deps/LuaJIT/src/lib_math.c',
            'deps/LuaJIT/src/lib_bit.c',
            'deps/LuaJIT/src/lib_string.c',
            'deps/LuaJIT/src/lib_table.c',
            'deps/LuaJIT/src/lib_io.c',
            'deps/LuaJIT/src/lib_os.c',
            'deps/LuaJIT/src/lib_package.c',
            'deps/LuaJIT/src/lib_debug.c',
            'deps/LuaJIT/src/lib_jit.c',
            'deps/LuaJIT/src/lib_ffi.c'
        }

        if not os.isdir(outputdir) then
            os.mkdir(outputdir)
        end

        print('building vm...')

        os.runv(buildvm, {'-m', 'peobj', '-o', lj_vm_obj})
        os.runv(buildvm, {'-m', 'bcdef', '-o', path.join(outputdir, 'lj_bcdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'ffdef', '-o', path.join(outputdir, 'lj_ffdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'libdef', '-o', path.join(outputdir, 'lj_libdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'recdef', '-o', path.join(outputdir, 'lj_recdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'vmdef', '-o', path.join(outputdir, 'lj_vmdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'folddef', '-o', path.join(outputdir, 'lj_folddef.h'), 'deps/LuaJIT/src/lj_opt_fold.c'})

        target_luajit:add("files", lj_vm_obj)
    end)

target('luajit')
    set_policy("build.across_targets_in_parallel", false)

    set_kind('static')

    add_options('disable-jit')

    add_deps('luajit-vm')

    add_defines('LUAJIT_USE_SYSMALLOC', 'LUAJIT_ENABLE_LUA52COMPAT')

    if is_mode('debug') then
        add_defines('LUA_USE_ASSERT')
    end

    add_files('deps/LuaJIT/src/ljamalg.c')

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

Please insert to target:objectfiles() in target("luajit")/before_build directly.

target('luajit')
    add_deps('luajit-vm')
    before_build(function (target)
        
        local buildvm = target:dep("luajit-buildvm"):targetfile()
        os.runv(buildvm, {'-m', 'peobj', '-o', lj_vm_obj})
        os.runv(buildvm, {'-m', 'bcdef', '-o', path.join(outputdir, 'lj_bcdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'ffdef', '-o', path.join(outputdir, 'lj_ffdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'libdef', '-o', path.join(outputdir, 'lj_libdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'recdef', '-o', path.join(outputdir, 'lj_recdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'vmdef', '-o', path.join(outputdir, 'lj_vmdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'folddef', '-o', path.join(outputdir, 'lj_folddef.h'), 'deps/LuaJIT/src/lj_opt_fold.c'})

       table.join2(target:objectfiles(), objs)
    end)

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

Or you should use rule('luajit-vm')/before_build instead of target('luajit-vm')/on_build

And add_deps('luajit-buildvm'), rule("luajit-vm") to target("luajit")

rule('luajit-vm')

    before_build(function (target)

        -- NOTE, this target is luajit if you apply rule to target("luajit")

        local buildvm = target:dep("luajit-buildvm"):targetfile()
        os.runv(buildvm, {'-m', 'peobj', '-o', lj_vm_obj})
        os.runv(buildvm, {'-m', 'bcdef', '-o', path.join(outputdir, 'lj_bcdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'ffdef', '-o', path.join(outputdir, 'lj_ffdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'libdef', '-o', path.join(outputdir, 'lj_libdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'recdef', '-o', path.join(outputdir, 'lj_recdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'vmdef', '-o', path.join(outputdir, 'lj_vmdef.h'), unpack(all_lib)})
        os.runv(buildvm, {'-m', 'folddef', '-o', path.join(outputdir, 'lj_folddef.h'), 'deps/LuaJIT/src/lj_opt_fold.c'})

       table.join2(target:objectfiles(), objs)
    end)

target('luajit')
    set_policy("build.across_targets_in_parallel", false)
    set_kind('static')
    add_options('disable-jit')

    -- NOTE this ..
    add_deps('luajit-buildvm')
    add_rules("luajit-vm")            

@caizongchao
Copy link
Author

Because before_build runs before the actual build process, buildvm is not available at that time thus os.runv(buildvm,...) failed:

building vm...
error: ...mdir\core\sandbox\modules\import\core\base\scheduler.lua:47: cannot runv(M:\luxe\windows\x64\release\luajit-buildvm.exe -m peobj -o M:\luxe\.objs\luajit\windows\x64\release\lj_vm.obj), No such file or directory

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

add_deps('luajit-buildvm') and set_policy("build.across_targets_in_parallel", false) will ensure that buildvm has been built before target(luajit)/before_build.

@caizongchao
Copy link
Author

already did that, you may try attached file with xmake.lua and dependent source files.

luxe.zip

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

You can try it

local function array_append(a, v, ...)
    if v ~= nil then
        if type(v) ~= 'table' then
            a[#a + 1] = v
        else
            array_append(a, unpack(v))
        end
        
        array_append(a, ...)
    end
end

local function array(...)
    local r = {}; array_append(r, ...); return r
end
    
set_policy("build.across_targets_in_parallel", false)

option('disable-jit')
    set_default(false)
    set_showmenu(true)
    add_defines('LUAJIT_DISABLE_JIT', 'LUAJIT_DISABLE_FFI')

target('luajit-minilua')
    set_kind('binary')
    add_files('deps/LuaJIT/src/host/minilua.c')

target('luajit-buildvm-arch')
    add_deps('luajit-minilua')

    on_build(function(target)
        import("core.base.option")
        import("core.project.depend")
        import("core.project.project")

        local sourcefile = 'deps/LuaJIT/src/vm_x64.dasc'
        local outputdir = target:objectdir()
        local outputfile = path.join(outputdir, 'buildvm_arch.h')

        target:add("includedirs", outputdir, {public = true})

        local dependfile = target:dependfile(outputfile)
        local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {})

        if not depend.is_changed(dependinfo, {lastmtime = os.mtime(outputfile)}) then
            return
        end

        local hasjit = not project.option('disable-jit'):enabled()

        local minilua = target:deps()['luajit-minilua']:targetfile()
        local dasm = 'deps/LuaJIT/dynasm/dynasm.lua'
        local flags = {'-D', 'WIN', '-D', 'P64'}; if hasjit then
            flags = {'-D', 'JIT', '-D', 'FFI', unpack(flags)}
        end
        local args = array(dasm, '-LN', flags, '-o', outputfile, sourcefile)

        if not os.isdir(outputdir) then
            os.mkdir(outputdir)
        end

        print('compiling', sourcefile)
        os.runv(minilua, args)

        dependinfo.files = {sourcefile}
        depend.save(dependinfo, dependfile)
    end)

target('luajit-buildvm')
    add_deps('luajit-buildvm-arch')

    set_kind('binary')

    add_options('disable-jit')

    add_includedirs('deps/LuaJIT/src')

    add_files('deps/LuaJIT/src/host/buildvm*.c')

    after_build(function (target)
        import("core.base.option")
        import("core.project.project")

        local outputdir = target:objectdir()
        if not os.isdir(outputdir) then
            os.mkdir(outputdir)
        end

        local lj_vm_h = path.join(outputdir, 'lj_vmdef.h')
        local lj_vm_obj = path.join(outputdir, 'lj_vm.obj')
        if os.exists(lj_vm_obj) and (not option.get("rebuild")) then
            return
        end

        local buildvm = target:targetfile()
        local all_lib = {
            'deps/LuaJIT/src/lib_base.c',
            'deps/LuaJIT/src/lib_math.c',
            'deps/LuaJIT/src/lib_bit.c',
            'deps/LuaJIT/src/lib_string.c',
            'deps/LuaJIT/src/lib_table.c',
            'deps/LuaJIT/src/lib_io.c',
            'deps/LuaJIT/src/lib_os.c',
            'deps/LuaJIT/src/lib_package.c',
            'deps/LuaJIT/src/lib_debug.c',
            'deps/LuaJIT/src/lib_jit.c',
            'deps/LuaJIT/src/lib_ffi.c'
        }

        print('building vm...')

        os.vrunv(buildvm, {'-m', 'peobj', '-o', lj_vm_obj})
        os.vrunv(buildvm, {'-m', 'bcdef', '-o', path.join(outputdir, 'lj_bcdef.h'), unpack(all_lib)})
        os.vrunv(buildvm, {'-m', 'ffdef', '-o', path.join(outputdir, 'lj_ffdef.h'), unpack(all_lib)})
        os.vrunv(buildvm, {'-m', 'libdef', '-o', path.join(outputdir, 'lj_libdef.h'), unpack(all_lib)})
        os.vrunv(buildvm, {'-m', 'recdef', '-o', path.join(outputdir, 'lj_recdef.h'), unpack(all_lib)})
        os.vrunv(buildvm, {'-m', 'vmdef', '-o', path.join(outputdir, 'lj_vmdef.h'), unpack(all_lib)})
        os.vrunv(buildvm, {'-m', 'folddef', '-o', path.join(outputdir, 'lj_folddef.h'), 'deps/LuaJIT/src/lj_opt_fold.c'})
    end)    

target('luajit')

    set_kind('static')

    add_options('disable-jit')

    add_deps('luajit-buildvm')

    add_defines('LUAJIT_USE_SYSMALLOC', 'LUAJIT_ENABLE_LUA52COMPAT')

    if is_mode('debug') then
        add_defines('LUA_USE_ASSERT')
    end

    add_files('deps/LuaJIT/src/ljamalg.c')

    on_load(function (target)
        local buildvm = target:dep("luajit-buildvm")
        local lj_vm_obj = path.join(buildvm:objectdir(), 'lj_vm.obj')
        target:add("includedirs", buildvm:objectdir())
        table.join2(target:objectfiles(), lj_vm_obj)
    end)

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

When the across_targets_in_parallel strategy is disabled, before_build is currently executed before all dependent target builds. This seems to have some problems, and I need to fix it.

And I will improve rule to support it better. You can try the above code first

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

I have improve rule to support add_files("src/*.c", {rules = {"xxx", override = true}}) to override the buitin-rules of sourcefile.

You can update to dev and try the following code. xmake update -s dev

local function array_append(a, v, ...)
    if v ~= nil then
        if type(v) ~= 'table' then
            a[#a + 1] = v
        else
            array_append(a, unpack(v))
        end
        
        array_append(a, ...)
    end
end

local function array(...)
    local r = {}; array_append(r, ...); return r
end
    
set_policy("build.across_targets_in_parallel", false)

option('disable-jit')
    set_default(false)
    set_showmenu(true)
    add_defines('LUAJIT_DISABLE_JIT', 'LUAJIT_DISABLE_FFI')

target('luajit-minilua')
    set_kind('binary')
    add_files('deps/LuaJIT/src/host/minilua.c')

target('luajit-buildvm-arch')
    add_deps('luajit-minilua')

    on_build(function(target)
        import("core.base.option")
        import("core.project.depend")
        import("core.project.project")

        local sourcefile = 'deps/LuaJIT/src/vm_x64.dasc'
        local outputdir = target:objectdir()
        local outputfile = path.join(outputdir, 'buildvm_arch.h')

        target:add("includedirs", outputdir, {public = true})

        local dependfile = target:dependfile(outputfile)
        local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {})

        if not depend.is_changed(dependinfo, {lastmtime = os.mtime(outputfile)}) then
            return
        end

        local hasjit = not project.option('disable-jit'):enabled()

        local minilua = target:deps()['luajit-minilua']:targetfile()
        local dasm = 'deps/LuaJIT/dynasm/dynasm.lua'
        local flags = {'-D', 'WIN', '-D', 'P64'}; if hasjit then
            flags = {'-D', 'JIT', '-D', 'FFI', unpack(flags)}
        end
        local args = array(dasm, '-LN', flags, '-o', outputfile, sourcefile)

        if not os.isdir(outputdir) then
            os.mkdir(outputdir)
        end

        print('compiling', sourcefile)
        os.runv(minilua, args)

        dependinfo.files = {sourcefile}
        depend.save(dependinfo, dependfile)
    end)

target('luajit-buildvm')
    add_deps('luajit-buildvm-arch')

    set_kind('binary')

    add_options('disable-jit')

    add_includedirs('deps/LuaJIT/src')

    add_files('deps/LuaJIT/src/host/buildvm*.c')

rule("luajit-vm")
    before_build_files(function (target, sourcefiles)
        import("core.base.option")
        import("core.project.project")

        local buildvm = target:dep("luajit-buildvm")
        local outputdir = buildvm:objectdir()
        if not os.isdir(outputdir) then
            os.mkdir(outputdir)
        end
        target:add("includedirs", buildvm:objectdir())

        local lj_vm_obj = path.join(outputdir, 'lj_vm.obj')
        table.join2(target:objectfiles(), lj_vm_obj)
        if os.exists(lj_vm_obj) and (not option.get("rebuild")) then
            return
        end

        print('building vm...')

        buildvm = buildvm:targetfile()
        os.vrunv(buildvm, {'-m', 'peobj', '-o', lj_vm_obj})
        os.vrunv(buildvm, {'-m', 'bcdef', '-o', path.join(outputdir, 'lj_bcdef.h'), unpack(sourcefiles)})
        os.vrunv(buildvm, {'-m', 'ffdef', '-o', path.join(outputdir, 'lj_ffdef.h'), unpack(sourcefiles)})
        os.vrunv(buildvm, {'-m', 'libdef', '-o', path.join(outputdir, 'lj_libdef.h'), unpack(sourcefiles)})
        os.vrunv(buildvm, {'-m', 'recdef', '-o', path.join(outputdir, 'lj_recdef.h'), unpack(sourcefiles)})
        os.vrunv(buildvm, {'-m', 'vmdef', '-o', path.join(outputdir, 'lj_vmdef.h'), unpack(sourcefiles)})
        os.vrunv(buildvm, {'-m', 'folddef', '-o', path.join(outputdir, 'lj_folddef.h'), 'deps/LuaJIT/src/lj_opt_fold.c'})
    end)    

target('luajit')

    set_kind('static')

    add_options('disable-jit')

    add_deps('luajit-buildvm')

    add_defines('LUAJIT_USE_SYSMALLOC', 'LUAJIT_ENABLE_LUA52COMPAT')

    if is_mode('debug') then
        add_defines('LUA_USE_ASSERT')
    end

    add_files('deps/LuaJIT/src/ljamalg.c')

    add_files('deps/LuaJIT/src/lib_base.c',
              'deps/LuaJIT/src/lib_math.c',
              'deps/LuaJIT/src/lib_bit.c',
              'deps/LuaJIT/src/lib_string.c',
              'deps/LuaJIT/src/lib_table.c',
              'deps/LuaJIT/src/lib_io.c',
              'deps/LuaJIT/src/lib_os.c',
              'deps/LuaJIT/src/lib_package.c',
              'deps/LuaJIT/src/lib_debug.c',
              'deps/LuaJIT/src/lib_jit.c',
              'deps/LuaJIT/src/lib_ffi.c', {rules = {"luajit-vm", override = true}})

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

I have fixed before_build problem when across_targets_in_parallel is disable. You can update to dev and try it again.

xmake update -s dev

When the across_targets_in_parallel strategy is disabled, before_build is currently executed before all dependent target builds. This seems to have some problems, and I need to fix it.

And I will improve rule to support it better. You can try the above code first

Because before_build runs before the actual build process, buildvm is not available at that time thus os.runv(buildvm,...) failed:

building vm...
error: ...mdir\core\sandbox\modules\import\core\base\scheduler.lua:47: cannot runv(M:\luxe\windows\x64\release\luajit-buildvm.exe -m peobj -o M:\luxe\.objs\luajit\windows\x64\release\lj_vm.obj), No such file or directory

@caizongchao
Copy link
Author

caizongchao commented Jun 13, 2020

thanks for the update and it almost works, only two small issues:

  1. in rule('luajit-vm'), the sourcefiles argument of before_build_files is not a list of source files. The source file list is actually sourcefiles.sourcefiles. Of course this might be by design.

  2. The order of sourcefiles is matter. The order of source files in before_build_files is different from the order specified in target('luajit')/add_files. The different order of source files generated different lj_ffdef.h and it caused compilation error.

Below is the rule('luajit-vm') with some modifications and then build succeeded:

rule('luajit-vm')
    before_build_files(function (target, sourcefiles)
        import("core.base.option")
        import("core.project.project")

        -- print(sourcefiles)

        -- the order of sourcefiles is changed
        -- sourcefiles = sourcefiles.sourcefiles

        sourcefiles = {
            'deps/LuaJIT/src/lib_base.c',
            'deps/LuaJIT/src/lib_math.c',
            'deps/LuaJIT/src/lib_bit.c',
            'deps/LuaJIT/src/lib_string.c',
            'deps/LuaJIT/src/lib_table.c',
            'deps/LuaJIT/src/lib_io.c',
            'deps/LuaJIT/src/lib_os.c',
            'deps/LuaJIT/src/lib_package.c',
            'deps/LuaJIT/src/lib_debug.c',
            'deps/LuaJIT/src/lib_jit.c',
            'deps/LuaJIT/src/lib_ffi.c'
        }
        ....

the full xmake.lua is attached. thanks
xmake.zip

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

in rule('luajit-vm'), the sourcefiles argument of before_build_files is not a list of source files. The source file list is actually sourcefiles.sourcefiles. Of course this might be by design.

I'm sorry, my mistake. It should be

rule('luajit-vm')
    before_build_files(function (target, sourcebatch, opt)
        import("core.base.option")
        import("core.project.project")

        local sourcefiles = sourcebatch.sourcefiles
        print(sourcefiles)

see https://xmake.io/#/manual/custom_rule?id=ruleon_build_files

The order of sourcefiles is matter. The order of source files in before_build_files is different from the order specified in target('luajit')/add_files. The different order of source files generated different lj_ffdef.h and it caused compilation error.

I have fixed the order of files. You can update to dev and try it again.

xmake update -s dev

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

local function array_append(a, v, ...)
    if v ~= nil then
        if type(v) ~= 'table' then
            a[#a + 1] = v
        else
            array_append(a, unpack(v))
        end
        
        array_append(a, ...)
    end
end

local function array(...)
    local r = {}; array_append(r, ...); return r
end

        local args = array(dasm, '-LN', flags, '-o', outputfile, sourcefile)

You can use local args = table.join(dasm, '-LN', flags, '-o', outputfile, sourcefile) instead of them.

@caizongchao
Copy link
Author

Thanks, now everything works smoothly, great!

@waruqi
Copy link
Member

waruqi commented Jun 13, 2020

ok

@waruqi waruqi closed this as completed Jun 13, 2020
@caizongchao
Copy link
Author

caizongchao commented Jun 14, 2020

luajit building works in windows, but there's a problem in linux.

In linux the luajit vm is generated as a .S file, so I insert the .S file using target:add('files', lj_vm_s), then xmake reports an error "unknown source file". I think .S file should be automatically recognaized. I then added add_rules('asm') in target('luajit'). The "unknown source file" error is gone but a new error says ".../lj_vm.S.o" not found. I searched the build directory and there's no lj_vm.S.o, it seems that the inserted lj_vm.S is not compiled.

the xmake.lua is attached, any suggestions are appreciated.
xmake.zip

@caizongchao
Copy link
Author

It's terrific to have a ready to use xmake.lua for luajit, thanks!

@waruqi
Copy link
Member

waruqi commented Jun 14, 2020

I am porting luajit to xmake-repo, you can use xmake.lua directly, and I will provide it on xmake-repo/master after finishing.

And we can use add_requires("luajit") directly. for example

add_requires("luajit")
target("test")
    add_files("src/*.c")
    add_packages("luajit")

@caizongchao
Copy link
Author

I've tried it in windows and linux without any problem, thanks.

@waruqi
Copy link
Member

waruqi commented Jun 28, 2020

I have merged luajit/port to xmake-repo/master. You can use it directly. It can also support iphoneos/android.

add_requires("luajit")
target("test")
    add_files("src/*.c")
    add_packages("luajit")

https://github.com/xmake-io/xmake-repo/blob/master/packages/l/luajit/port/xmake.lua

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants