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

Build with CMake on Windows and Linux #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ mbed-os.lib
*.jlink
*.py
*.pyc
/build_x64_vs2019_release
82 changes: 82 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
cmake_minimum_required(VERSION 3.0)
project(wic_client VERSION 1.0.0.0 LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 99)

set(CMAKE_CXX_FLAGS "-D__STDC_CONSTANT_MACROS -fPIC")
set(CMAKE_C_FLAGS "-fPIC")

IF(MSVC)
SET(CMAKE_CXX_FLAGS "/EHsc")
set(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
ENDIF(MSVC)

set(SOURCE
examples/demo_client/demo_client.c
examples/demo_client/port.h
examples/demo_client/log.h
)

set(SOURCE_LIB
src/http_parser.c
src/wic.c
examples/transport/transport.h
examples/transport/transport.c
)

if(WIN32)
add_definitions(/DWIN32 /DWIN32_LEAN_AND_MEAN)
endif()

if(WIN32)
set(SYSTEM_LIB
WS2_32
)
else()
if(APPLE)
set(SYSTEM_LIB
"-framework Foundation"
"-framework CoreFoundation"
"-framework AppKit"
"-framework IOKit"
"-framework AVFoundation"
)
else()
set(SYSTEM_LIB
)
endif()
endif()


add_library(${CMAKE_PROJECT_NAME} STATIC ${SOURCE_PROTO} ${SOURCE_LIB})
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE ${SYSTEM_LIB})
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE include examples/transport examples/demo_client)

add_executable(${CMAKE_PROJECT_NAME}_bin ${SOURCE})
add_dependencies(${CMAKE_PROJECT_NAME}_bin ${CMAKE_PROJECT_NAME})
target_link_libraries(${CMAKE_PROJECT_NAME}_bin PRIVATE ${CMAKE_PROJECT_NAME})
target_include_directories(${CMAKE_PROJECT_NAME}_bin PRIVATE include examples/transport examples/demo_client)

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC" AND CMAKE_BUILD_TYPE MATCHES "Release")
target_compile_options(${CMAKE_PROJECT_NAME}_bin PRIVATE /Zi)
set_target_properties(${CMAKE_PROJECT_NAME}_bin PROPERTIES
LINK_FLAGS "/INCREMENTAL:NO /DEBUG /OPT:REF /OPT:ICF"
)
set_target_properties(${CMAKE_PROJECT_NAME}_bin PROPERTIES
COMPILE_PDB_NAME ${CMAKE_PROJECT_NAME}
COMPILE_PDB_OUTPUT_DIR ${CMAKE_BINARY_DIR}
)
endif()

set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT "${CMAKE_PROJECT_NAME}_bin")
4 changes: 4 additions & 0 deletions build_linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mkdir build
cd build
cmake ..
cmake --build . --config Release
4 changes: 4 additions & 0 deletions build_macos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mkdir build
cd build
cmake -G "Xcode" ..
cmake --build . --config Release
5 changes: 5 additions & 0 deletions build_win64_vs2019_release.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mkdir build_x64_vs2019_release
cd build_x64_vs2019_release

cmake -G"Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release
45 changes: 40 additions & 5 deletions examples/transport/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,26 @@
*
* */

#include "transport.h"
#include "log.h"
#ifdef WIN32

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#define close closesocket
#undef ssize_t
#ifdef _WIN64
typedef __int64 ssize_t;
#else
typedef int ssize_t;
#endif
void transport_init() {
WORD wVersionRequested; WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
WSAStartup(wVersionRequested, &wsaData);
}

#else

#include <sys/socket.h>
#include <sys/types.h>
Expand All @@ -30,10 +48,19 @@
#include <netdb.h>
#include <poll.h>
#include <errno.h>

void transport_init() {}

#endif

#include <string.h>
#include "transport.h"
#include "log.h"

bool transport_open_client(enum wic_schema schema, const char *host, uint16_t port, int *s)
{
transport_init();

char pbuf[20];
struct addrinfo *res;
bool retval = false;
Expand Down Expand Up @@ -71,7 +98,11 @@ bool transport_open_client(enum wic_schema schema, const char *host, uint16_t po
}
else{

#ifdef __APPLE__
*s = socket(res->ai_family, /*res->ai_socktype*/SOCK_STREAM, /*res->ai_protocol*/IPPROTO_TCP); // was datagram/udp on macos
#else
*s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
#endif

if(*s < 0){

Expand Down Expand Up @@ -105,14 +136,18 @@ bool transport_open_client(enum wic_schema schema, const char *host, uint16_t po

void transport_write(int s, const void *data, size_t size)
{
const uint8_t *ptr = data;
const uint8_t *ptr = (const uint8_t *)data;
size_t pos;
int retval;

for(pos=0U; pos < size; pos += retval){

#ifdef WIN32
retval = send(s, &ptr[pos], size - pos, 0);
#else
retval = write(s, &ptr[pos], size - pos);

#endif

if(retval <= 0){

break;
Expand Down
6 changes: 6 additions & 0 deletions examples/transport/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@

#include "wic.h"

#ifdef __cplusplus
extern "C" {
#endif
bool transport_open_client(enum wic_schema schema, const char *host, uint16_t port, int *s);
bool transport_recv(int s, struct wic_inst *inst);
void transport_write(int s, const void *data, size_t size);
void transport_close(int *s);
#ifdef __cplusplus
}
#endif

#endif