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

Fix #165, integrate NASA OSAL for OS abstraction #173

Closed
wants to merge 1 commit into from
Closed
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
111 changes: 84 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,77 @@
cmake_minimum_required(VERSION 3.5)
project(BPLIB C)

option(BPLIB_INCLUDE_POSIX "Whether or not to the POSIX operating system abstraction as part of BPLib (standalone builds only)" ON)
set(BPLIB_OS_LAYER OSAL CACHE STRING "Operating system adapter layer to enable (POSIX or NASA OSAL)")

# Check if this is a subdirectory within a larger project, or if it was built standalone
# This affects how to find dependencies (e.g. OSAL) and whether to install the libraries
get_directory_property(HAS_PARENT PARENT_DIRECTORY)
if (HAS_PARENT)
set(BPLIB_STANDALONE_BUILD_MODE OFF)
else()
set(BPLIB_STANDALONE_BUILD_MODE ON)
endif()

# The functional test program (bpcat) will only be built by default in a standalone build using POSIX OS adapter
# This is because the bpcat tool relies on direct file handle I/O as well as signals and other POSIX concepts
if (BPLIB_STANDALONE_BUILD_MODE AND BPLIB_OS_LAYER STREQUAL POSIX)
set(BPLIB_DEFAULT_BUILD_TEST_TOOLS ON)
else()
set(BPLIB_DEFAULT_BUILD_TEST_TOOLS OFF)
endif()

# The other unit tests (coverage) can only be built in conjuction with NASA OSAL, as this provides the UT asser
# framework that these tests are built upon. The CFS build communicates this through a variable called ENABLE_UNIT_TESTS.
# If this is set, then inherit its value as the default for BPLIB_ENABLE_UNIT_TESTS (the user can still set it specifically though)
# Otherwise BPLIB_ENABLE_UNIT_TESTS can get its default based on whether this build is using OSAL
if (DEFINED ENABLE_UNIT_TESTS)
set(BPLIB_DEFAULT_BUILD_UNIT_TESTS ${ENABLE_UNIT_TESTS})
elseif (BPLIB_OS_LAYER STREQUAL OSAL)
set(BPLIB_DEFAULT_BUILD_UNIT_TESTS ON)
else()
set(BPLIB_DEFAULT_BUILD_UNIT_TESTS OFF)
endif()

option(BPLIB_BUILD_TEST_TOOLS "Whether or not to build the test programs as part of BPLib (standalone POSIX builds only)" ${BPLIB_DEFAULT_BUILD_TEST_TOOLS})
option(BPLIB_INSTALL_LIBRARIES "Whether or not to install the libraries" ${BPLIB_STANDALONE_BUILD_MODE})
option(BPLIB_USE_EXTERNAL_OSAL "Whether to use an external OSAL package, if OSAL is selected as OS layer" ${BPLIB_STANDALONE_BUILD_MODE})
option(BPLIB_ENABLE_UNIT_TESTS "Whether to build unit tests (requires NASA OSAL and UT Assert)" ${BPLIB_DEFAULT_BUILD_UNIT_TESTS})

set(BPLIB_VERSION_STRING "3.0.99") # development

# no extra link libraries at first
set(BPLIB_SRC)
set(BPLIB_COMMON_COMPILE_OPTIONS)
set(BPLIB_COMMON_PROPERTIES)

# If doing a CFS build, then build position independent code (-fPIC) for all objects
if(IS_CFS_ARCH_BUILD OR BUILD_SHARED_LIBS)
list(APPEND BPLIB_COMMON_PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
# If using an external OSAL package, then locate it now
# This needs to be done at the top level so the imported targets are
# available to all submodules, for unit testing in particular.
if (BPLIB_OS_LAYER STREQUAL OSAL)

if (BPLIB_USE_EXTERNAL_OSAL)
find_package(NasaOsal REQUIRED)
message(STATUS "Using external OSAL ${NasaOsal_VERSION}")
endif()

else()
list(APPEND BPLIB_COMMON_PROPERTIES POSITION_INDEPENDENT_CODE FALSE)
endif()

# Sanity Check - NASA OSAL provides UT assert, so unit tests cannot be built without this
if (BPLIB_ENABLE_UNIT_TESTS)
message(FATAL_ERROR "NASA OSAL is required to build unit tests")
endif()
if (IS_CFS_ARCH_BUILD)
message(FATAL_ERROR "NASA OSAL is required to build CFS library")
endif()

endif()

# If using a GNU-style compiler then enable full warnings here.
# May want to add equivalent for other compiler flavors too
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
list(APPEND BPLIB_COMMON_COMPILE_OPTIONS -Wall -Werror -pedantic -Wno-format-truncation -Wno-stringop-truncation)
endif()

# no extra link libraries at first
set(BPLIB_LINK_LIBRARIES)

add_subdirectory(os)
add_subdirectory(common)
add_subdirectory(lib)
add_subdirectory(mpool)
Expand All @@ -44,6 +91,7 @@ add_subdirectory(v7)
list(APPEND BPLIB_SRC
store/file_offload.c

$<TARGET_OBJECTS:bplib_os>
$<TARGET_OBJECTS:bplib_common>
$<TARGET_OBJECTS:bplib_cache>
$<TARGET_OBJECTS:bplib_v7>
Expand All @@ -52,6 +100,7 @@ list(APPEND BPLIB_SRC
)

list(APPEND BPLIB_PRIVATE_INCLUDE_DIRS
$<TARGET_PROPERTY:bplib_os,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_base,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_v7,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_mpool,INTERFACE_INCLUDE_DIRECTORIES>
Expand All @@ -66,35 +115,29 @@ if (IS_CFS_ARCH_BUILD)
# Create the app module using the CFE/OSAL adapter layer
# Note this ignores the setting of BPLIB_INCLUDE_POSIX, as OSAL provides this service
# The CFE build system determines whether to create a shared or static object inside this routine
# TEMPORARY - for now this will also use POSIX until OSAL has the needed features.
add_cfe_app(bplib ${BPLIB_SRC} os/heap.c os/posix.c)
add_cfe_app(bplib ${BPLIB_SRC})

target_link_libraries(bplib ${TINYCBOR_LDFLAGS})

else()

# Building as a standalone library
# This by will usually require a POSIX OS layer
if (BPLIB_INCLUDE_POSIX)

list(APPEND BPLIB_SRC os/heap.c os/posix.c)
list(APPEND BPLIB_LINK_LIBRARIES rt pthread)

endif()

# Add the generic library target
# The flavor of library (shared/static) being built here depends on the BUILD_SHARED_LIBS option
# This directory may be built twice, setting this option differently to build both flavors
add_library(bplib ${BPLIB_SRC})

# compile this library as c99 (but this does not impose the same requirement on other users)
target_compile_features(bplib PRIVATE c_std_99)

# If using GNU GCC, then also enable full warning reporting
target_compile_options(bplib PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS})
# link with the requisite dependencies
target_link_libraries(bplib ${TINYCBOR_LDFLAGS})

# link with the requisite dependencies (this is mainly for POSIX, if using that adapter)
target_link_libraries(bplib ${TINYCBOR_LDFLAGS} ${BPLIB_LINK_LIBRARIES})
# Add in the required link libraries based on OS adapter selection
# this should preferably be in OS subdirectory, but it needs to be done
# at the bplib target which is created here.
if (BPLIB_OS_LAYER STREQUAL OSAL)
target_link_libraries(bplib osal)
elseif(BPLIB_OS_LAYER STREQUAL POSIX)
target_link_libraries(bplib rt pthread)
endif()

# Install and also export this library, so it can be found via
# "find_package()" from some other CMake build
Expand Down Expand Up @@ -134,6 +177,20 @@ else()

endif()

get_target_property(IS_PIC bplib POSITION_INDEPENDENT_CODE)
if (IS_PIC)
set_target_properties(
bplib_base bplib_common bplib_mpool bplib_cache bplib_v7
PROPERTIES POSITION_INDEPENDENT_CODE ${IS_PIC}
)
endif()

# Set the standard compile options for all submodules (c99, full warnings)
foreach(TGT bplib bplib_base bplib_common bplib_mpool bplib_cache bplib_v7)
target_compile_features(${TGT} PRIVATE c_std_99)
target_compile_options(${TGT} PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS})
endforeach()

# Internal/private header files exist within the implementation directories
target_include_directories(bplib PRIVATE ${BPLIB_PRIVATE_INCLUDE_DIRS})

Expand Down
2 changes: 1 addition & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ project(BPLIB_TEST_APP C)
# Sanity check: BPLib test app assumes a POSIX system
# and uses pthread calls. If not POSIX, then this tool
# will not build.
if (NOT BPLIB_INCLUDE_POSIX)
if (NOT BPLIB_OS_LAYER STREQUAL POSIX)
message(FATAL_ERROR "BPLib Test applications require POSIX")
endif()

Expand Down
9 changes: 2 additions & 7 deletions cache/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ add_library(bplib_cache OBJECT
)

target_include_directories(bplib_cache PRIVATE
$<TARGET_PROPERTY:bplib,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_mpool,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_v7,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_common,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_base,INTERFACE_INCLUDE_DIRECTORIES>
)
target_include_directories(bplib_cache PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
$<TARGET_PROPERTY:bplib,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_base,INTERFACE_INCLUDE_DIRECTORIES>
)

# Use the same basic compiler flags as bplib
target_compile_features(bplib_cache PRIVATE c_std_99)
target_compile_options(bplib_cache PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS})
set_target_properties(bplib_cache PROPERTIES ${BPLIB_COMMON_PROPERTIES})
12 changes: 1 addition & 11 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ target_include_directories(bplib_common PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
)

target_include_directories(bplib_common PRIVATE
target_include_directories(bplib_common PUBLIC
$<TARGET_PROPERTY:bplib_base,INTERFACE_INCLUDE_DIRECTORIES>
)

# Use the same basic compiler flags as bplib
target_compile_features(bplib_common PRIVATE c_std_99)
target_compile_options(bplib_common PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS})
set_target_properties(bplib_common PROPERTIES ${BPLIB_COMMON_PROPERTIES})

if(ENABLE_UNIT_TESTS)
#add_subdirectory(ut-stubs)
#add_subdirectory(ut-coverage)
endif(ENABLE_UNIT_TESTS)
5 changes: 0 additions & 5 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,3 @@ target_include_directories(bplib_base PRIVATE
$<TARGET_PROPERTY:bplib_common,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_v7,INTERFACE_INCLUDE_DIRECTORIES>
)

# Use the same basic compiler flags as bplib
target_compile_features(bplib_base PRIVATE c_std_99)
target_compile_options(bplib_base PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS})
set_target_properties(bplib_base PROPERTIES ${BPLIB_COMMON_PROPERTIES})
12 changes: 3 additions & 9 deletions mpool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ add_library(bplib_mpool OBJECT
src/v7_mpstream.c
)

target_include_directories(bplib_mpool PRIVATE
target_include_directories(bplib_mpool PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc

$<TARGET_PROPERTY:bplib,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_v7,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:bplib_common,INTERFACE_INCLUDE_DIRECTORIES>
)
target_include_directories(bplib_mpool PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
)

# Use the same basic compiler flags as bplib
target_compile_features(bplib_mpool PRIVATE c_std_99)
target_compile_options(bplib_mpool PRIVATE ${BPLIB_COMMON_COMPILE_OPTIONS})
set_target_properties(bplib_mpool PROPERTIES ${BPLIB_COMMON_PROPERTIES})
5 changes: 4 additions & 1 deletion mpool/src/v7_mpool_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <string.h>

#include "bplib_api_types.h"
#include "bplib_os.h"

#include "v7_rbtree.h"
#include "v7_mpool.h"
#include "v7_mpool_bblocks.h"
Expand Down Expand Up @@ -344,6 +346,7 @@ const bplib_mpool_block_content_t *bplib_mpool_get_block_content_const(const bpl
bplib_mpool_block_content_t *bplib_mpool_block_dereference_content(bplib_mpool_block_t *cb);

bplib_mpool_block_content_t *bplib_mpool_alloc_block_internal(bplib_mpool_t *pool, bplib_mpool_blocktype_t blocktype,
uint32_t content_type_signature, void *init_arg, uint8_t priority);
uint32_t content_type_signature, void *init_arg,
uint8_t priority);

#endif /* V7_MPOOL_INTERNAL_H */
36 changes: 36 additions & 0 deletions os/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
##################################################################
#
# Bundle Protocol Library (BPLib) CMake build recipe
#
# This is for the "os" submodule
#
##################################################################

set(bplib_os_SOURCES
src/heap.c
)

if (BPLIB_OS_LAYER STREQUAL OSAL)

if (IS_CFS_ARCH_BUILD)
list(APPEND bplib_os_SOURCES src/cfe.c)
endif()

list(APPEND bplib_os_SOURCES src/osal.c)

elseif(BPLIB_OS_LAYER STREQUAL POSIX)

list(APPEND bplib_os_SOURCES src/posix.c)

else()
message(FATAL_ERROR "Unknown OS adapter: ${BPLIB_OS_LAYER}")
endif()

add_library(bplib_os OBJECT
${bplib_os_SOURCES}
)

target_include_directories(bplib_os PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
$<TARGET_PROPERTY:bplib_base,INTERFACE_INCLUDE_DIRECTORIES>
)
20 changes: 5 additions & 15 deletions inc/bplib_os.h → os/inc/bplib_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@
COMPILE TIME CONFIGURATION SETTINGS
******************************************************************************/

#ifdef _GNU_
#define VARG_CHECK(f, a, b) __attribute__((format(f, a, b)))
#else
#define VARG_CHECK(f, a, b)
#endif

/******************************************************************************
DEFINES
******************************************************************************/
Expand All @@ -62,13 +56,11 @@
PROTOTYPES
******************************************************************************/

void bplib_os_init(void);
void bplib_os_enable_log_flags(uint32_t enable_mask);
int bplib_os_log(const char *file, unsigned int line, uint32_t *flags, uint32_t event, const char *fmt, ...)
VARG_CHECK(printf, 5, 6);
int bplib_os_systime(unsigned long *sysnow); /* seconds */
uint64_t bplib_os_get_dtntime_ms(
void); /* get the OS time compatible with the "dtn time" definition (ms resolution + dtn epoch) */
void bplib_os_init(void);
void bplib_os_enable_log_flags(uint32_t enable_mask);
int bplib_os_log(const char *file, unsigned int line, uint32_t *flags, uint32_t event, const char *fmt, ...);
int bplib_os_systime(unsigned long *sysnow); /* seconds */
uint64_t bplib_os_get_dtntime_ms(void);
void bplib_os_sleep(int seconds);
uint32_t bplib_os_random(void);
bp_handle_t bplib_os_createlock(void);
Expand All @@ -82,7 +74,5 @@ int bplib_os_waiton(bp_handle_t h, int timeout_ms);
int bplib_os_wait_until_ms(bp_handle_t h, uint64_t abs_dtntime_ms);
void *bplib_os_calloc(size_t size);
void bplib_os_free(void *ptr);
size_t bplib_os_memused(void);
size_t bplib_os_memhigh(void);

#endif /* BPLIB_OS_H */
Loading