Remove unnecessary CMake modules and fixup os/CMakeLists.txt

Signed-off-by: Jeroen Koekkoek <jeroen@koekkoek.nl>
This commit is contained in:
Jeroen Koekkoek 2018-12-20 16:26:15 +01:00
parent 1990007614
commit e25656a4c5
39 changed files with 406 additions and 210 deletions

View file

@ -9,58 +9,115 @@
#
# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#
string(TOLOWER ${CMAKE_SYSTEM_NAME} platform)
include(CheckCSourceCompiles)
include(CheckLibraryExists)
include(GenerateExportHeader)
set (posix_platforms darwin linux sunos)
IF(${platform} IN_LIST posix_platforms)
set(platform posix)
ENDIF()
set(sources
src/os_atomics.c
src/os_init.c
src/os_log.c
src/os_ifaddrs.c
src/os_socket.c
src/os_thread.c
src/os_time.c
src/os_errno.c
src/os_iter.c
src/os_strlcpy.c)
# For posix platforms include the files in the posix/ directory.
PREPEND(srcs_platform ${platform} os_platform_errno.c os_platform_heap.c os_platform_init.c os_platform_process.c os_platform_ifaddrs.c os_platform_socket.c os_platform_stdlib.c os_platform_sync.c os_platform_thread.c os_platform_time.c)
string(TOLOWER ${CMAKE_SYSTEM_NAME} system_name)
set(system_sources
os_platform_errno.c
os_platform_heap.c
os_platform_ifaddrs.c
os_platform_socket.c
os_platform_stdlib.c
os_platform_sync.c
os_platform_thread.c
os_platform_time.c
os_platform_init.c
os_platform_process.c)
include (GenerateExportHeader)
PREPEND(srcs_os "${CMAKE_CURRENT_SOURCE_DIR}/src" os_atomics.c os_init.c os_log.c os_ifaddrs.c os_socket.c os_thread.c os_time.c os_errno.c os_iter.c ${srcs_platform})
foreach(source ${system_sources})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/${system_name}/${source}")
list(APPEND sources "src/${system_name}/${source}")
else()
list(APPEND sources "src/posix/${source}")
endif()
endforeach()
add_library(OSAPI ${srcs_os})
configure_file(
"${CMAKE_CURRENT_LIST_DIR}/cmake/os_project.h.in"
"include/os/os_project.h")
add_library(OSAPI ${sources})
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
target_link_libraries(OSAPI INTERFACE Threads::Threads)
if(WIN32)
# Link with Win32 core-libraries
target_link_libraries(OSAPI INTERFACE wsock32 ws2_32 iphlpapi)
# Many of the secure versions provided by Microsoft have failure modes
# which are not supported by our abstraction layer, so efforts trying
# to use the _s versions aren't typically the proper solution and C11
# (which contains most of the secure versions) is 'too new'. So we rely
# on static detection of misuse instead of runtime detection, so all
# these warnings can be disabled on Windows.
target_compile_definitions(OSAPI PRIVATE _CRT_SECURE_NO_WARNINGS)
# Disable warnings for deprecated Winsock API calls in general.
target_compile_definitions(OSAPI PRIVATE _WINSOCK_DEPRECATED_NO_WARNINGS)
# Disable warnings for deprecated POSIX names.
target_compile_definitions(OSAPI PRIVATE -D_CRT_NONSTDC_NO_DEPRECATE)
elseif(UNIX AND NOT APPLE)
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
# Shared libs will have this by default. Static libs need it too on x86_64.
set_property(TARGET OSAPI PROPERTY POSITION_INDEPENDENT_CODE TRUE)
endif()
check_library_exists(c clock_gettime "" HAVE_CLOCK_GETTIME)
if(NOT HAVE_CLOCK_GETTIME)
# Before glibc 2.17, clock_gettime was in librt.
check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME)
if(HAVE_CLOCK_GETTIME)
target_link_libraries(OSAPI INTERFACE rt)
endif()
endif()
if(NOT HAVE_CLOCK_GETTIME)
message(FATAL_ERROR "clock_gettime is not available")
endif()
endif()
if(${CMAKE_C_COMPILER_ID} STREQUAL "SunPro")
target_link_libraries(OSAPI INTERFACE -lsocket -lnsl)
target_compile_definitions(OSAPI PRIVATE -KPIC)
endif()
configure_file("cmake/os_project.h.in" "include/os/os_project.h")
target_sources(OSAPI PRIVATE "include/os/os_project.h")
generate_export_header(OSAPI EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/exports/os/osapi_export.h")
target_link_libraries(OSAPI INTERFACE Abstraction)
target_include_directories(OSAPI PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/exports/>"
"$<INSTALL_INTERFACE:${INSTALL_PREFIX}/include/>" "$<INSTALL_INTERFACE:${INSTALL_PREFIX}/exports/>")
target_sources(OSAPI PRIVATE "include/os/os_project.h")
target_include_directories(OSAPI
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/>")
target_include_directories(
OSAPI PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/exports/>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/>"
"$<INSTALL_INTERFACE:${INSTALL_PREFIX}/include/>"
"$<INSTALL_INTERFACE:${INSTALL_PREFIX}/exports/>")
if(BUILD_TESTING)
add_subdirectory(tests)
endif()
if(${CMAKE_C_COMPILER_ID} STREQUAL "SunPro")
target_link_libraries(OSAPI INTERFACE -lsocket -lnsl)
add_definitions(-KPIC)
endif()
install(
FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_public.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_decl_attributes.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_decl_attributes_sal.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ddsc/os"
COMPONENT dev)
# Currently, only windows and posix platforms are supported.
IF(WIN32 AND NOT UNIX)
install(
FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/os/windows/os_platform_public.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ddsc/os"
COMPONENT dev)
ELSE()
install(
FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/os/posix/os_platform_public.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ddsc/os"
COMPONENT dev)
ENDIF()
FILES
"${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_public.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_decl_attributes.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/os/os_decl_attributes_sal.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/os/${system_name}/os_platform_public.h"
DESTINATION
"${CMAKE_INSTALL_INCLUDEDIR}/ddsc/os"
COMPONENT
dev)

View file

@ -54,6 +54,7 @@
#include "os_errno.h"
#include "os_iter.h"
#include "os_log.h"
#include "os_strlcpy.h"
#define OSPL_VERSION_STR "aap"
#define OSPL_HOST_STR "noot"

View file

@ -0,0 +1,72 @@
/*
* Copyright(c) 2006 to 2018 ADLINK Technology Limited and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
* v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
#ifndef OS_STRLCPY_H
#define OS_STRLCPY_H
#include "os/os_defs.h"
#if defined (__cplusplus)
extern "C" {
#endif
/**
* @brief Copy string.
*
* Copy string to buffer with specified size. The string is truncated if there
* is not enough space. The resulting string is guaranteed to be null
* terminated if there is space.
*
* @param[out] dest Destination buffer.
* @param[in] src Null terminated string to copy to dest.
* @param[in] size Number of bytes available in dest.
*
* @returns Number of characters copied to dest (excluding the null byte), or
* the number of characters that would have been copied if dest is not
* sufficiently large enough.
*/
_Success_(return < size)
OSAPI_EXPORT
size_t
os_strlcpy(
_Out_writes_z_(size) char * __restrict dest,
_In_z_ const char * __restrict src,
_In_ size_t size);
/**
* @brief Concatenate strings.
*
* Append the string specified by src to the string specified by dest. The
* terminating null byte at the end of dest is overwritten. The resulting
* string is truncated if there is not enough space. The resulting string
* guaranteed to be null terminated if there is space.
*
* @param[inout] dest Destination buffer.
* @param[in] src Null terminated string to append to dest.
* @param[in] size Number of bytes available in dest.
*
* @returns Number of characters copied to dest (excluding the null byte), or
* the number of characters that would have been copied if dest is not
* sufficiently large enough.
*/
_Success_(return < size)
OSAPI_EXPORT
size_t
os_strlcat(
_Inout_updates_z_(size) char * __restrict dest,
_In_z_ const char * __restrict src,
_In_ size_t size);
#if defined (__cplusplus)
}
#endif
#endif /* OS_STRLCPY_H */

80
src/os/src/os_strlcpy.c Normal file
View file

@ -0,0 +1,80 @@
/*
* Copyright(c) 2006 to 2018 ADLINK Technology Limited and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
* v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
#include <assert.h>
#include <string.h>
#include "os/os.h"
_Success_(return < size)
size_t
os_strlcpy(
_Out_writes_z_(size) char * __restrict dest,
_In_z_ const char * __restrict src,
_In_ size_t size)
{
size_t srclen = 0;
assert(dest != NULL);
assert(src != NULL);
/* strlcpy must return the number of bytes that (would) have been written,
i.e. the length of src. */
srclen = strlen(src);
if (size > 0) {
size_t len = srclen;
if (size <= srclen) {
len = size - 1;
}
memcpy(dest, src, len);
dest[len] = '\0';
}
return srclen;
}
/* NOTE: os_strlcat does not forward to strlcat too avoid a bug in the macOS
implementation where it does not return the right result if dest
contains more characters than the size specified if size is either
0 or 1. */
_Success_(return < size)
size_t
os_strlcat(
_Inout_updates_z_(size) char * __restrict dest,
_In_z_ const char * __restrict src,
_In_ size_t size)
{
size_t destlen, srclen;
assert(dest != NULL);
assert(src != NULL);
/* strlcat must return the number of bytes that (would) have been written,
i.e. the length of dest plus the length of src. */
destlen = strlen(dest);
srclen = strlen(src);
if (SIZE_MAX == destlen) {
srclen = 0;
} else if ((SIZE_MAX - destlen) <= srclen) {
srclen = (SIZE_MAX - destlen) - 1;
}
if (size > 0 && --size > destlen) {
size_t len = srclen;
size -= destlen;
if (size <= srclen) {
len = size;
}
memcpy(dest + destlen, src, len);
dest[destlen + len] = '\0';
}
return destlen + srclen;
}

View file

@ -11,6 +11,8 @@
*/
#include <string.h>
#include "os/os.h"
os_result
os_gethostname(
char *hostname,
@ -23,7 +25,7 @@ os_gethostname(
if ((strlen(hostnamebuf)+1) > buffersize) {
result = os_resultFail;
} else {
strncpy (hostname, hostnamebuf, buffersize);
os_strlcpy (hostname, hostnamebuf, buffersize);
result = os_resultSuccess;
}
} else {

View file

@ -25,8 +25,9 @@ set(sources
"thread.c"
"thread_cleanup.c"
"strcasecmp.c"
"log.c")
"log.c"
"strlcpy.c")
add_cunit_executable(abstraction ${sources})
target_link_libraries(abstraction OSAPI)
add_cunit_executable(cunit_abstraction ${sources})
target_link_libraries(cunit_abstraction OSAPI)

View file

@ -81,6 +81,7 @@ FILE *fmemopen(void *buf, size_t size, const char *mode)
if (err) {
errno = err;
} else {
OS_WARNING_MSVC_OFF(4996);
if ((fd = _open_osfhandle((intptr_t)hdl, _O_APPEND)) == -1) {
/* errno set by _open_osfhandle. */
CloseHandle(hdl);
@ -90,6 +91,7 @@ FILE *fmemopen(void *buf, size_t size, const char *mode)
} else {
return fh;
}
OS_WARNING_MSVC_ON(4996);
}
return NULL;

View file

@ -355,7 +355,7 @@ CU_Test(os_rwlock, read, false)
printf ("concurrent_tryread_access = %d\n", sd.concurrent_tryread_access);
printf ("concurrent_trywrite_access = %d\n", sd.concurrent_trywrite_access);
sprintf (buffer, "Corrupt counter = %d, Loop counter is %d",
snprintf (buffer, sizeof(buffer), "Corrupt counter = %d, Loop counter is %d",
sd.read_corrupt_count + sd.write_corrupt_count + sd.tryread_corrupt_count + sd.trywrite_corrupt_count,
sd.concurrent_read_access + sd.concurrent_write_access + sd.concurrent_tryread_access + sd.concurrent_trywrite_access);
@ -419,7 +419,7 @@ CU_Test(os_rwlock, read, false)
par[i].read_access, par[i].concurrent_read_access, i);
}
sprintf (buffer, "Corrupt read counter = %d, Read loop counter is %d", sd.read_corrupt_count, sd.concurrent_read_access);
snprintf (buffer, sizeof(buffer), "Corrupt read counter = %d, Read loop counter is %d", sd.read_corrupt_count, sd.concurrent_read_access);
CU_ASSERT (sd.read_corrupt_count == 0 && sd.concurrent_read_access > 0);
/* Test read on rwlock with PRIVATE scope and Success result & not locked */
@ -443,7 +443,7 @@ CU_Test(os_rwlock, write, false)
/* Test critical section WRITE access with locking and PRIVATE scope */
printf ("Starting os_rwlock_write_001\n");
sprintf (buffer, "Corrupt write counter = %d, Write loop counter is %d", sd.write_corrupt_count, sd.concurrent_write_access);
snprintf (buffer, sizeof(buffer), "Corrupt write counter = %d, Write loop counter is %d", sd.write_corrupt_count, sd.concurrent_write_access);
CU_ASSERT (sd.write_corrupt_count == 0 && sd.concurrent_write_access > 0);
/* Test write on rwlock with PRIVATE scope and Success result */
@ -464,7 +464,7 @@ CU_Test(rwlock, tryread, false)
/* Test critical section READ access with trylocking and PRIVATE scope */
printf ("Starting os_rwlock_tryread_001\n");
sprintf (buffer, "Corrupt tryread counter = %d, Tryread loop counter is %d, Busy counter = %d", sd.tryread_corrupt_count, sd.concurrent_tryread_access, sd.tryread_busy_count);
snprintf (buffer, sizeof(buffer), "Corrupt tryread counter = %d, Tryread loop counter is %d, Busy counter = %d", sd.tryread_corrupt_count, sd.concurrent_tryread_access, sd.tryread_busy_count);
CU_ASSERT (sd.tryread_corrupt_count == 0 && sd.concurrent_tryread_access > 0);
/* Test try read on rwlock with PRIVATE scope and Success result & not locked */
@ -494,7 +494,7 @@ CU_Test(os_rwlock, trywrite, false)
/* Test critical section WRITE access with trylocking and PRIVATE scope */
printf ("Starting os_rwlock_trywrite_001\n");
sprintf (buffer, "Corrupt trywrite counter = %d, Trywrite loop counter is %d, Busy counter = %d", sd.trywrite_corrupt_count, sd.concurrent_trywrite_access, sd.trywrite_busy_count);
snprintf (buffer, sizeof(buffer), "Corrupt trywrite counter = %d, Trywrite loop counter is %d, Busy counter = %d", sd.trywrite_corrupt_count, sd.concurrent_trywrite_access, sd.trywrite_busy_count);
CU_ASSERT (sd.trywrite_corrupt_count == 0 && sd.concurrent_trywrite_access > 0);
/* Test try write on rwlock with PRIVATE scope and Success result */

80
src/os/tests/strlcpy.c Normal file
View file

@ -0,0 +1,80 @@
/*
* Copyright(c) 2006 to 2018 ADLINK Technology Limited and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
* v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include "os/os.h"
#include "CUnit/Theory.h"
CU_TheoryDataPoints(os_strlcpy, dest_size) = {
CU_DataPoints(char *, "foo", "foo", "foo", "foo", "foo", "", "", ""),
CU_DataPoints(size_t, 0, 1, 3, 4, 5, 0, 1, 2)
};
CU_Theory((char *src, size_t size), os_strlcpy, dest_size)
{
char dest[] = "................";
size_t len, srclen;
srclen = strlen(src);
len = os_strlcpy(dest, src, size);
CU_ASSERT_EQUAL(len, srclen);
if (size > 0) {
if ((size - 1) < len) {
len = size - 1;
}
CU_ASSERT_EQUAL(dest[len], '\0');
CU_ASSERT_EQUAL(dest[len+1], '.');
CU_ASSERT((strncmp(dest, src, len) == 0));
} else {
CU_ASSERT_EQUAL(dest[0], '.');
}
}
CU_TheoryDataPoints(os_strlcat, dest_size) = {
CU_DataPoints(char *, "", "", "", "", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "", "", "foo", "foo", "foo"),
CU_DataPoints(char *, "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "", "", "", "", ""),
CU_DataPoints(size_t, 0, 1, 3, 4, 0, 1, 3, 4, 5, 6, 7, 0, 1, 3, 4, 5)
};
CU_Theory((char *seed, char *src, size_t size), os_strlcat, dest_size)
{
char dest[] = "................";
size_t len, seedlen, srclen;
seedlen = strlen(seed);
srclen = strlen(src);
memcpy(dest, seed, seedlen);
dest[seedlen] = '\0';
len = os_strlcat(dest, src, size);
CU_ASSERT_EQUAL(len, (seedlen + srclen));
if (size > 0) {
char foobar[sizeof(dest)];
if ((size - 1) <= seedlen) {
len = seedlen;
} else if ((size - 1) <= len) {
len = size - 1;
}
CU_ASSERT_EQUAL(dest[len], '\0');
if (seedlen < (size - 1)) {
CU_ASSERT_EQUAL(dest[len+1], '.');
}
(void)snprintf(foobar, len+1, "%s%s", seed, src);
CU_ASSERT((strncmp(dest, foobar, len) == 0));
} else {
CU_ASSERT((strcmp(dest, seed) == 0));
}
}