Skip to content

deps: Strengthen optional dependency guards and fallback implementations #190

Description

@kcenon

Summary

Strengthen messaging_system's optional dependency guards to ensure clean builds in isolation and provide robust fallback implementations when optional dependencies are unavailable.

5W1H Specification

  • Who: messaging_system maintainers
  • What: Audit and strengthen conditional compilation guards for optional dependencies
  • Where: adapters/, collectors/, CMakeLists.txt
  • When: v0.4.0.0 release cycle
  • Why:
    • messaging_system has multiple optional dependencies (network, monitoring)
    • Guards must be consistent and complete
    • Fallback implementations must be functional, not just compile stubs
  • How:
    1. Audit all #if KCENON_WITH_* guards
    2. Ensure each guarded section has a fallback
    3. Add CMake preset for isolated build testing

Priority

MEDIUM - Risk level: LOW per CROSS_MODULE_INTEGRATION.md (required deps are lower tier)

Current Dependency Analysis

Dependency Type Guard Fallback
common_system REQUIRED None N/A
thread_system REQUIRED None N/A
container_system REQUIRED None N/A
network_system OPTIONAL KCENON_WITH_NETWORK_SYSTEM error::not_supported
monitoring_system OPTIONAL KCENON_WITH_MONITORING_SYSTEM stub class

Current Guard Files

File Guard Purpose
adapters/http_transport.h #if KCENON_WITH_NETWORK_SYSTEM HTTP message transport
adapters/websocket_transport.h #if KCENON_WITH_NETWORK_SYSTEM WebSocket transport
collectors/message_bus_collector.h #if KCENON_WITH_MONITORING_SYSTEM Bus metrics

Issues to Address

1. Incomplete Fallback for network_system

// Current: Returns error for all operations
#if !KCENON_WITH_NETWORK_SYSTEM
class http_transport {
    auto send(const message& msg) -> Result<void> {
        return error::not_supported("network_system not available");
    }
};
#endif

// Improved: Add compile-time detection + runtime check
#if !KCENON_WITH_NETWORK_SYSTEM
class http_transport {
public:
    static constexpr bool is_available = false;
    
    auto send(const message& msg) -> Result<void> {
        static_assert(!is_available, 
            "http_transport requires KCENON_WITH_NETWORK_SYSTEM");
        return error::not_supported("network_system not available");
    }
};
#else
class http_transport {
public:
    static constexpr bool is_available = true;
    // ... actual implementation
};
#endif

2. Missing Fallback for monitoring_system

// Current: May have incomplete stub
#if KCENON_WITH_MONITORING_SYSTEM
class message_bus_collector { /* real impl */ };
#else
class message_bus_collector { /* needs verification */ };
#endif

// Improved: Explicit null implementation
#if !KCENON_WITH_MONITORING_SYSTEM
class null_message_bus_collector {
public:
    void record_message_sent(const message&) { /* no-op */ }
    void record_message_received(const message&) { /* no-op */ }
    void record_error(std::string_view) { /* no-op */ }
    
    static auto instance() -> null_message_bus_collector& {
        static null_message_bus_collector instance;
        return instance;
    }
};
using message_bus_collector = null_message_bus_collector;
#endif

Tasks

  • Audit all files with KCENON_WITH_* guards
  • Ensure each guarded block has matching #else fallback
  • Add static constexpr bool is_available to each optional component
  • Implement proper null collectors for monitoring
  • Add CMake preset for minimal (isolated) build
  • Create integration test that builds without optional deps
  • Document optional dependency matrix in README

CMake Isolated Build Preset

# CMakePresets.json
{
  "configurePresets": [
    {
      "name": "isolated",
      "displayName": "Isolated Build (no optional deps)",
      "description": "Build with only required dependencies",
      "cacheVariables": {
        "KCENON_WITH_NETWORK_SYSTEM": "OFF",
        "KCENON_WITH_MONITORING_SYSTEM": "OFF"
      }
    }
  ]
}

Acceptance Criteria

  • cmake --preset=isolated && cmake --build --preset=isolated succeeds
  • All tests pass with isolated preset
  • Each optional component has is_available constexpr
  • No undefined behavior when optional deps unavailable
  • README documents which features require which deps

Verification Script

#!/bin/bash
# scripts/verify_isolated_build.sh

echo "Building messaging_system without optional dependencies..."
cmake -B build_isolated \
    -DKCENON_WITH_NETWORK_SYSTEM=OFF \
    -DKCENON_WITH_MONITORING_SYSTEM=OFF

cmake --build build_isolated

echo "Running tests in isolated mode..."
ctest --test-dir build_isolated --output-on-failure

echo "Isolated build verification complete."

Dependencies

  • None - This is a hardening task

Parent Epic

Related Issues

Metadata

Metadata

Assignees

Labels

architectureArchitectural changes and designbuildBuild system (CMake, etc)priority:highHigh priority issuerefactorCode refactoring without changing functionality

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions