xmake-io / xmake

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

Support for cppfront #4132

Closed paul-reilly closed 1 year ago

paul-reilly commented 1 year ago

Xmake Version

v2.8.1+dev.bdb28e5

Operating System Version and Architecture

Linux 6.1.44-1-MANJARO x86_64 GNU/Linux

Describe Bug

When running this xmake.lua file to generate a single .cpp file, the build fails on first run even though the .cpp file is generated.

rule("cppfront")
    set_extensions(".cpp2")
    on_load(function(target)
        local rule = target:rule("c++.build"):clone()
        rule:add("deps", "cppfront", { order = true })
        target:rule_add(rule)
    end)

    on_build_file(function(target, sourcefile, opt)
        cprint('${cyan}cppfront compiling: ${bright green}%s', sourcefile)
        local dir = path.directory(sourcefile)
        local outdata, errdata = os.iorunv("cppfront", { sourcefile })
        cprint("res: '%s'  :  '%s'", outdata, errdata)
    end)

target("app")
    set_kind("binary")
    add_rules("cppfront")
    add_files("src/*.cpp2")
    add_files("src/*.cpp")
    add_packages("fmt")

Expected Behavior

The generated main.cpp is compiled.

Project Configuration

Test cpp2 file:

main: () -> int = 
    println("Hello world!\n");

println: (msg: _) -> int = {
    std::cout << "msg: " << msg;
    return 0;
}

Additional Information and Error Logs

Message with failed compilation:

>xmake -rv                                  ✔ 
cppfront compiling: src/main.cpp2
res: 'src/main.cpp2... ok (all Cpp2, passes safety checks)

'  :  ''
[ 50%]: linking.release app
/usr/bin/g++ -o build/linux/x86_64/release/app -m64 -L/home/dev/.xmake/packages/f/fmt/10.0.0/cf4a2efd8ed34f738dfde8eaa4fb9660/lib -lfmt
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../lib/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status
error: execv(/usr/bin/g++ -o build/linux/x86_64/release/app -m64 -L/home/dev/.xmake/packages/f/fmt/10.0.0/cf4a2efd8ed34f738dfde8eaa4fb9660/lib -lfmt) failed(1)
warning: ./xmake.lua:26: cannot match add_files("src/*.cpp") in target(app)
waruqi commented 1 year ago

I will attempt to add a builtin cppfront rule.

paul-reilly commented 1 year ago

Official cppfront support would be a good addition to xmake.

However this issue is general to files generated at build time for compilation with that pattern, which is used in the xmake documentation. I have seen it when adding a rule for files generated by drogon_ctl, for example.

waruqi commented 1 year ago

see https://github.com/xmake-io/xmake/tree/master/tests/projects/other/autogen_codedep

paul-reilly commented 1 year ago

Thanks, the docs imply that the rule in the first post here would work:

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

I don't think a description of the autogen_codedep pattern is in the docs yet.

waruqi commented 1 year ago

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

This is just to say the dynamic creation and injection of rules, not the automatic generation of code.

It's just that I happened to put in a small example of dynamic injection about cppfront.

waruqi commented 1 year ago

I added it. cppfront https://github.com/xmake-io/xmake/pull/4140

paul-reilly commented 1 year ago

Good stuff!

Imo that cppfront example should be removed from the docs because it implies that enforcing rule-ordering means that generated files are then compiled.

cassepipe commented 9 months ago

Thanks waruqi !