|
9 | 9 | return() |
10 | 10 | endif() |
11 | 11 |
|
12 | | -# lib_registry: exports the default registry's static policy state |
13 | | -add_library(boost_openmethod-dl_test_registry SHARED registry.cpp) |
14 | | -target_link_libraries( |
15 | | - boost_openmethod-dl_test_registry |
16 | | - PUBLIC Boost::openmethod PRIVATE Boost::dll) |
| 12 | +# Build one set of {registry, method, overrider} shared libraries plus a test |
| 13 | +# executable. Each variant compiles the very same sources but with a different |
| 14 | +# default registry, selected by extra preprocessor definitions (ARGN) applied to |
| 15 | +# every target so all modules agree (ODR). Targets are suffixed and outputs go |
| 16 | +# to a per-variant directory so that main.cpp, which locates its sibling |
| 17 | +# libraries by filename fragment at runtime, never picks up the other variant. |
| 18 | +function(boost_openmethod_add_dynamic_loading_variant suffix) |
| 19 | + set(extra_defs ${ARGN}) |
| 20 | + set(reg boost_openmethod-dl_test_registry${suffix}) |
| 21 | + set(meth boost_openmethod-dl_test_method${suffix}) |
| 22 | + set(ovr boost_openmethod-dl_test_overrider${suffix}) |
| 23 | + set(exe boost_openmethod-test_dynamic_loading${suffix}) |
| 24 | + set(outdir "${CMAKE_CURRENT_BINARY_DIR}/dynamic_loading${suffix}") |
17 | 25 |
|
18 | | -# lib_method: exports the speak method; imports registry state from lib_registry |
19 | | -add_library(boost_openmethod-dl_test_method SHARED method.cpp) |
20 | | -target_link_libraries( |
21 | | - boost_openmethod-dl_test_method |
22 | | - PUBLIC Boost::openmethod boost_openmethod-dl_test_registry PRIVATE Boost::dll) |
| 26 | + # lib_registry: exports the registry's static policy state |
| 27 | + add_library(${reg} SHARED registry.cpp) |
| 28 | + target_link_libraries(${reg} PUBLIC Boost::openmethod PRIVATE Boost::dll) |
23 | 29 |
|
24 | | -# lib_overrider: adds a Dog overrider; imported at runtime via Boost.DLL |
25 | | -add_library(boost_openmethod-dl_test_overrider SHARED overrider.cpp) |
26 | | -target_link_libraries( |
27 | | - boost_openmethod-dl_test_overrider |
28 | | - PRIVATE Boost::openmethod boost_openmethod-dl_test_method Boost::dll) |
| 30 | + # lib_method: exports the speak method; imports registry state from lib_registry |
| 31 | + add_library(${meth} SHARED method.cpp) |
| 32 | + target_link_libraries(${meth} PUBLIC Boost::openmethod ${reg} PRIVATE Boost::dll) |
29 | 33 |
|
30 | | -# Test executable: links against lib_method (and lib_registry transitively); |
31 | | -# dynamically loads lib_overrider at runtime. |
32 | | -add_executable( |
33 | | - boost_openmethod-test_dynamic_loading main.cpp) |
34 | | -target_link_libraries( |
35 | | - boost_openmethod-test_dynamic_loading |
36 | | - PUBLIC |
37 | | - Boost::openmethod boost_openmethod-dl_test_registry |
38 | | - Boost::openmethod boost_openmethod-dl_test_method |
39 | | - PRIVATE Boost::openmethod Boost::unit_test_framework Boost::dll) |
40 | | -add_dependencies( |
41 | | - boost_openmethod-test_dynamic_loading |
42 | | - boost_openmethod-dl_test_registry |
43 | | - boost_openmethod-dl_test_method |
44 | | - boost_openmethod-dl_test_overrider) |
| 34 | + # lib_overrider: adds a Dog overrider; imported at runtime via Boost.DLL |
| 35 | + add_library(${ovr} SHARED overrider.cpp) |
| 36 | + target_link_libraries(${ovr} PRIVATE Boost::openmethod ${meth} Boost::dll) |
45 | 37 |
|
46 | | -# Put all outputs in the same directory so Boost.DLL can locate the shared |
47 | | -# libraries relative to the test executable at runtime. |
48 | | -# ENABLE_EXPORTS is required so that symbols from each shared library are |
49 | | -# visible to other shared libraries loaded at runtime (e.g. boost_openmethod-dl_test_overrider |
50 | | -# can resolve symbols from boost_openmethod-dl_test_method/boost_openmethod-dl_test_registry). |
51 | | -# CXX_VISIBILITY_PRESET must be "default" so that the template static variables |
52 | | -# used as the shared registry state (policy statics) are exported with DEFAULT |
53 | | -# ELF visibility. With hidden visibility (as set by BoostRoot.cmake in the |
54 | | -# super-project build) they become UNIQUE HIDDEN and are not deduplicated by |
55 | | -# the dynamic linker, breaking cross-DSO state sharing. |
56 | | -foreach( |
57 | | - target |
58 | | - boost_openmethod-dl_test_registry |
59 | | - boost_openmethod-dl_test_method |
60 | | - boost_openmethod-dl_test_overrider |
61 | | - boost_openmethod-test_dynamic_loading) |
62 | | - set_target_properties( |
63 | | - ${target} |
64 | | - PROPERTIES |
65 | | - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" |
66 | | - LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" |
67 | | - ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" |
68 | | - ENABLE_EXPORTS ON |
69 | | - CXX_VISIBILITY_PRESET default |
70 | | - VISIBILITY_INLINES_HIDDEN OFF) |
71 | | -endforeach() |
| 38 | + # Test executable: links against lib_method (and lib_registry transitively); |
| 39 | + # dynamically loads lib_overrider at runtime. |
| 40 | + add_executable(${exe} main.cpp) |
| 41 | + target_link_libraries(${exe} |
| 42 | + PUBLIC Boost::openmethod ${reg} ${meth} |
| 43 | + PRIVATE Boost::openmethod Boost::unit_test_framework Boost::dll) |
| 44 | + add_dependencies(${exe} ${reg} ${meth} ${ovr}) |
72 | 45 |
|
73 | | -if (TARGET tests) |
74 | | - add_dependencies(tests boost_openmethod-test_dynamic_loading) |
75 | | -endif() |
| 46 | + # Put all of this variant's outputs in one directory so Boost.DLL can locate |
| 47 | + # the shared libraries relative to the test executable at runtime. |
| 48 | + # ENABLE_EXPORTS is required so that symbols from each shared library are |
| 49 | + # visible to other shared libraries loaded at runtime (e.g. the overrider |
| 50 | + # can resolve symbols from the method/registry libraries). |
| 51 | + # CXX_VISIBILITY_PRESET must be "default" so that the template static |
| 52 | + # variables used as the shared registry state are exported with DEFAULT ELF |
| 53 | + # visibility. With hidden visibility (as set by BoostRoot.cmake in the |
| 54 | + # super-project build) they become UNIQUE HIDDEN and are not deduplicated by |
| 55 | + # the dynamic linker, breaking cross-DSO state sharing. |
| 56 | + foreach(target ${reg} ${meth} ${ovr} ${exe}) |
| 57 | + set_target_properties( |
| 58 | + ${target} |
| 59 | + PROPERTIES |
| 60 | + RUNTIME_OUTPUT_DIRECTORY "${outdir}" |
| 61 | + LIBRARY_OUTPUT_DIRECTORY "${outdir}" |
| 62 | + ARCHIVE_OUTPUT_DIRECTORY "${outdir}" |
| 63 | + ENABLE_EXPORTS ON |
| 64 | + CXX_VISIBILITY_PRESET default |
| 65 | + VISIBILITY_INLINES_HIDDEN OFF) |
| 66 | + if (extra_defs) |
| 67 | + target_compile_definitions(${target} PRIVATE ${extra_defs}) |
| 68 | + endif() |
| 69 | + endforeach() |
| 70 | + |
| 71 | + if (TARGET tests) |
| 72 | + add_dependencies(tests ${exe}) |
| 73 | + endif() |
| 74 | + |
| 75 | + boost_openmethod_add_test(${exe}) |
| 76 | +endfunction() |
| 77 | + |
| 78 | +# Variant 1: the default registry. |
| 79 | +boost_openmethod_add_dynamic_loading_variant("") |
76 | 80 |
|
77 | | -boost_openmethod_add_test(boost_openmethod-test_dynamic_loading) |
| 81 | +# Variant 2: indirect_registry (default_registry + indirect_vptr policy), |
| 82 | +# selected via BOOST_OPENMETHOD_DEFAULT_REGISTRY. |
| 83 | +boost_openmethod_add_dynamic_loading_variant( |
| 84 | + "_indirect" |
| 85 | + BOOST_OPENMETHOD_DEFAULT_REGISTRY=::boost::openmethod::indirect_registry) |
0 commit comments