Skip to content
Merged
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
3 changes: 1 addition & 2 deletions .github/workflows/cpp-build-test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ jobs:
- name: Run tests
run: |
cd build
ls -l
./cpp_lab_project_unit_test
ctest --output-on-failure

# -------------------------------------------------------
# Step 5: Generate code coverage report
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*build
*private*
*.vscode
*Identifier
*Identifier
*Testing*
261 changes: 60 additions & 201 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# ROOT CMAKE
# │
# ┌─────────────────┴─────────────────┐
# │ │
# src module tests module
# │ │
# ┌────┴────┐ │
# │ │ │
# core socket │
# │ │ │
# └──────┬──┘ │
# │ │
# ▼ ▼
# cpp_lab_project cpp_lab_project_unit_test
# (main executable) (GoogleTest executable)

cmake_minimum_required(VERSION 3.14)

# ----------------------------------------------------------------------------------------
Expand All @@ -9,34 +25,59 @@ project(cpp_lab_project # ${PROJECT_NAME}
LANGUAGES CXX
)

# Build timestamp
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/include)
# ----------------------------------------------------------------------------------------
# Output directories to build/bin
# ----------------------------------------------------------------------------------------
# Executables
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Shared libraries
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# Static libraries
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# ----------------------------------------------------------------------------------------
# Compiler and language configuration
# ----------------------------------------------------------------------------------------
# Require at least C++17 for GoogleTest and modern C++ features
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Export compile_commands.json (useful for clang-tidy, clangd, IDEs)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ----------------------------------------------------------------------------------------
# Build metadata
# ----------------------------------------------------------------------------------------
# Ensure directory exists for generated headers
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/generated)

# Generate build timestamp
string(TIMESTAMP BUILD_TIME "%Y-%m-%d %H:%M:%S")

# Generate version header from template
configure_file(
${CMAKE_SOURCE_DIR}/include/version.h.in
${CMAKE_BINARY_DIR}/generated/version.h
)

set(PROJECT_NAME_TEST ${PROJECT_NAME}_unit_test) # name for the unit-test executable

# ----------------------------------------------------------------------------------------
# Compiler and language configuration
# External dependencies (GoogleTest,...)
# ----------------------------------------------------------------------------------------
# Require at least C++17 for GoogleTest and modern C++ features
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Enable output of compile commands for clang-tidy
include(cmake/Dependencies.cmake)

# Optionally enforce warnings (good for learning/debugging)
# ----------------------------------------------------------------------------------------
# Compiler warnings (useful for learning/debugging)
# ----------------------------------------------------------------------------------------
add_compile_options(-Wall -Wextra -Wpedantic)

message("C Compiler: ${CMAKE_C_COMPILER}")
message("C++ Compiler: ${CMAKE_CXX_COMPILER}")
message("C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
message("C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "C Compiler: ${CMAKE_C_COMPILER}")
message(STATUS "C++ Compiler: ${CMAKE_CXX_COMPILER}")
message(STATUS "C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")

# Option to enable coverage
# ----------------------------------------------------------------------------------------
# Code coverage configuration
# ----------------------------------------------------------------------------------------
option(ENABLE_COVERAGE "Enable coverage reporting" OFF)

if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
Expand All @@ -46,194 +87,12 @@ if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
endif()

# ----------------------------------------------------------------------------------------
# Dependencies - GoogleTest
# Enable CTest framework (used by GoogleTest)
# ----------------------------------------------------------------------------------------
# FetchContent allows downloading dependencies at configure time
include(FetchContent)

# Declare GoogleTest dependency
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

# Make GoogleTest available
FetchContent_MakeAvailable(googletest)

# Enable CTest framework to run tests with `ctest`
enable_testing()

# ----------------------------------------------------------------------------------------
# Project directories and files
# ----------------------------------------------------------------------------------------
# Header files directory
set(APP_HEADERS
"include"
"${CMAKE_BINARY_DIR}/generated" # cmake generated headers
)

# Core source files
set(APP_SOURCES
"src/DeleteMe.cpp"
"src/core/basics/InitializeVariable.cpp"
"src/core/basics/Operations.cpp"
"src/core/basics/TypeQualifier.cpp"
"src/core/basics/ControlFlow.cpp"
"src/core/linkage/Internal.cpp"
"src/core/linkage/Linkage.cpp"
"src/core/linkage/External.cpp"
"src/core/linkage/sharing/Sharing.cpp"
"src/core/linkage/sharing/external/constants.cpp"
"src/core/datatypes/Fundamental.cpp"
"src/core/datatypes/CArray.cpp"
"src/core/datatypes/CReferences.cpp"
"src/core/datatypes/CPointers.cpp"
"src/core/datatypes/CEnum.cpp"
"src/core/datatypes/CStruct.cpp"
"src/core/datatypes/CUnion.cpp"
"src/core/datatypes/TypeConVersions.cpp"

# # Class
"src/core/datatypes/class/Friend.cpp"
"src/core/datatypes/class/CConstructors.cpp"
"src/core/datatypes/class/CDestructors.cpp"
"src/core/datatypes/class/SallowDeepCopying.cpp"
"src/core/datatypes/class/RoleOfThreeFiveZero.cpp"
"src/core/string/CString.cpp"
"src/patterns/structural/Adapter.cpp"
"src/patterns/structural/Bridge.cpp"
"src/patterns/structural/Proxy.cpp"
"src/patterns/structural/Composite.cpp"
"src/patterns/structural/Flyweight.cpp"
"src/patterns/structural/Facade.cpp"
"src/patterns/structural/Decorator.cpp"
"src/patterns/behavioral/ChainOfCommand.cpp"
"src/patterns/behavioral/Command.cpp"
"src/patterns/behavioral/Iterator.cpp"
"src/patterns/behavioral/Mediator.cpp"
"src/patterns/behavioral/Memento.cpp"
"src/patterns/behavioral/Visitor.cpp"
"src/patterns/behavioral/TemplateMethod.cpp"
"src/patterns/behavioral/Strategy.cpp"
"src/patterns/behavioral/State.cpp"
"src/patterns/behavioral/Observer.cpp"
"src/patterns/creational/Singleton.cpp"
"src/patterns/creational/FactoryMethod.cpp"
"src/patterns/creational/AbstractFactory.cpp"
"src/patterns/creational/Builder.cpp"
"src/patterns/creational/Prototype.cpp"
"src/core/datatypes/class/Relationship.cpp"
"src/core/datatypes/class/VirtualBase.cpp"
"src/core/datatypes/class/Binding.cpp"

# # Exceptions
"src/core/exception/BasicHandle.cpp"
"src/core/exception/ThrowNoexcept.cpp"

# # Streams
"src/core/filehandle/IOStream.cpp"
"src/core/filehandle/StringStream.cpp"
"src/core/filehandle/FileIO.cpp"
"src/core/filehandle/Directory.cpp"
"src/core/filehandle/OutputFormatting.cpp"
"src/core/filehandle/BinaryFileHandling.cpp"

# # Container
"src/core/datatypes/container/sequence/Array.cpp"
"src/core/datatypes/container/sequence/Vector.cpp"
"src/core/datatypes/container/unordered/UnorderedMap.cpp"
"src/core/expression/Lambda.cpp"
"src/core/expression/FunctionPointer.cpp"
"src/core/datatypes/container/adapter/Queue.cpp"
"src/core/datatypes/container/adapter/Stack.cpp"
"src/core/datatypes/container/sequence/Deque.cpp"
"src/core/datatypes/container/associative/Set.cpp"

# # LC
"src/leetcode/arrays/two_sum/TwoSum.cpp"
"src/leetcode/arrays/median_two_arrays/MedianTwoSortedArrays.cpp"
"src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp"
"src/leetcode/arrays/longest_common_prefix/Solution.cpp"
"src/leetcode/arrays/3sum/Solution.cpp"
"src/leetcode/arrays/4sum/Solution.cpp"

# # Controller
"src/controller/pid/pid.cpp"
"src/controller/pid/PIDSim.cpp"

# # Smart Pointers
"src/core/datatypes/smart_pointer/Unique.cpp"
"src/core/datatypes/smart_pointer/Shared.cpp"
"src/core/datatypes/smart_pointer/Weak.cpp"

# # Overloading
"src/core/overloading/ArithmeticOperator.cpp"
"src/core/overloading/IOOperator.cpp"
"src/core/overloading/UnaryOperator.cpp"
"src/core/overloading/ComparisonOperator.cpp"
"src/core/overloading/InDecOperator.cpp"
"src/core/overloading/SubscriptOperator.cpp"
"src/core/overloading/ParenthesisOperator.cpp"
"src/core/overloading/TypeCast.cpp"
"src/core/overloading/AssignmentOperator.cpp"
"src/core/overloading/ClassMemberAccessOperator.cpp"
"src/core/overloading/AllocationOperator.cpp"

# # Date and Time
"src/core/datetime/CTime.cpp"

# # Socket
"src/socket/simple_tcp/TCPServer.cpp"
"src/socket/simple_tcp/SimpleTCPServer.cpp"
)

# Test files
set(APP_TESTS
"tests/DeleteMeTest.cpp"
"tests/two_sum_ut.cpp"
"tests/median_two_sorted_arrays_ut.cpp"
"tests/container_with_most_water_ut.cpp"
"tests/longest_common_prefix_ut.cpp"
"tests/three_sum_ut.cpp"
"tests/four_sum_ut.cpp"
)

# ----------------------------------------------------------------------------------------
# Main application target
# ----------------------------------------------------------------------------------------
add_executable(${PROJECT_NAME}
${APP_SOURCES}
"src/main.cpp"
)

# Add header include path
target_include_directories(${PROJECT_NAME}
PRIVATE ${APP_HEADERS}
)

# Optional: add project-specific compiler options
target_compile_options(${PROJECT_NAME}
PRIVATE -Wall -Wextra -Wpedantic
)

# Add project modules
# ----------------------------------------------------------------------------------------
# Unit testing target
# ----------------------------------------------------------------------------------------
add_executable(${PROJECT_NAME_TEST}
${APP_SOURCES}
${APP_TESTS}
)

target_include_directories(${PROJECT_NAME_TEST}
PRIVATE ${APP_HEADERS} # PRIVATE for internal use within a target,
)

# Link GoogleTest, GoogleMock to the test executable
target_link_libraries(${PROJECT_NAME_TEST}
GTest::gtest_main
GTest::gmock_main
)

# Register tests with CTest
include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME_TEST})
add_subdirectory(src)
add_subdirectory(tests)
15 changes: 15 additions & 0 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

# ----------------------------------------------------------------------------------------
# Dependencies - GoogleTest
# ----------------------------------------------------------------------------------------
# FetchContent allows downloading dependencies at configure time
include(FetchContent)

# Declare GoogleTest dependency
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

# Make GoogleTest available
FetchContent_MakeAvailable(googletest)
16 changes: 15 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@

set -e # Exit immediately if a command fails

PROJECT_EXEC="./build/cpp_lab_project"
PROJECT_EXEC="./build/bin/cpp_lab_project"
BUILD_DIR="./build"

if [ ! -d "build" ]; then
echo "Build directory not found. Creating build directory..."

rm -rf build
mkdir build
cd build || exit

cmake ..
cd ..

else
echo "Build directory already exists."
fi

clear
echo "=============================="
echo " Starting build pipeline... "
Expand Down
39 changes: 39 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ----------------------------------------------------------------------------------------
# Main application target
# ----------------------------------------------------------------------------------------

# Add project submodules
add_subdirectory(core)
add_subdirectory(controller)
add_subdirectory(patterns)
add_subdirectory(socket)


# main application executable does NOT link to this library.
add_subdirectory(leetcode)

# Header files directory
set(APP_HEADERS
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/generated # cmake generated headers
)

# Define the main application executable
add_executable(${PROJECT_NAME}
main.cpp
${CORE_SOURCES}
${SOCKET_SOURCES}
)

# Add header include paths
# PRIVATE -> this target only
# PUBLIC -> this target + dependent targets
# INTERFACE -> dependent targets only
target_include_directories(${PROJECT_NAME}
PRIVATE ${APP_HEADERS}
)

# Optional: add project-specific compiler options
target_compile_options(${PROJECT_NAME}
PRIVATE -Wall -Wextra -Wpedantic
)
Loading