Salome HOME
[CEA 40693] COTECH 156.5: Check usage of C++17 as default standard for SALOME’s modules
[tools/configuration.git] / cmake / SalomeSetupPlatform.cmake
1 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 INCLUDE(CheckCXXCompilerFlag)
21
22 ## Detect architecture
23 IF(WIN32)
24   SET(MACHINE WINDOWS)
25 ELSE()
26   SET(MACHINE PCLINUX)
27 ENDIF()
28
29 ## Test for 64 bits
30 IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
31   SET(MACHINE_IS_64 TRUE)
32 ELSE()
33   SET(MACHINE_IS_64 FALSE)
34 ENDIF()
35
36 ## Force CMAKE_BUILD_TYPE to Release if not set
37 IF(NOT CMAKE_BUILD_TYPE)
38   SET(CMAKE_BUILD_TYPE $ENV{CMAKE_BUILD_TYPE})
39 ENDIF(NOT CMAKE_BUILD_TYPE)
40 IF(NOT CMAKE_BUILD_TYPE)
41   SET(CMAKE_BUILD_TYPE Release)
42 ENDIF(NOT CMAKE_BUILD_TYPE)
43
44 ## Define the log level according to the build type
45 IF(CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR CMAKE_BUILD_TYPE STREQUAL "Debug")
46   SET(PYLOGLEVEL DEBUG)
47 ELSE()
48   SET(PYLOGLEVEL WARNING)
49 ENDIF()
50
51 IF(WIN32)
52   ## Windows specific:  
53   ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)  # To disable windows warnings for strcpy, fopen, ...
54   ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)  # To disable windows warnings generated by checked iterators(e.g. std::copy, std::transform, ...)
55   ADD_DEFINITIONS(-DWNT -DWIN32)
56   ADD_DEFINITIONS(-D_WIN32_WINNT=0x0500)      # Windows 2000 or later API is required
57   ADD_DEFINITIONS(-DPPRO_NT)                  # For medfile
58   ADD_DEFINITIONS(-DNOMINMAX)
59
60   SET(PLATFORM_LIBS Ws2_32.lib)
61   LIST(APPEND PLATFORM_LIBS Userenv.lib)      # At least for GEOM suit
62
63   IF(MACHINE_IS_64)
64     SET(SIZE_OF_LONG 4)                       # Set sizeof(long) to 4 bytes
65   ELSE()
66     SET(SIZE_OF_LONG ${CMAKE_SIZEOF_VOID_P})  # Set sizeof(long) the same as size of pointers
67   ENDIF()
68   ADD_DEFINITIONS(-DUNICODE)                  # Unicode 
69   ADD_DEFINITIONS(-D_UNICODE)
70 ELSE()
71   ## Linux specific:
72   SET(PLATFORM_LIBS dl)                       # Dynamic loading (dlopen, dlsym)
73   IF(MACHINE_IS_64) 
74     ADD_DEFINITIONS(-DPCLINUX64)
75   ENDIF(MACHINE_IS_64)
76 ENDIF()
77
78 ## define _DEBUG_ macro
79 IF(NOT CMAKE_BUILD_TYPE STREQUAL "RELEASE" AND NOT CMAKE_BUILD_TYPE STREQUAL "Release")
80   ADD_DEFINITIONS(-D_DEBUG_)
81 ENDIF()
82
83 ## Apple specific:
84 IF(APPLE)
85   # Default is clang(llvm) with mountain lion at least
86   OPTION(SALOME_APPLE_USE_GCC "Use GCC compiler" OFF)
87   MARK_AS_ADVANCED(SALOME_APPLE_USE_GCC)
88   IF(SALOME_APPLE_USE_GCC)
89     SET(CMAKE_C_COMPILER gcc)
90     SET(CMAKE_CXX_COMPILER g++)
91   ENDIF()
92 ENDIF()
93
94 # Compiler flags for coverage testing
95 IF(NOT WIN32) 
96   OPTION(SALOME_BUILD_FOR_GCOV "Add the compilation flags for GCov/LCov" OFF)
97   MARK_AS_ADVANCED(SALOME_BUILD_FOR_GCOV)
98   IF(SALOME_BUILD_FOR_GCOV)
99     SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
100     SET(CMAKE_C_FLAGS    "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
101   ENDIF()
102 ENDIF()
103
104 # Compiler flag to disable treating alternative C++ tokens (compatibility with MSVS)
105 CHECK_CXX_COMPILER_FLAG("-fno-operator-names" COMPILER_SUPPORTS_NO_OPERATOR_NAMES)
106 IF(COMPILER_SUPPORTS_NO_OPERATOR_NAMES)
107   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-operator-names")
108 ENDIF()
109
110 # Option: C++ standard to use
111 # Note: CMake by default enables C++ extensions; they can be disabled by additionally
112 #       specifying -DCMAKE_CXX_EXTENSIONS=OFF
113 SET(SALOME_CXX_STANDARD 17 CACHE STRING "C++ standard to use")
114 SET(_supported_standards 11 14 17 20)
115 SET(_standard_ok FALSE)
116 FOREACH(_standard ${_supported_standards})
117   IF(SALOME_CXX_STANDARD STREQUAL _standard)
118     SET(_standard_ok TRUE)
119     BREAK()
120   ENDIF()
121 ENDFOREACH()
122 IF(NOT _standard_ok)
123   MESSAGE(FATAL_ERROR "Unsupported C++ standard: ${SALOME_CXX_STANDARD}; allowed values: ${_supported_standards}")
124 ENDIF()
125 UNSET(_supported_standards)
126 UNSET(_standard)
127 UNSET(_standard_ok)
128 MESSAGE(STATUS "Setting C++ standard to ${SALOME_CXX_STANDARD}")
129 SET(CMAKE_CXX_STANDARD ${SALOME_CXX_STANDARD})
130 SET(CMAKE_CXX_STANDARD_REQUIRED ON)
131
132 # Fight warnings
133 IF(NOT APPLE)
134   OPTION(SALOME_DEBUG_WARNINGS "Report more warnings" OFF)
135   OPTION(SALOME_TREAT_WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
136   # Report more warnings
137   MARK_AS_ADVANCED(SALOME_DEBUG_WARNINGS SALOME_TREAT_WARNINGS_AS_ERRORS)
138   IF(SALOME_DEBUG_WARNINGS)
139     IF(WIN32)
140       SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
141       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
142     ELSE()
143       SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic")
144       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
145     ENDIF()
146   ENDIF()
147   ## Treat all warnings as errors
148   IF(SALOME_TREAT_WARNINGS_AS_ERRORS)
149     IF(WIN32)
150       SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
151       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
152     ELSE()
153       SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
154       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
155     ENDIF()
156   ENDIF()
157 ENDIF()