Libspinscale: Add separate CMake project config

This commit is contained in:
2025-12-28 03:44:01 -04:00
parent 7acdfcc337
commit 5a4f498663
21 changed files with 328 additions and 100 deletions
+4
View File
@@ -0,0 +1,4 @@
build-test
b-*
build
b
+136 -9
View File
@@ -1,3 +1,75 @@
cmake_minimum_required(VERSION 3.16)
project(libspinscale VERSION 0.1.0 LANGUAGES CXX)
# Set C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug FORCE)
endif()
# Compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
# Debug options - allow parent to override when used as subdirectory
# option() will respect existing cache values, so parent can set them before add_subdirectory()
option(ENABLE_DEBUG_LOCKS "Enable debug features for locking system" OFF)
option(ENABLE_DEBUG_TRACE_CALLABLES
"Enable callable tracing for debugging boost::asio post operations" OFF)
# Qutex deadlock detection configuration
if(NOT DEFINED DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS)
set(DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS 500 CACHE STRING
"Timeout in milliseconds for deadlock detection in qutex system")
endif()
if(ENABLE_DEBUG_LOCKS)
# Validate the timeout value
if(NOT DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS OR DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS STREQUAL "")
message(FATAL_ERROR "DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS must be a positive integer > 0")
endif()
# Convert to integer and validate
math(EXPR timeout_int "${DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS}")
if(timeout_int LESS_EQUAL 0)
message(FATAL_ERROR "DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS must be a positive integer > 0")
endif()
endif()
# Set config variables for config.h
if(ENABLE_DEBUG_LOCKS)
set(CONFIG_ENABLE_DEBUG_LOCKS TRUE)
endif()
if(ENABLE_DEBUG_TRACE_CALLABLES)
set(CONFIG_DEBUG_TRACE_CALLABLES TRUE)
# Suppress frame-address warnings when using __builtin_return_address()
# with values above 0 (See callableTracer.h).
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-frame-address")
endif()
set(CONFIG_DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS ${DEBUG_QUTEX_DEADLOCK_TIMEOUT_MS})
# Configure config.h
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/include/config.h.in
${CMAKE_CURRENT_BINARY_DIR}/include/config.h
@ONLY
)
# Find dependencies
# Tell CMake we're linking against the shared library (not header-only)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_HEADER_ONLY OFF)
find_package(Boost REQUIRED COMPONENTS system log)
# Define BOOST_ALL_DYN_LINK project-wide to ensure all Boost libraries use dynamic linking
add_compile_definitions(BOOST_ALL_DYN_LINK)
find_package(Threads REQUIRED)
# Create the library
add_library(spinscale SHARED
src/qutex.cpp
src/lockerAndInvokerBase.cpp
@@ -12,33 +84,88 @@ if(ENABLE_DEBUG_LOCKS)
target_sources(spinscale PRIVATE src/qutexAcquisitionHistoryTracker.cpp)
endif()
# Ensure Boost uses dynamic linking (project-wide setting should handle this,
# but being explicit here for clarity)
target_compile_definitions(spinscale PRIVATE BOOST_ALL_DYN_LINK)
# Set compile features
target_compile_features(spinscale PUBLIC cxx_std_20)
# Include directories
target_include_directories(spinscale PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}/include
${Boost_INCLUDE_DIRS}
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# Link against required dependencies for shared library
# Boost::system is PUBLIC because componentThread.h exposes Boost.Asio types
find_package(Threads REQUIRED)
target_link_libraries(spinscale PUBLIC
Threads::Threads
Boost::system
Boost::log
)
# Verify Boost dynamic dependencies after build (only if script exists)
# Verify Boost dynamic dependencies after build
# Prefer parent project's script when used as subdirectory, fall back to our own for standalone builds
set(VERIFY_SCRIPT "")
if(EXISTS ${CMAKE_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake)
set(VERIFY_SCRIPT ${CMAKE_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake)
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake)
set(VERIFY_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake)
endif()
if(VERIFY_SCRIPT)
add_custom_command(TARGET spinscale POST_BUILD
COMMAND ${CMAKE_COMMAND} -DVERIFY_FILE="$<TARGET_FILE:spinscale>"
-P ${CMAKE_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake
-P ${VERIFY_SCRIPT}
COMMENT "Verifying Boost dynamic dependencies for spinscale"
)
else()
message(WARNING "VerifyBoostDynamic.cmake not found - cannot verify Boost dependencies for spinscale")
endif()
# Install rules
install(TARGETS spinscale
EXPORT spinscaleTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
)
install(DIRECTORY include/spinscale
DESTINATION include
FILES_MATCHING PATTERN "*.h"
)
install(FILES include/boostAsioLinkageFix.h
DESTINATION include
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/config.h
DESTINATION include
)
# Install CMake config files for find_package() support
install(EXPORT spinscaleTargets
FILE spinscaleTargets.cmake
NAMESPACE spinscale::
DESTINATION lib/cmake/spinscale
)
# Create config file for find_package()
include(CMakePackageConfigHelpers)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/spinscaleConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/spinscaleConfig.cmake
INSTALL_DESTINATION lib/cmake/spinscale
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/spinscaleConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/spinscaleConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/spinscaleConfigVersion.cmake
DESTINATION lib/cmake/spinscale
)
@@ -0,0 +1,63 @@
# SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY
# Verifies that a target file (executable or shared library) has Boost libraries
# in its dynamic dependency list via ldd.
#
# Usage as function:
# SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY(<target_file>)
#
# Usage as script (with -P):
# cmake -DVERIFY_FILE=<target_file> -P VerifyBoostDynamic.cmake
#
# This function/script:
# 1. Runs ldd on the target file
# 2. Checks for boost libraries in the dependency list
# 3. Reports success or failure with appropriate messages
#
function(SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY target_file)
_verify_boost_dynamic_dependency("${target_file}")
endfunction()
# Internal implementation that can be called from script mode or function mode
function(_verify_boost_dynamic_dependency target_file)
if(NOT EXISTS "${target_file}")
message(WARNING "SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY: Target file '${target_file}' does not exist")
return()
endif()
# Run ldd on the target file
execute_process(
COMMAND ldd "${target_file}"
OUTPUT_VARIABLE ldd_output
ERROR_VARIABLE ldd_error
RESULT_VARIABLE ldd_result
)
if(ldd_result)
message(WARNING "SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY: Failed to run ldd on '${target_file}': ${ldd_error}")
return()
endif()
# Check if output contains boost libraries
string(TOLOWER "${ldd_output}" ldd_output_lower)
string(FIND "${ldd_output_lower}" "libboost" boost_found)
if(boost_found EQUAL -1)
message(STATUS "SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY: WARNING - No Boost libraries found in dependencies of '${target_file}'")
message(STATUS "ldd output:")
message(STATUS "${ldd_output}")
else()
# Extract boost library lines
string(REGEX MATCHALL "libboost[^\n]*" boost_libs "${ldd_output}")
message(STATUS "SMO_VERIFY_BOOST_DYNAMIC_DEPENDENCY: SUCCESS - Boost libraries found in '${target_file}':")
foreach(boost_lib ${boost_libs})
string(STRIP "${boost_lib}" boost_lib_stripped)
message(STATUS " ${boost_lib_stripped}")
endforeach()
endif()
endfunction()
# Script mode: if VERIFY_FILE is defined, run the verification
if(VERIFY_FILE)
_verify_boost_dynamic_dependency("${VERIFY_FILE}")
endif()
@@ -0,0 +1,5 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/spinscaleTargets.cmake")
check_required_components(spinscale)
@@ -0,0 +1,25 @@
#ifndef BOOST_ASIO_LINKAGE_FIX_H
#define BOOST_ASIO_LINKAGE_FIX_H
#include <boost/asio/detail/call_stack.hpp>
#include <boost/asio/detail/thread_context.hpp>
#include <boost/asio/detail/tss_ptr.hpp>
namespace boost {
namespace asio {
namespace detail {
/** EXPLANATION:
* Extern declaration of the template instantiation
* This ensures that the .o translation units don't have their
* own copies of `call_stack<>::top_` defined in them.
*/
extern template
tss_ptr<call_stack<thread_context, thread_info_base>::context>
call_stack<thread_context, thread_info_base>::top_;
} // namespace detail
} // namespace asio
} // namespace boost
#endif // BOOST_ASIO_LINKAGE_FIX_H
@@ -4,7 +4,7 @@
#include <functional>
#include <memory>
#include <exception>
#include <componentThread.h>
#include <spinscale/componentThread.h>
#include <spinscale/callback.h>
#include <spinscale/callableTracer.h>
#include <spinscale/asynchronousContinuationChainLink.h>
@@ -154,19 +154,14 @@ public:
class ThreadLifetimeMgmtOp;
};
} // namespace sscl
namespace smo {
namespace mrntt {
extern std::shared_ptr<sscl::MarionetteThread> thread;
} // namespace mrntt
} // namespace smo
extern std::shared_ptr<MarionetteThread> thread;
// Forward declaration for sscl namespace functions and variables
// Forward declaration for marionette thread ID management
// Must be after sscl namespace so ThreadId is defined
namespace sscl {
extern sscl::ThreadId marionetteThreadId;
void setMarionetteThreadId(sscl::ThreadId id);
extern ThreadId marionetteThreadId;
void setMarionetteThreadId(ThreadId id);
} // namespace mrntt
}
#endif // COMPONENT_THREAD_H
@@ -0,0 +1,58 @@
#ifndef _MARIONETTE_H
#define _MARIONETTE_H
#include <cstdint>
#include <atomic>
#include <memory>
#include <spinscale/component.h>
namespace sscl {
class MarionetteThread;
namespace mrntt {
class MarionetteComponent
: public sscl::Component
{
public:
MarionetteComponent(const std::shared_ptr<sscl::ComponentThread> &thread);
~MarionetteComponent() = default;
public:
typedef std::function<void(bool)> mrnttLifetimeMgmtOpCbFn;
void initializeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback);
void finalizeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback);
// Intentionally doesn't take a callback.
void exceptionInd();
private:
class MrnttLifetimeMgmtOp;
class TerminationEvent;
};
extern std::shared_ptr<sscl::MarionetteThread> thread;
extern std::atomic<int> exitCode;
void exitMarionetteLoop();
void marionetteFinalizeReqCb(bool success);
extern MarionetteComponent mrntt;
} // namespace mrntt
struct CrtCommandLineArgs
{
CrtCommandLineArgs(int argc, char *argv[], char *envp[])
: argc(argc), argv(argv), envp(envp)
{}
int argc;
char **argv;
char **envp;
static void set(int argc, char *argv[], char *envp[]);
};
} // namespace sscl
#endif // _MARIONETTE_H
@@ -7,7 +7,7 @@
#include <chrono>
#include <iostream>
#include <optional>
#include <componentThread.h>
#include <spinscale/componentThread.h>
#include <spinscale/lockSet.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/lockerAndInvokerBase.h>
+2 -5
View File
@@ -1,6 +1,6 @@
#include <spinscale/component.h>
#include <spinscale/puppetApplication.h>
#include <marionette/marionette.h>
#include <spinscale/marionette.h>
namespace sscl {
@@ -16,9 +16,6 @@ parent(parent)
{
}
} // namespace sscl
namespace smo {
namespace mrntt {
MarionetteComponent::MarionetteComponent(
@@ -28,4 +25,4 @@ MarionetteComponent::MarionetteComponent(
}
} // namespace mrntt
} // namespace smo
} // namespace sscl
+16 -9
View File
@@ -8,19 +8,21 @@
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/callback.h>
#include <spinscale/callableTracer.h>
#include <componentThread.h>
#include <marionette/marionette.h>
#include <spinscale/componentThread.h>
#include <spinscale/marionette.h>
namespace sscl {
namespace mrntt {
// Global variable to store the marionette thread ID
// Default value is 0, but should be set by application code via setMarionetteThreadId()
sscl::ThreadId marionetteThreadId = 0;
ThreadId marionetteThreadId = 0;
void setMarionetteThreadId(sscl::ThreadId id)
void setMarionetteThreadId(ThreadId id)
{
marionetteThreadId = id;
}
} // namespace mrntt
} // namespace sscl
@@ -28,10 +30,15 @@ namespace sscl {
thread_local std::shared_ptr<ComponentThread> thisComponentThread;
namespace mrntt {
// Global marionette thread instance - defined here but initialized by application
std::shared_ptr<MarionetteThread> thread;
} // namespace mrntt
// Implementation of static method
std::shared_ptr<MarionetteThread> ComponentThread::getMrntt()
{
return smo::mrntt::thread;
return sscl::mrntt::thread;
}
void MarionetteThread::initializeTls(void)
@@ -180,13 +187,13 @@ void PuppetThread::joltThreadReq(
* To obtain a sh_ptr to the target thread, we use the selfPtr parameter
* passed in by the caller.
*/
if (id == sscl::marionetteThreadId)
if (id == sscl::mrntt::marionetteThreadId)
{
throw std::runtime_error(std::string(__func__)
+ ": invoked on mrntt thread");
}
std::shared_ptr<MarionetteThread> mrntt = smo::mrntt::thread;
std::shared_ptr<MarionetteThread> mrntt = sscl::mrntt::thread;
auto request = std::make_shared<ThreadLifetimeMgmtOp>(
mrntt, selfPtr, callback);
@@ -231,7 +238,7 @@ void PuppetThread::exitThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
void PuppetThread::pauseThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
{
if (id == sscl::marionetteThreadId)
if (id == sscl::mrntt::marionetteThreadId)
{
throw std::runtime_error(std::string(__func__)
+ ": invoked on mrntt thread");
@@ -250,7 +257,7 @@ void PuppetThread::pauseThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
void PuppetThread::resumeThreadReq(Callback<threadLifetimeMgmtOpCbFn> callback)
{
if (id == sscl::marionetteThreadId)
if (id == sscl::mrntt::marionetteThreadId)
{
throw std::runtime_error(std::string(__func__)
+ ": invoked on mrntt thread");