Automatically detect C99 flags for supported compilers.
[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 #pragma GCC diagnostic error "-Wall"
39 #pragma GCC diagnostic error "-Werror"
40 #pragma GCC diagnostic error "-pedantic"
41 """ + def_config._c99_test_program()
42
43     # -------------------------------------------------------------
44     # Get flags known to enable C99 support for GCC C compiler.
45     # -------------------------------------------------------------
46     def _c99_flags(self):
47         # Favor flags that do not enable GNU extensions by default,
48         # e.g. '-std=c99'.
49         return [ '-std=c99',
50                  '-std=iso9899:1999',
51                  '-std=gnu99',
52                  '-std=c9x',
53                  '-std=iso9899:199x',
54                  '-std=gnu9x' ]
55
56     # ------------------------------------------------------------
57     # Return test program to be used when checking for basic C++11
58     # support in GCC.
59     # ------------------------------------------------------------
60     def _cxx11_test_program(self):
61         # Besides checking for a basic C++11 feature, this contrived
62         # test program is designed to trigger an issue in some older
63         # (pre-C++11) C library headers that causes functions like
64         # snprintf() to remain undeclared when -std=c++0x or -ansi,
65         # for example, is added to the g++ command line flags, and
66         # despite the fact the appropriate feature test macro to make
67         # the prototypes visible is defined.
68         #
69         # The recommended workaround is to instead of -std=c++0x use
70         # -std=gnu++0x to explicitly enable GNU extensions.  Failure
71         # will force the configuration test to attempt the build with
72         # -std=gnu++0x, for example.
73         #
74         # For a more complete description of this issue, see:
75         #     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=34032
76         #
77         # Other compilers should be able to compile this code without
78         # issue.
79         return """
80 #define _POSIX_C_SOURCE 200112L
81 #include <stdio.h>
82
83 int main()
84 {
85     int x = 3210;
86     auto f = [x](){
87         char buf[15] = { 0 };
88         snprintf(buf, sizeof(buf) / sizeof(buf[0]), "%d", x);
89         return buf[0];
90     };
91
92     return f() != '3';
93 }
94 """
95
96     # -------------------------------------------------------------
97     # Get flags known to enable C++11 support for GCC C++ compiler.
98     # -------------------------------------------------------------
99     def _cxx11_flags(self):
100         # Favor command line flags from more recent versions of the
101         # compiler over older/deprecated flags.  Also, favor flags
102         # that do not enable GNU extensions by default,
103         # e.g. '-std=c++..'.
104         return [ '-std=c++11',
105                  '-std=gnu++11',
106                  '-std=c++0x',
107                  '-std=gnu++0x' ]