Fix build error with scons-4.4.0 version which is based on python3
[platform/upstream/iotivity.git] / build_common / iotivityconfig / compiler / gcc_configuration.py
1 # ------------------------------------------------------------------------
2 # Copyright 2015 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 from .configuration import Configuration
18
19 # GCC compiler-specific configuration
20 class GccConfiguration(Configuration):
21     def __init__(self, context):
22         Configuration.__init__(self, context)
23
24     # ------------------------------------------------------------
25     # Return test program to be used when checking for basic C99
26     # support in GCC.
27     # ------------------------------------------------------------
28     def _c99_test_program(self):
29         # Use the default C99 test program but enable pedantic
30         # diagnostics specific to GCC to force errors to occur if a
31         # flag is required to compile C99 code without warning or
32         # error.
33
34         from .default_configuration import DefaultConfiguration
35         def_config = DefaultConfiguration(self._context)
36
37         return """
38 #ifndef __clang__
39 #pragma GCC diagnostic error "-Wall"
40 #pragma GCC diagnostic error "-Werror"
41 #pragma GCC diagnostic error "-pedantic"
42 #endif
43 """ + def_config._c99_test_program()
44
45     # -------------------------------------------------------------
46     # Get flags known to enable C99 support for GCC C compiler.
47     # -------------------------------------------------------------
48     def _c99_flags(self):
49         # Favor flags that do not enable GNU extensions by default,
50         # e.g. '-std=c99'.
51         return [ '-std=c99',
52                  '-std=iso9899:1999',
53                  '-std=gnu99',
54                  '-std=c9x',
55                  '-std=iso9899:199x',
56                  '-std=gnu9x' ]
57
58     # ------------------------------------------------------------
59     # Return test program to be used when checking for basic C++11
60     # support in GCC.
61     # ------------------------------------------------------------
62     def _cxx11_test_program(self):
63         # Besides checking for a basic C++11 feature, this contrived
64         # test program is designed to trigger an issue in some older
65         # (pre-C++11) C library headers that causes functions like
66         # snprintf() to remain undeclared when -std=c++0x or -ansi,
67         # for example, is added to the g++ command line flags, and
68         # despite the fact the appropriate feature test macro to make
69         # the prototypes visible is defined.
70         #
71         # The recommended workaround is to instead of -std=c++0x use
72         # -std=gnu++0x to explicitly enable GNU extensions.  Failure
73         # will force the configuration test to attempt the build with
74         # -std=gnu++0x, for example.
75         #
76         # For a more complete description of this issue, see:
77         #     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=34032
78         #
79         # Other compilers should be able to compile this code without
80         # issue.
81         return """
82 #define _POSIX_C_SOURCE 200112L
83 #include <stdio.h>
84
85 int main()
86 {
87     int x = 3210;
88     auto f = [x](){
89         char buf[15] = { 0 };
90         snprintf(buf, sizeof(buf) / sizeof(buf[0]), "%d", x);
91         return buf[0];
92     };
93
94     return f() != '3';
95 }
96 """
97
98     # -------------------------------------------------------------
99     # Get flags known to enable C++11 support for GCC C++ compiler.
100     # -------------------------------------------------------------
101     def _cxx11_flags(self):
102         # Favor command line flags from more recent versions of the
103         # compiler over older/deprecated flags.  Also, favor flags
104         # that do not enable GNU extensions by default,
105         # e.g. '-std=c++..'.
106         return [ '-std=c++11',
107                  '-std=gnu++11',
108                  '-std=c++0x',
109                  '-std=gnu++0x' ]