From 620c6a5b81fa1a42ac22605f86fbfbafae60e134 Mon Sep 17 00:00:00 2001 From: Adam Kewley Date: Thu, 6 Feb 2025 13:51:10 +0100 Subject: [PATCH] Add OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION cmake option --- CMakeLists.txt | 22 +++++++++++++++++++ .../Actuators/RegisterTypes_osimActuators.cpp | 5 +++-- .../Analyses/RegisterTypes_osimAnalyses.cpp | 4 +++- OpenSim/Common/Object.cpp | 7 +++--- .../RegisterTypes_osimExampleComponents.cpp | 4 +++- ...RegisterTypes_osimMocoCustomEffortGoal.cpp | 4 +++- .../RegisterTypes_osimPlugin.cpp | 4 +++- .../RegisterTypes_osimExpPlugin.cpp | 4 +++- OpenSim/Moco/RegisterTypes_osimMoco.cpp | 4 +++- OpenSim/OpenSim.h | 5 ++++- .../RegisterTypes_osimSimulation.cpp | 4 +++- OpenSim/Tools/RegisterTypes_osimTools.cpp | 6 +++-- 12 files changed, 58 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd1b7496c6..662f4836ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,9 +173,31 @@ By default, any application linking to osimCommon will create an written by OpenSim (incl. during static initialization) are written to both this file and the standard output streams." OFF) +option(OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION +"Disable OpenSim from registering bundled `Object` types at static +initialization time. + +By default, the static initializer for each OpenSim library will +call `RegisterTypes_osimCommon`. This has the handy side-effect of +making all available `Object` types available at (post-static-init) +runtime, so that (e.g.) downstream code can immediately load XML +files. However, it has the drawback that downstream code cannot +control when, or in which order, registration happens, which can +(e.g.) prevent setting up application-level logging before registering +things. + +If this is set to `ON` (i.e. disable static type registration) then +downstream code *must* manually register the OpenSim types it plans +on using by calling `RegisterTypes_osimLIBRARY` (e.g. `RegisterTypes_osimActuators`), +or by manually registering each type (e.g. `Object::registerType(PhysicalOffsetFrame());`)." OFF) +mark_as_advanced(OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION) + if(OPENSIM_DISABLE_LOG_FILE) add_definitions(-DOPENSIM_DISABLE_LOG_FILE=1) endif() +if(OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION) + add_definitions(-DOPENSIM_DISABLE_STATIC_TYPE_REGISTRATION=1) +endif() set(OPENSIM_BUILD_INDIVIDUAL_APPS_DEFAULT OFF) if(WIN32) diff --git a/OpenSim/Actuators/RegisterTypes_osimActuators.cpp b/OpenSim/Actuators/RegisterTypes_osimActuators.cpp index f7c3458214..5abffc7fae 100644 --- a/OpenSim/Actuators/RegisterTypes_osimActuators.cpp +++ b/OpenSim/Actuators/RegisterTypes_osimActuators.cpp @@ -37,8 +37,9 @@ using namespace OpenSim; using namespace std; -static osimActuatorsInstantiator instantiator; - +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimActuatorsInstantiator instantiator; +#endif //_____________________________________________________________________________ /** diff --git a/OpenSim/Analyses/RegisterTypes_osimAnalyses.cpp b/OpenSim/Analyses/RegisterTypes_osimAnalyses.cpp index 5b27dccf89..26aad1c8a2 100644 --- a/OpenSim/Analyses/RegisterTypes_osimAnalyses.cpp +++ b/OpenSim/Analyses/RegisterTypes_osimAnalyses.cpp @@ -31,7 +31,9 @@ using namespace OpenSim; using namespace std; -static osimAnalysesInstantiator instantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimAnalysesInstantiator instantiator; +#endif //_____________________________________________________________________________ /** diff --git a/OpenSim/Common/Object.cpp b/OpenSim/Common/Object.cpp index 8f400537be..19c5a48b18 100644 --- a/OpenSim/Common/Object.cpp +++ b/OpenSim/Common/Object.cpp @@ -1747,7 +1747,8 @@ extern "C" OSIMCOMMON_API void RegisterTypes_osimCommon(); void osimCommonInstantiator::registerDllClasses() { RegisterTypes_osimCommon(); -} - -static osimCommonInstantiator instantiator; +} +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimCommonInstantiator instantiator; +#endif /// @endcond diff --git a/OpenSim/ExampleComponents/RegisterTypes_osimExampleComponents.cpp b/OpenSim/ExampleComponents/RegisterTypes_osimExampleComponents.cpp index 2ea062a7ee..1a20fcdde4 100644 --- a/OpenSim/ExampleComponents/RegisterTypes_osimExampleComponents.cpp +++ b/OpenSim/ExampleComponents/RegisterTypes_osimExampleComponents.cpp @@ -33,7 +33,9 @@ using namespace std; using namespace OpenSim; -static osimExampleComponentsInstantiator osimExampleComponentsInstantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimExampleComponentsInstantiator osimExampleComponentsInstantiator; +#endif //_____________________________________________________________________________ /** diff --git a/OpenSim/Examples/Moco/exampleMocoCustomEffortGoal/RegisterTypes_osimMocoCustomEffortGoal.cpp b/OpenSim/Examples/Moco/exampleMocoCustomEffortGoal/RegisterTypes_osimMocoCustomEffortGoal.cpp index 08422c8db8..8279981779 100644 --- a/OpenSim/Examples/Moco/exampleMocoCustomEffortGoal/RegisterTypes_osimMocoCustomEffortGoal.cpp +++ b/OpenSim/Examples/Moco/exampleMocoCustomEffortGoal/RegisterTypes_osimMocoCustomEffortGoal.cpp @@ -20,7 +20,9 @@ using namespace OpenSim; -static osimMocoCustomEffortGoalInstantiator instantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimMocoCustomEffortGoalInstantiator instantiator; +#endif OSIMMOCOCUSTOMEFFORTGOAL_API void RegisterTypes_osimMocoCustomEffortGoal() { try { diff --git a/OpenSim/Examples/Plugins/CoupledBushingForceExample/RegisterTypes_osimPlugin.cpp b/OpenSim/Examples/Plugins/CoupledBushingForceExample/RegisterTypes_osimPlugin.cpp index 4dfa7f61a2..4d947808dd 100644 --- a/OpenSim/Examples/Plugins/CoupledBushingForceExample/RegisterTypes_osimPlugin.cpp +++ b/OpenSim/Examples/Plugins/CoupledBushingForceExample/RegisterTypes_osimPlugin.cpp @@ -30,7 +30,9 @@ using namespace OpenSim; using namespace std; -static dllObjectInstantiator instantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static dllObjectInstantiator instantiator; +#endif //_____________________________________________________________________________ /** diff --git a/OpenSim/Examples/SymbolicExpressionReporter/RegisterTypes_osimExpPlugin.cpp b/OpenSim/Examples/SymbolicExpressionReporter/RegisterTypes_osimExpPlugin.cpp index e1087cbb71..72b377f073 100644 --- a/OpenSim/Examples/SymbolicExpressionReporter/RegisterTypes_osimExpPlugin.cpp +++ b/OpenSim/Examples/SymbolicExpressionReporter/RegisterTypes_osimExpPlugin.cpp @@ -30,7 +30,9 @@ using namespace OpenSim; using namespace std; -static dllPluginObjectInstantiator dInstantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static dllPluginObjectInstantiator dInstantiator; +#endif //_____________________________________________________________________________ /** diff --git a/OpenSim/Moco/RegisterTypes_osimMoco.cpp b/OpenSim/Moco/RegisterTypes_osimMoco.cpp index eba929ae94..7a794481eb 100644 --- a/OpenSim/Moco/RegisterTypes_osimMoco.cpp +++ b/OpenSim/Moco/RegisterTypes_osimMoco.cpp @@ -64,7 +64,9 @@ using namespace OpenSim; -static osimMocoInstantiator instantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimMocoInstantiator instantiator; +#endif OSIMMOCO_API void RegisterTypes_osimMoco() { try { diff --git a/OpenSim/OpenSim.h b/OpenSim/OpenSim.h index 906c1292ac..4f1fcbd66e 100644 --- a/OpenSim/OpenSim.h +++ b/OpenSim/OpenSim.h @@ -42,5 +42,8 @@ class osimInstantiator } }; -static osimInstantiator instantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimInstantiator instantiator; +#endif + #endif // _opensim_h_ diff --git a/OpenSim/Simulation/RegisterTypes_osimSimulation.cpp b/OpenSim/Simulation/RegisterTypes_osimSimulation.cpp index 10835dec2d..9458593919 100644 --- a/OpenSim/Simulation/RegisterTypes_osimSimulation.cpp +++ b/OpenSim/Simulation/RegisterTypes_osimSimulation.cpp @@ -146,7 +146,9 @@ using namespace std; using namespace OpenSim; -static osimSimulationInstantiator instantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimSimulationInstantiator instantiator; +#endif //_____________________________________________________________________________ /** diff --git a/OpenSim/Tools/RegisterTypes_osimTools.cpp b/OpenSim/Tools/RegisterTypes_osimTools.cpp index 6def930d80..21d033d481 100644 --- a/OpenSim/Tools/RegisterTypes_osimTools.cpp +++ b/OpenSim/Tools/RegisterTypes_osimTools.cpp @@ -62,7 +62,9 @@ using namespace std; using namespace OpenSim; -static osimToolsInstantiator instantiator; +#ifndef OPENSIM_DISABLE_STATIC_TYPE_REGISTRATION + static osimToolsInstantiator instantiator; +#endif //_____________________________________________________________________________ /** @@ -125,4 +127,4 @@ osimToolsInstantiator::osimToolsInstantiator() void osimToolsInstantiator::registerDllClasses() { RegisterTypes_osimTools(); -} \ No newline at end of file +}