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

Feature request: Unity build #1019

Closed
Milerius opened this issue Nov 8, 2020 · 5 comments
Closed

Feature request: Unity build #1019

Milerius opened this issue Nov 8, 2020 · 5 comments

Comments

@Milerius
Copy link

Milerius commented Nov 8, 2020

Is your feature request related to a problem? Please describe.

with CMake we have this fantastic feature: https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD.html

which at work help us to reduce the compilation time by ~4-5 times, do you think it's possible to have the equivalent ?

Describe the solution you'd like

Same unity build as CMake with possibility to exclude some source from the unity such as (obj-c) file

Describe alternatives you've considered

Using CMake

@waruqi
Copy link
Member

waruqi commented Nov 8, 2020

This seems to be a great optimization feature, and I will consider supporting this feature in future versions.

But the current 2.3.9 version focuses on improving package management, so I do not plan to add support for it in 2.3.9 version.

@waruqi waruqi added this to the todo milestone Nov 8, 2020
@xq114
Copy link
Contributor

xq114 commented Apr 10, 2021

https://github.com/Kitware/CMake/blob/aa283fdba161eb458b02caaa9f0c32d97c648d6f/Source/cmLocalGenerator.cxx#L2915-L2982

This file contains the implementation detail for unity build mode: writing a file like

// unity_block_1.c
#define <UNIQUE_ID> <file1_c_md5>
#include "file1.c"
#undef <UNIQUE_ID>
#define <UNIQUE_ID> <file2_c_md5>
#include "file2.c"
#undef <UNIQUE_ID>
...

There are some rules for files to be included, for instance, any files with different compiler definitions can't be included in one block.

For information about UNIQUE_ID, see
https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD_UNIQUE_ID.html

@waruqi waruqi modified the milestones: todo, v2.5.9 Oct 17, 2021
@waruqi
Copy link
Member

waruqi commented Oct 17, 2021

I have supported it. #1749

C Unity

add_rules("c.unity_build")

C++ Unity

add_rules("c++.unity_build")

Batch mode (default mode)

We can use batchsize set the batch size. It will merge a single file if no given batchsize.

target("test")
    set_kind("binary")
    add_includedirs("src")
    add_rules("c++.unity_build", {batchsize = 2})
    add_files("src/*.c", "src/*.cpp")

Group mode

target("test")
    set_kind("binary")
    add_rules("c++.unity_build", {batchsize = 0}) -- disable batch mode
    add_files("src/*.c", "src/*.cpp")
    add_files("src/foo/*.c", {unity_group = "foo"})
    add_files("src/bar/*.c", {unity_group = "bar"})

Batch and group mode

target("test")
    set_kind("binary")
    add_includedirs("src")
    add_rules("c++.unity_build", {batchsize = 2})
    add_files("src/*.c", "src/*.cpp")
    add_files("src/foo/*.c", {unity_group = "foo"})
    add_files("src/bar/*.c", {unity_group = "bar"})

Ignore the given files

target("test")
    set_kind("binary")
    add_includedirs("src")
    add_rules("c++.unity_build", {batchsize = 2})
    add_files("src/*.c", "src/*.cpp")
    add_files("src/test/*.c", {unity_ignored = true}) -- ignore these files
$ xmake -r
[ 11%]: ccache compiling.release build/.gens/test/unity_build/unity_642A245F.c
[ 11%]: ccache compiling.release build/.gens/test/unity_build/unity_bar.c
[ 11%]: ccache compiling.release build/.gens/test/unity_build/unity_73161A20.c
[ 11%]: ccache compiling.release build/.gens/test/unity_build/unity_F905F036.c
[ 11%]: ccache compiling.release build/.gens/test/unity_build/unity_foo.c
[ 11%]: ccache compiling.release build/.gens/test/unity_build/unity_642A245F.cpp
[ 77%]: linking.release test
[100%]: build ok

Unique ID

target("test")
    set_kind("binary")
    add_includedirs("src")
    add_rules("c++.unity_build", {batchsize = 2, uniqueid = "MY_UNITY_ID"})
    add_files("src/*.c", "src/*.cpp")

@Milerius
Copy link
Author

That's nice ! Thanks a lot

@waruqi
Copy link
Member

waruqi commented Oct 18, 2021

I have supported Unique ID.

@waruqi waruqi closed this as completed Oct 18, 2021
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

3 participants