Skip to content

Commit

Permalink
Add CMake build configuration files
Browse files Browse the repository at this point in the history
Fix warnings surfaced due to warnings level bump.
  • Loading branch information
mikedld committed Aug 22, 2021
1 parent 6748011 commit aacb859
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 51 deletions.
157 changes: 157 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(libutp VERSION 3.4 LANGUAGES C CXX)

set(LIBUTP_STANDALONE_BUILD OFF)
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(LIBUTP_STANDALONE_BUILD ON)
endif()

option(LIBUTP_ENABLE_INSTALL "${PROJECT_NAME}: Enable install" ${LIBUTP_STANDALONE_BUILD})
option(LIBUTP_ENABLE_WERROR "${PROJECT_NAME}: Treat warnings as errors" ${LIBUTP_STANDALONE_BUILD})
option(LIBUTP_BUILD_PROGRAMS "${PROJECT_NAME}: Build programs" ${LIBUTP_STANDALONE_BUILD})

if(LIBUTP_ENABLE_INSTALL)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
endif()

if(LIBUTP_STANDALONE_BUILD)
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
endif()

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()

if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(
-Wall
-Wextra
-pedantic
$<$<BOOL:${LIBUTP_ENABLE_WERROR}>:-Werror>)
elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
add_compile_options(
/W4
/wd4244 # '...': conversion from '...' to '...', possible loss of data
/wd4267 # '...': conversion from '...' to '...', possible loss of data
$<$<BOOL:${LIBUTP_ENABLE_WERROR}>:/WX>)
endif()
endif()

set(LIBUTP_ADD_SOURCES)
if(WIN32)
list(APPEND LIBUTP_ADD_SOURCES
libutp_inet_ntop.cpp
libutp_inet_ntop.h)
endif()

add_library(libutp
include/libutp/utp.h
include/libutp/utp_types.h
utp_api.cpp
utp_callbacks.cpp
utp_callbacks.h
utp_hash.cpp
utp_hash.h
utp_internal.cpp
utp_internal.h
utp_packedsockaddr.cpp
utp_packedsockaddr.h
utp_templates.h
utp_utils.cpp
utp_utils.h
${LIBUTP_ADD_SOURCES})

if(NOT LIBUTP_STANDALONE_BUILD)
add_library(${PROJECT_NAME}::libutp ALIAS libutp)
endif()

set_target_properties(libutp
PROPERTIES
OUTPUT_NAME utp)

target_compile_definitions(libutp
PRIVATE
$<$<BOOL:${WIN32}>:_WINSOCK_DEPRECATED_NO_WARNINGS>
PUBLIC
$<$<BOOL:${WIN32}>:WIN32>
$<$<NOT:$<BOOL:${WIN32}>>:POSIX>)

target_include_directories(libutp
PRIVATE
include/libutp
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

if(LIBUTP_ENABLE_INSTALL)
install(
TARGETS libutp
EXPORT ${PROJECT_NAME}-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
DIRECTORY ${PROJECT_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()

if(LIBUTP_BUILD_PROGRAMS AND NOT MSVC)
add_executable(ucat
ucat.c)

if(NOT LIBB64_STANDALONE_BUILD)
add_executable(${PROJECT_NAME}::ucat ALIAS ucat)
endif()

target_compile_definitions(ucat
PRIVATE
$<$<NOT:$<BOOL:${WIN32}>>:_POSIX_C_SOURCE=200112L>
$<$<BOOL:${APPLE}>:_DARWIN_C_SOURCE>)

target_link_libraries(ucat
PRIVATE
libutp)

if(LIBUTP_ENABLE_INSTALL)
install(
TARGETS ucat
EXPORT ${PROJECT_NAME}-targets
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
endif()

if(LIBUTP_ENABLE_INSTALL)
write_basic_package_version_file(
${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)

configure_file(
config.cmake.in
${PROJECT_NAME}-config.cmake
@ONLY)

install(
EXPORT ${PROJECT_NAME}-targets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
NAMESPACE ${PROJECT_NAME}::)

install(
FILES
${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})

install(
FILES
LICENSE
README.md
DESTINATION ${CMAKE_INSTALL_DOCDIR})
endif()
1 change: 1 addition & 0 deletions config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake)
8 changes: 4 additions & 4 deletions utp.h → include/libutp/utp.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef struct UTPSocket utp_socket;
typedef struct struct_utp_context utp_context;

enum {
UTP_UDP_DONTFRAG = 2, // Used to be a #define as UDP_IP_DONTFRAG
UTP_UDP_DONTFRAG = 2 // Used to be a #define as UDP_IP_DONTFRAG
};

enum {
Expand All @@ -50,7 +50,7 @@ enum {

// socket is being destroyed, meaning all data has been sent if possible.
// it is not valid to refer to the socket after this state change occurs
UTP_STATE_DESTROYING = 4,
UTP_STATE_DESTROYING = 4
};

extern const char *utp_state_names[];
Expand All @@ -59,7 +59,7 @@ extern const char *utp_state_names[];
enum {
UTP_ECONNREFUSED = 0,
UTP_ECONNRESET,
UTP_ETIMEDOUT,
UTP_ETIMEDOUT
};

extern const char *utp_error_code_names[];
Expand Down Expand Up @@ -91,7 +91,7 @@ enum {
UTP_RCVBUF,
UTP_TARGET_DELAY,

UTP_ARRAY_SIZE, // must be last
UTP_ARRAY_SIZE // must be last
};

extern const char *utp_callback_names[];
Expand Down
15 changes: 4 additions & 11 deletions utp_types.h → include/libutp/utp_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifndef __UTP_TYPES_H__
#define __UTP_TYPES_H__

#include <inttypes.h>

// Allow libutp consumers or prerequisites to override PACKED_ATTRIBUTE
#ifndef PACKED_ATTRIBUTE
#if defined BROKEN_GCC_STRUCTURE_PACKING && defined __GNUC__
Expand Down Expand Up @@ -91,13 +93,8 @@ typedef unsigned int uint;
typedef unsigned int uint32;
typedef signed int int32;

#ifdef _MSC_VER
typedef unsigned __int64 uint64;
typedef signed __int64 int64;
#else
typedef unsigned long long uint64;
typedef long long int64;
#endif
typedef uint64_t uint64;
typedef int64_t int64;

/* compile-time assert */
#ifndef CASSERT
Expand All @@ -107,10 +104,6 @@ typedef long long int64;
CASSERT(8 == sizeof(uint64), sizeof_uint64_is_8)
CASSERT(8 == sizeof(int64), sizeof_int64_is_8)

#ifndef INT64_MAX
#define INT64_MAX 0x7fffffffffffffffLL
#endif

// always ANSI
typedef const char * cstr;
typedef char * str;
Expand Down
2 changes: 1 addition & 1 deletion libutp_inet_ntop.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ int inet_pton(int af, const char* src, void* dest);

} //namespace libutp

#endif // LIBUTP_INET_NTOP_H
#endif // LIBUTP_INET_NTOP_H
11 changes: 8 additions & 3 deletions ucat.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <netinet/ip_icmp.h>
#endif

#include "utp.h"
#include <libutp/utp.h>

// options
int o_debug;
Expand Down Expand Up @@ -94,13 +94,14 @@ void pdie(char *err)

void hexdump(const void *p, size_t len)
{
const unsigned char* pc = p;
int count = 1;

while (len--) {
if (count == 1)
fprintf(stderr, " %p: ", p);
fprintf(stderr, " %p: ", (void*)pc);

fprintf(stderr, " %02x", *(unsigned char*)p++ & 0xff);
fprintf(stderr, " %02x", *pc++ & 0xff);

if (count++ == 16) {
fprintf(stderr, "\n");
Expand All @@ -114,6 +115,8 @@ void hexdump(const void *p, size_t len)

void handler(int number)
{
(void)number;

debug("caught signal\n");
if (s)
utp_close(s);
Expand Down Expand Up @@ -178,6 +181,8 @@ uint64 callback_on_read(utp_callback_arguments *a)

uint64 callback_on_firewall(utp_callback_arguments *a)
{
(void)a;

if (! o_listen) {
debug("Firewalling unexpected inbound connection in non-listen mode\n");
return 1;
Expand Down
2 changes: 1 addition & 1 deletion utp_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ inline uint32 Read32(const void *p) { return *(uint32*)p; }
// Get the amount of memory required for the hash parameters and the bucket set
// Waste a space for an unused bucket in order to ensure the following managed memory have 32-bit aligned addresses
// TODO: make this 64-bit clean
#define BASE_SIZE(bc) (sizeof(utp_hash_t) + sizeof(utp_link_t) * ((bc) + 1))
#define BASE_SIZE(bc) (sizeof(utp_hash_t) + sizeof(utp_link_t) * ((bc) + 1 - 1))

// Get a pointer to the base of the structure array managed by the hash table
#define get_bep(h) ((byte*)(h)) + BASE_SIZE((h)->N)
Expand Down
6 changes: 3 additions & 3 deletions utp_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct utp_hash_t {
utp_link_t allocated;
utp_link_t used;
utp_link_t free;
utp_link_t inits[0];
utp_link_t inits[1]; // dynamic
};

#ifdef _MSC_VER
Expand Down Expand Up @@ -126,10 +126,10 @@ void utp_hash_free_mem(utp_hash_t *hash);
template<typename K, typename T> class utpHashTable {
utp_hash_t *hash;
public:
static uint compare(const void *k1, const void *k2, size_t ks) {
static uint compare(const void *k1, const void *k2, size_t /*ks*/) {
return *((K*)k1) == *((K*)k2);
}
static uint32 compute_hash(const void *k, size_t ks) {
static uint32 compute_hash(const void *k, size_t /*ks*/) {
return ((K*)k)->compute_hash();
}
void Init() { hash = NULL; }
Expand Down
Loading

0 comments on commit aacb859

Please sign in to comment.