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

CMake: Disable unity builds project-wide #158

Closed

Conversation

dextercd
Copy link
Contributor

I have a project that uses xz via CMake's FetchContent module. This basically downloads xz and makes it part of the parent project using add_subdirectory.

After adding xz like this, I could no longer compile my project as a unity build because xz doesn't appear to support this. xz could certainly be modified to support this type of build, but that's not simply a one-time fix but a long term commitment.

These are the types of errors it generates:
In file included from /home/dexter/src/xz/src/liblzma/common/alone_decoder.c:14,
                 from /tmp/xz/CMakeFiles/liblzma.dir/Unity/unity_0_c.c:133:
/home/dexter/src/xz/src/liblzma/lz/lz_decoder.h:75:3: error: conflicting types for ‘lzma_lz_options’; have ‘struct <anonymous>’
   75 | } lzma_lz_options;
      |   ^~~~~~~~~~~~~~~
In file included from /home/dexter/src/xz/src/liblzma/lzma/lzma_encoder_private.h:16,
                 from /home/dexter/src/xz/src/liblzma/lzma/lzma_encoder.c:14:
/home/dexter/src/xz/src/liblzma/lz/lz_encoder.h:172:3: note: previous declaration of ‘lzma_lz_options’ with type ‘lzma_lz_options’
  172 | } lzma_lz_options;
      |   ^~~~~~~~~~~~~~~
/home/dexter/src/xz/src/xz/file_io.h:40:3: error: conflicting types for ‘io_buf’; have ‘union <anonymous>’
   40 | } io_buf;
      |   ^~~~~~
/home/dexter/src/xz/src/xz/file_io.h:40:3: note: previous declaration of ‘io_buf’ with type ‘io_buf’
   40 | } io_buf;
      |   ^~~~~~

This PR disables unity builds for the entire xz project directory. Because of how variable scoping works, if someone includes xz in their project and enables unity builds then it's still applied to any other part of the build that doesn't explicitly disable it for themselves.

Disabling unity builds when it doesn't work is recommended as per the UNITY_BUILD target property documentation:

Projects should not directly set the UNITY_BUILD property or its associated CMAKE_UNITY_BUILD variable to true. [...] However, it IS recommended to set the UNITY_BUILD property to false if it is known that enabling unity builds for target can lead to problems.


Unity builds can sometimes reveal UB as compilation errors because the compiler can suddenly analyse across what would normally be separate compilation units. I just want to make it abundantly clear that I DON'T think that's what is going on here.

xz just employs some code patterns that happen to be incompatible with unity builds.

liblzma and xz can't be compiled as a unity/jumbo build because of
redeclarations and type name reuse. The CMake documentation recommends
setting UNITY_BUILD to false in this case.

This is especially important if we're compiled as a subproject and the
consumer wants to use CMAKE_UNITY_BUILD=ON for the rest of their code
base.
@Larhzu Larhzu closed this in bf6da9a Dec 22, 2024
Larhzu pushed a commit that referenced this pull request Dec 22, 2024
liblzma and xz can't be compiled as a unity/jumbo build because of
redeclarations and type name reuse. The CMake documentation recommends
setting UNITY_BUILD to false in this case.

This is especially important if we're compiled as a subproject and the
consumer wants to use CMAKE_UNITY_BUILD=ON for the rest of their code
base.

Closes: #158
(cherry picked from commit bf6da9a)
@Larhzu
Copy link
Member

Larhzu commented Dec 22, 2024 via email

@thesamesam
Copy link
Member

Thanks, this looks good to me as well.

@dextercd
Copy link
Contributor Author

Thanks for merging! I hadn't looked at the differences in v5.6 vs. master. I've only been playing with the master branch for the moment.

@Larhzu
Copy link
Member

Larhzu commented Dec 22, 2024 via email

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

Successfully merging this pull request may close these issues.

3 participants