• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1if(MINGW OR (X86 AND UNIX AND NOT APPLE))
2  # mingw compiler is known to produce unstable SSE code with -O3 hence we are trying to use -O2 instead
3  if(CMAKE_COMPILER_IS_GNUCXX)
4    foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
5      string(REPLACE "-O3" "-O2" ${flags} "${${flags}}")
6    endforeach()
7  endif()
8
9  if(CMAKE_COMPILER_IS_GNUCC)
10    foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG)
11      string(REPLACE "-O3" "-O2" ${flags} "${${flags}}")
12    endforeach()
13  endif()
14endif()
15
16if(MSVC)
17  string(REGEX REPLACE "^  *| * $" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
18  string(REGEX REPLACE "^  *| * $" "" CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT}")
19  if(CMAKE_CXX_FLAGS STREQUAL CMAKE_CXX_FLAGS_INIT)
20    # override cmake default exception handling option
21    string(REPLACE "/EHsc" "/EHa" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
22    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}"  CACHE STRING "Flags used by the compiler during all build types." FORCE)
23  endif()
24endif()
25
26set(OPENCV_EXTRA_FLAGS "")
27set(OPENCV_EXTRA_C_FLAGS "")
28set(OPENCV_EXTRA_CXX_FLAGS "")
29set(OPENCV_EXTRA_FLAGS_RELEASE "")
30set(OPENCV_EXTRA_FLAGS_DEBUG "")
31set(OPENCV_EXTRA_EXE_LINKER_FLAGS "")
32set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "")
33set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG "")
34
35macro(add_extra_compiler_option option)
36  if(CMAKE_BUILD_TYPE)
37    set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_BUILD_TYPE})
38  endif()
39  ocv_check_flag_support(CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}")
40  if(${_varname})
41    set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
42  endif()
43
44  ocv_check_flag_support(C "${option}" _varname "${OPENCV_EXTRA_C_FLAGS} ${ARGN}")
45  if(${_varname})
46    set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
47  endif()
48endmacro()
49
50# OpenCV fails some tests when 'char' is 'unsigned' by default
51add_extra_compiler_option(-fsigned-char)
52
53if(MINGW)
54  # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838
55  # here we are trying to workaround the problem
56  add_extra_compiler_option(-mstackrealign)
57  if(NOT HAVE_CXX_MSTACKREALIGN)
58    add_extra_compiler_option(-mpreferred-stack-boundary=2)
59  endif()
60endif()
61
62if(CMAKE_COMPILER_IS_GNUCXX)
63  # High level of warnings.
64  add_extra_compiler_option(-W)
65  add_extra_compiler_option(-Wall)
66  add_extra_compiler_option(-Werror=return-type)
67  add_extra_compiler_option(-Werror=non-virtual-dtor)
68  add_extra_compiler_option(-Werror=address)
69  add_extra_compiler_option(-Werror=sequence-point)
70  add_extra_compiler_option(-Wformat)
71  add_extra_compiler_option(-Werror=format-security -Wformat)
72  add_extra_compiler_option(-Wmissing-declarations)
73  add_extra_compiler_option(-Wmissing-prototypes)
74  add_extra_compiler_option(-Wstrict-prototypes)
75  add_extra_compiler_option(-Wundef)
76  add_extra_compiler_option(-Winit-self)
77  add_extra_compiler_option(-Wpointer-arith)
78  add_extra_compiler_option(-Wshadow)
79  add_extra_compiler_option(-Wsign-promo)
80
81  if(ENABLE_NOISY_WARNINGS)
82    add_extra_compiler_option(-Wcast-align)
83    add_extra_compiler_option(-Wstrict-aliasing=2)
84  else()
85    add_extra_compiler_option(-Wno-narrowing)
86    add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
87    add_extra_compiler_option(-Wno-unnamed-type-template-args)
88  endif()
89  add_extra_compiler_option(-fdiagnostics-show-option)
90
91  # The -Wno-long-long is required in 64bit systems when including sytem headers.
92  if(X86_64)
93    add_extra_compiler_option(-Wno-long-long)
94  endif()
95
96  # We need pthread's
97  if(UNIX AND NOT ANDROID AND NOT (APPLE AND CMAKE_COMPILER_IS_CLANGCXX))
98    add_extra_compiler_option(-pthread)
99  endif()
100
101  if(CMAKE_COMPILER_IS_CLANGCXX)
102    add_extra_compiler_option(-Qunused-arguments)
103  endif()
104
105  if(OPENCV_WARNINGS_ARE_ERRORS)
106    add_extra_compiler_option(-Werror)
107  endif()
108
109  if(X86 AND NOT MINGW64 AND NOT X86_64 AND NOT APPLE)
110    add_extra_compiler_option(-march=i686)
111  endif()
112
113  if(APPLE)
114    add_extra_compiler_option(-Wno-semicolon-before-method-body)
115  endif()
116
117  # Other optimizations
118  if(ENABLE_OMIT_FRAME_POINTER)
119    add_extra_compiler_option(-fomit-frame-pointer)
120  else()
121    add_extra_compiler_option(-fno-omit-frame-pointer)
122  endif()
123  if(ENABLE_FAST_MATH)
124    add_extra_compiler_option(-ffast-math)
125  endif()
126  if(ENABLE_POWERPC)
127    add_extra_compiler_option("-mcpu=G3 -mtune=G5")
128  endif()
129  if(ENABLE_SSE)
130    add_extra_compiler_option(-msse)
131  endif()
132  if(ENABLE_SSE2)
133    add_extra_compiler_option(-msse2)
134  elseif(X86 OR X86_64)
135    add_extra_compiler_option(-mno-sse2)
136  endif()
137  if(ENABLE_NEON)
138    add_extra_compiler_option("-mfpu=neon")
139  endif()
140  if(ENABLE_VFPV3 AND NOT ENABLE_NEON)
141    add_extra_compiler_option("-mfpu=vfpv3")
142  endif()
143
144  # SSE3 and further should be disabled under MingW because it generates compiler errors
145  if(NOT MINGW)
146    if(ENABLE_AVX)
147      add_extra_compiler_option(-mavx)
148    elseif(X86 OR X86_64)
149      add_extra_compiler_option(-mno-avx)
150    endif()
151    if(ENABLE_AVX2)
152      add_extra_compiler_option(-mavx2)
153
154      if(ENABLE_FMA3)
155        add_extra_compiler_option(-mfma)
156      endif()
157    endif()
158
159    # GCC depresses SSEx instructions when -mavx is used. Instead, it generates new AVX instructions or AVX equivalence for all SSEx instructions when needed.
160    if(NOT OPENCV_EXTRA_CXX_FLAGS MATCHES "-mavx")
161      if(ENABLE_SSE3)
162        add_extra_compiler_option(-msse3)
163      elseif(X86 OR X86_64)
164        add_extra_compiler_option(-mno-sse3)
165      endif()
166
167      if(ENABLE_SSSE3)
168        add_extra_compiler_option(-mssse3)
169      elseif(X86 OR X86_64)
170        add_extra_compiler_option(-mno-ssse3)
171      endif()
172
173      if(ENABLE_SSE41)
174        add_extra_compiler_option(-msse4.1)
175      elseif(X86 OR X86_64)
176        add_extra_compiler_option(-mno-sse4.1)
177      endif()
178
179      if(ENABLE_SSE42)
180        add_extra_compiler_option(-msse4.2)
181      elseif(X86 OR X86_64)
182        add_extra_compiler_option(-mno-sse4.2)
183      endif()
184
185      if(ENABLE_POPCNT)
186        add_extra_compiler_option(-mpopcnt)
187      endif()
188    endif()
189  endif(NOT MINGW)
190
191  if(X86 OR X86_64)
192    if(NOT APPLE AND CMAKE_SIZEOF_VOID_P EQUAL 4)
193      if(OPENCV_EXTRA_CXX_FLAGS MATCHES "-m(sse2|avx)")
194        add_extra_compiler_option(-mfpmath=sse)# !! important - be on the same wave with x64 compilers
195      else()
196        add_extra_compiler_option(-mfpmath=387)
197      endif()
198    endif()
199  endif()
200
201  # Profiling?
202  if(ENABLE_PROFILING)
203    add_extra_compiler_option("-pg -g")
204    # turn off incompatible options
205    foreach(flags CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
206                  OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
207      string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
208      string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
209    endforeach()
210  elseif(NOT APPLE AND NOT ANDROID)
211    # Remove unreferenced functions: function level linking
212    add_extra_compiler_option(-ffunction-sections)
213  endif()
214
215  if(ENABLE_COVERAGE)
216    set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} --coverage")
217    set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} --coverage")
218  endif()
219
220  set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
221  set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0 -DDEBUG -D_DEBUG")
222endif()
223
224if(MSVC)
225  set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
226  # 64-bit portability warnings, in MSVC80
227  if(MSVC80)
228    set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Wp64")
229  endif()
230
231  if(BUILD_WITH_DEBUG_INFO)
232    set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
233  endif()
234
235  # Remove unreferenced functions: function level linking
236  set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
237  if(NOT MSVC_VERSION LESS 1400)
238    set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
239  endif()
240  if(BUILD_WITH_DEBUG_INFO)
241    set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
242  endif()
243
244  if(ENABLE_AVX2 AND NOT MSVC_VERSION LESS 1800)
245    set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:AVX2")
246  endif()
247  if(ENABLE_AVX AND NOT MSVC_VERSION LESS 1600 AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
248    set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:AVX")
249  endif()
250
251  if(ENABLE_SSE4_1 AND CV_ICC AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
252    set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE4.1")
253  endif()
254
255  if(ENABLE_SSE3 AND CV_ICC AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
256    set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE3")
257  endif()
258
259  if(NOT MSVC64)
260    # 64-bit MSVC compiler uses SSE/SSE2 by default
261    if(ENABLE_SSE2 AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
262      set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE2")
263    endif()
264    if(ENABLE_SSE AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
265      set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE")
266    endif()
267  endif()
268
269  if(ENABLE_SSE OR ENABLE_SSE2 OR ENABLE_SSE3 OR ENABLE_SSE4_1 OR ENABLE_AVX OR ENABLE_AVX2)
270    set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Oi")
271  endif()
272
273  if(X86 OR X86_64)
274    if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND ENABLE_SSE2)
275      set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /fp:fast") # !! important - be on the same wave with x64 compilers
276    endif()
277  endif()
278
279  if(OPENCV_WARNINGS_ARE_ERRORS)
280    set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
281  endif()
282endif()
283
284if(MSVC12 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
285  set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS")
286  set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS")
287endif()
288
289# Extra link libs if the user selects building static libs:
290if(NOT BUILD_SHARED_LIBS AND CMAKE_COMPILER_IS_GNUCXX AND NOT ANDROID)
291  # Android does not need these settings because they are already set by toolchain file
292  set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} stdc++)
293  set(OPENCV_EXTRA_FLAGS "-fPIC ${OPENCV_EXTRA_FLAGS}")
294endif()
295
296# Add user supplied extra options (optimization, etc...)
297# ==========================================================
298set(OPENCV_EXTRA_FLAGS         "${OPENCV_EXTRA_FLAGS}"         CACHE INTERNAL "Extra compiler options")
299set(OPENCV_EXTRA_C_FLAGS       "${OPENCV_EXTRA_C_FLAGS}"       CACHE INTERNAL "Extra compiler options for C sources")
300set(OPENCV_EXTRA_CXX_FLAGS     "${OPENCV_EXTRA_CXX_FLAGS}"     CACHE INTERNAL "Extra compiler options for C++ sources")
301set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE}" CACHE INTERNAL "Extra compiler options for Release build")
302set(OPENCV_EXTRA_FLAGS_DEBUG   "${OPENCV_EXTRA_FLAGS_DEBUG}"   CACHE INTERNAL "Extra compiler options for Debug build")
303set(OPENCV_EXTRA_EXE_LINKER_FLAGS         "${OPENCV_EXTRA_EXE_LINKER_FLAGS}"         CACHE INTERNAL "Extra linker flags")
304set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}" CACHE INTERNAL "Extra linker flags for Release build")
305set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG   "${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}"   CACHE INTERNAL "Extra linker flags for Debug build")
306
307# set default visibility to hidden
308if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_OPENCV_GCC_VERSION_NUM GREATER 399)
309  add_extra_compiler_option(-fvisibility=hidden)
310  add_extra_compiler_option(-fvisibility-inlines-hidden)
311endif()
312
313#combine all "extra" options
314set(CMAKE_C_FLAGS           "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
315set(CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
316set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
317set(CMAKE_C_FLAGS_RELEASE   "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
318set(CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
319set(CMAKE_C_FLAGS_DEBUG     "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
320set(CMAKE_EXE_LINKER_FLAGS         "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
321set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
322set(CMAKE_EXE_LINKER_FLAGS_DEBUG   "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
323
324if(MSVC)
325  # avoid warnings from MSVC about overriding the /W* option
326  # we replace /W3 with /W4 only for C++ files,
327  # since all the 3rd-party libraries OpenCV uses are in C,
328  # and we do not care about their warnings.
329  string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS}")
330  string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
331  string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG}")
332
333  if(NOT ENABLE_NOISY_WARNINGS AND MSVC_VERSION EQUAL 1400)
334    ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
335  endif()
336
337  # allow extern "C" functions throw exceptions
338  foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
339    string(REPLACE "/EHsc-" "/EHs" ${flags} "${${flags}}")
340    string(REPLACE "/EHsc"  "/EHs" ${flags} "${${flags}}")
341
342    string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
343  endforeach()
344
345  if(NOT ENABLE_NOISY_WARNINGS)
346    ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4251) # class 'std::XXX' needs to have dll-interface to be used by clients of YYY
347    ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4324) # 'struct_name' : structure was padded due to __declspec(align())
348  endif()
349endif()
350