Skip to content

An assortment of CMake fixes and cleanups #8

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
92 changes: 28 additions & 64 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,97 +7,61 @@ set(BUILD_MINOR "4")
set(BUILD_VERSION "3")
set(BUILD_VERSION ${BUILD_MAJOR}.${BUILD_MINOR}.${BUILD_VERSION})

include_directories(src)

SET(CLSOCKET_HEADERS
set(CLSOCKET_HEADERS
src/ActiveSocket.h
src/Host.h
src/PassiveSocket.h
src/SimpleSocket.h
src/StatTimer.h
)

SET(CLSOCKET_SOURCES
set(CLSOCKET_SOURCES
src/SimpleSocket.cpp
src/ActiveSocket.cpp
src/PassiveSocket.cpp
)

# mark headers as headers...
SET_SOURCE_FILES_PROPERTIES( ${CLSOCKET_HEADERS} PROPERTIES HEADER_FILE_ONLY TRUE )
# append to sources so that dependency checks work on headers
LIST(APPEND CLSOCKET_SOURCES ${CLSOCKET_HEADERS})

# OS and compiler checks.
if(UNIX)
# linux / normal unix
add_definitions(-D_LINUX)
if(CYGWIN)
# Special Cygwin stuff here
elseif(APPLE)
# Special Apple stuff here
remove_definitions(-D_LINUX)
add_definitions(-D_DARWIN)
endif()
elseif(WIN32)
add_definitions(-DWIN32)
SET(PROJECT_LIBS Ws2_32.lib)
if(MINGW)
# Special MINGW stuff here
elseif(MSVC)
# Special MSVC stuff here
add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS)
else()
# No idea what it is, but there's a good chance it's too weird.
MESSAGE( FATAL_ERROR "Using unknown WIN32 compiler... NOT. Please add to build system." )
endif()
option(BUILD_SHARED_LIBS "Build libs as shared" ON)
if(DEFINED CLSOCKET_SHARED)
message(FATAL_ERROR "The CLSOCKET_SHARED option is deprecated, instead use BUILD_SHARED_LIBS.")
endif()
if(DEFINED CLSOCKET_DEP_ONLY)
message(FATAL_ERROR "The CLSOCKET_DEP_ONLY option is deprecated, instead use add_subdirectory() with EXCLUDE_FROM_ALL.")
endif()

OPTION(CLSOCKET_SHARED "Build clsocket lib as shared." ON)
OPTION(CLSOCKET_DEP_ONLY "Build for use inside other CMake projects as dependency." OFF)

# make the lib
if(CLSOCKET_SHARED)
if(CLSOCKET_DEP_ONLY)
ADD_LIBRARY(clsocket SHARED EXCLUDE_FROM_ALL ${CLSOCKET_SOURCES})
else()
ADD_LIBRARY(clsocket SHARED ${CLSOCKET_SOURCES})
endif()
else()
if(CLSOCKET_DEP_ONLY)
ADD_LIBRARY(clsocket STATIC EXCLUDE_FROM_ALL ${CLSOCKET_SOURCES})
else()
ADD_LIBRARY(clsocket STATIC ${CLSOCKET_SOURCES})
endif()
# add headers so that they appear in IDEs
add_library(clsocket ${CLSOCKET_SOURCES} ${CLSOCKET_HEADERS})
if(WIN32)
add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS)
target_link_libraries(clsocket PRIVATE Ws2_32)
endif()
TARGET_LINK_LIBRARIES(clsocket ${PROJECT_LIBS})
# target_include_directories was added in 2.8.11
set_target_properties(clsocket PROPERTIES
INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/src
INTERFACE_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/src
)

# install into configured prefix
if(NOT CLSOCKET_DEP_ONLY)
install(TARGETS clsocket ARCHIVE DESTINATION lib LIBRARY DESTINATION lib)
install(FILES ${CLSOCKET_HEADERS} DESTINATION include)
else()

endif()
install(TARGETS clsocket DESTINATION lib)
install(FILES ${CLSOCKET_HEADERS} DESTINATION include)

set_target_properties(clsocket PROPERTIES VERSION ${BUILD_VERSION}
SOVERSION ${BUILD_MAJOR})

if(UNIX)
OPTION(CLSOCKET_EXAMPLES "Build the examples" OFF)
option(CLSOCKET_EXAMPLES "Build the examples" OFF)

if(CLSOCKET_EXAMPLES)
ADD_EXECUTABLE(clsocket-example examples/RecvAsync.cpp)
TARGET_LINK_LIBRARIES(clsocket-example clsocket pthread)
if(NOT CLSOCKET_DEP_ONLY)
install(TARGETS clsocket-example DESTINATION bin)
endif()
add_executable(clsocket-example examples/RecvAsync.cpp)
target_link_libraries(clsocket-example clsocket pthread)
install(TARGETS clsocket-example DESTINATION bin)

ADD_EXECUTABLE(querydaytime-example examples/QueryDayTime.cpp)
TARGET_LINK_LIBRARIES(querydaytime-example clsocket)
add_executable(querydaytime-example examples/QueryDayTime.cpp)
target_link_libraries(querydaytime-example clsocket)

ADD_EXECUTABLE(echoserver-example examples/EchoServer.cpp)
TARGET_LINK_LIBRARIES(echoserver-example clsocket)
add_executable(echoserver-example examples/EchoServer.cpp)
target_link_libraries(echoserver-example clsocket)
endif()
endif()

5 changes: 1 addition & 4 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ This is a very small library and is very easy to build and configure. To build
make sure you are logged in as a user who has access to the recommend GNU installation
directories. Then type

make -BUILD=Release && make install
cmake -DCMAKE_BUILD_TYPE=Release && make && make install

That is it now you are off and running.

NOTE: When using the library with WINDOWS you must define _WIN32 and when using with LINUX
you must define _LINUX.

------------------------------------------------------------------------------------------
* SimpleSocket Class Overview
------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion examples/RecvAsync.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <pthread.h>
#include "PassiveSocket.h"

#ifdef WIN32
#ifdef _WIN32
#include <windows.h>

// usually defined with #include <unistd.h>
Expand Down
6 changes: 3 additions & 3 deletions src/ActiveSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ bool CActiveSocket::ConnectTCP(const char *pAddr, uint16 nPort)

if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
{
#ifdef WIN32
#ifdef _WIN32
TranslateSocketError();
#else
if (h_errno == HOST_NOT_FOUND)
Expand Down Expand Up @@ -144,7 +144,7 @@ bool CActiveSocket::ConnectUDP(const char *pAddr, uint16 nPort)

if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
{
#ifdef WIN32
#ifdef _WIN32
TranslateSocketError();
#else
if (h_errno == HOST_NOT_FOUND)
Expand Down Expand Up @@ -202,7 +202,7 @@ bool CActiveSocket::ConnectRAW(const char *pAddr, uint16 nPort)

if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
{
#ifdef WIN32
#ifdef _WIN32
TranslateSocketError();
#else
if (h_errno == HOST_NOT_FOUND)
Expand Down
22 changes: 11 additions & 11 deletions src/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extern "C"
#define __WORDSIZE 32
#endif

#if defined(_LINUX) || defined(_DARWIN)
#if defined(__unix__) || defined(__APPLE__)
typedef unsigned char uint8;
typedef char int8;
typedef unsigned short uint16;
Expand All @@ -71,7 +71,7 @@ extern "C"
typedef int SOCKET;
#endif

#ifdef WIN32
#ifdef _WIN32
struct iovec {
void *iov_base;
size_t iov_len;
Expand All @@ -85,11 +85,11 @@ extern "C"
typedef int int32;
#endif

#ifdef WIN32
#ifdef _WIN32
typedef int socklen_t;
#endif

#if defined(WIN32)
#if defined(_WIN32)
typedef unsigned long long int uint64;
typedef long long int int64;
#elif (__WORDSIZE == 32)
Expand All @@ -102,7 +102,7 @@ extern "C"
typedef long int int64;
#endif

#ifdef WIN32
#ifdef _WIN32

#ifndef UINT8_MAX
#define UINT8_MAX (UCHAR_MAX)
Expand All @@ -123,7 +123,7 @@ extern "C"
#endif
#endif

#if defined(WIN32)
#if defined(_WIN32)
#define ssize_t size_t
#endif

Expand All @@ -150,7 +150,7 @@ extern "C"
/* Socket Macros */
/* */
/*---------------------------------------------------------------------------*/
#ifdef WIN32
#ifdef _WIN32
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
Expand All @@ -176,7 +176,7 @@ extern "C"
#define GETHOSTBYNAME(a) gethostbyname(a)
#endif

#if defined(_LINUX) || defined(_DARWIN)
#if defined(__unix__) || defined(__APPLE__)
#define ACCEPT(a,b,c) accept(a,b,c)
#define CONNECT(a,b,c) connect(a,b,c)
#define CLOSE(a) close(a)
Expand Down Expand Up @@ -226,19 +226,19 @@ extern "C"
/* Misc Macros */
/* */
/*---------------------------------------------------------------------------*/
#if defined(WIN32)
#if defined(_WIN32)
#define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x)
#else
#define GET_CLOCK_COUNT(x) gettimeofday(x, NULL)
#endif

#if defined(WIN32)
#if defined(_WIN32)
#define STRTOULL(x) _atoi64(x)
#else
#define STRTOULL(x) strtoull(x, NULL, 10)
#endif

#if defined(WIN32)
#if defined(_WIN32)
#define SNPRINTF _snprintf
#define PRINTF printf
#define VPRINTF vprintf
Expand Down
4 changes: 2 additions & 2 deletions src/PassiveSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CPassiveSocket::CPassiveSocket(CSocketType nType) : CSimpleSocket(nType)
bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, uint16 nPort)
{
bool bRetVal = false;
#ifdef WIN32
#ifdef _WIN32
ULONG inAddr;
#else
in_addr_t inAddr;
Expand Down Expand Up @@ -130,7 +130,7 @@ bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, u
bool CPassiveSocket::Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog)
{
bool bRetVal = false;
#ifdef WIN32
#ifdef _WIN32
ULONG inAddr;
#else
in_addr_t inAddr;
Expand Down
12 changes: 6 additions & 6 deletions src/SimpleSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ CSimpleSocket::CSimpleSocket(CSocketType nType) :
//----------------------------------------------------------------------
case CSimpleSocket::SocketTypeRaw:
{
#if defined(_LINUX) && !defined(_DARWIN)
#if defined(__unix__) && !defined(__APPLE__)
m_nSocketDomain = AF_PACKET;
m_nSocketType = CSimpleSocket::SocketTypeRaw;
#endif
Expand Down Expand Up @@ -137,7 +137,7 @@ bool CSimpleSocket::Initialize()
{
errno = CSimpleSocket::SocketSuccess;

#ifdef WIN32
#ifdef _WIN32
//-------------------------------------------------------------------------
// Data structure containing general Windows Sockets Info
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -831,7 +831,7 @@ bool CSimpleSocket::SetNonblocking(void)
{
int32 nCurFlags;

#if WIN32
#if _WIN32
nCurFlags = 1;

if (ioctlsocket(m_socket, FIONBIO, (ULONG *)&nCurFlags) != 0)
Expand Down Expand Up @@ -870,7 +870,7 @@ bool CSimpleSocket::SetBlocking(void)
{
int32 nCurFlags;

#if WIN32
#if _WIN32
nCurFlags = 0;

if (ioctlsocket(m_socket, FIONBIO, (ULONG *)&nCurFlags) != 0)
Expand Down Expand Up @@ -947,7 +947,7 @@ int32 CSimpleSocket::SendFile(int32 nOutFd, int32 nInFd, off_t *pOffset, int32 n
//------------------------------------------------------------------------------
void CSimpleSocket::TranslateSocketError(void)
{
#if defined(_LINUX) || defined(_DARWIN)
#if defined(__unix__) || defined(__APPLE__)
switch (errno)
{
case EXIT_SUCCESS:
Expand Down Expand Up @@ -1010,7 +1010,7 @@ void CSimpleSocket::TranslateSocketError(void)
break;
}
#endif
#ifdef WIN32
#ifdef _WIN32
int32 nError = WSAGetLastError();
switch (nError)
{
Expand Down
10 changes: 5 additions & 5 deletions src/SimpleSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@
#include <stdarg.h>
#include <errno.h>

#if defined(_LINUX) || defined (_DARWIN)
#if defined(__unix__) || defined (__APPLE__)
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netdb.h>
#endif
#ifdef _LINUX
#ifdef __unix__
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if.h>
#include <sys/sendfile.h>
#endif
#ifdef _DARWIN
#ifdef __APPLE__
#include <net/if.h>
#endif
#if defined(_LINUX) || defined (_DARWIN)
#if defined(__unix__) || defined (__APPLE__)
#include <sys/time.h>
#include <sys/uio.h>
#include <unistd.h>
Expand Down Expand Up @@ -570,7 +570,7 @@ class EXPORT CSimpleSocket {
struct sockaddr_in m_stMulticastGroup; /// multicast group to bind to
struct linger m_stLinger; /// linger flag
CStatTimer m_timer; /// internal statistics.
#ifdef WIN32
#ifdef _WIN32
WSADATA m_hWSAData; /// Windows
#endif
fd_set m_writeFds; /// write file descriptor set
Expand Down
Loading