Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / cmake / platform.cmake
1 #===============================================================================
2 # Copyright 2016-2018 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #===============================================================================
16
17 # Manage platform-specific quirks
18 #===============================================================================
19
20 if(platform_cmake_included)
21     return()
22 endif()
23 set(platform_cmake_included true)
24
25 include("cmake/utils.cmake")
26
27 add_definitions(-DMKLDNN_DLL -DMKLDNN_DLL_EXPORTS)
28
29 # UNIT8_MAX-like macros are a part of the C99 standard and not a part of the
30 # C++ standard (see C99 standard 7.18.2 and 7.18.4)
31 add_definitions(-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS)
32
33 set(CMAKE_CCXX_FLAGS)
34 set(CMAKE_CCXX_NOWARN_FLAGS)
35 set(DEF_ARCH_OPT_FLAGS)
36
37 if(MSVC)
38     set(USERCONFIG_PLATFORM "x64")
39     if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
40         append(CMAKE_CCXX_FLAGS "/MP")
41         # int -> bool
42         append(CMAKE_CCXX_NOWARN_FLAGS "/wd4800")
43         # unknown pragma
44         append(CMAKE_CCXX_NOWARN_FLAGS "/wd4068")
45         # double -> float
46         append(CMAKE_CCXX_NOWARN_FLAGS "/wd4305")
47         # UNUSED(func)
48         append(CMAKE_CCXX_NOWARN_FLAGS "/wd4551")
49     endif()
50     if(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
51         append(CMAKE_CCXX_FLAGS "/MP")
52         set(DEF_ARCH_OPT_FLAGS "-QxHOST")
53         # disable: loop was not vectorized with "simd"
54         append(CMAKE_CCXX_NOWARN_FLAGS "-Qdiag-disable:15552")
55         # disable: unknown pragma
56         append(CMAKE_CCXX_NOWARN_FLAGS "-Qdiag-disable:3180")
57     endif()
58     if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
59         # Clang cannot vectorize some loops with #pragma omp simd and gets
60         # very upset. Tell it that it's okay and that we love it
61         # unconditionally.
62         append(CMAKE_CCXX_FLAGS "-Wno-pass-failed")
63     endif()
64 elseif(UNIX OR MINGW)
65     append(CMAKE_CCXX_FLAGS "-Wall -Wno-unknown-pragmas")
66     append_if_product(CMAKE_CCXX_FLAGS "-Werror")
67     append(CMAKE_CCXX_FLAGS "-fvisibility=internal")
68     append(CMAKE_C_FLAGS "-std=c99")
69     append(CMAKE_CXX_FLAGS "-std=c++11 -fvisibility-inlines-hidden")
70     # compiler specific settings
71     if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
72         # Clang cannot vectorize some loops with #pragma omp simd and gets
73         # very upset. Tell it that it's okay and that we love it
74         # unconditionally.
75         append(CMAKE_CCXX_NOWARN_FLAGS "-Wno-pass-failed")
76         if(MKLDNN_USE_CLANG_SANITIZER MATCHES "Memory(WithOrigin)?")
77             if(NOT MKLDNN_THREADING STREQUAL "SEQ")
78                 message(WARNING "Clang OpenMP is not compatible with MSan! "
79                     "Expect a lot of false positives!")
80             endif()
81             append(CMAKE_CCXX_SANITIZER_FLAGS "-fsanitize=memory")
82             if(MKLDNN_USE_CLANG_SANITIZER STREQUAL "MemoryWithOrigin")
83                 append(CMAKE_CCXX_SANITIZER_FLAGS
84                     "-fsanitize-memory-track-origins=2")
85                 append(CMAKE_CCXX_SANITIZER_FLAGS
86                     "-fno-omit-frame-pointer")
87             endif()
88             set(MKLDNN_ENABLED_CLANG_SANITIZER "${MKLDNN_USE_CLANG_SANITIZER}")
89         elseif(MKLDNN_USE_CLANG_SANITIZER STREQUAL "Undefined")
90             append(CMAKE_CCXX_SANITIZER_FLAGS "-fsanitize=undefined")
91             append(CMAKE_CCXX_SANITIZER_FLAGS
92                 "-fno-sanitize=function,vptr")  # work around linking problems
93             append(CMAKE_CCXX_SANITIZER_FLAGS "-fno-omit-frame-pointer")
94             set(MKLDNN_ENABLED_CLANG_SANITIZER "${MKLDNN_USE_CLANG_SANITIZER}")
95         elseif(MKLDNN_USE_CLANG_SANITIZER STREQUAL "Address")
96             append(CMAKE_CCXX_SANITIZER_FLAGS "-fsanitize=address")
97             set(MKLDNN_ENABLED_CLANG_SANITIZER "${MKLDNN_USE_CLANG_SANITIZER}")
98         elseif(MKLDNN_USE_CLANG_SANITIZER STREQUAL "Thread")
99             append(CMAKE_CCXX_SANITIZER_FLAGS "-fsanitize=thread")
100             set(MKLDNN_ENABLED_CLANG_SANITIZER "${MKLDNN_USE_CLANG_SANITIZER}")
101         elseif(MKLDNN_USE_CLANG_SANITIZER STREQUAL "Leak")
102             append(CMAKE_CCXX_SANITIZER_FLAGS "-fsanitize=leak")
103             set(MKLDNN_ENABLED_CLANG_SANITIZER "${MKLDNN_USE_CLANG_SANITIZER}")
104         elseif(NOT MKLDNN_USE_CLANG_SANITIZER STREQUAL "")
105             message(FATAL_ERROR
106                 "Unsupported Clang sanitizer '${MKLDNN_USE_CLANG_SANITIZER}'")
107         endif()
108         if(MKLDNN_ENABLED_CLANG_SANITIZER)
109             message(STATUS
110                 "Using Clang ${MKLDNN_ENABLED_CLANG_SANITIZER} "
111                 "sanitizer (experimental!)")
112             append(CMAKE_CCXX_SANITIZER_FLAGS "-g -fno-omit-frame-pointer")
113         endif()
114     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
115         if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
116             set(DEF_ARCH_OPT_FLAGS "-march=native -mtune=native")
117         endif()
118         # suppress warning on assumptions made regarding overflow (#146)
119         append(CMAKE_CCXX_NOWARN_FLAGS "-Wno-strict-overflow")
120     elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
121         set(DEF_ARCH_OPT_FLAGS "-xHOST")
122         # workaround for Intel Compiler 16.0 that produces error caused
123         # by pragma omp simd collapse(..)
124         if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "17.0")
125             append(CMAKE_CCXX_NOWARN_FLAGS "-diag-disable:13379")
126         endif()
127         append(CMAKE_CCXX_NOWARN_FLAGS "-diag-disable:15552")
128         # disable `was not vectorized: vectorization seems inefficient` remark
129         append(CMAKE_CCXX_NOWARN_FLAGS "-diag-disable:15335")
130     endif()
131 endif()
132
133 if(UNIX OR MINGW)
134     if(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
135         # Link Intel libraries statically (except for iomp5)
136         append(CMAKE_SHARED_LINKER_FLAGS "-liomp5 -static-intel")
137         # Tell linker to not complain about missing static libraries
138         append(CMAKE_SHARED_LINKER_FLAGS "-diag-disable:10237")
139     endif()
140 endif()
141
142 if(ARCH_OPT_FLAGS STREQUAL "HostOpts")
143     set(ARCH_OPT_FLAGS "${DEF_ARCH_OPT_FLAGS}")
144 endif()
145
146 append(CMAKE_C_FLAGS "${CMAKE_CCXX_FLAGS} ${ARCH_OPT_FLAGS}")
147 append(CMAKE_CXX_FLAGS "${CMAKE_CCXX_FLAGS} ${ARCH_OPT_FLAGS}")
148
149 if(APPLE)
150     set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
151     # FIXME: this is ugly but required when compiler does not add its library
152     # paths to rpath (like Intel compiler...)
153     foreach(_ ${CMAKE_C_IMPLICIT_LINK_DIRECTORIES})
154         set(_rpath "-Wl,-rpath,${_}")
155         append(CMAKE_SHARED_LINKER_FLAGS "${_rpath}")
156         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${_rpath}")
157     endforeach()
158 endif()