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

[CHERRY-PICK] [Release/202302] UnitTestFrameworkPkg: Fix Google Test components with multiple files #893

Conversation

Flickdm
Copy link
Member

@Flickdm Flickdm commented Jun 6, 2024

Preface

REF: #891

  • Dropping GOOGLETEST_HOST_UNIT_BUILD option as release/202302 does
    not have any expectation to support it.

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4610

Google Test hides test registration in global constructors on global objects. Global constructors are traditionally implemented by placing references to the global constructor's symbol in special sections (traditionally named .ctors or .init_array). These sections are not explicitly referenced by the linker, and libc only looks at special start and end symbols (and calls them).

This works fine if you're linking a program manually using

gcc a.o b.o c.o -o test_suite

but fails miserably when using static libraries (such as what EDK2 does), because traditional static archive symbol resolution rules don't allow for object files to be pulled in to the link if there isn't an undefined symbol reference to that .o elsewhere.

Fix it by passing --whole-archive (GCC) and /WHOLEARCHIVE (MSVC). These options force the linker to pull in the entire static library, thus including previously-unreferenced constructors and making sure multi-file gtest EDK2 components work.

Cc: Michael D Kinney michael.d.kinney@intel.com
Cc: Michael Kubacki mikuback@linux.microsoft.com
Cc: Sean Brogan sean.brogan@microsoft.com

Reviewed-by: Cc: Michael D Kinney michael.d.kinney@intel.com

Description

This corrects an issue with MSVC where unit tests are not being linked correctly - thus creating unit tests with zero tests

For each item, place an "x" in between [ and ] if true. Example: [x].
(you can also check items in the GitHub UI)

  • Impacts functionality?
  • Impacts security?
  • Breaking change?
  • Includes tests?
    • This will cause unit tests to actually run with MSVC
  • Includes documentation?

How This Was Tested

Local builds with Host Based Unit Tests

Integration Instructions

N/A

@Flickdm Flickdm added impact:non-functional Does not have a functional impact impact:testing Affects testing type:bug Something isn't working labels Jun 6, 2024
@Flickdm Flickdm force-pushed the cherrypick/release/202302/unittestframeworkpkg/wholearchive branch from eb3f011 to 6bc99b3 Compare June 6, 2024 23:08
REF: #891
  - Dropping GOOGLETEST_HOST_UNIT_BUILD option as release/202302 does
    not have any expectation to support it.

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4610

Google Test hides test registration in global constructors on global
objects. Global constructors are traditionally implemented by placing
references to the global constructor's symbol in special sections
(traditionally named .ctors or .init_array). These sections are not
explicitly referenced by the linker, and libc only looks at special
start and end symbols (and calls them).

This works fine if you're linking a program manually using

    gcc a.o b.o c.o -o test_suite

but fails miserably when using static libraries (such as what EDK2
does), because traditional static archive symbol resolution rules don't
allow for object files to be pulled in to the link if there isn't an
undefined symbol reference to that .o elsewhere.

Fix it by passing --whole-archive (GCC) and /WHOLEARCHIVE (MSVC). These
options force the linker to pull in the entire static library, thus
including previously-unreferenced constructors and making sure
multi-file gtest EDK2 components work.

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Michael Kubacki <mikuback@linux.microsoft.com>
Cc: Sean Brogan <sean.brogan@microsoft.com>

Reviewed-by: Cc: Michael D Kinney <michael.d.kinney@intel.com>

This corrects an issue with MSVC where unit tests are not being linked
correctly - thus creating unit tests with zero tests

For each item, place an "x" in between `[` and `]` if true. Example:
`[x]`.
_(you can also check items in the GitHub UI)_

- [ ] Impacts functionality?
- [ ] Impacts security?
- [ ] Breaking change?
- [x] Includes tests?
  - This will cause unit tests to actually run with MSVC
- [ ] Includes documentation?

Local builds with Host Based Unit Tests

N/A

---------

Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
Co-authored-by: Pedro Falcato <pedro.falcato@gmail.com>
Co-authored-by: Oliver Smith-Denny <osde@microsoft.com>
@Flickdm Flickdm force-pushed the cherrypick/release/202302/unittestframeworkpkg/wholearchive branch from 6bc99b3 to a2fc339 Compare June 6, 2024 23:09
@Flickdm Flickdm enabled auto-merge (squash) June 6, 2024 23:10
@Flickdm Flickdm merged commit 8bc847a into release/202302 Jun 7, 2024
35 checks passed
@Flickdm Flickdm deleted the cherrypick/release/202302/unittestframeworkpkg/wholearchive branch June 7, 2024 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
impact:non-functional Does not have a functional impact impact:testing Affects testing type:bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants