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

Better support for compilation of automatically generated code #1540

Closed
waruqi opened this issue Jul 30, 2021 · 0 comments
Closed

Better support for compilation of automatically generated code #1540

waruqi opened this issue Jul 30, 2021 · 0 comments

Comments

@waruqi
Copy link
Member

waruqi commented Jul 30, 2021

Normally, add_files will ignore all files that do not currently exist, which is no problem.

However, when we need to dynamically generate source code to compile them, because the current file does not yet exist, the compilation of them will be lost.

We can only compile them by actively invoking the compiler in the rule and adding object files to target.

like this

rule("vala")
    before_buildcmd_file(function (target, batchcmds, sourcefile_vala, opt)

        -- get valac
        import("lib.detect.find_tool")
        local valac = assert(find_tool("valac"), "valac not found!")

        -- get c source file for vala
        local sourcefile_c = target:autogenfile((sourcefile_vala:gsub(".vala$", ".c")))
        local basedir = path.directory(sourcefile_c)

        -- add objectfile
        local objectfile = target:objectfile(sourcefile_c)
        table.insert(target:objectfiles(), objectfile)

        -- add commands
        batchcmds:show_progress(opt.progress, "${color.build.object}compiling.vala %s", sourcefile_vala)
        batchcmds:mkdir(basedir)
        local argv = {"-C", "-b", basedir}
        local packages = target:values("vala.packages")
        if packages then
            for _, package in ipairs(packages) do
                table.insert(argv, "--pkg")
                table.insert(argv, package)
            end
        end
        table.insert(argv, sourcefile_vala)
        batchcmds:vrunv(valac.program, argv)
        batchcmds:compile(sourcefile_c, objectfile)

        -- add deps
        batchcmds:add_depfiles(sourcefile_vala)
        batchcmds:set_depmtime(os.mtime(objectfile))
        batchcmds:set_depcache(target:dependfile(objectfile))
    end)

Although this is very flexible, it is not very friendly to some users who are just getting started.

Therefore, we improved the support for add_files and added the {always_added = true} configuration to tell xmake that we always need to add the specified source file, even if it does not exist yet.

In this way, we can rely on xmake's default compilation process to compile the automatically generated code.

like this

add_rules("mode.debug", "mode.release")

target("autogen_code")
    set_kind("binary")
    add_files("$(buildir)/autogen.cpp", {always_added = true})
    before_build(function (target)
        io.writefile("$(buildir)/autogen.cpp", [[
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
    cout << "hello world!" << endl;
    return 0;
}
        ]])
    end)

However, we also need to pay attention to that, since the currently automatically generated source file may not yet exist, we cannot use pattern matching in add_files("src/*.cpp", {always_added = true}), we only can add each source file path explicitly.

Related issues:

#1090
#1246

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

No branches or pull requests

1 participant