diff --git a/.gitignore b/.gitignore index bcf89dc..da19f2d 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,15 @@ *.app build/ -.vscode/ \ No newline at end of file +.vscode/ + +# CMake generated files +CMakeFiles/ +CMakeCache.txt +cmake_install.cmake +Makefile +*.cmake + +# Build output directories +bin/ +lib/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index b499aca..85929bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,23 +1,107 @@ -cmake_minimum_required(VERSION 3.10) -project(CPP_CONSOLE VERSION 1.0.0) +cmake_minimum_required(VERSION 3.15) -set(CMAKE_CXX_STANDARD 17) -set(PROJECT_NAME "CPP_CONSOLE") -if (UNIX) - set(PROJECT_NAME "CPP_CONSOLE.exe") +# Modern project declaration with explicit language specification +project(CPPConsole + VERSION 1.0.0 + DESCRIPTION "A header-only C++ library for enhanced console output with colors, cursor control, and formatting" + HOMEPAGE_URL "https://github.com/Villy-P/CPPConsole" + LANGUAGES CXX +) + +# Set default build type to Release if not specified +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) endif() -include_directories(include/) +# Create header-only interface library +add_library(CPPConsole INTERFACE) +add_library(CPPConsole::CPPConsole ALIAS CPPConsole) + +# Set C++ standard and features using modern target-based approach +target_compile_features(CPPConsole INTERFACE cxx_std_17) -add_executable(${PROJECT_NAME} - "${CMAKE_CURRENT_SOURCE_DIR}/test/test.cpp" +# Add include directories using target-based approach +target_include_directories(CPPConsole + INTERFACE + $ + $ ) -set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +# Platform-specific compile definitions +if(WIN32) + target_compile_definitions(CPPConsole INTERFACE __WIN32) +endif() + +# Option to build tests (default: ON for development, can be disabled for packaging) +option(CPPCONSOLE_BUILD_TESTS "Build CPPConsole tests" ON) + +if(CPPCONSOLE_BUILD_TESTS) + # Test executable with proper naming convention + add_executable(CPPConsole_test + "${CMAKE_CURRENT_SOURCE_DIR}/test/test.cpp" + ) + + # Link the test executable to our library + target_link_libraries(CPPConsole_test PRIVATE CPPConsole::CPPConsole) + + # Modern compiler options using generator expressions + target_compile_options(CPPConsole_test PRIVATE + $<$:-Wall -Wextra -pedantic> + $<$:$<$:-g -O0>> + $<$:$<$:-O3 -DNDEBUG>> + $<$:/W4> + $<$:$<$:/Od /Zi>> + $<$:$<$:/O2 /DNDEBUG>> + ) + + # Set output directory for test executable + set_target_properties(CPPConsole_test PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin" + OUTPUT_NAME "cppconsole_test" + ) + + # Enable colored diagnostics if supported + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(CPPConsole_test PRIVATE -fdiagnostics-color=always) + endif() +endif() + +# Installation rules for header-only library +include(GNUInstallDirs) + +install(TARGETS CPPConsole + EXPORT CPPConsoleTargets + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) + +install(DIRECTORY include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + FILES_MATCHING PATTERN "*.hpp" +) + +install(EXPORT CPPConsoleTargets + FILE CPPConsoleTargets.cmake + NAMESPACE CPPConsole:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CPPConsole +) + +# Generate and install config files for find_package support +include(CMakePackageConfigHelpers) + +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPPConsoleConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/CPPConsoleConfig.cmake" + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CPPConsole +) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/CPPConsoleConfigVersion.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion +) -target_compile_options(${PROJECT_NAME} PRIVATE - "-Wall" - "-std=c++17" - "-fdiagnostics-color=always" - "-g" +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/CPPConsoleConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/CPPConsoleConfigVersion.cmake" + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CPPConsole ) \ No newline at end of file diff --git a/README.md b/README.md index 7b6ae3f..7039651 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,65 @@ CPPConsole is a header-only library for `C++` projects that adds features to the ## Using CPPConsole +### Header-Only Installation (Simple) + To use CPPConsole in your project, copy all the file located in the `include/` directory into your own personal `include/` folder. After that, you can add `#include "cppconsole.hpp"` to your file to include all CPPConsole functions, or include each file individually. +### CMake Installation (Recommended) + +CPPConsole now includes a modern CMake build system that allows easy integration into CMake-based projects. + +#### Installing CPPConsole System-Wide + +```bash +# Clone and build +git clone https://github.com/Villy-P/CPPConsole.git +cd CPPConsole +mkdir build && cd build + +# Configure (library-only, no tests) +cmake .. -DCPPCONSOLE_BUILD_TESTS=OFF + +# Install to system +sudo make install +``` + +#### Using CPPConsole in Your CMake Project + +Once installed, you can easily use CPPConsole in your CMake projects: + +```cmake +# Find the library +find_package(CPPConsole REQUIRED) + +# Create your executable +add_executable(your_app main.cpp) + +# Link to CPPConsole +target_link_libraries(your_app PRIVATE CPPConsole::CPPConsole) + +# Set C++ standard (required) +target_compile_features(your_app PRIVATE cxx_std_17) +``` + +#### Building and Testing CPPConsole + +```bash +# Build with tests (default) +make build + +# Run tests (once build issues are resolved) +make test + +# Build library only +make configure-lib-only && make build + +# Get help +make help +``` + ## Features * ANSI Escape Code support diff --git a/cmake/CPPConsoleConfig.cmake.in b/cmake/CPPConsoleConfig.cmake.in new file mode 100644 index 0000000..325d2b4 --- /dev/null +++ b/cmake/CPPConsoleConfig.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/CPPConsoleTargets.cmake") + +check_required_components(CPPConsole) \ No newline at end of file diff --git a/makefile b/makefile index aacce6d..2b42f8f 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,69 @@ .ONESHELL: +# Platform detection +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows + EXE_EXT := .exe + RM_CMD := rmdir /s /q + MKDIR_CMD := mkdir +else + DETECTED_OS := Unix + EXE_EXT := + RM_CMD := rm -rf + MKDIR_CMD := mkdir -p +endif + +# Default target +.PHONY: all build test clean reset run install + +all: build + +# Reset build directory reset: - rmdir build - mkdir build - cd build - cmake -GNinja .. + $(RM_CMD) build + $(MKDIR_CMD) build + cd build && cmake -GNinja .. -run: +# Build the project +build: + @if [ ! -d "build" ]; then $(MAKE) reset; fi ninja -C build - .\CPP_CONSOLE.exe \ No newline at end of file + +# Build and run tests +test: build + cd build && ctest --output-on-failure + +# Run the test executable +run: build +ifeq ($(DETECTED_OS),Windows) + .\build\bin\cppconsole_test$(EXE_EXT) +else + ./build/bin/cppconsole_test$(EXE_EXT) +endif + +# Install to system (requires privileges) +install: build + ninja -C build install + +# Clean build artifacts +clean: + $(RM_CMD) build + +# Configure without tests +configure-lib-only: + $(RM_CMD) build + $(MKDIR_CMD) build + cd build && cmake -GNinja -DCPPCONSOLE_BUILD_TESTS=OFF .. + +# Help target +help: + @echo "Available targets:" + @echo " all - Build the project (default)" + @echo " build - Build the project" + @echo " test - Build and run tests" + @echo " run - Build and run the test executable" + @echo " reset - Clean and reconfigure build directory" + @echo " install - Install library to system" + @echo " clean - Remove build directory" + @echo " configure-lib-only - Configure without building tests" + @echo " help - Show this help message" \ No newline at end of file