Skip to content

Commit 6277665

Browse files
committed
cmake & swig: more generic way to replace std::vector<size_t> by its correct-sized std::vector<TYPE>, but only with SWIG < 3.0; this issue seems to have been fixed with SWIG >= 3.0.
1 parent 1425e48 commit 6277665

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

cmake/Modules/GrSwig.cmake

+30-11
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,36 @@ endfunction(GR_SWIG_MAKE_DOCS)
105105
macro(GR_SWIG_MAKE name)
106106
set(ifiles ${ARGN})
107107

108-
# Shimming this in here to take care of a SWIG bug with handling
109-
# vector<size_t> and vector<unsigned int> (on 32-bit machines) and
110-
# vector<long unsigned int> (on 64-bit machines). Use this to test
111-
# the size of size_t, then set SIZE_T_32 if it's a 32-bit machine
112-
# or not if it's 64-bit. The logic in gr_type.i handles the rest.
113-
INCLUDE(CheckTypeSize)
114-
CHECK_TYPE_SIZE("size_t" SIZEOF_SIZE_T)
115-
CHECK_TYPE_SIZE("unsigned int" SIZEOF_UINT)
116-
if(${SIZEOF_SIZE_T} EQUAL ${SIZEOF_UINT})
117-
list(APPEND GR_SWIG_FLAGS -DSIZE_T_32)
118-
endif(${SIZEOF_SIZE_T} EQUAL ${SIZEOF_UINT})
108+
# Take care of a SWIG < 3.0 bug with handling std::vector<size_t>,
109+
# by mapping to the correct sized type on the runtime system, one
110+
# of "unsigned int", "unsigned long", or "unsigned long long".
111+
# Compare the sizeof(size_t) with the sizeof the other types, and
112+
# pick the first one in the list with the same sizeof. The logic
113+
# in gnuradio-runtime/swig/gr_types.i handles the rest. It is
114+
# probably not necessary to do this assignment all of the time,
115+
# but it's easier to do it this way than to figure out the
116+
# conditions when it is necessary -- and doing it this way won't
117+
# hurt. This bug seems to have been fixed with SWIG >= 3.0, and
118+
# mostly happens when not doing a native build (e.g., on Mac OS X
119+
# when using a 64-bit CPU but building for 32-bit).
120+
121+
if(SWIG_VERSION VERSION_LESS "3.0.0")
122+
include(CheckTypeSize)
123+
check_type_size("size_t" SIZEOF_SIZE_T)
124+
check_type_size("unsigned int" SIZEOF_UINT)
125+
check_type_size("unsigned long" SIZEOF_UL)
126+
check_type_size("unsigned long long" SIZEOF_ULL)
127+
128+
if(${SIZEOF_SIZE_T} EQUAL ${SIZEOF_UINT})
129+
list(APPEND GR_SWIG_FLAGS -DSIZE_T_UINT)
130+
elseif(${SIZEOF_SIZE_T} EQUAL ${SIZEOF_UL})
131+
list(APPEND GR_SWIG_FLAGS -DSIZE_T_UL)
132+
elseif(${SIZEOF_SIZE_T} EQUAL ${SIZEOF_ULL})
133+
list(APPEND GR_SWIG_FLAGS -DSIZE_T_ULL)
134+
else()
135+
message(FATAL_ERROR "GrSwig: Unable to find replace for std::vector<size_t>; this should never happen!")
136+
endif()
137+
endif()
119138

120139
#do swig doc generation if specified
121140
if(GR_SWIG_DOC_FILE)

gnuradio-runtime/swig/gr_types.i

+12-8
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,19 @@ namespace std {
8080
%template(gr_vector_vector_complexf) std::vector< std::vector< std::complex<float> > >;
8181
%template(gr_vector_vector_complexd) std::vector< std::vector< std::complex<double> > >;
8282

83-
// Fix for Issue #529
84-
#ifdef SIZE_T_32
85-
// On 32-bit systems, whenever we see std::vector<size_t>, replace it
86-
// with vector<unsigned int>
83+
// Fix for Issue #529: replace std::vector<size_t> with its equivalent
84+
// in element size, one of "unsigned int", "unsigned long", or
85+
// "unsigned long long". The replacement depends on the sizeof each
86+
// type, as determined in GrSwig.cmake GR_SWIG_MAKE. For SWIG >=
87+
// 3.0.0, none of these will be defined because this issue seems to
88+
// have been fixed.
89+
90+
#if defined(SIZE_T_UINT)
8791
%apply std::vector<unsigned int> { std::vector<size_t> };
88-
#else
89-
// On 64-bit systems, whenever we see std::vector<size_t>, replace it
90-
// with vector<long unsigned int>
91-
%apply std::vector<long unsigned int> { std::vector<size_t> };
92+
#elif defined(SIZE_T_UL)
93+
%apply std::vector<unsigned long> { std::vector<size_t> };
94+
#elif defined(SIZE_T_ULL)
95+
%apply std::vector<unsigned long long> { std::vector<size_t> };
9296
#endif
9397

9498
#endif /* SWIG_GR_TYPES_I */

0 commit comments

Comments
 (0)