Skip to content

cmake: allow building tests in unity mode #14765

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

Closed
wants to merge 15 commits into from

Conversation

vszakats
Copy link
Member

@vszakats vszakats commented Sep 2, 2024

Makes building tests noticeably faster.

Apply changes/fixes/workarounds to make Unity work:

  • rename test variables to avoid collisions or shadowing each other when
    combined into single units.
  • add workaround to avoid applying lib/memdebug.h overrides to system
    headers declaring/defining getaddrinfo()/freeaddrinfo() for
    tests/server/resolve.c. This replaces a previous workaround that
    worked for that specific source.
  • rename test macro CTRL clashing with Cygwin sys/ioctl.h.
  • add include guard to test.h.

Also:

  • exclude tests/http/clients which are all single-source. (like
    docs/examples.)

Build time improvements for tests:


  • try #define CURL_NO_GETADDRINFO_OVERRIDE as an alternative hack for getaddrinfo().

@dfandrich
Copy link
Contributor

Analysis of PR #14765 at e7e164e8:

Test 900 failed, which has NOT been flaky recently, so there could be a real issue in the PR.

Test 940 failed, which has NOT been flaky recently, so there could be a real issue in the PR.

Test 3005 failed, which has NOT been flaky recently, so there could be a real issue in the PR.

Generated by Testclutch

@vszakats vszakats force-pushed the cm-tests-server-unity branch 2 times, most recently from f9ead44 to 474f743 Compare September 3, 2024 12:40
@vszakats vszakats marked this pull request as ready for review September 3, 2024 13:23
@vszakats vszakats force-pushed the cm-tests-server-unity branch from 15a346c to 276e686 Compare September 3, 2024 13:23
@vszakats vszakats force-pushed the cm-tests-server-unity branch from 34d33a7 to d810072 Compare September 4, 2024 13:12
@vszakats vszakats force-pushed the cm-tests-server-unity branch from d810072 to 4b2b42a Compare September 19, 2024 14:58
Except tests/http/clients which are always single sources.
```
C:\projects\curl\tests\libtest\first.c(123,36): warning C4459: declaration of 'buffer' hides global declaration [C:\projects\curl\_bld\tests\libtest\lib651.vcxproj]
C:\projects\curl\tests\libtest\first.c(123,36): warning C4459: declaration of 'buffer' hides global declaration [C:\projects\curl\_bld\tests\libtest\lib652.vcxproj]
C:\projects\curl\tests\libtest\first.c(123,36): warning C4459: declaration of 'buffer' hides global declaration [C:\projects\curl\_bld\tests\libtest\lib666.vcxproj]
C:\projects\curl\tests\libtest\first.c(123,36): warning C4459: declaration of 'buffer' hides global declaration [C:\projects\curl\_bld\tests\libtest\lib1911.vcxproj]
C:\projects\curl\tests\libtest\first.c(139,12): warning C4459: declaration of 'result' hides global declaration [C:\projects\curl\_bld\tests\unit\unit1652.vcxproj]
```
Ref: https://ci.appveyor.com/project/curlorg/curl/builds/50522821/job/8q3um37s94uudsen
```
In file included from /cygdrive/d/a/curl/curl/bld/tests/server/CMakeFiles/sws.dir/Unity/unity_0_c.c:37:
/cygdrive/d/a/curl/curl/tests/server/sws.c:1389: error: "CTRL" redefined
 1389 | #define CTRL  0
[  5%] Building C object tests/server/CMakeFiles/sws.dir/__/__/lib/memdebug.c.o
      |
In file included from /usr/include/sys/ioctl.h:15,
                 from /cygdrive/d/a/curl/curl/lib/nonblock.c:28,
                 from /cygdrive/d/a/curl/curl/bld/tests/server/CMakeFiles/sws.dir/Unity/unity_0_c.c:7:
/usr/include/sys/termios.h:74: note: this is the location of the previous definition
   74 | #define CTRL(ch)        ((ch)&0x1F)
      |
```
https://github.com/curl/curl/actions/runs/10674023027/job/29583891142?pr=14765 (cygwin)
macOS: SecureTransport: https://github.com/curl/curl/actions/runs/10674833829/job/29585938056?pr=14765
Linux: https://github.com/curl/curl/actions/runs/10674833840/job/29585935178?pr=14765
msys2, mingw ucrt, uwp: https://github.com/curl/curl/actions/runs/10674833839/job/29585940816?pr=14765
netbsd/openbsd: https://github.com/curl/curl/actions/runs/10674833831/job/29585934198?pr=14765

resolve.c is unique by using getaddrinfo/freeaddrinfo. In some build
envs/config these functions are macros.

lib/memdebug.h gets included via other sources before including system
headers declaring (or rather: defining) these functions. This makes
memdebug.h redefinitions applied to the system headers, breaking them,
triggering bogus warnings, and missing to declare/define the functions
when trying to call them in resolve.c.

The workaround is to undefing them before the system headers (e.g.
`netdb.h` on macOS), and forcing to re-apply `memdebug.h` on the next
inclusion (which always comes _after_ system headers). This causes
'multiple definition' warnings for functions declared in `memdebug.h`.
Fix those by guarding the declarations with an extra guard that is never
undefined.

This surely has a cleaner solution, but it'd need rethinking the way
these overrides are done, or perhaps offer apply/deapply/reapply
headers to make them compatible with sources with spots where the
`memdebug.h` effect is undesired.

Or, perhaps make sure to include all potentially affected system headers
from `memdebug.h` itself, _before_ it applying any redefinitions. This
ensures no interference.

Or, drop memdebug.h from server builds. (Though this issue is also
present in `src`, though in a more "lucky" constellation.)

Or, turn off UNITY for resolve.c specifically.
```
D:/a/curl/curl/lib/warnless.c: In function 'curlx_read':
D:/a/curl/curl/lib/warnless.c:372:34: error: declaration of 'buf' shadows a global declaration [-Werror=shadow]
  372 | ssize_t curlx_read(int fd, void *buf, size_t count)
      |                            ~~~~~~^~~
In file included from D:/a/curl/curl/bld/tests/libtest/CMakeFiles/lib677.dir/Unity/unity_0_c.c:4:
D:/a/curl/curl/tests/libtest/lib677.c:31:13: note: shadowed declaration is here
   31 | static char buf[1024];
      |             ^~~
D:/a/curl/curl/lib/warnless.c: In function 'curlx_write':
D:/a/curl/curl/lib/warnless.c:377:41: error: declaration of 'buf' shadows a global declaration [-Werror=shadow]
  377 | ssize_t curlx_write(int fd, const void *buf, size_t count)
      |                             ~~~~~~~~~~~~^~~
D:/a/curl/curl/tests/libtest/lib677.c:31:13: note: shadowed declaration is here
   31 | static char buf[1024];
      |             ^~~
```
Ref: https://github.com/curl/curl/actions/runs/10682565270/job/29608750021?pr=14765
This reverts commit 23550c447dcbdc3a94ee9a64ea0772385d6c23b0.
This reverts commit d5174ac3f4a749e106be9eed919a052462f2bea6.
set it globally for autotools, to stay in sync with cmake.
@vszakats vszakats force-pushed the cm-tests-server-unity branch from 4b2b42a to 1ca473f Compare September 19, 2024 16:05
@vszakats vszakats closed this in 3efba94 Sep 19, 2024
@vszakats vszakats deleted the cm-tests-server-unity branch September 19, 2024 19:34
vszakats added a commit that referenced this pull request Sep 20, 2024
vszakats added a commit to vszakats/curl that referenced this pull request Feb 9, 2025
Before this patch curl code was redefining `getaddrinfo` and
`freeaddrinfo` system symbols to plug in its debug wrappers. This was
causing pains to avoid applying the redefinitions to system headers
defining these functions, and to the local debug wrappers. Especially
in unity builds. It also required workarounds for systems where these
symbols are already macros.

Introduce curl-namespaced macros for these functions and use them.
This allows to drop all workarounds and makes it work in all envs,
local targets and unity/bundle combinations.

Ref: curl#16272
Ref: 71cf0d1 curl#14772
Ref: 3efba94 curl#14765
Ref: f7d5f47 curl#14399

Closes curl#16274
vszakats added a commit to vszakats/curl that referenced this pull request Feb 9, 2025
Before this patch curl code was redefining `getaddrinfo` and
`freeaddrinfo` system symbols to plug in its debug wrappers. This was
causing pains to avoid applying the redefinitions to system headers
defining these functions, and to the local debug wrappers. Especially
in unity builds. It also required workarounds for systems where these
symbols are already macros.

Introduce curl-namespaced macros for these functions and use them.
This allows to drop all workarounds and makes it work in all envs,
local targets and unity/bundle combinations.

Ref: curl#16272
Ref: 71cf0d1 curl#14772
Ref: 3efba94 curl#14765
Ref: f7d5f47 curl#14399

Closes curl#16274
vszakats added a commit that referenced this pull request Feb 13, 2025
Before this patch curl code was redefining `getaddrinfo` and
`freeaddrinfo` system symbols to plug in its debug wrappers. This was
causing pains to avoid applying the redefinitions to system headers
defining these functions, and to the local debug wrappers. Especially
in unity builds. It also required workarounds for systems where these
symbols are already macros.

Introduce curl-namespaced macros for these functions and use them.
This allows to drop all workarounds and makes it work in all envs,
local targets and unity/bundle combinations.

Also drop GHA/windows workaround and use the same unity batch across
all jobs. Follow-up to 29e4eda #16272

Ref: #16272
Ref: 71cf0d1 #14772
Ref: 3efba94 #14765
Ref: f7d5f47 #14399

Closes #16274
vszakats added a commit to vszakats/curl that referenced this pull request Mar 13, 2025
Include two more sources in unity mode to optimize builds, syncing this
pattern with `lib` and `src`.

Follow-up to de0693f curl#16274
Follow-up to 3efba94 curl#14765
Cherry-picked from curl#15000
vszakats added a commit to vszakats/curl that referenced this pull request Mar 13, 2025
Include two more sources in unity mode to optimize builds, syncing this
pattern with `lib` and `src`.

Follow-up to de0693f curl#16274
Follow-up to 3efba94 curl#14765
Cherry-picked from curl#15000
vszakats added a commit that referenced this pull request Mar 13, 2025
Include more sources in unity mode to optimize libtest and tests/server
builds for non-debug-enabled builds, syncing this pattern with `lib` and
`src`.

It reduces build steps from 62 to 47 (-14, -24%) with test bundles.
Without test bundles, from 680 to 642 (-38, -6%).

Follow-up to de0693f #16274
Follow-up to 3efba94 #14765
Cherry-picked from #15000
Closes #16695
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
Makes building tests noticeably faster.

Apply changes/fixes/workarounds to make Unity work:
- rename test variables to avoid collisions or shadowing each other when
  combined into single units.
- add workaround to avoid applying `lib/memdebug.h` overrides to system
  headers declaring/defining `getaddrinfo()`/`freeaddrinfo()` for
  `tests/server/resolve.c`. This replaces a previous workaround that
  worked for that specific source.
- rename test macro `CTRL` clashing with Cygwin `sys/ioctl.h`.
- add include guard to `test.h`.

Also:
- exclude `tests/http/clients` which are all single-source. (like
  `docs/examples`.)

Build time improvements for tests:
- AppVeyor CI:
  - MSVC 2008, 2010: 1 minute faster (4m8s -> 2m56s, 3m19s -> 2m24s)
  - MSVC 2022 arm64: 3.5 minutes faster (10m18s -> 6m48s)
  before: https://ci.appveyor.com/project/curlorg/curl/builds/50522785
  after: https://ci.appveyor.com/project/curlorg/curl/builds/50522942
- GHA:
  - Cygwin: 1.5 minutes faster (3m13s -> 1m43s)
    before: https://github.com/curl/curl/actions/runs/10681535327/job/29605384398
    after: https://github.com/curl/curl/actions/runs/10680818726/job/29603130637
  - Windows:
    before: https://github.com/curl/curl/actions/runs/10680818713
    after: https://github.com/curl/curl/actions/runs/10683850187
    - MSYS2, mingw-w64: 1 minute faster
    - MSVC: 30 seconds faster (3m17s -> 2m48s)
  - macOS: double speed (39s -> 18s)
    before: https://github.com/curl/curl/actions/runs/10680818753/job/29603133447
    after: https://github.com/curl/curl/actions/runs/10683850174/job/29612914515
  - Linux: almost double speed (30/31s -> 18s)
    before: https://github.com/curl/curl/actions/runs/10681535311/job/29605387156
    after: https://github.com/curl/curl/actions/runs/10680818721/job/29603133976
  - non-native: no obvious effect.
    before: https://github.com/curl/curl/actions/runs/10680818722
    after: https://github.com/curl/curl/actions/runs/10683850187
  - Old Linux: Unity mode not supported by old CMake, no effect.

Closes curl#14765
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
Before this patch curl code was redefining `getaddrinfo` and
`freeaddrinfo` system symbols to plug in its debug wrappers. This was
causing pains to avoid applying the redefinitions to system headers
defining these functions, and to the local debug wrappers. Especially
in unity builds. It also required workarounds for systems where these
symbols are already macros.

Introduce curl-namespaced macros for these functions and use them.
This allows to drop all workarounds and makes it work in all envs,
local targets and unity/bundle combinations.

Also drop GHA/windows workaround and use the same unity batch across
all jobs. Follow-up to 29e4eda curl#16272

Ref: curl#16272
Ref: 71cf0d1 curl#14772
Ref: 3efba94 curl#14765
Ref: f7d5f47 curl#14399

Closes curl#16274
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
Include more sources in unity mode to optimize libtest and tests/server
builds for non-debug-enabled builds, syncing this pattern with `lib` and
`src`.

It reduces build steps from 62 to 47 (-14, -24%) with test bundles.
Without test bundles, from 680 to 642 (-38, -6%).

Follow-up to de0693f curl#16274
Follow-up to 3efba94 curl#14765
Cherry-picked from curl#15000
Closes curl#16695
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants