Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / build_common / iotivityconfig / compiler / factory.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 default_configuration import *
18 from gcc_configuration import *
19
20 # Canonicalize the C or C++ compiler name to "gcc" if gcc is being
21 # used to simplify mapping to the GCC compiler configuration since GCC
22 # may be installed under a different name.  This will be used when
23 # mapping compiler name to configuration in the factory submodule.
24 _GCC = 'gcc'
25
26 # Update this dictionary with new compiler configurations as needed.
27 _CONFIG_MAP = { _GCC : GccConfiguration }
28
29 _c_compiler_config = None
30 _cxx_compiler_config = None
31
32 def check_for_gcc_c(context):
33     """
34     Check if the C compiler is GCC
35
36     Returns 1 if gcc, 0 otherwise
37     """
38
39     test_program = """
40 #if !defined(__GNUC__)
41 #  error "Not the GCC C compiler."
42 #endif
43
44 int foo(void)
45 {
46     return 0;
47 }
48 """
49
50     return context.TryCompile(test_program, '.c')
51
52 def check_for_gcc_cxx(context):
53     """
54     Check if the C++ compiler is GCC
55
56     Returns 1 if gcc, 0 otherwise
57     """
58
59     test_program = """
60 #if !defined(__GNUC__) || !defined(__cplusplus)
61 #  error "Not the GCC C++ compiler."
62 #endif
63
64 class foo
65 {
66 public:
67     foo() : x_() {}
68     int x() const { return x_; }
69 private:
70     int x_;
71 };
72 """
73
74     return context.TryCompile(test_program, '.cpp')
75
76 def make_c_compiler_config(context):
77     """
78     Create C compiler-specific configuration object.
79
80     Arguments:
81     context -- the scons configure context
82
83     The 'CC' key in the SCons environment will be mapped to the
84     appropriate supported compiler configuration.  If no match is
85     found compiler configuration operations will simply be no-ops.
86     """
87
88     global _c_compiler_config
89
90     if _c_compiler_config is None:
91         cc = context.env['CC']
92
93         if check_for_gcc_c(context):
94             cc = _GCC
95
96         config = _CONFIG_MAP.get(cc, DefaultConfiguration)
97
98         _c_compiler_config = config(context)
99
100     return _c_compiler_config
101
102 def make_cxx_compiler_config(context):
103     """
104     Create C++ compiler-specific configuration object.
105
106     Arguments:
107     context -- the scons configure context
108
109     The 'CXX' key in the SCons environment will be mapped to the
110     appropriate supported compiler configuration.  If no match is
111     found compiler configuration operations will simply be no-ops.
112     """
113
114     global _cxx_compiler_config
115
116     if _cxx_compiler_config is None:
117         cxx = context.env['CXX']
118
119         if check_for_gcc_cxx(context):
120             cxx = _GCC
121
122         config = _CONFIG_MAP.get(cxx, DefaultConfiguration)
123
124         _cxx_compiler_config = config(context)
125
126     return _cxx_compiler_config