0027196: CMake - avoid setting option -EHa for compilers other than MSVC
[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
cb728702 8# force option -fp:precise for Visual Studio projects.
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)
100592c1 15 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fp:precise")
16 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fp:precise")
9f33b387 17endif()
18
19# set compiler short name and choose SSE2 option for appropriate MSVC compilers
20# ONLY for 32-bit
21if (NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
22 if (MSVC80 OR MSVC90 OR MSVC10)
100592c1 23 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:SSE2")
24 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:SSE2")
9f33b387 25 endif()
26endif()
27
9f33b387 28if (WIN32)
fe9b8ff2 29 add_definitions (-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE)
9f33b387 30else()
100592c1 31 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -fPIC")
32 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexceptions -fPIC")
33 add_definitions(-DOCC_CONVERT_SIGNALS)
9f33b387 34endif()
35
36# enable structured exceptions for MSVC
37string (REGEX MATCH "EHsc" ISFLAG "${CMAKE_CXX_FLAGS}")
38if (ISFLAG)
39 string (REGEX REPLACE "EHsc" "EHa" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
cb728702 40elseif (MSVC)
9f33b387 41 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -EHa")
42endif()
43
57c28b61 44# remove _WINDOWS flag if it exists
45string (REGEX MATCH "/D_WINDOWS" IS_WINDOWSFLAG "${CMAKE_CXX_FLAGS}")
46if (IS_WINDOWSFLAG)
a6874708 47 message (STATUS "Info: /D_WINDOWS has been removed from CMAKE_CXX_FLAGS")
57c28b61 48 string (REGEX REPLACE "/D_WINDOWS" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
49endif()
50
51# remove WIN32 flag if it exists
52string (REGEX MATCH "/DWIN32" IS_WIN32FLAG "${CMAKE_CXX_FLAGS}")
53if (IS_WIN32FLAG)
a6874708 54 message (STATUS "Info: /DWIN32 has been removed from CMAKE_CXX_FLAGS")
57c28b61 55 string (REGEX REPLACE "/DWIN32" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
56endif()
57
58# remove _WINDOWS flag if it exists
59string (REGEX MATCH "/D_WINDOWS" IS_WINDOWSFLAG "${CMAKE_C_FLAGS}")
60if (IS_WINDOWSFLAG)
a6874708 61 message (STATUS "Info: /D_WINDOWS has been removed from CMAKE_C_FLAGS")
57c28b61 62 string (REGEX REPLACE "/D_WINDOWS" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
63endif()
64
65# remove WIN32 flag if it exists
66string (REGEX MATCH "/DWIN32" IS_WIN32FLAG "${CMAKE_C_FLAGS}")
67if (IS_WIN32FLAG)
a6874708 68 message (STATUS "Info: /DWIN32 has been removed from CMAKE_C_FLAGS")
57c28b61 69 string (REGEX REPLACE "/DWIN32" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
70endif()
71
9f33b387 72# remove DEBUG flag if it exists
73string (REGEX MATCH "-DDEBUG" IS_DEBUG_CXX "${CMAKE_CXX_FLAGS_DEBUG}")
74if (IS_DEBUG_CXX)
a6874708 75 message (STATUS "Info: -DDEBUG has been removed from CMAKE_CXX_FLAGS_DEBUG")
9f33b387 76 string (REGEX REPLACE "-DDEBUG" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
77endif()
78
79string (REGEX MATCH "-DDEBUG" IS_DEBUG_C "${CMAKE_C_FLAGS_DEBUG}")
80if (IS_DEBUG_C)
a6874708 81 message (STATUS "Info: -DDEBUG has been removed from CMAKE_C_FLAGS_DEBUG")
9f33b387 82 string (REGEX REPLACE "-DDEBUG" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
83endif()
9f33b387 84# enable parallel compilation on MSVC 9 and above
85if (MSVC AND NOT MSVC70 AND NOT MSVC80)
86 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -MP")
87endif()
88
89# generate a single response file which enlist all of the object files
90SET(CMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS 1)
91SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS 1)
7dc2c165 92# increase compiler warnings level (-W4 for MSVC, -Wextra for GCC)
9f33b387 93if (MSVC)
94 if (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
95 string (REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
96 else()
97 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
98 endif()
a082e7d0 99elseif (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
7dc2c165 100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
c2c9890c 101 if (BUILD_SHARED_LIBS)
102 if (APPLE)
103 set (CMAKE_SHARED_LINKER_FLAGS "-lm ${CMAKE_SHARED_LINKER_FLAGS}")
104 else()
105 set (CMAKE_SHARED_LINKER_FLAGS "-lm -z defs ${CMAKE_SHARED_LINKER_FLAGS}")
106 endif()
107 else()
108 if (NOT ANDROID)
109 set (CMAKE_STATIC_LINKER_FLAGS "-lm ${CMAKE_SHARED_STATIC_FLAGS}")
110 endif()
111 endif()
9f33b387 112endif()
113
19286d7c 114if (DEFINED CMAKE_COMPILER_IS_GNUCXX OR "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xClang")
115 set (CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")
116endif()
117
41ec04db 118# Optimize size of binaries
4d8eca1d 119if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR MINGW)
41ec04db 120 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")
121 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
122endif()
123
9f33b387 124set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNo_Exception")
57c28b61 125set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DNo_Exception")