Skip to content

Commit

Permalink
safestringlib: add Cmake support for creating a Debian package
Browse files Browse the repository at this point in the history
Signed-off-by: Soren Friis <[email protected]>
  • Loading branch information
friissoren authored and tomasbw committed Aug 21, 2023
1 parent 02fdf9d commit b0cc18c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
50 changes: 48 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# SPDX-License-Identifier: MIT
# Copyright (C) 2019-2023 Intel Corporation
cmake_minimum_required(VERSION 3.5)
project(safestring)

include(version.cmake)

project(safestring
VERSION ${SAFEC_VERSION_STRING}
DESCRIPTION "Safe C string library"
)

option(BUILD_UNITTESTS "Build also project unit-tests" OFF)

if(NOT DEFINED BUILD_OPT_DEFAULT)
Expand Down Expand Up @@ -184,7 +188,11 @@ target_compile_options(${PROJECT_NAME}_objlib PRIVATE $<$<CONFIG:RELEASE>:-s>)

add_library(${PROJECT_NAME}_shared SHARED $<TARGET_OBJECTS:${PROJECT_NAME}_objlib>)
add_library(${PROJECT_NAME}_static STATIC $<TARGET_OBJECTS:${PROJECT_NAME}_objlib>)
target_include_directories(${PROJECT_NAME}_shared PUBLIC include)

target_include_directories(${PROJECT_NAME}_shared PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_include_directories(${PROJECT_NAME}_static PUBLIC include)

set_target_properties(${PROJECT_NAME}_shared PROPERTIES VERSION ${SAFEC_VERSION_STRING})
Expand All @@ -193,3 +201,41 @@ set_target_properties(${PROJECT_NAME}_static PROPERTIES VERSION ${SAFEC_VERSION_
if(BUILD_UNITTESTS)
add_subdirectory(unittests)
endif()

# Install targets
set(PUBLIC_HEADERS
include/safe_lib_errno.h
include/safe_lib.h
include/safe_mem_lib.h
include/safe_str_lib.h
include/safe_types.h
include/snprintf_s.h
)

set_target_properties(${PROJECT_NAME}_shared PROPERTIES
PUBLIC_HEADER "${PUBLIC_HEADERS}"
)

install(TARGETS ${PROJECT_NAME}_shared EXPORT ${PROJECT_NAME}Config
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT ${PROJECT_NAME}Config DESTINATION share/${PROJECT_NAME}/cmake)

export(TARGETS ${PROJECT_NAME}_shared FILE ${PROJECT_NAME}Config.cmake)

# Debian package information for CPack
set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Intel")
set(CPACK_PACKAGE_VERSION_MAJOR "${SAFEC_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${SAFEC_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${SAFEC_VERSION_PATCH}")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "SafeStringLib is an Intel enhanced version of the Safe C Library by Cisco.")
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_PACKAGE_NAME "lib${PROJECT_NAME}")
set(CPACK_DISPLAY_NAME "lib${PROJECT_NAME} run-time library")
set(CPACK_PACKAGE_NAME "lib${PROJECT_NAME}")
set(CPACK_DESCRIPTION "Safe String library and header files.")
include(GNUInstallDirs)
include(CPack)
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,31 @@ PLANNED ENHANCEMENTS:
----------------------
- Add full sprintf_s() support
- Add full sscanf_s() support


# Compile and create Debian package (Ubuntu)
On Ubuntu (probably also works on other Linux distributions), use the following commands to compile a library and create a Debian package for distribution.
```
cmake -S . -B build
cd build
make -j
cpack
```

The generated package can be installed and removed using the following commands:
```
sudo dpkg -i libsafestring_<version>_amd64.deb
sudo dpkg --purge libsafestring
```

When compiling other projects against the safestring library installed via the Debian package, in the source files:
```
#include <safe_lib.h>
#include <other relevant safestringlib headers>
```

In the CMakeLists.txt, add:
```
target_link_libraries(<target name> safestring_shared <other possible library dependencies>)
```

0 comments on commit b0cc18c

Please sign in to comment.