Files
Volodymyr FialkoandBIAGINI Nathan 15d823dab6 Enable build flags and fix compilation warnings (gcc 14.1.0) (#66)
* Fix rpc build with other examples

CPP_COMPILE is an option, a cache variable, thus regular unset() is not
working for it since it tries to unset the scope variable.

This causing cmake to skip compilation of rpc.
Unset CPP_COMPILE from CACHE to make it work as intended.

* Fix compiler flags with build targets

Currently tests, soak and examples are build without extra flags,
because add_compile_options is specified after defining build targets.

From add_compile_options() docs:
Adds options to the compiler command line for targets in the current
directory and below that are added *after* this command is invoked.

* Pass debug callbacks as struct

Fixes:
warning: ISO C forbids conversion of function pointer to object pointer
type

* Move global variables under NBNET_IMPL

Nb net could be included multiple times, to prevent creation of global
variables in each file move them under NBNET_IMPL.

Also use designated initializer to initiate a driver structure to
remove warnings about partial initialization. Designated initializer
would initialize other members to zero.

* Resolve unused variables warnings.

Cast unused function arguments to (void) to suspend warnings.

* Resolve signed vs unsigned warnings

Remove unnecessary lt 0 checks and sync types to remove signed vs
unsigned warnings.

* Resolve string related warnings

Fixes:
nbnet/tests/string_tests.c:18:9: error: ‘strncpy’
output truncated before terminating nul copying 5 bytes from a string of
the same length
[-Werror=stringop-truncation]
   18 |         strncpy(res + pos, b, len_b);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

nbnet/tests/string_tests.c:24:9: error: ‘strncpy’
specified bound depends on the length of the source argument
[-Werror=stringop-truncation]

* Resolve function defined but not used warnings

Mark GameServer_GetClientCount() as inline.

* Attempt to fix warning in ASSERT_VALUE_IN_RANGE

In case when value in unsigned type and min is 0, this commit just
obfuscates types, maybe there's better solution.

Fixes:
warning: comparison of unsigned expression in ‘>= 0’ is always true
[-Wtype-limits]

* Ensure that read len is less than word size

In release mode(-DCMAKE_BUILD_TYPE=Release) tests returning following
error:
nbnet/tests/../nbnet.h:2272:9: error: ‘memcpy’ forming offset [4, 7] is
out of the bound s [0, 4] of object ‘word’ with type ‘Word’ {aka
‘unsigned int’} [-Werror=array-bounds=]
 2272 |         memcpy(bytes, &word, length); |
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Add check to verify that length is less than Word size.

* add -Wno-unknown-pragmas flag

* fix examples compilation on windows

* fix examples compilation on windows

* fix examples compilation on windows

* fix unit tests compilation on windows

* fix soak test compilation on windows

* fix unit tests compilation on windows

* fix unit tests compilation on windows

---------

Co-authored-by: BIAGINI Nathan <nath.biagini@gmail.com>
2024-08-10 12:02:42 +02:00

62 lines
1.9 KiB
CMake

cmake_minimum_required(VERSION 3.1)
project(echo_bytes)
option(CPP_COMPILE OFF)
# allow to compile as cpp
if (CPP_COMPILE)
file(GLOB_RECURSE CFILES "${CMAKE_SOURCE_DIR}/*.c")
SET_SOURCE_FILES_PROPERTIES(${CFILES} PROPERTIES LANGUAGE CXX)
set (CMAKE_CXX_STANDARD 20)
endif (CPP_COMPILE)
unset(CPP_COMPILE CACHE)
add_compile_options(-Wall)
if(CMAKE_COMPILER_IS_GNUCXX)
add_compile_options(-Wextra -Wpedantic)
endif (CMAKE_COMPILER_IS_GNUCXX)
add_executable(echo_bytes_client client.c shared.c)
add_executable(echo_bytes_server server.c shared.c)
target_compile_definitions(echo_bytes_client PUBLIC NBN_DEBUG)
target_compile_definitions(echo_bytes_server PUBLIC NBN_DEBUG)
if(WIN32)
target_link_libraries(echo_bytes_client wsock32 ws2_32)
target_link_libraries(echo_bytes_server wsock32 ws2_32)
else()
# link with pthread when we are not on windows
target_link_libraries(echo_bytes_client pthread)
target_link_libraries(echo_bytes_server pthread)
endif(WIN32)
if (UNIX)
# link with libm on unix
target_link_libraries(echo_bytes_client m)
target_link_libraries(echo_bytes_server m)
endif (UNIX)
if (EMSCRIPTEN)
set(ASYNCIFY_IMPORTS "[\"__js_game_server_start\", \"__js_game_client_start\", \"__js_game_client_close\"]")
set_target_properties(echo_bytes_server PROPERTIES LINK_FLAGS "--js-library ${CMAKE_CURRENT_SOURCE_DIR}/../../net_drivers/webrtc/js/api.js \
-s ALLOW_MEMORY_GROWTH=1 \
-s TOTAL_MEMORY=30MB \
-s EXIT_RUNTIME=1 \
-s ASSERTIONS=1 \
-s ASYNCIFY \
-s ASYNCIFY_IMPORTS=\"${ASYNCIFY_IMPORTS}\"")
set_target_properties(echo_bytes_client PROPERTIES LINK_FLAGS "--js-library ${CMAKE_CURRENT_SOURCE_DIR}/../../net_drivers/webrtc/js/api.js \
-s ALLOW_MEMORY_GROWTH=1 \
-s TOTAL_MEMORY=30MB \
-s EXIT_RUNTIME=1 \
-s ASSERTIONS=1 \
-s ASYNCIFY \
-s ASYNCIFY_IMPORTS=\"${ASYNCIFY_IMPORTS}\"")
endif()