0031606: Configuration, CMake - build fails with Android NDK + CLang compiler + MinGW...
[occt.git] / adm / cmake / occt_defs_flags.cmake
CommitLineData
e713baee 1##
2
3if(FLAGS_ALREADY_INCLUDED)
4 return()
5endif()
6set(FLAGS_ALREADY_INCLUDED 1)
7
2e48c1cb 8# force option /fp:precise for Visual Studio projects.
cb728702 9#
10# Note that while this option is default for MSVC compiler, Visual Studio
11# project can be switched later to use Intel Compiler (ICC).
12# Enforcing -fp:precise ensures that in such case ICC will use correct
13# option instead of its default -fp:fast which is harmful for OCCT.
9f33b387 14if (MSVC)
2e48c1cb 15 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise")
16 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /fp:precise")
9f33b387 17endif()
18
1167d6cc 19# add SSE2 option for old MSVC compilers (VS 2005 - 2010, 32 bit only)
9f33b387 20if (NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
1167d6cc 21 if (MSVC AND ((MSVC_VERSION EQUAL 1400) OR (MSVC_VERSION EQUAL 1500) OR (MSVC_VERSION EQUAL 1600)))
100592c1 22 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:SSE2")
23 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:SSE2")
9f33b387 24 endif()
25endif()
26
9e479117 27if (MSVC)
fe9b8ff2 28 add_definitions (-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE)
9f33b387 29else()
100592c1 30 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -fPIC")
31 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexceptions -fPIC")
32 add_definitions(-DOCC_CONVERT_SIGNALS)
9f33b387 33endif()
34
35# enable structured exceptions for MSVC
36string (REGEX MATCH "EHsc" ISFLAG "${CMAKE_CXX_FLAGS}")
37if (ISFLAG)
38 string (REGEX REPLACE "EHsc" "EHa" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
cb728702 39elseif (MSVC)
2e48c1cb 40 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa")
9f33b387 41endif()
42
57c28b61 43# remove _WINDOWS flag if it exists
44string (REGEX MATCH "/D_WINDOWS" IS_WINDOWSFLAG "${CMAKE_CXX_FLAGS}")
45if (IS_WINDOWSFLAG)
a6874708 46 message (STATUS "Info: /D_WINDOWS has been removed from CMAKE_CXX_FLAGS")
57c28b61 47 string (REGEX REPLACE "/D_WINDOWS" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
48endif()
49
50# remove WIN32 flag if it exists
51string (REGEX MATCH "/DWIN32" IS_WIN32FLAG "${CMAKE_CXX_FLAGS}")
52if (IS_WIN32FLAG)
a6874708 53 message (STATUS "Info: /DWIN32 has been removed from CMAKE_CXX_FLAGS")
57c28b61 54 string (REGEX REPLACE "/DWIN32" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
55endif()
56
57# remove _WINDOWS flag if it exists
58string (REGEX MATCH "/D_WINDOWS" IS_WINDOWSFLAG "${CMAKE_C_FLAGS}")
59if (IS_WINDOWSFLAG)
a6874708 60 message (STATUS "Info: /D_WINDOWS has been removed from CMAKE_C_FLAGS")
57c28b61 61 string (REGEX REPLACE "/D_WINDOWS" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
62endif()
63
64# remove WIN32 flag if it exists
65string (REGEX MATCH "/DWIN32" IS_WIN32FLAG "${CMAKE_C_FLAGS}")
66if (IS_WIN32FLAG)
a6874708 67 message (STATUS "Info: /DWIN32 has been removed from CMAKE_C_FLAGS")
57c28b61 68 string (REGEX REPLACE "/DWIN32" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
69endif()
70
9f33b387 71# remove DEBUG flag if it exists
72string (REGEX MATCH "-DDEBUG" IS_DEBUG_CXX "${CMAKE_CXX_FLAGS_DEBUG}")
73if (IS_DEBUG_CXX)
a6874708 74 message (STATUS "Info: -DDEBUG has been removed from CMAKE_CXX_FLAGS_DEBUG")
9f33b387 75 string (REGEX REPLACE "-DDEBUG" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
76endif()
77
78string (REGEX MATCH "-DDEBUG" IS_DEBUG_C "${CMAKE_C_FLAGS_DEBUG}")
79if (IS_DEBUG_C)
a6874708 80 message (STATUS "Info: -DDEBUG has been removed from CMAKE_C_FLAGS_DEBUG")
9f33b387 81 string (REGEX REPLACE "-DDEBUG" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
82endif()
9f33b387 83# enable parallel compilation on MSVC 9 and above
1167d6cc 84if (MSVC AND (MSVC_VERSION GREATER 1400))
2e48c1cb 85 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
9f33b387 86endif()
87
88# generate a single response file which enlist all of the object files
27a4b067 89if (NOT DEFINED CMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS)
90 SET(CMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS 1)
91endif()
92if (NOT DEFINED CMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS)
93 SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS 1)
94endif()
95
7dc2c165 96# increase compiler warnings level (-W4 for MSVC, -Wextra for GCC)
9f33b387 97if (MSVC)
98 if (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
99 string (REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
100 else()
101 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
102 endif()
a082e7d0 103elseif (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
7dc2c165 104 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
c2c9890c 105 if (BUILD_SHARED_LIBS)
106 if (APPLE)
107 set (CMAKE_SHARED_LINKER_FLAGS "-lm ${CMAKE_SHARED_LINKER_FLAGS}")
7c65581d 108 elseif(NOT WIN32)
109 set (CMAKE_SHARED_LINKER_FLAGS "-lm ${CMAKE_SHARED_LINKER_FLAGS}")
c2c9890c 110 endif()
c2c9890c 111 endif()
9f33b387 112endif()
113
32b723d5 114if ("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xClang")
115 if (APPLE)
116 # CLang can be used with both libstdc++ and libc++, however on OS X libstdc++ is outdated.
117 set (CMAKE_CXX_FLAGS "-std=c++0x -stdlib=libc++ ${CMAKE_CXX_FLAGS}")
118 elseif(NOT WIN32)
119 # CLang for Windows (at least CLang 8.0 distributed with VS 2019)
120 # does not support option "-std=c++0x"
121 set (CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")
122 endif()
123 # Optimize size of binaries
124 set (CMAKE_SHARED_LINKER_FLAGS "-Wl,-s ${CMAKE_SHARED_LINKER_FLAGS}")
125elseif(MINGW)
a139a353
BB
126 # Set default release optimization option to O2 instead of O3, since in
127 # some OCCT related examples, this gives significantly smaller binaries
128 # at comparable performace with MinGW-w64.
129 string (REGEX MATCH "-O3" IS_O3_CXX "${CMAKE_CXX_FLAGS_RELEASE}")
130 if (IS_O3_CXX)
131 string (REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
132 else()
133 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
134 endif()
135
7c65581d 136 add_definitions(-D_WIN32_WINNT=0x0501)
137 # workaround bugs in mingw with vtable export
138 set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--export-all-symbols")
32b723d5 139
140 # Require C++11
141 set (CMAKE_CXX_FLAGS "-std=gnu++0x ${CMAKE_CXX_FLAGS}")
142 # Optimize size of binaries
143 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")
144 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
dace4784 145elseif (DEFINED CMAKE_COMPILER_IS_GNUCXX)
32b723d5 146 # Require C++11
19286d7c 147 set (CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")
32b723d5 148 # Optimize size of binaries
41ec04db 149 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")
150 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
151endif()
152
6072d597 153if (BUILD_RELEASE_DISABLE_EXCEPTIONS)
154 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNo_Exception")
155 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DNo_Exception")
156endif()