Skip to content

Commit e78a2cf

Browse files
committed
Tidying up for potential reuse.
1 parent 23a0eaa commit e78a2cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+8288
-603
lines changed

CMakeLists.txt

Lines changed: 160 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,198 +1,182 @@
1-
cmake_minimum_required(VERSION 3.18)
2-
project(protocol_toolkit)
3-
4-
# Determine if this is the main project or a subproject
5-
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
6-
set(PTK_IS_MAIN_PROJECT TRUE)
7-
else()
8-
set(PTK_IS_MAIN_PROJECT FALSE)
9-
endif()
10-
11-
# Options
12-
option(PTK_BUILD_EXAMPLES "Build Protocol Toolkit examples" ${PTK_IS_MAIN_PROJECT})
13-
option(PTK_BUILD_TESTS "Build Protocol Toolkit tests" ${PTK_IS_MAIN_PROJECT})
14-
option(PTK_INSTALL "Install Protocol Toolkit files" ${PTK_IS_MAIN_PROJECT})
1+
cmake_minimum_required(VERSION 3.16)
2+
project(protocol_toolkit VERSION 4.0.0 LANGUAGES C)
153

164
# Set C standard
17-
set(CMAKE_C_STANDARD 11)
5+
set(CMAKE_C_STANDARD 99)
186
set(CMAKE_C_STANDARD_REQUIRED ON)
197

20-
# Set build type if not specified
21-
if(NOT CMAKE_BUILD_TYPE)
22-
set(CMAKE_BUILD_TYPE Release)
23-
endif()
24-
25-
# Set output directories
26-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
27-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
28-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
29-
30-
# Compiler-specific options
31-
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
32-
# GCC and Clang options
33-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter")
34-
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -DDEBUG")
35-
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
36-
elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
37-
# MSVC options
38-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
39-
set(CMAKE_C_FLAGS_DEBUG "/Zi /Od /DDEBUG")
40-
set(CMAKE_C_FLAGS_RELEASE "/O2 /DNDEBUG")
41-
endif()
8+
# Enable testing
9+
enable_testing()
4210

43-
# Platform-specific settings
44-
if(WIN32)
45-
# Windows-specific settings
46-
add_definitions(-D_WIN32_WINNT=0x0601) # Windows 7 and later
47-
if(MSVC)
48-
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
49-
endif()
50-
elseif(APPLE)
51-
# macOS-specific settings
52-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
11+
# Platform detection
12+
if(APPLE)
13+
set(PTK_PLATFORM "macos")
14+
set(PTK_PLATFORM_DIR "include/macos")
15+
set(PTK_PLATFORM_SOURCES "")
5316
elseif(UNIX)
54-
# Linux/BSD-specific settings
55-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
56-
57-
# Find required libraries
58-
find_library(RT_LIBRARY rt)
59-
if(RT_LIBRARY)
60-
set(PLATFORM_LIBS ${PLATFORM_LIBS} ${RT_LIBRARY})
61-
endif()
17+
set(PTK_PLATFORM "linux")
18+
set(PTK_PLATFORM_DIR "lib/linux")
19+
set(PTK_PLATFORM_SOURCES "src/lib/linux/protocol_toolkit_linux.c")
20+
find_package(Threads REQUIRED)
21+
else()
22+
set(PTK_PLATFORM "generic")
23+
set(PTK_PLATFORM_DIR "include")
24+
set(PTK_PLATFORM_SOURCES "")
6225
endif()
6326

64-
#=============================================================================
27+
message(STATUS "Building for platform: ${PTK_PLATFORM}")
28+
29+
# ========================================================================
6530
# PROTOCOL TOOLKIT LIBRARY
66-
#=============================================================================
31+
# ========================================================================
32+
33+
# Create the main library target
34+
add_library(protocol_toolkit INTERFACE)
35+
36+
# Set include directories based on platform
37+
target_include_directories(protocol_toolkit INTERFACE
38+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/${PTK_PLATFORM_DIR}>
39+
$<INSTALL_INTERFACE:include>
40+
)
41+
42+
# Platform-specific configuration
43+
if(PTK_PLATFORM STREQUAL "linux")
44+
# Linux requires pthread
45+
target_link_libraries(protocol_toolkit INTERFACE Threads::Threads)
46+
47+
# Add the implementation source for Linux
48+
add_library(protocol_toolkit_impl STATIC ${PTK_PLATFORM_SOURCES})
49+
target_include_directories(protocol_toolkit_impl PUBLIC
50+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/${PTK_PLATFORM_DIR}>
51+
)
52+
target_link_libraries(protocol_toolkit_impl PUBLIC Threads::Threads)
53+
target_link_libraries(protocol_toolkit INTERFACE protocol_toolkit_impl)
6754

68-
# Add protocol toolkit library
69-
add_library(ptk STATIC ${SOURCES})
55+
elseif(PTK_PLATFORM STREQUAL "macos")
56+
# macOS requires system frameworks
57+
find_library(FOUNDATION_FRAMEWORK Foundation REQUIRED)
58+
target_link_libraries(protocol_toolkit INTERFACE ${FOUNDATION_FRAMEWORK})
59+
endif()
7060

71-
#=============================================================================
72-
# EXAMPLES
73-
#=============================================================================
74-
75-
# Add examples (only if requested)
76-
# if(PTK_BUILD_EXAMPLES)
77-
# add_subdirectory(src/examples)
78-
# endif()
79-
80-
# Add tests (only if requested)
81-
# if(PTK_BUILD_TESTS)
82-
# add_subdirectory(src/tests)
83-
# endif()
84-
85-
#=============================================================================
86-
# INSTALLATION
87-
#=============================================================================
88-
89-
if(PTK_INSTALL)
90-
include(CMakePackageConfigHelpers)
91-
92-
# Install the main library target
93-
install(TARGETS ptk
94-
EXPORT PTKTargets
95-
LIBRARY DESTINATION lib
96-
ARCHIVE DESTINATION lib
97-
RUNTIME DESTINATION bin
98-
INCLUDES DESTINATION include
99-
)
61+
# Compiler-specific flags
62+
target_compile_options(protocol_toolkit INTERFACE
63+
$<$<C_COMPILER_ID:GNU>:-Wall -Wextra -Wpedantic>
64+
$<$<C_COMPILER_ID:Clang>:-Wall -Wextra -Wpedantic>
65+
$<$<C_COMPILER_ID:MSVC>:/W4>
66+
)
10067

101-
# Install headers
102-
install(FILES
103-
# none
104-
DESTINATION include
105-
)
68+
# ========================================================================
69+
# EXAMPLES
70+
# ========================================================================
10671

107-
# Install example executables if built
108-
# if(TARGET arithmetic_server)
109-
# install(TARGETS arithmetic_server arithmetic_client
110-
# RUNTIME DESTINATION bin
111-
# )
112-
# endif()
113-
114-
# Generate and install CMake config files
115-
write_basic_package_version_file(
116-
PTKConfigVersion.cmake
117-
VERSION 1.0.0
118-
COMPATIBILITY AnyNewerVersion
119-
)
72+
# Function to add an example
73+
function(add_ptk_example name source_file)
74+
add_executable(${name} ${source_file})
75+
target_link_libraries(${name} PRIVATE protocol_toolkit)
12076

121-
install(EXPORT PTKTargets
122-
FILE PTKTargets.cmake
123-
NAMESPACE ptk::
124-
DESTINATION lib/cmake/PTK
125-
)
77+
# Platform-specific example configuration
78+
if(PTK_PLATFORM STREQUAL "linux")
79+
target_link_libraries(${name} PRIVATE protocol_toolkit_impl)
80+
endif()
12681

127-
configure_file(cmake/PTKConfig.cmake.in PTKConfig.cmake @ONLY)
128-
install(FILES
129-
${CMAKE_CURRENT_BINARY_DIR}/PTKConfig.cmake
130-
${CMAKE_CURRENT_BINARY_DIR}/PTKConfigVersion.cmake
131-
DESTINATION lib/cmake/PTK
82+
# Set output directory
83+
set_target_properties(${name} PROPERTIES
84+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/examples"
13285
)
86+
endfunction()
13387

134-
# Install documentation
135-
install(FILES
136-
how-to-use-protocol-toolkit.md
137-
DESTINATION share/doc/protocol_toolkit
138-
)
88+
# Add all examples
89+
add_ptk_example(simple_tcp_protothread "src/examples/simple_tcp_protothread.c")
90+
add_ptk_example(tcp_client_protothread_example "src/examples/tcp_client_protothread_example.c")
91+
add_ptk_example(embedded_pattern_example "src/examples/embedded_pattern_example.c")
13992

140-
# Install example documentation if it exists
141-
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/examples/howto/howto.md)
142-
install(FILES
143-
src/examples/howto/howto.md
144-
DESTINATION share/doc/protocol_toolkit/examples
145-
)
146-
endif()
93+
# Platform-specific examples
94+
if(PTK_PLATFORM STREQUAL "linux")
95+
add_ptk_example(linux_example "src/examples/linux_example.c")
14796
endif()
14897

149-
#=============================================================================
150-
# TESTING
151-
#=============================================================================
152-
153-
# Add test targets
154-
# add_custom_target(test_sockets
155-
# COMMENT "Testing socket implementation"
156-
# DEPENDS test_tcp_echo_abort test_udp_echo_abort
157-
# )
158-
159-
#=============================================================================
160-
# INFORMATION
161-
#=============================================================================
162-
163-
# Only print detailed information if this is the main project
164-
if(PTK_IS_MAIN_PROJECT)
165-
# Print build information
166-
message(STATUS "Building Protocol Toolkit")
167-
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
168-
message(STATUS " C Compiler: ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}")
169-
message(STATUS " Platform: ${CMAKE_SYSTEM_NAME}")
170-
171-
# Usage instructions
172-
message(STATUS "")
173-
message(STATUS "Build instructions:")
174-
message(STATUS " mkdir build && cd build")
175-
message(STATUS " cmake ..")
176-
message(STATUS " make")
177-
message(STATUS "")
178-
message(STATUS "Libraries built:")
179-
message(STATUS " - protocol_toolkit: Core utilities (buf, err, log, socket, thread, utils)")
180-
message(STATUS "")
181-
182-
if(PTK_BUILD_EXAMPLES)
183-
message(STATUS "Examples:")
184-
message(STATUS " - ethernetip: EtherNet/IP device discovery tool")
185-
message(STATUS " - modbus: Implementation guide and documentation")
186-
message(STATUS " - howto: Arithmetic protocol tutorial (client/server with CRC)")
187-
message(STATUS "")
188-
endif()
98+
# ========================================================================
99+
# TESTS
100+
# ========================================================================
189101

190-
if(PTK_BUILD_TESTS)
191-
message(STATUS "Tests available:")
192-
message(STATUS " - test_tcp_echo_abort: TCP socket abort functionality")
193-
message(STATUS " - test_udp_echo_abort: UDP socket abort functionality")
194-
message(STATUS "")
102+
# Function to add a test
103+
function(add_ptk_test name source_file)
104+
add_executable(${name} ${source_file})
105+
target_link_libraries(${name} PRIVATE protocol_toolkit)
106+
107+
# Platform-specific test configuration
108+
if(PTK_PLATFORM STREQUAL "linux")
109+
target_link_libraries(${name} PRIVATE protocol_toolkit_impl)
195110
endif()
196-
else()
197-
message(STATUS "Protocol Toolkit - included as subproject")
198-
endif()
111+
112+
# Set output directory
113+
set_target_properties(${name} PROPERTIES
114+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
115+
)
116+
117+
# Register with CTest
118+
add_test(NAME ${name} COMMAND ${name})
119+
endfunction()
120+
121+
# Add all tests
122+
add_ptk_test(test_basic_functionality "src/tests/test_basic_functionality.c")
123+
add_ptk_test(test_protothread_macros "src/tests/test_protothread_macros.c")
124+
125+
# ========================================================================
126+
# CUSTOM TARGETS
127+
# ========================================================================
128+
129+
# Run all tests
130+
add_custom_target(run_tests
131+
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
132+
DEPENDS test_basic_functionality test_protothread_macros
133+
COMMENT "Running all Protocol Toolkit tests"
134+
)
135+
136+
# Run all examples
137+
add_custom_target(run_examples
138+
COMMENT "Running all Protocol Toolkit examples"
139+
)
140+
141+
# Add example dependencies
142+
add_dependencies(run_examples
143+
simple_tcp_protothread
144+
tcp_client_protothread_example
145+
embedded_pattern_example
146+
)
147+
148+
if(PTK_PLATFORM STREQUAL "linux")
149+
add_dependencies(run_examples linux_example)
150+
endif()
151+
152+
# ========================================================================
153+
# BUILD SUMMARY
154+
# ========================================================================
155+
156+
message(STATUS "")
157+
message(STATUS "========================================================================")
158+
message(STATUS "Protocol Toolkit Build Configuration")
159+
message(STATUS "========================================================================")
160+
message(STATUS "Platform: ${PTK_PLATFORM}")
161+
message(STATUS "Include Directory: ${PTK_PLATFORM_DIR}")
162+
message(STATUS "")
163+
message(STATUS "Available targets:")
164+
message(STATUS " run_tests - Run all tests using CTest")
165+
message(STATUS " run_examples - Build all examples")
166+
message(STATUS " test_basic_functionality - Basic functionality tests")
167+
message(STATUS " test_protothread_macros - Protothread macro tests")
168+
message(STATUS " simple_tcp_protothread - Simple TCP example")
169+
message(STATUS " tcp_client_protothread_example - TCP client example")
170+
message(STATUS " embedded_pattern_example - Embedded pattern demo")
171+
if(PTK_PLATFORM STREQUAL "linux")
172+
message(STATUS " protocol_toolkit_impl - Linux implementation")
173+
message(STATUS " linux_example - Linux-specific example")
174+
endif()
175+
message(STATUS "")
176+
message(STATUS "Build commands:")
177+
message(STATUS " mkdir build && cd build")
178+
message(STATUS " cmake ..")
179+
message(STATUS " make run_tests")
180+
message(STATUS " make run_examples")
181+
message(STATUS "========================================================================")
182+
message(STATUS "")

0 commit comments

Comments
 (0)