WSL/SLF GitLab Repository

SetCompilerOptions.cmake 7.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#Set different variables according to the detected compiler and processor
#based on $CMAKE_CXX_COMPILER_ID it sets the following variables:
# WARNINGS, EXTRA_WARNINGS, EXTRA, OPTIM, ARCH, DEBUG, _VERSION, PROFILING
# It can also edit CMAKE_SHARED_LINKER_FLAGS and CMAKE_EXE_LINKER_FLAGS

INCLUDE("${CMAKE_SOURCE_DIR}/tools/cmake/BuildVersion.cmake")
BuildVersion()

MACRO (SET_COMPILER_OPTIONS)
	###########################################################
	IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
12
13
14
15
		IF(DEBUG_ARITHM)
			SET(EXTRA "${EXTRA} /EHa")
		ENDIF(DEBUG_ARITHM)
		
16
17
18
19
20
21
22
23
24
25
26
27
28
29
		#SET(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "limited configs"  FORCE)
		SET(WARNINGS "/W4 /D_CRT_SECURE_NO_WARNINGS /EHsc") #Za: strict ansi EHsc: handle c++ exceptions
		#SET(EXTRA_WARNINGS "/Wp64") #/Wall
		SET(OPTIM "/O2 /DNDEBUG /MD /DNOSAFECHECKS")
		SET(ARCH_OPTIM "/arch:SSE2")
		SET(ARCH_SAFE "")
		SET(DEBUG "/Z7 /Od /D__DEBUG /MDd")
		SET(_VERSION "/D_VERSION=${_versionString}")
		IF(BUILD_SHARED_LIBS)
			ADD_DEFINITIONS(/DMIO_DLL)
		ENDIF(BUILD_SHARED_LIBS)
		
	###########################################################
	ELSEIF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
30
31
32
33
34
35
36
		IF(ENABLE_LAPACK)
			SET(EXTRA "${EXTRA} -DCLAPACK")
		ENDIF(ENABLE_LAPACK)
		IF(DEBUG_ARITHM)
			SET(EXTRA "${EXTRA} -DDEBUG_ARITHM")
		ENDIF(DEBUG_ARITHM)
		
37
38
39
40
41
42
43
44
45
46
47
48
		SET(WARNINGS "-Wall -Wno-long-long  -Wswitch")
		SET(DEEP_WARNINGS "-Wshadow -Wpointer-arith -Wconversion -Winline -Wdisabled-optimization") #-Wfloat-equal -Wpadded
		SET(EXTRA_WARNINGS "-Wextra -pedantic ${DEEP_WARNINGS}")
		SET(OPTIM "-g -O3 -DNDEBUG -DNOSAFECHECKS")
		IF("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "x86_64" OR "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "AMD64")
			SET(ARCH_SAFE  "-march=nocona -mtune=nocona")
		ENDIF()
		SET(DEBUG "-g3 -O0 -D__DEBUG")
		SET(_VERSION "-D_VERSION=${_versionString}")
		
	###########################################################
	ELSEIF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Cray")
49
50
51
52
53
54
55
		IF(ENABLE_LAPACK)
			SET(EXTRA "${EXTRA} -DCLAPACK")
		ENDIF(ENABLE_LAPACK)
		IF(DEBUG_ARITHM)
			SET(EXTRA "${EXTRA} -DDEBUG_ARITHM")
		ENDIF(DEBUG_ARITHM)
		
56
57
58
59
60
61
62
63
64
65
		SET(WARNINGS "-hlist=m -h negmsgs -h msglevel_3 -h nomessage=870") #870: accept multibyte chars
		#SET(EXTRA_WARNINGS "-h msglevel_2")
		SET(OPTIM "-O3 -hfp3 -h msglevel_4 -DNDEBUG -DNOSAFECHECKS")
		IF("$ENV{CRAY_CPU_TARGET}" STREQUAL "")
			IF("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "x86_64" OR "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "AMD64")
				SET(ARCH_SAFE  "-h cpu=x86-64")
				MESSAGE("No CRAY_CPU_TARGET set, setting it to x86-64; please consider loading the proper target module.")
			ELSE()
				MESSAGE("No CRAY_CPU_TARGET set; please consider loading the proper target module.")
			ENDIF()
66
		ENDIF()
67
		SET(DEBUG "-g -D__DEBUG")
68
		SET(_VERSION "-D_VERSION=${_versionString}")
69
	
70
71
72
	###########################################################
	ELSEIF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
		#we consider that all other compilers support "-" options and silently ignore what they don't know
73
74
75
76
77
78
79
80
81
82
		IF(ENABLE_LAPACK)
			SET(EXTRA "${EXTRA} -DCLAPACK")
		ENDIF(ENABLE_LAPACK)
		IF(WIN32)
			LIST(APPEND CFLAGS " -D_USE_MATH_DEFINES") #USE_MATH_DEFINES needed for Win32
		ENDIF(WIN32)
		IF(DEBUG_ARITHM)
			SET(EXTRA "${EXTRA} -DDEBUG_ARITHM")
		ENDIF(DEBUG_ARITHM)
		
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
		SET(WARNINGS "-Wall -Wno-long-long  -Wswitch")
		SET(DEEP_WARNINGS "-Wunused-value -Wshadow -Wpointer-arith -Wconversion -Winline -Wdisabled-optimization -Wctor-dtor-privacy") #-Wfloat-equal -Wpadded
		SET(EXTRA_WARNINGS "-Wextra -pedantic -Weffc++ ${DEEP_WARNINGS}")
		SET(OPTIM "-g -O3 -DNDEBUG -DNOSAFECHECKS")
		IF("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "x86_64" OR "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "AMD64")
			SET(ARCH_SAFE  "-march=nocona -mtune=nocona")
		ENDIF()
		SET(DEBUG "-g3 -O0 -D__DEBUG")
		SET(_VERSION "-D_VERSION=${_versionString}")
		#IF(BUILD_SHARED_LIBS)
		#	ADD_DEFINITIONS(-DMIO_DLL)
		#ENDIF(BUILD_SHARED_LIBS)
		
		SET(PROFILING "-pg -fprofile-arcs") #add ${PROFILING} to the CFLAGS when necessary
		SET(EXTRA_WARNINGS "${EXTRA_WARNINGS} -Wunsafe-loop-optimizations -Wvector-operation-performance -Wwrite-strings")
		IF(NOT ANDROID)
			SET(EXTRA_WARNINGS "${EXTRA_WARNINGS} -ansi")
			IF(WIN32) #for gcc on windows
				SET(CMAKE_SHARED_LINKER_FLAGS "--enable-auto-import")
				SET(CMAKE_EXE_LINKER_FLAGS "--enable-auto-import")
			ENDIF(WIN32)
		ENDIF(NOT ANDROID)
		EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
		IF(GCC_VERSION VERSION_GREATER 4.2 OR GCC_VERSION VERSION_EQUAL 4.2)
			SET(ARCH_OPTIM  "-march=native -mtune=native")
		ENDIF()
		IF(GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
			IF(NOT WIN32)
				SET(OPTIM "${OPTIM} -flto") #for gcc>4.5, but first implementations were slow, so it is safe to enforce 4.8
			ENDIF(NOT WIN32)
113
			#if set to ON, all binaries depending on the library have to be compiled the same way.
114
115
116
117
118
119
			#Then, do an "export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4" and run with "ASAN_OPTIONS=symbolize=1"
			SET(LEAKS_CHECK OFF CACHE BOOL "Set to ON to dynamically check for memory corruption (and do the same for applications linked with MeteoIO)")
			IF (LEAKS_CHECK)
				SET(EXTRA "${EXTRA} -fsanitize=address -fno-omit-frame-pointer")
			ENDIF(LEAKS_CHECK)
		ENDIF()
120
121
122
123
124
125
126

		IF(GCC_VERSION VERSION_GREATER 5.0 OR GCC_VERSION VERSION_EQUAL 5.0)
			IF (PLUGIN_IMISIO) #HACK: current OCCI does not support the short strings optimizations of gcc>=5
				SET(EXTRA "-D_GLIBCXX_USE_CXX11_ABI=0 ${EXTRA}")
			ENDIF(PLUGIN_IMISIO)
		ENDIF()

127
128
	###########################################################
	ELSEIF("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
129
130
131
132
133
134
135
136
137
138
		IF(ENABLE_LAPACK)
			SET(EXTRA "${EXTRA} -DCLAPACK")
		ENDIF(ENABLE_LAPACK)
		IF(WIN32)
			LIST(APPEND CFLAGS " -D_USE_MATH_DEFINES") #USE_MATH_DEFINES needed for Win32
		ENDIF(WIN32)
		IF(DEBUG_ARITHM)
			SET(EXTRA "${EXTRA} -DDEBUG_ARITHM")
		ENDIF(DEBUG_ARITHM)
		
139
		SET(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")
140
		SET(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...
Mathias Bavay's avatar
Mathias Bavay committed
141
		SET(DEEP_WARNINGS "-Wunused-value -Wshadow -Wpointer-arith -Wconversion -Winline -Wdisabled-optimization -Wctor-dtor-privacy") #-Rpass=.* for static analysis
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
		SET(EXTRA_WARNINGS "-Wextra -pedantic -Weffc++ ${DEEP_WARNINGS}")
		SET(OPTIM "-g -O3 -DNDEBUG -DNOSAFECHECKS -flto")
		IF("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "x86_64" OR "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "AMD64")
			SET(ARCH_SAFE  "-march=nocona -mtune=nocona")
		ENDIF()
		SET(DEBUG "-g3 -O0 -D__DEBUG")
		SET(_VERSION "-D_VERSION=${_versionString}")
		
		SET(PROFILING "-pg") #add ${PROFILING} to the CFLAGS when necessary
		SET(EXTRA "${EXTRA} -fcolor-diagnostics") #-fapple-pragma-pack does not seems necessary; -ftrapv should be replaced by sanitize=integer
		SET(LEAKS_CHECK OFF CACHE BOOL "Set to ON to dynamically check for memory corruption (and do the same for applications linked with MeteoIO)")
			IF (LEAKS_CHECK)
				SET(EXTRA "${EXTRA} -ftrapv -fno-omit-frame-pointer") #-fsanitize=address,undefined,integer,undefined-trap but this is currently not supported by Apple
			ENDIF(LEAKS_CHECK)
		SET(ARCH_OPTIM  "-march=native")
	ENDIF()
	
	###########################################################
	#targets providing SETs of compiler options
	IF(NOT DEST)
		SET(DEST "safe" CACHE STRING "Choose safe or optimized" FORCE)
	ENDIF(NOT DEST)

	IF (DEST STREQUAL "safe")
		SET(ARCH  "${ARCH_SAFE}")
	ENDIF(DEST STREQUAL "safe")

	IF(DEST STREQUAL "optimized")
		SET(ARCH  "${ARCH_OPTIM}")
	ENDIF(DEST STREQUAL "optimized")

	#show exception messages in a graphical message box
	SET(GUI_EXCEPTIONS OFF CACHE BOOL "Show a message box with exceptions texts ON or OFF")

ENDMACRO (SET_COMPILER_OPTIONS)