diff --git a/.gitignore b/.gitignore index 99505f5..2501485 100644 --- a/.gitignore +++ b/.gitignore @@ -6,9 +6,11 @@ cmake_install.cmake compile_commands.json .dir-locals.el Makefile -urchin-* +urchin-server +urchin-client lib*.a lib*.so #Generated code src/protobuf_gen +urchin-util/src/protobuf_gen diff --git a/CMakeLists.txt b/CMakeLists.txt index f6306b1..42d2493 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,49 +9,8 @@ set(CMAKE_C_STANDARD 90) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_C_CLANG_TIDY) -# Get protobuf to serialize data between the server and client -# cmake config not packaged with the lib. resort to pkg config -include(FindPkgConfig) -pkg_check_modules(protobuf-c REQUIRED libprotobuf-c>=1.4.1) - -# Compile Shared Library -file(GLOB_RECURSE LIBRARY_SOURCE_FILES - ${CMAKE_SOURCE_DIR}/src/shared_lib/*.c) - -file(GLOB_RECURSE LIBRARY_HEADER_FILES - ${CMAKE_SOURCE_DIR}/src/shared_lib/*.h) - -# Generate Protobuf code -find_program(protoc-c_EXECUTABLE NAMES protoc-c REQUIRED) -find_package_handle_standard_args(protoc-c REQUIRED_VARS protoc-c_EXECUTABLE) -file(GLOB_RECURSE PROTOBUF_PROTO_FILES - ${CMAKE_SOURCE_DIR}/protobuf/*.proto) -add_custom_target(GenerateProtobuf) -add_custom_command( - OUTPUT GenerateProtobuf - DEPENDS ${protoc-c_EXECUTABLE} - COMMAND ${protoc-c_EXECUTABLE} --c_out=${CMAKE_SOURCE_DIR}/src/protobuf_gen/ --proto_path=${CMAKE_SOURCE_DIR}/protobuf/ ${PROTOBUF_PROTO_FILES} -) - -# Include protobuf generated message formats -file(GLOB_RECURSE PROTOBUF_SOURCE_FILES - ${CMAKE_SOURCE_DIR}/src/protobuf_gen/*.c) - -file(GLOB_RECURSE PROTOBUF_HEADER_FILES - ${CMAKE_SOURCE_DIR}/src/protobuf_gen/*.h) -set_source_files_properties(${PROTOBUF_HEADER_FILES} ${PROTOBUF_SOURCE_FILES} PROPERTIES GENERATED TRUE) - -add_library(${PROJECT_NAME} STATIC - GenerateProtobuf - ${LIBRARY_HEADER_FILES} - ${PROTOBUF_HEADER_FILES} - ${LIBRARY_SOURCE_FILES} - ${PROTOBUF_SOURCE_FILES} -) -add_dependencies(${PROJECT_NAME} GenerateProtobuf) - -target_link_libraries(${PROJECT_NAME} PUBLIC protobuf-c) - +include(CheckIncludeFile) +add_subdirectory(${CMAKE_SOURCE_DIR}/urchin-util) # Compile Server file(GLOB_RECURSE SERVER_SOURCE_FILES @@ -61,7 +20,7 @@ file(GLOB_RECURSE SERVER_HEADER_FILES ${CMAKE_SOURCE_DIR}/src/server/*.h) add_executable(${PROJECT_NAME}-server ${SERVER_HEADER_FILES} ${SERVER_SOURCE_FILES}) -target_link_libraries(${PROJECT_NAME}-server PUBLIC ${PROJECT_NAME}) +target_link_libraries(${PROJECT_NAME}-server PUBLIC urchin-util) # Compile Client file(GLOB_RECURSE CLIENT_SOURCE_FILES @@ -71,4 +30,4 @@ file(GLOB_RECURSE CLIENT_HEADER_FILES ${CMAKE_SOURCE_DIR}/src/client/*.h) add_executable(${PROJECT_NAME}-client ${CLIENT_HEADER_FILES} ${CLIENT_SOURCE_FILES}) -target_link_libraries(${PROJECT_NAME}-client PUBLIC ${PROJECT_NAME}) +target_link_libraries(${PROJECT_NAME}-client PUBLIC urchin-util) diff --git a/urchin-util/CMakeLists.txt b/urchin-util/CMakeLists.txt new file mode 100644 index 0000000..b323a3a --- /dev/null +++ b/urchin-util/CMakeLists.txt @@ -0,0 +1,59 @@ +cmake_minimum_required(VERSION 3.16) +project(urchin-util + VERSION 0 + DESCRIPTION "Simple Mud Experiment in C" + HOMEPAGE_URL "https://git.warwick-new.co.uk/" + LANGUAGES C) + +set(CMAKE_C_STANDARD 90) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_C_CLANG_TIDY) + +# Get protobuf to serialize data between the server and client +# cmake config not packaged with the lib. resort to pkg config +include(FindPkgConfig) +pkg_check_modules(protobuf-c REQUIRED libprotobuf-c>=1.4.1) + +# Compile Shared Library +file(GLOB LIBRARY_SOURCE_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c) + +file(GLOB LIBRARY_HEADER_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h) + +# Generate Protobuf code +find_program(protoc-c_EXECUTABLE NAMES protoc-c REQUIRED) +find_package_handle_standard_args(protoc-c REQUIRED_VARS protoc-c_EXECUTABLE) +file(GLOB_RECURSE PROTOBUF_PROTO_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/protobuf/*.proto) +file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf_gen/) +add_custom_target(GenerateProtobuf) +add_custom_command( + OUTPUT GenerateProtobuf + DEPENDS ${protoc-c_EXECUTABLE} + COMMAND ${protoc-c_EXECUTABLE} --c_out=${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf_gen/ --proto_path=${CMAKE_CURRENT_SOURCE_DIR}/protobuf/ ${PROTOBUF_PROTO_FILES} +) + +# Include protobuf generated message formats +file(GLOB_RECURSE PROTOBUF_SOURCE_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf_gen/*.c) + +file(GLOB_RECURSE PROTOBUF_HEADER_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf_gen/*.h) +set_source_files_properties(${PROTOBUF_HEADER_FILES} ${PROTOBUF_SOURCE_FILES} PROPERTIES GENERATED TRUE) + +add_library(${PROJECT_NAME} SHARED + GenerateProtobuf + ${LIBRARY_HEADER_FILES} + ${PROTOBUF_HEADER_FILES} + ${LIBRARY_SOURCE_FILES} + ${PROTOBUF_SOURCE_FILES} +) +target_link_libraries(${PROJECT_NAME} PUBLIC protobuf-c) +target_include_directories(${PROJECT_NAME} PUBLIC include) + +install( + TARGETS ${PROJECT_NAME} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) diff --git a/src/shared_lib/error.h b/urchin-util/include/error.h similarity index 86% rename from src/shared_lib/error.h rename to urchin-util/include/error.h index 9f7b500..b40f069 100644 --- a/src/shared_lib/error.h +++ b/urchin-util/include/error.h @@ -9,5 +9,3 @@ void error_crash(const char *msg); void error_warn(const char *msg); void error_log(const char *msg); - -void msg_amsg_test(int a); diff --git a/urchin-util/include/message.h b/urchin-util/include/message.h new file mode 100644 index 0000000..789e222 --- /dev/null +++ b/urchin-util/include/message.h @@ -0,0 +1,8 @@ +#pragma once + +#include "error.h" +#include +#include +#include + +void msg_amsg_test(int a); diff --git a/urchin-util/protobuf/amessage.proto b/urchin-util/protobuf/amessage.proto new file mode 100644 index 0000000..5885125 --- /dev/null +++ b/urchin-util/protobuf/amessage.proto @@ -0,0 +1,4 @@ +message AMessage { + required int32 a=1; + optional int32 b=2; +} diff --git a/urchin-util/src/error.c b/urchin-util/src/error.c new file mode 100644 index 0000000..7b3dc6e --- /dev/null +++ b/urchin-util/src/error.c @@ -0,0 +1,13 @@ +#include "error.h" +#include "protobuf_gen/amessage.pb-c.h" +#define MAX_MSG_SIZE 1024 + +void error_crash(const char *msg) { + fprintf(stderr, "error crash: %s\n", msg); + exit(EXIT_FAILURE); +} + +void error_warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); } + +// TODO: Save the logs somewhere +void error_log(const char *msg) { fprintf(stderr, "log: %s\n", msg); } diff --git a/src/shared_lib/error.c b/urchin-util/src/message.c similarity index 75% rename from src/shared_lib/error.c rename to urchin-util/src/message.c index 02e1bc9..4e9c959 100644 --- a/src/shared_lib/error.c +++ b/urchin-util/src/message.c @@ -1,16 +1,6 @@ -#include "error.h" -#include "../protobuf_gen/amessage.pb-c.h" +#include "message.h" #define MAX_MSG_SIZE 1024 - -void error_crash(const char *msg) { - fprintf(stderr, "error crash: %s\n", msg); - exit(EXIT_FAILURE); -} - -void error_warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); } - -// TODO: Save the logs somewhere -void error_log(const char *msg) { fprintf(stderr, "log: %s\n", msg); } +#include "protobuf_gen/amessage.pb-c.h" void msg_amsg_test(int a) { AMessage msg = AMESSAGE__INIT; // The massage format