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

Added shared library and pkg-config support for libspdm #2402

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 2.8.12)

project("libspdm" C)
FILE(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/VERSION.md CMAKE_INPUT_PROJECT_VERSION)
string(REPLACE " " "_" CMAKE_PROJECT_VERSION ${CMAKE_INPUT_PROJECT_VERSION})

#
# Build Configuration Macro Definition
Expand All @@ -16,6 +18,7 @@ SET(CMAKE_BUILD_TYPE ${TARGET} CACHE STRING "Choose the target of build: Debug R
SET(CRYPTO ${CRYPTO} CACHE STRING "Choose the crypto of build: mbedtls openssl" FORCE)
SET(GCOV ${GCOV} CACHE STRING "Choose the target of Gcov: ON OFF, and default is OFF" FORCE)
SET(STACK_USAGE ${STACK_USAGE} CACHE STRING "Choose the target of STACK_USAGE: ON OFF, and default is OFF" FORCE)
SET(BUILD_LINUX_SHARED_LIB ${BUILD_LINUX_SHARED_LIB} CACHE STRING "Choose if libspdm shared library should be built for linux: ON OFF, and default is OFF" FORCE)

if(NOT GCOV)
SET(GCOV "OFF")
Expand All @@ -25,6 +28,10 @@ if(NOT STACK_USAGE)
SET(STACK_USAGE "OFF")
endif()

if(NOT BUILD_LINUX_SHARED_LIB)
SET(BUILD_LINUX_SHARED_LIB "OFF")
endif()

SET(LIBSPDM_DIR ${PROJECT_SOURCE_DIR})

#
Expand Down Expand Up @@ -974,4 +981,65 @@ else()
endif()
endif()
endif()

if ((BUILD_LINUX_SHARED_LIB STREQUAL "ON") AND (CMAKE_SYSTEM_NAME MATCHES "Linux"))
SET(LIB_NAME "spdm")
SET(CMAKE_PROJECT_NAME "libspdm")
SET(CMAKE_PROJECT_DESCRIPTION "libspdm shared library")

SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

add_library(${LIB_NAME} SHARED
$<TARGET_OBJECTS:spdm_common_lib>
$<TARGET_OBJECTS:spdm_crypt_lib>
$<TARGET_OBJECTS:spdm_requester_lib>
$<TARGET_OBJECTS:spdm_responder_lib>
$<TARGET_OBJECTS:spdm_secured_message_lib>
$<TARGET_OBJECTS:spdm_transport_mctp_lib>
$<TARGET_OBJECTS:spdm_transport_pcidoe_lib>
)

add_library(${LIB_NAME}_platform SHARED
$<TARGET_OBJECTS:debuglib>
$<TARGET_OBJECTS:malloclib>
$<TARGET_OBJECTS:platform_lib>
uvinrg marked this conversation as resolved.
Show resolved Hide resolved
$<TARGET_OBJECTS:rnglib>
$<TARGET_OBJECTS:memlib>
)

add_library(${LIB_NAME}_crypto SHARED
$<TARGET_OBJECTS:spdm_crypt_ext_lib>
$<TARGET_OBJECTS:cryptlib_${CRYPTO}>
)

if(CRYPTO STREQUAL "mbedtls")
SET(CRYPTO_DEPS "-lmbedtls -lmbedx509 -lmbedcrypto")
TARGET_LINK_LIBRARIES(${LIB_NAME}_crypto
PUBLIC mbedtls
PUBLIC mbedx509
PUBLIC mbedcrypto
)
elseif(CRYPTO STREQUAL "openssl")
SET(CRYPTO_DEPS "-lssl -lcrypto")
TARGET_LINK_LIBRARIES(${LIB_NAME}_crypto
PUBLIC ssl
PUBLIC crypto
)
endif()

TARGET_LINK_LIBRARIES(${LIB_NAME}
PUBLIC ${LIB_NAME}_platform
PUBLIC ${LIB_NAME}_crypto
)

install(DIRECTORY include DESTINATION include/libspdm)
configure_file(libspdm.pc.in lib/pkgconfig/libspdm.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/lib/pkgconfig/libspdm.pc DESTINATION lib/pkgconfig/)
install(TARGETS ${LIB_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS ${LIB_NAME}_platform LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS ${LIB_NAME}_crypto LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
endif()
21 changes: 21 additions & 0 deletions doc/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ cmake -DARCH=<arch> -DTOOLCHAIN=NONE -DTARGET=<Debug|Release> -DCRYPTO=<mbedtls|
make
```

### Linux Shared Library Builds

Supports shared libraries building and pkg-config '.pc' file generation and installation.
Will generate:
- libspdm.so - main library code, all subprojects from "library" folder
- libspdm_platform.so - subprojects in the "os_stub" folder related to platform code, like memory allocation, random number generator, etc.
- libspdm_crypto.so - cryptography related code for libspdm to dynamically link to either Mbed TLS or OpenSSL shared libraries.
All three libraries are required for an application that uses libspdm, but the integrator is free to implement their own versions of libspdm_platform or libspdm_crypto libraries and link with their implementations.
Will install pc file and all required headers and shared libraries (except for spdm_device_secret_lib_sample which the integrator has to implement), so application developers can use 'pkg-config --libs libspdm' and 'pkg-config --cflags libspdm' to link with libspdm

To build with shared library support:
```
cmake -DARCH=x64 -DTOOLCHAIN=GCC -DTARGET=Release -DCRYPTO=mbedtls -DBUILD_LINUX_SHARED_LIB=ON ..
```

To compile and link with libspdm:
```
gcc `pkg-config --cflags libspdm` -c libspdm_app.c -o libspdm_app.o
gcc libspdm_app.o `pkg-config --libs libspdm` libspdm_app
```

### Disabling unit and fuzz tests

Unit tests can be disable by adding -DDISABLE_TESTS=1 to CMake.
Expand Down
12 changes: 12 additions & 0 deletions libspdm.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=${exec_prefix}/lib
includedir=${prefix}/include/lib@LIB_NAME@

Name: @CMAKE_PROJECT_NAME@
Description: @CMAKE_PROJECT_DESCRIPTION@
Version: @CMAKE_PROJECT_VERSION@

Requires:
Cflags: -I${includedir} -I${includedir}/include
Libs: -L${libdir} -l@LIB_NAME@ -l@LIB_NAME@_platform -l@LIB_NAME@_crypto @CRYPTO_DEPS@