jyao1 / openspdm

This openspdm is a sample implementation for the DMTF SPDM specification.
Other
20 stars 22 forks source link

Cmake compilation fails on linux with {x64, gcc8.4.1, debug, spdmemu} #187

Closed hMcLauchlan closed 3 years ago

hMcLauchlan commented 3 years ago

Following up on https://github.com/jyao1/openspdm/pull/185, I tried to get cmake compiling but found I could not. Two problems arose:

LTO issues

  1. It looks like GCC plays poorly with link time optimizations. By following the instructions on the README.md, I got some output that looked like:
...
[ 94%] Linking C static library ../../lib/libSpdmDeviceSecretLib.a
/bin/ranlib: ../../lib/libSpdmDeviceSecretLib.a(SpdmDeviceSecretLib.c.o): plugin needed to handle lto object
/bin/ranlib: ../../lib/libSpdmDeviceSecretLib.a(SpdmDeviceSecretLibCert.c.o): plugin needed to handle lto object

...
/tmp/ccifeBRc.ltrans0.ltrans.o: In function `SpdmClientInit':
<artificial>:(.text+0x29e): undefined reference to `SpdmGetContextSize'
<artificial>:(.text+0x301): undefined reference to `SpdmInitContext'
<artificial>:(.text+0x32d): undefined reference to `SpdmRegisterDeviceIoFunc'

After snooping around a bit, it looks a bit like this issue here (https://stackoverflow.com/questions/39236917/using-gccs-link-time-optimization-with-static-linked-libraries).

As far as I can tell, this might be a linux-y problem that doesn't manifest on windows. Although I don't have a windows machine handy so it's hard to test. In either case, I applied the recommended fix from that post

CMAKE_C_ARCHIVE_CREATE ( = <CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>)
CMAKE_C_ARCHIVE_FINISH ( = true) # Or any other no-op command

Missing library issues

Following the previous fix, I hit:

[100%] Linking C executable ../bin/SpdmDump
/bin/ld: cannot find -lDebugLibNull
/bin/ld: cannot find -lSpdmDeviceSecretLibNull
collect2: error: ld returned 1 exit status
make[2]: *** [SpdmDump/CMakeFiles/SpdmDump.dir/build.make:278: bin/SpdmDump] Error 1
make[1]: *** [CMakeFiles/Makefile2:942: SpdmDump/CMakeFiles/SpdmDump.dir/all] Error 2

which seemed to have a fairly simple fix. I just added

@@ -429,14 +431,16 @@ if(TESTTYPE STREQUAL "SpdmEmu")
             OsStub/DebugLib
             OsStub/RngLib
             OsStub/MemoryAllocationLib
+            OsStub/SpdmDeviceSecretLibNull
+            OsStub/DebugLibNull
             SpdmEmu/SpdmDeviceSecretLib
     )

after which my build works.

Scanning dependencies of target SpdmDump
[ 98%] Linking C executable ../bin/SpdmDump
[100%] Built target SpdmDump

Followup

As followup, I want to ask:

For now I will keep working on top of these fixes on modifications to #185. Thanks!

jyao1 commented 3 years ago

Thank you. I have fixed lib missing issue in efce80e1b7cde73aaa4ee71cfb1e767de04a92e2.

However, I did not encounter LTO issue. I can build it successfully. I am using below: gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0 cmake version 3.16.3

What is your configuration?

Or you can try to fix and send patch for review.

hMcLauchlan commented 3 years ago

I'm currently testing on:

gcc version 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
cmake version 3.18.2

which fails.

I also compiled off temp_master on my dev machine, which has

gcc version 10.3.1 20210422 (Red Hat 10.3.1-1) (GCC) 
cmake version 3.19.7

which succeeds. I reckon some improvement to LTO between GCC 8 and GCC 9+ is the reason.

I think the fix I made (forcing the LTO plugin to be loaded) should be idempotent with any GCC fixes. That is to say, it will fix builds on older GCC without breaking newer builds. I just ported that change on my personal machine with GCC10 and it works fine.

I'll send a patch shortly. Thanks!