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: allow building tests in unity mode #14765

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

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 23550c4.
set it globally for autotools, to stay in sync with cmake.
@vszakats vszakats deleted the cm-tests-server-unity branch September 19, 2024 19:34
moritzbuhl pushed a commit to moritzbuhl/curl that referenced this pull request Sep 20, 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:
- 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
vszakats added a commit that referenced this pull request Sep 20, 2024
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