Alpine3D

Alpine3D Svn Source Tree

Root/trunk/tools/cmake/SetCompilerOptions.cmake

1#Set different variables according to the detected compiler and processor
2#based on $CMAKE_CXX_COMPILER_ID it sets the following variables:
3# WARNINGS, EXTRA_WARNINGS, EXTRA, OPTIM, ARCH, DEBUG, _VERSION, PROFILING
4# It can also edit CMAKE_SHARED_LINKER_FLAGS and CMAKE_EXE_LINKER_FLAGS
5
6INCLUDE("${CMAKE_SOURCE_DIR}/tools/cmake/BuildVersion.cmake")
7BuildVersion()
8
9MACRO (SET_COMPILER_OPTIONS)
10SET(USER_COMPILER_OPTIONS "" CACHE STRING "Provide some extra compiler options")
11MARK_AS_ADVANCED(FORCE USER_COMPILER_OPTIONS)
12SET(EXTRA "${EXTRA} ${USER_COMPILER_OPTIONS}")
13
14###########################################################
15IF(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
16SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)#this is required for building libraries
17SET(EXTRA "${EXTRA} /D_USE_MATH_DEFINES")#USE_MATH_DEFINES needed for VC++
18IF(DEBUG_ARITHM)
19SET(EXTRA "${EXTRA} /EHa")
20ENDIF(DEBUG_ARITHM)
21
22#SET(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "limited configs" FORCE)
23SET(WARNINGS "/W4 /D_CRT_SECURE_NO_WARNINGS /EHsc") #Za: strict ansi EHsc: handle c++ exceptions /w35045: inform about Spectre mitigation
24#SET(EXTRA_WARNINGS "/Wp64") #/Wall
25SET(OPTIM "/O2 /DNDEBUG /DEBUG:FASTLINK /MD /DNOSAFECHECKS")
26SET(ARCH_OPTIM "/arch:AVX2")
27SET(ARCH_SAFE "")
28IF(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")
29SET(ARCH_SAFE "/arch:SSE2")
30ENDIF()
31SET(DEBUG "/Z7 /Od /D__DEBUG /MDd")
32SET(_VERSION "/D_VERSION=${_versionString}")
33
34###########################################################
35ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL Intel)
36IF(ENABLE_LAPACK)
37SET(EXTRA "${EXTRA} -DCLAPACK")
38ENDIF(ENABLE_LAPACK)
39IF(DEBUG_ARITHM)
40SET(EXTRA "${EXTRA} -DDEBUG_ARITHM")
41ENDIF(DEBUG_ARITHM)
42
43SET(WARNINGS_OFF "-Wno-long-long -Wno-unknown-pragmas -wd2015,11071")
44SET(WARNINGS "-Wall -Wswitch ${WARNINGS_OFF}")
45SET(DEEP_WARNINGS "-Wshadow -Wpointer-arith -Wconversion -Winline -Wdisabled-optimization") #-Wfloat-equal -Wpadded
46SET(EXTRA_WARNINGS "-Wextra -pedantic ${DEEP_WARNINGS}")
47SET(OPTIM "-g -O3 -DNDEBUG -DNOSAFECHECKS")
48IF(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")
49SET(ARCH_SAFE "-march=nocona -mtune=nocona")
50SET(ARCH_OPTIM "-march=native -mtune=native")
51ENDIF()
52SET(DEBUG "-g3 -O0 -D__DEBUG")
53SET(_VERSION "-D_VERSION=${_versionString}")
54
55###########################################################
56ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL Cray)
57IF(ENABLE_LAPACK)
58SET(EXTRA "${EXTRA} -DCLAPACK")
59ENDIF(ENABLE_LAPACK)
60IF(DEBUG_ARITHM)
61SET(EXTRA "${EXTRA} -DDEBUG_ARITHM")
62ENDIF(DEBUG_ARITHM)
63
64SET(WARNINGS "-hlist=m -h negmsgs -h msglevel_3 -h nomessage=870") #870: accept multibyte chars
65#SET(EXTRA_WARNINGS "-h msglevel_2")
66SET(OPTIM "-O3 -hfp3 -h msglevel_4 -DNDEBUG -DNOSAFECHECKS")
67IF($ENV{CRAY_CPU_TARGET} MATCHES "^$")
68IF(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")
69SET(ARCH_SAFE "-h cpu=x86-64")
70MESSAGE("No CRAY_CPU_TARGET set, setting it to x86-64; please consider loading the proper target module.")
71ELSE()
72MESSAGE("No CRAY_CPU_TARGET set; please consider loading the proper target module.")
73ENDIF()
74ENDIF()
75SET(DEBUG "-g -D__DEBUG")
76SET(_VERSION "-D_VERSION=${_versionString}")
77
78###########################################################
79ELSEIF(CMAKE_CXX_COMPILER_ID MATCHES "^GNU$")
80#we consider that all other compilers support "-" options and silently ignore what they don't know
81IF(ENABLE_LAPACK)
82SET(EXTRA "${EXTRA} -DCLAPACK")
83ENDIF(ENABLE_LAPACK)
84IF(WIN32)
85LIST(APPEND CFLAGS " -D_USE_MATH_DEFINES") #USE_MATH_DEFINES needed for Win32
86ENDIF(WIN32)
87IF(DEBUG_ARITHM)
88SET(EXTRA "${EXTRA} -DDEBUG_ARITHM")
89ENDIF(DEBUG_ARITHM)
90
91SET(WARNINGS "-Wall -Wno-long-long -Wno-unknown-pragmas -Wswitch")
92SET(DEEP_WARNINGS "-Wunused-value -Wshadow -Wpointer-arith -Winline -Wdisabled-optimization -Wctor-dtor-privacy") #-Wfloat-equal -Wpadded -Wconversion
93SET(EXTRA_WARNINGS "-Wextra -pedantic ${DEEP_WARNINGS}") #-Weffc++
94SET(OPTIM "-g -O3 -DNDEBUG -DNOSAFECHECKS")
95IF(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")
96SET(ARCH_SAFE "-march=nocona -mtune=nocona")
97ENDIF()
98SET(DEBUG "-g3 -O0 -D__DEBUG")
99SET(_VERSION "-D_VERSION=${_versionString}")
100
101SET(PROFILING "-pg -fprofile-arcs") #add ${PROFILING} to the CFLAGS when necessary
102SET(EXTRA_WARNINGS "${EXTRA_WARNINGS} -Wunsafe-loop-optimizations -Wwrite-strings")
103IF(NOT ANDROID)
104SET(EXTRA_WARNINGS "${EXTRA_WARNINGS} -ansi")
105IF(WIN32) #for gcc on windows
106SET(CMAKE_SHARED_LINKER_FLAGS "--enable-auto-import")
107SET(CMAKE_EXE_LINKER_FLAGS "--enable-auto-import")
108#SET(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstdc++ -lwsock32 -lws2_32 ${CMAKE_CXX_STANDARD_LIBRARIES}")
109#SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive")
110ENDIF(WIN32)
111ENDIF(NOT ANDROID)
112EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
113IF(GCC_VERSION VERSION_GREATER 4.2 OR GCC_VERSION VERSION_EQUAL 4.2)
114SET(ARCH_OPTIM "-march=native -mtune=native")
115ENDIF()
116IF(GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
117SET(EXTRA_WARNINGS "${EXTRA_WARNINGS} -Wvector-operation-performance") #for gcc>=4.7.0
118IF(NOT WIN32)
119#for gcc>4.5, but first implementations were slow, so it is safe to enforce 4.8
120FIND_PROGRAM(CMAKE_GCC_AR NAMES ${_CMAKE_TOOLCHAIN_PREFIX}gcc-ar${_CMAKE_TOOLCHAIN_SUFFIX} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
121FIND_PROGRAM(CMAKE_GCC_NM NAMES ${_CMAKE_TOOLCHAIN_PREFIX}gcc-nm HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
122FIND_PROGRAM(CMAKE_GCC_RANLIB NAMES ${_CMAKE_TOOLCHAIN_PREFIX}gcc-ranlib HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
123
124IF(CMAKE_GCC_AR AND CMAKE_GCC_NM AND CMAKE_GCC_RANLIB)
125SET(USE_LTO_OPTIMIZATIONS ON CACHE BOOL "Use Link Time Optmizations when compiling (memory heavy while compiling)")
126MARK_AS_ADVANCED(FORCE USE_LTO_OPTIMIZATIONS)
127IF(USE_LTO_OPTIMIZATIONS)
128SET(OPTIM "${OPTIM} -flto")
129ENDIF()
130SET( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto -fno-fat-lto-objects" )
131SET( CMAKE_AR "${CMAKE_GCC_AR}" )
132SET( CMAKE_NM "${CMAKE_GCC_NM}" )
133SET( CMAKE_RANLIB "${CMAKE_GCC_RANLIB}" )
134ELSE()
135MESSAGE( WARNING "GCC indicates LTO support, but binutils wrappers could not be found. Disabling LTO." )
136ENDIF()
137ENDIF(NOT WIN32)
138#if set to ON, all binaries depending on the library have to be compiled the same way.
139#Then, do an "export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4" and run with "ASAN_OPTIONS=symbolize=1"
140SET(LEAKS_CHECK OFF CACHE BOOL "Set to ON to dynamically check for memory corruption (and do the same for applications linked with MeteoIO)")
141IF (LEAKS_CHECK)
142SET(EXTRA "${EXTRA} -fsanitize=address -fno-omit-frame-pointer")
143ENDIF(LEAKS_CHECK)
144ENDIF()
145
146IF(GCC_VERSION VERSION_GREATER 5.0 OR GCC_VERSION VERSION_EQUAL 5.0)
147IF(PLUGIN_IMISIO) #HACK: current OCCI does not support the short strings optimizations of gcc>=5
148SET(EXTRA "-D_GLIBCXX_USE_CXX11_ABI=0 ${EXTRA}")
149ELSE(PLUGIN_IMISIO)
150SET(FORCE_C03_ABI OFF CACHE BOOL "Compatibility with Oracle's OCCI")
151IF (FORCE_C03_ABI)
152SET(EXTRA "-D_GLIBCXX_USE_CXX11_ABI=0 ${EXTRA}")
153ENDIF(FORCE_C03_ABI)
154ENDIF(PLUGIN_IMISIO)
155ENDIF()
156
157###########################################################
158ELSEIF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
159IF(ENABLE_LAPACK)
160SET(EXTRA "${EXTRA} -DCLAPACK")
161ENDIF(ENABLE_LAPACK)
162IF(WIN32)
163LIST(APPEND CFLAGS " -D_USE_MATH_DEFINES") #USE_MATH_DEFINES needed for Win32
164ENDIF(WIN32)
165IF(DEBUG_ARITHM)
166SET(EXTRA "${EXTRA} -DDEBUG_ARITHM")
167ENDIF(DEBUG_ARITHM)
168
169SET(WARNINGS_OFF "-Wno-long-long -Wno-float-equal -Wno-documentation -Wno-documentation-unknown-command -Wno-old-style-cast -Wno-padded -Wno-missing-noreturn -Wno-weak-vtables -Wno-switch-enum -Wno-covered-switch-default -Wno-global-constructors -Wno-exit-time-destructors -Wno-unknown-pragmas -Wno-format-nonliteral -Wno-date-time -Wno-unused-template -Wno-disabled-macro-expansion")
170SET(WARNINGS "-Wall -Wswitch -Weverything ${WARNINGS_OFF}") #obviously, we should try to fix the warnings! Keeping in mind that some of these W are half buggy...
171SET(DEEP_WARNINGS "-Wunused-value -Wshadow -Wpointer-arith -Wconversion -Winline -Wdisabled-optimization -Wctor-dtor-privacy") #-Rpass=.* for static analysis
172SET(EXTRA_WARNINGS "-Wextra -pedantic -Weffc++ ${DEEP_WARNINGS}")
173SET(OPTIM "-g -O3 -DNDEBUG -DNOSAFECHECKS -flto")
174IF(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")
175SET(ARCH_SAFE "-march=nocona -mtune=nocona")
176ENDIF()
177SET(DEBUG "-g3 -O0 -D__DEBUG")
178SET(_VERSION "-D_VERSION=${_versionString}")
179
180SET(PROFILING "-pg") #add ${PROFILING} to the CFLAGS when necessary
181SET(EXTRA "${EXTRA} -fcolor-diagnostics") #-fapple-pragma-pack does not seems necessary; -ftrapv should be replaced by sanitize=integer
182SET(LEAKS_CHECK OFF CACHE BOOL "Set to ON to dynamically check for memory corruption (and do the same for applications linked with MeteoIO)")
183IF (LEAKS_CHECK)
184SET(EXTRA "${EXTRA} -ftrapv -fno-omit-frame-pointer") #-fsanitize=address,undefined,integer,undefined-trap but this is currently not supported by Apple
185ENDIF(LEAKS_CHECK)
186SET(ARCH_OPTIM "-march=native")
187ENDIF()
188
189###########################################################
190#targets providing SETs of compiler options
191IF(NOT DEST)
192SET(DEST "safe" CACHE STRING "Choose safe or optimized" FORCE)
193ENDIF(NOT DEST)
194
195IF (DEST STREQUAL "safe")
196SET(ARCH "${ARCH_SAFE}")
197ENDIF(DEST STREQUAL "safe")
198
199IF(DEST STREQUAL "optimized")
200SET(ARCH "${ARCH_OPTIM}")
201ENDIF(DEST STREQUAL "optimized")
202
203#show exception messages in a graphical message box
204#SET(GUI_EXCEPTIONS OFF CACHE BOOL "Show a message box with exceptions texts ON or OFF")
205
206ENDMACRO (SET_COMPILER_OPTIONS)

Archive Download this file

Revision: HEAD