Skip to content

Commit 6fb9d42

Browse files
parojmshabunin
authored andcommitted
Hid symbols in static builds, added LTO flags, removed exports from ts
1 parent ef04ca9 commit 6fb9d42

File tree

25 files changed

+174
-375
lines changed

25 files changed

+174
-375
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ else()
2929
cmake_minimum_required(VERSION "${MIN_VER_CMAKE}" FATAL_ERROR)
3030
endif()
3131

32+
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
33+
3234
# Following block can break build in case of cross-compilng
3335
# but CMAKE_CROSSCOMPILING variable will be set only on project(OpenCV) command
3436
# so we will try to detect cross-compiling by presense of CMAKE_TOOLCHAIN_FILE
@@ -311,6 +313,7 @@ OCV_OPTION(ENABLE_IMPL_COLLECTION "Collect implementation data on function c
311313
OCV_OPTION(ENABLE_INSTRUMENTATION "Instrument functions to collect calls trace and performance" OFF )
312314
OCV_OPTION(ENABLE_GNU_STL_DEBUG "Enable GNU STL Debug mode (defines _GLIBCXX_DEBUG)" OFF IF ((NOT CMAKE_VERSION VERSION_LESS "2.8.11") AND CMAKE_COMPILER_IS_GNUCXX) )
313315
OCV_OPTION(ENABLE_BUILD_HARDENING "Enable hardening of the resulting binaries (against security attacks, detects memory corruption, etc)" OFF)
316+
OCV_OPTION(ENABLE_LTO "Enable Link Time Optimization" OFF IF CMAKE_COMPILER_IS_GNUCXX OR MSVC)
314317
OCV_OPTION(GENERATE_ABI_DESCRIPTOR "Generate XML file for abi_compliance_checker tool" OFF IF UNIX)
315318
OCV_OPTION(CV_ENABLE_INTRINSICS "Use intrinsic-based optimized code" ON )
316319
OCV_OPTION(CV_DISABLE_OPTIMIZATION "Disable explicit optimized code (dispatched code/intrinsics/loop unrolling/etc)" OFF )

apps/traincascade/old_ml.hpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,32 +1895,6 @@ class CvANN_MLP : public CvStatModel
18951895
cv::RNG* rng;
18961896
};
18971897

1898-
/****************************************************************************************\
1899-
* Auxilary functions declarations *
1900-
\****************************************************************************************/
1901-
1902-
/* Generates <sample> from multivariate normal distribution, where <mean> - is an
1903-
average row vector, <cov> - symmetric covariation matrix */
1904-
CVAPI(void) cvRandMVNormal( CvMat* mean, CvMat* cov, CvMat* sample,
1905-
CvRNG* rng CV_DEFAULT(0) );
1906-
1907-
/* Generates sample from gaussian mixture distribution */
1908-
CVAPI(void) cvRandGaussMixture( CvMat* means[],
1909-
CvMat* covs[],
1910-
float weights[],
1911-
int clsnum,
1912-
CvMat* sample,
1913-
CvMat* sampClasses CV_DEFAULT(0) );
1914-
1915-
#define CV_TS_CONCENTRIC_SPHERES 0
1916-
1917-
/* creates test set */
1918-
CVAPI(void) cvCreateTestSet( int type, CvMat** samples,
1919-
int num_samples,
1920-
int num_features,
1921-
CvMat** responses,
1922-
int num_classes, ... );
1923-
19241898
/****************************************************************************************\
19251899
* Data *
19261900
\****************************************************************************************/

apps/traincascade/old_ml_inner_functions.cpp

Lines changed: 0 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -114,153 +114,11 @@ void CvStatModel::write( CvFileStorage*, const char* ) const
114114
OPENCV_ERROR( CV_StsNotImplemented, "CvStatModel::write", "" );
115115
}
116116

117-
118117
void CvStatModel::read( CvFileStorage*, CvFileNode* )
119118
{
120119
OPENCV_ERROR( CV_StsNotImplemented, "CvStatModel::read", "" );
121120
}
122121

123-
124-
/* Calculates upper triangular matrix S, where A is a symmetrical matrix A=S'*S */
125-
static void cvChol( CvMat* A, CvMat* S )
126-
{
127-
int dim = A->rows;
128-
129-
int i, j, k;
130-
float sum;
131-
132-
for( i = 0; i < dim; i++ )
133-
{
134-
for( j = 0; j < i; j++ )
135-
CV_MAT_ELEM(*S, float, i, j) = 0;
136-
137-
sum = 0;
138-
for( k = 0; k < i; k++ )
139-
sum += CV_MAT_ELEM(*S, float, k, i) * CV_MAT_ELEM(*S, float, k, i);
140-
141-
CV_MAT_ELEM(*S, float, i, i) = (float)sqrt(CV_MAT_ELEM(*A, float, i, i) - sum);
142-
143-
for( j = i + 1; j < dim; j++ )
144-
{
145-
sum = 0;
146-
for( k = 0; k < i; k++ )
147-
sum += CV_MAT_ELEM(*S, float, k, i) * CV_MAT_ELEM(*S, float, k, j);
148-
149-
CV_MAT_ELEM(*S, float, i, j) =
150-
(CV_MAT_ELEM(*A, float, i, j) - sum) / CV_MAT_ELEM(*S, float, i, i);
151-
152-
}
153-
}
154-
}
155-
156-
/* Generates <sample> from multivariate normal distribution, where <mean> - is an
157-
average row vector, <cov> - symmetric covariation matrix */
158-
CV_IMPL void cvRandMVNormal( CvMat* mean, CvMat* cov, CvMat* sample, CvRNG* rng )
159-
{
160-
int dim = sample->cols;
161-
int amount = sample->rows;
162-
163-
CvRNG state = rng ? *rng : cvRNG( cvGetTickCount() );
164-
cvRandArr(&state, sample, CV_RAND_NORMAL, cvScalarAll(0), cvScalarAll(1) );
165-
166-
CvMat* utmat = cvCreateMat(dim, dim, sample->type);
167-
CvMat* vect = cvCreateMatHeader(1, dim, sample->type);
168-
169-
cvChol(cov, utmat);
170-
171-
int i;
172-
for( i = 0; i < amount; i++ )
173-
{
174-
cvGetRow(sample, vect, i);
175-
cvMatMulAdd(vect, utmat, mean, vect);
176-
}
177-
178-
cvReleaseMat(&vect);
179-
cvReleaseMat(&utmat);
180-
}
181-
182-
183-
/* Generates <sample> of <amount> points from a discrete variate xi,
184-
where Pr{xi = k} == probs[k], 0 < k < len - 1. */
185-
static void cvRandSeries( float probs[], int len, int sample[], int amount )
186-
{
187-
CvMat* univals = cvCreateMat(1, amount, CV_32FC1);
188-
float* knots = (float*)cvAlloc( len * sizeof(float) );
189-
190-
int i, j;
191-
192-
CvRNG state = cvRNG(-1);
193-
cvRandArr(&state, univals, CV_RAND_UNI, cvScalarAll(0), cvScalarAll(1) );
194-
195-
knots[0] = probs[0];
196-
for( i = 1; i < len; i++ )
197-
knots[i] = knots[i - 1] + probs[i];
198-
199-
for( i = 0; i < amount; i++ )
200-
for( j = 0; j < len; j++ )
201-
{
202-
if ( CV_MAT_ELEM(*univals, float, 0, i) <= knots[j] )
203-
{
204-
sample[i] = j;
205-
break;
206-
}
207-
}
208-
209-
cvFree(&knots);
210-
}
211-
212-
/* Generates <sample> from gaussian mixture distribution */
213-
CV_IMPL void cvRandGaussMixture( CvMat* means[],
214-
CvMat* covs[],
215-
float weights[],
216-
int clsnum,
217-
CvMat* sample,
218-
CvMat* sampClasses )
219-
{
220-
int dim = sample->cols;
221-
int amount = sample->rows;
222-
223-
int i, clss;
224-
225-
int* sample_clsnum = (int*)cvAlloc( amount * sizeof(int) );
226-
CvMat** utmats = (CvMat**)cvAlloc( clsnum * sizeof(CvMat*) );
227-
CvMat* vect = cvCreateMatHeader(1, dim, CV_32FC1);
228-
229-
CvMat* classes;
230-
if( sampClasses )
231-
classes = sampClasses;
232-
else
233-
classes = cvCreateMat(1, amount, CV_32FC1);
234-
235-
CvRNG state = cvRNG(-1);
236-
cvRandArr(&state, sample, CV_RAND_NORMAL, cvScalarAll(0), cvScalarAll(1));
237-
238-
cvRandSeries(weights, clsnum, sample_clsnum, amount);
239-
240-
for( i = 0; i < clsnum; i++ )
241-
{
242-
utmats[i] = cvCreateMat(dim, dim, CV_32FC1);
243-
cvChol(covs[i], utmats[i]);
244-
}
245-
246-
for( i = 0; i < amount; i++ )
247-
{
248-
CV_MAT_ELEM(*classes, float, 0, i) = (float)sample_clsnum[i];
249-
cvGetRow(sample, vect, i);
250-
clss = sample_clsnum[i];
251-
cvMatMulAdd(vect, utmats[clss], means[clss], vect);
252-
}
253-
254-
if( !sampClasses )
255-
cvReleaseMat(&classes);
256-
for( i = 0; i < clsnum; i++ )
257-
cvReleaseMat(&utmats[i]);
258-
cvFree(&utmats);
259-
cvFree(&sample_clsnum);
260-
cvReleaseMat(&vect);
261-
}
262-
263-
264122
CvMat* icvGenerateRandomClusterCenters ( int seed, const CvMat* data,
265123
int num_of_clusters, CvMat* _centers )
266124
{
@@ -317,55 +175,6 @@ CvMat* icvGenerateRandomClusterCenters ( int seed, const CvMat* data,
317175
return _centers ? _centers : centers;
318176
} // end of icvGenerateRandomClusterCenters
319177

320-
// By S. Dilman - begin -
321-
322-
#define ICV_RAND_MAX 4294967296 // == 2^32
323-
324-
// static void cvRandRoundUni (CvMat* center,
325-
// float radius_small,
326-
// float radius_large,
327-
// CvMat* desired_matrix,
328-
// CvRNG* rng_state_ptr)
329-
// {
330-
// float rad, norm, coefficient;
331-
// int dim, size, i, j;
332-
// CvMat *cov, sample;
333-
// CvRNG rng_local;
334-
335-
// CV_FUNCNAME("cvRandRoundUni");
336-
// __BEGIN__
337-
338-
// rng_local = *rng_state_ptr;
339-
340-
// CV_ASSERT ((radius_small >= 0) &&
341-
// (radius_large > 0) &&
342-
// (radius_small <= radius_large));
343-
// CV_ASSERT (center && desired_matrix && rng_state_ptr);
344-
// CV_ASSERT (center->rows == 1);
345-
// CV_ASSERT (center->cols == desired_matrix->cols);
346-
347-
// dim = desired_matrix->cols;
348-
// size = desired_matrix->rows;
349-
// cov = cvCreateMat (dim, dim, CV_32FC1);
350-
// cvSetIdentity (cov);
351-
// cvRandMVNormal (center, cov, desired_matrix, &rng_local);
352-
353-
// for (i = 0; i < size; i++)
354-
// {
355-
// rad = (float)(cvRandReal(&rng_local)*(radius_large - radius_small) + radius_small);
356-
// cvGetRow (desired_matrix, &sample, i);
357-
// norm = (float) cvNorm (&sample, 0, CV_L2);
358-
// coefficient = rad / norm;
359-
// for (j = 0; j < dim; j++)
360-
// CV_MAT_ELEM (sample, float, 0, j) *= coefficient;
361-
// }
362-
363-
// __END__
364-
365-
// }
366-
367-
// By S. Dilman - end -
368-
369178
static int CV_CDECL
370179
icvCmpIntegers( const void* a, const void* b )
371180
{

apps/traincascade/old_ml_tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,7 @@ double CvDTree::calc_node_dir( CvDTreeNode* node )
18801880
namespace cv
18811881
{
18821882

1883-
template<> CV_EXPORTS void DefaultDeleter<CvDTreeSplit>::operator ()(CvDTreeSplit* obj) const
1883+
template<> void DefaultDeleter<CvDTreeSplit>::operator ()(CvDTreeSplit* obj) const
18841884
{
18851885
fastFree(obj);
18861886
}

cmake/OpenCVCompilerOptions.cmake

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,15 @@ if(CMAKE_COMPILER_IS_GNUCXX)
178178
OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
179179
string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
180180
string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
181+
string(REPLACE "-fdata-sections" "" ${flags} "${${flags}}")
181182
endforeach()
182183
elseif(NOT ((IOS OR ANDROID) AND NOT BUILD_SHARED_LIBS))
183184
# Remove unreferenced functions: function level linking
184185
add_extra_compiler_option(-ffunction-sections)
186+
add_extra_compiler_option(-fdata-sections)
187+
if(NOT APPLE)
188+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
189+
endif()
185190
endif()
186191

187192
if(ENABLE_COVERAGE)
@@ -195,6 +200,10 @@ if(CMAKE_COMPILER_IS_GNUCXX)
195200
set(WITH_VTK OFF) # There are issues with VTK 6.0
196201
endif()
197202

203+
if(ENABLE_LTO)
204+
add_extra_compiler_option(-flto)
205+
endif()
206+
198207
set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
199208
if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} " MATCHES "-O")
200209
set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0")
@@ -229,6 +238,12 @@ if(MSVC)
229238
if(OPENCV_WARNINGS_ARE_ERRORS)
230239
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
231240
endif()
241+
242+
if(ENABLE_LTO)
243+
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /GL")
244+
set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} /LTCG")
245+
endif()
246+
232247
endif()
233248

234249
if(MSVC12 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
@@ -241,15 +256,6 @@ if(WINRT_PHONE AND WINRT_8_0)
241256
set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /AI\$(WindowsSDK_MetadataPath)")
242257
endif()
243258

244-
# Extra link libs if the user selects building static libs:
245-
if(NOT BUILD_SHARED_LIBS AND CMAKE_COMPILER_IS_GNUCXX AND NOT ANDROID)
246-
# Android does not need these settings because they are already set by toolchain file
247-
if(NOT MINGW)
248-
set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} stdc++)
249-
endif()
250-
set(OPENCV_EXTRA_FLAGS "-fPIC ${OPENCV_EXTRA_FLAGS}")
251-
endif()
252-
253259
include(cmake/OpenCVCompilerOptimizations.cmake)
254260

255261
if(COMMAND ocv_compiler_optimization_options)

cmake/OpenCVModule.cmake

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,6 @@ macro(_ocv_create_module)
852852
${${the_module}_pch}
853853
${_VS_VERSION_FILE}
854854
)
855-
856855
set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
857856
set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
858857
PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
@@ -879,8 +878,13 @@ macro(_ocv_create_module)
879878
COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
880879
LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
881880
RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
881+
DEFINE_SYMBOL CVAPI_EXPORTS
882882
)
883883

884+
if(ANDROID AND BUILD_FAT_JAVA_LIB)
885+
target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
886+
endif()
887+
884888
# For dynamic link numbering convenions
885889
if(NOT ANDROID)
886890
# Android SDK build scripts can include only .so files into final .apk
@@ -891,11 +895,6 @@ macro(_ocv_create_module)
891895
)
892896
endif()
893897

894-
if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
895-
OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
896-
set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
897-
endif()
898-
899898
if (ENABLE_GNU_STL_DEBUG)
900899
target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
901900
endif()

modules/core/include/opencv2/core/cvdef.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,22 @@ Cv64suf;
248248
# define DISABLE_OPENCV_24_COMPATIBILITY
249249
#endif
250250

251-
#if (defined _WIN32 || defined WINCE || defined __CYGWIN__) && defined CVAPI_EXPORTS
252-
# define CV_EXPORTS __declspec(dllexport)
253-
#elif defined __GNUC__ && __GNUC__ >= 4
254-
# define CV_EXPORTS __attribute__ ((visibility ("default")))
251+
#ifdef CVAPI_EXPORTS
252+
# if (defined _WIN32 || defined WINCE || defined __CYGWIN__)
253+
# define CV_EXPORTS __declspec(dllexport)
254+
# elif defined __GNUC__ && __GNUC__ >= 4
255+
# define CV_EXPORTS __attribute__ ((visibility ("default")))
256+
# endif
257+
#endif
258+
259+
#ifndef CV_EXPORTS
260+
# define CV_EXPORTS
261+
#endif
262+
263+
#ifdef _MSC_VER
264+
# define CV_EXPORTS_TEMPLATE
255265
#else
256-
# define CV_EXPORTS
266+
# define CV_EXPORTS_TEMPLATE CV_EXPORTS
257267
#endif
258268

259269
#ifndef CV_DEPRECATED

modules/core/include/opencv2/core/private.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ CV_EXPORTS void scalarToRawData(const cv::Scalar& s, void* buf, int type, int un
162162

163163
//! Allocate all memory buffers which will not be freed, ease filtering memcheck issues
164164
template <typename T>
165-
CV_EXPORTS T* allocSingleton(size_t count) { return static_cast<T*>(fastMalloc(sizeof(T) * count)); }
165+
T* allocSingleton(size_t count) { return static_cast<T*>(fastMalloc(sizeof(T) * count)); }
166166
}
167167

168168
// property implementation macros

0 commit comments

Comments
 (0)