Skip to content

Commit 59314c2

Browse files
committed
Reorganize repository files and optimize CMake configs
1 parent eabf4ca commit 59314c2

File tree

9 files changed

+119
-49
lines changed

9 files changed

+119
-49
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: CMake on multiple platforms
2+
3+
on:
4+
push:
5+
branches: [ "master", "main" ]
6+
pull_request:
7+
branches: [ "master", "main" ]
8+
9+
jobs:
10+
build:
11+
runs-on: ${{ matrix.os }}
12+
13+
strategy:
14+
fail-fast: false
15+
16+
matrix:
17+
os: [ubuntu-latest, windows-latest]
18+
build_type: [Release]
19+
c_compiler: [gcc, clang, cl]
20+
include:
21+
- os: windows-latest
22+
c_compiler: cl
23+
cpp_compiler: cl
24+
- os: ubuntu-latest
25+
c_compiler: gcc
26+
cpp_compiler: g++
27+
- os: ubuntu-latest
28+
c_compiler: clang
29+
cpp_compiler: clang++
30+
exclude:
31+
- os: windows-latest
32+
c_compiler: gcc
33+
- os: windows-latest
34+
c_compiler: clang
35+
- os: ubuntu-latest
36+
c_compiler: cl
37+
38+
steps:
39+
- uses: actions/checkout@v4
40+
41+
- name: Configure CMake
42+
run: >
43+
cmake -B ${{ github.workspace }}/build
44+
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
45+
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
46+
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
47+
-DIP_SOCKETS_CPP_LITE_BUILD_EXAMPLES=ON
48+
-S ${{ github.workspace }}
49+
50+
- name: Build
51+
run: cmake --build ${{ github.workspace }}/build --config ${{ matrix.build_type }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/out
22
/bin
3-
.vs
3+
.vs
4+
CMakeSettings.json

CMakeLists.txt

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,65 @@
22
cmake_minimum_required(VERSION 3.12)
33
project(ip-sockets-cpp-lite)
44

5-
option(IP_SOCKETS_CPP_LITE_BUILD_EXAMPLES "Build with examples" ON)
5+
# =============================================================================
6+
# Options
7+
# =============================================================================
68

7-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
8-
9-
if(WIN32)
10-
11-
# it prevent create Debug/ and Release folders in Visual Studio
12-
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
13-
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
14-
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_SOURCE_DIR}/bin )
15-
endforeach()
9+
option(IP_SOCKETS_CPP_LITE_BUILD_EXAMPLES "Build with examples" OFF)
1610

17-
set (INSTALL_PATH_BIN "${PROJECT_SOURCE_DIR}/installed/bin/")
11+
# =============================================================================
12+
# Output directories
13+
# =============================================================================
1814

19-
else() # not WIN32
15+
# for initialise variables: CMAKE_INSTALL_INCLUDEDIR, CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_BINDIR
16+
include(GNUInstallDirs)
2017

21-
set (INSTALL_PATH_BIN "bin/")
18+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
2219

20+
if(WIN32)
21+
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
22+
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG)
23+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_SOURCE_DIR}/bin)
24+
endforeach()
2325
endif()
2426

25-
# include all header files from current directory
27+
# =============================================================================
28+
# Collect header files
29+
# =============================================================================
2630

27-
file(GLOB_RECURSE IP_SOCKETS_CPP_LITE_HEADERS *.h)
31+
# include all header files from current directory
32+
#file(GLOB IP_SOCKETS_CPP_LITE_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
33+
# manual include header files
34+
file(GLOB IP_SOCKETS_CPP_LITE_HEADERS
35+
"${CMAKE_CURRENT_SOURCE_DIR}/include/ip_address.h"
36+
"${CMAKE_CURRENT_SOURCE_DIR}/include/udp_socket.h"
37+
"${CMAKE_CURRENT_SOURCE_DIR}/include/tcp_socket.h"
38+
)
39+
40+
# =============================================================================
41+
# Main target
42+
# =============================================================================
2843

2944
# it is header only target
30-
3145
add_library (${PROJECT_NAME} INTERFACE)
3246
target_sources (${PROJECT_NAME} INTERFACE ${IP_SOCKETS_CPP_LITE_HEADERS})
33-
target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
34-
add_custom_target (${PROJECT_NAME}-ide SOURCES ${IP_SOCKETS_CPP_LITE_HEADERS})
47+
target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE: ${CMAKE_CURRENT_SOURCE_DIR}/include>
48+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> )
49+
# it was need to show header files of INTERFACE target in IDEs like Visual Studio 2015 with CMake < 3.0
50+
#add_custom_target (${PROJECT_NAME}-ide SOURCES ${IP_SOCKETS_CPP_LITE_HEADERS})
3551

36-
## If this project is added as a subdirectory of another project, export the
37-
## include path to the parent scope. When this is the top-level project
38-
## (used standalone), don't use PARENT_SCOPE to avoid warnings.
52+
# =============================================================================
53+
# Install
54+
# =============================================================================
3955

40-
if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
41-
set(${PROJECT_NAME}_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE)
42-
else()
43-
set(${PROJECT_NAME}_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include)
44-
endif()
56+
install(TARGETS ${PROJECT_NAME})
57+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
4558

46-
# include examples if the option is enabled
59+
# =============================================================================
60+
# Examples
61+
# =============================================================================
4762

63+
# include examples if the option is enabled
4864
if(IP_SOCKETS_CPP_LITE_BUILD_EXAMPLES)
4965
add_subdirectory("./examples")
5066
endif()

examples/CMakeLists.txt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ if(NOT WIN32)
44
target_link_libraries(${PROJECT_NAME} INTERFACE ${CMAKE_THREAD_LIBS_INIT})
55
endif()
66

7-
add_executable (ip_address "ip_address.cpp")
8-
target_link_libraries(ip_address ip-sockets-cpp-lite)
7+
function(add_example NAME_EXAMPLE REQUIRED_TARGETS)
8+
string(CONCAT SUBPROJECT_NAME "ipsockets_" ${NAME_EXAMPLE})
9+
message(STATUS " Adding ${PROJECT_NAME}::${NAME_EXAMPLE} example..")
910

10-
add_executable (udp_socket "udp_socket.cpp")
11-
target_link_libraries(udp_socket ip-sockets-cpp-lite)
11+
add_executable (${SUBPROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${NAME_EXAMPLE}.cpp)
12+
target_link_libraries (${SUBPROJECT_NAME} ${REQUIRED_TARGETS})
13+
set_target_properties (${SUBPROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/examples)
14+
endfunction()
1215

13-
add_executable (tcp_socket "tcp_socket.cpp")
14-
target_link_libraries(tcp_socket ip-sockets-cpp-lite)
16+
add_example(ip_address ip-sockets-cpp-lite)
17+
add_example(udp_socket ip-sockets-cpp-lite)
18+
add_example(tcp_socket ip-sockets-cpp-lite)
19+
add_example(resolve_host ip-sockets-cpp-lite)
20+
add_example(http_server ip-sockets-cpp-lite)
1521

16-
add_executable (resolve_host "resolve_host.cpp")
17-
target_link_libraries(resolve_host ip-sockets-cpp-lite)
18-
19-
add_executable (http_server "http_server.cpp")
20-
target_link_libraries(http_server ip-sockets-cpp-lite)

examples/http_server.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,26 @@ struct mini_http_server_t {
168168
headers << "Content-Length: " << body.size() << "\r\n";
169169
headers << "Cache-Control: public, max-age=86400\r\n";
170170
headers << "Connection: close\r\n";
171+
headers << "Server: mini-http-server\r\n";
171172
// Date header
172-
std::time_t now = std::time(NULL);
173-
char buf[128];
174-
std::strftime(buf,sizeof(buf),"%a, %d %b %Y %H:%M:%S GMT",std::gmtime(&now));
175-
headers << "Date: " << buf << "\r\n";
176-
// Server header and empty line to separate headers from body
177-
headers << "Server: mini-http-server\r\n\r\n";
173+
std::time_t now = std::time (NULL);
174+
headers << "Date: " << std::put_time (gmtime (&now), "%a, %d %b %Y %H:%M:%S %Z") << "\r\n";
175+
// Empty line to separate headers from body
176+
headers << "\r\n";
178177

179178
// Send headers
180179
std::string h = headers.str();
181-
client.send (h.c_str(),(int)h.size());
182-
// Send body (if any)
180+
client.send (h.data(), (int)h.size());
181+
// Send body (if it not empty)
183182
if (!body.empty())
184-
client.send (body.c_str(),(int)body.size());
183+
client.send (body.data(), (int)body.size());
185184
std::cout << "Response: " << status << " " << status_text << " (" << body.size() << " bytes)" << std::endl;
186185
}
187186

188187
// ===== uptime =====
189188

190189
std::string uptime() const {
191-
std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start_time);
190+
std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds> (std::chrono::steady_clock::now() - start_time);
192191
uint64_t h = (uint64_t)s.count()/3600, m = ((uint64_t)s.count()%3600)/60, sec = (uint64_t)s.count()%60;
193192
std::ostringstream os;
194193
os << std::setw(2) << std::setfill('0') << h << ":" << std::setw(2) << m << ":" << std::setw(2) << sec;
File renamed without changes.
File renamed without changes.
File renamed without changes.

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ Keep it simple. Keep it portable.
105105
106106
**Option 1 — Copy headers**
107107
108+
from /include folder copy:
109+
108110
* `ip_address.h`
109111
* `udp_socket.h`
110112
* `tcp_socket.h`

0 commit comments

Comments
 (0)