~ubuntu-branches/ubuntu/wily/ruby-passenger/wily-proposed

« back to all changes in this revision

Viewing changes to lib/phusion_passenger/platform_info/cxx_portability.rb

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-11-23 23:50:02 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131123235002-8fdhsq7afj15o2z2
Tags: 4.0.25-1
* New upstream release.
* Refresh fix_install_path.patch.
* Build for Ruby 2.0 instead of 1.8. (Closes: #725591)
* Add fix_ftbfs_fortify_source.patch.
* Install passenger template files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
module PhusionPassenger
29
29
 
30
30
module PlatformInfo
31
 
        # Compiler flags that should be used for compiling every C/C++ program,
 
31
        # Linker flags that should be used for linking every C/C++ program,
32
32
        # for portability reasons. These flags should be specified as last
33
 
        # when invoking the compiler.
34
 
        def self.portability_cflags
35
 
                flags = ["-D_REENTRANT -I/usr/local/include"]
36
 
                
37
 
                # There are too many implementations of of the hash map!
38
 
                # Figure out the right one.
39
 
                ok = try_compile("Checking for tr1/unordered_map", :cxx, %Q{
40
 
                        #include <tr1/unordered_map>
 
33
        # when invoking the linker.
 
34
        def self.portability_ldflags
 
35
                if os_name =~ /solaris/
 
36
                        result = '-lxnet -lsocket -lnsl -lpthread'
 
37
                else
 
38
                        result = '-lpthread'
 
39
                end
 
40
                result << ' -lrt' if has_rt_library?
 
41
                result << ' -lmath' if has_math_library?
 
42
                return result
 
43
        end
 
44
        memoize :portability_ldflags
 
45
 
 
46
        # Extra compiler flags that should always be passed to the C compiler,
 
47
        # last in the command string.
 
48
        def self.default_extra_cflags
 
49
                return default_extra_c_or_cxxflags(:cc)
 
50
        end
 
51
        memoize :default_extra_cflags, true
 
52
 
 
53
        # Extra compiler flags that should always be passed to the C++ compiler,
 
54
        # last in the command string.
 
55
        def self.default_extra_cxxflags
 
56
                return default_extra_c_or_cxxflags(:cxx)
 
57
        end
 
58
        memoize :default_extra_cxxflags, true
 
59
 
 
60
private
 
61
        def self.check_unordered_map(flags, class_name, header_name, macro_name)
 
62
                ok = try_compile("Checking for unordered_map", :cxx, %Q{
 
63
                        #include <#{header_name}>
41
64
                        int
42
65
                        main() {
43
 
                                std::tr1::unordered_map<int, int> m;
 
66
                                #{class_name}<int, int> m;
44
67
                                return 0;
45
68
                        }
46
69
                })
47
 
                if ok
48
 
                        flags << "-DHAS_TR1_UNORDERED_MAP"
49
 
                else
50
 
                        hash_namespace = nil
51
 
                        ok = false
52
 
                        ['__gnu_cxx', '', 'std', 'stdext'].each do |namespace|
53
 
                                ['hash_map', 'ext/hash_map'].each do |hash_map_header|
54
 
                                        ok = try_compile("Checking for #{hash_map_header}", :cxx, %Q{
55
 
                                                #include <#{hash_map_header}>
56
 
                                                int
57
 
                                                main() {
58
 
                                                        #{namespace}::hash_map<int, int> m;
59
 
                                                        return 0;
60
 
                                                }
61
 
                                        })
62
 
                                        if ok
63
 
                                                hash_namespace = namespace
64
 
                                                flags << "-DHASH_NAMESPACE=\"#{namespace}\""
65
 
                                                flags << "-DHASH_MAP_HEADER=\"<#{hash_map_header}>\""
66
 
                                                flags << "-DHASH_MAP_CLASS=\"hash_map\""
67
 
                                        end
68
 
                                        break if ok
69
 
                                end
70
 
                                break if ok
71
 
                        end
72
 
                        ['ext/hash_fun.h', 'functional', 'tr1/functional',
73
 
                         'ext/stl_hash_fun.h', 'hash_fun.h', 'stl_hash_fun.h',
74
 
                         'stl/_hash_fun.h'].each do |hash_function_header|
75
 
                                ok = try_compile("Checking for #{hash_function_header}", :cxx, %Q{
76
 
                                        #include <#{hash_function_header}>
 
70
                flags << "-D#{macro_name}" if ok
 
71
                return ok
 
72
        end
 
73
        private_class_method :check_unordered_map
 
74
        
 
75
        def self.check_hash_map(flags)
 
76
                hash_namespace = nil
 
77
                ok = false
 
78
                ['__gnu_cxx', '', 'std', 'stdext'].each do |namespace|
 
79
                        ['hash_map', 'ext/hash_map'].each do |hash_map_header|
 
80
                                ok = try_compile("Checking for #{hash_map_header}", :cxx, %Q{
 
81
                                        #include <#{hash_map_header}>
77
82
                                        int
78
83
                                        main() {
79
 
                                                #{hash_namespace}::hash<int>()(5);
 
84
                                                #{namespace}::hash_map<int, int> m;
80
85
                                                return 0;
81
86
                                        }
82
87
                                })
83
88
                                if ok
84
 
                                        flags << "-DHASH_FUN_H=\"<#{hash_function_header}>\""
 
89
                                        hash_namespace = namespace
 
90
                                        flags << "-DHASH_NAMESPACE=\"#{namespace}\""
 
91
                                        flags << "-DHASH_MAP_HEADER=\"<#{hash_map_header}>\""
 
92
                                        flags << "-DHASH_MAP_CLASS=\"hash_map\""
85
93
                                        break
86
94
                                end
87
95
                        end
88
 
                end
89
 
 
90
 
                ok = try_compile("Checking for accept4()", :c, %Q{
91
 
                        #define _GNU_SOURCE
92
 
                        #include <sys/socket.h>
93
 
                        static void *foo = accept4;
94
 
                })
95
 
                flags << '-DHAVE_ACCEPT4' if ok
96
 
                
97
 
                if RUBY_PLATFORM =~ /solaris/
98
 
                        flags << '-pthreads'
99
 
                        if RUBY_PLATFORM =~ /solaris2.11/
 
96
                        break if ok
 
97
                end
 
98
                ['ext/hash_fun.h', 'functional', 'tr1/functional',
 
99
                 'ext/stl_hash_fun.h', 'hash_fun.h', 'stl_hash_fun.h',
 
100
                 'stl/_hash_fun.h'].each do |hash_function_header|
 
101
                        ok = try_compile("Checking for #{hash_function_header}", :cxx, %Q{
 
102
                                #include <#{hash_function_header}>
 
103
                                int
 
104
                                main() {
 
105
                                        #{hash_namespace}::hash<int>()(5);
 
106
                                        return 0;
 
107
                                }
 
108
                        })
 
109
                        if ok
 
110
                                flags << "-DHASH_FUN_H=\"<#{hash_function_header}>\""
 
111
                                break
 
112
                        end
 
113
                end
 
114
        end
 
115
        private_class_method :check_hash_map
 
116
 
 
117
        def self.default_extra_c_or_cxxflags(cc_or_cxx)
 
118
                flags = ["-D_REENTRANT", "-I/usr/local/include"]
 
119
 
 
120
                if !send("#{cc_or_cxx}_is_sun_studio?")
 
121
                        flags << "-Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-long-long"
 
122
                        if send("#{cc_or_cxx}_supports_wno_missing_field_initializers_flag?")
 
123
                                flags << "-Wno-missing-field-initializers"
 
124
                        end
 
125
                        if requires_no_tls_direct_seg_refs? && send("#{cc_or_cxx}_supports_no_tls_direct_seg_refs_option?")
 
126
                                flags << "-mno-tls-direct-seg-refs"
 
127
                        end
 
128
                        # Work around Clang warnings in ev++.h.
 
129
                        if send("#{cc_or_cxx}_is_clang?")
 
130
                                flags << "-Wno-ambiguous-member-template"
 
131
                        end
 
132
                end
 
133
 
 
134
                if !send("#{cc_or_cxx}_is_sun_studio?")
 
135
                        flags << "-fcommon"
 
136
                        if send("#{cc_or_cxx}_supports_feliminate_unused_debug?")
 
137
                                flags << "-feliminate-unused-debug-symbols -feliminate-unused-debug-types"
 
138
                        end
 
139
                        if send("#{cc_or_cxx}_supports_visibility_flag?")
 
140
                                flags << "-fvisibility=hidden -DVISIBILITY_ATTRIBUTE_SUPPORTED"
 
141
                                if send("#{cc_or_cxx}_visibility_flag_generates_warnings?") &&
 
142
                                   send("#{cc_or_cxx}_supports_wno_attributes_flag?")
 
143
                                        flags << "-Wno-attributes"
 
144
                                end
 
145
                        end
 
146
                end
 
147
 
 
148
                flags << debugging_cflags
 
149
                flags << '-DHAS_ALLOCA_H' if has_alloca_h?
 
150
                flags << '-DHAVE_ACCEPT4' if has_accept4?
 
151
                flags << '-DHAS_SFENCE' if supports_sfence_instruction?
 
152
                flags << '-DHAS_LFENCE' if supports_lfence_instruction?
 
153
                flags << "-DPASSENGER_DEBUG -DBOOST_DISABLE_ASSERTS"
 
154
 
 
155
                if cc_or_cxx == :cxx
 
156
                        # There are too many implementations of of the hash map!
 
157
                        # Figure out the right one.
 
158
                        check_unordered_map(flags, "std::unordered_map", "unordered_map", "HAS_UNORDERED_MAP") ||
 
159
                                check_unordered_map(flags, "std::tr1::unordered_map", "unordered_map", "HAS_TR1_UNORDERED_MAP") ||
 
160
                                check_hash_map(flags)
 
161
                end
 
162
 
 
163
                if os_name =~ /solaris/
 
164
                        if send("#{cc_or_cxx}_is_sun_studio?")
 
165
                                flags << '-mt'
 
166
                        else
 
167
                                flags << '-pthreads'
 
168
                        end
 
169
                        if os_name =~ /solaris2\.11/
100
170
                                # skip the _XOPEN_SOURCE and _XPG4_2 definitions in later versions of Solaris / OpenIndiana
101
171
                                flags << '-D__EXTENSIONS__ -D__SOLARIS__ -D_FILE_OFFSET_BITS=64'
102
172
                        else
103
173
                                flags << '-D_XOPEN_SOURCE=500 -D_XPG4_2 -D__EXTENSIONS__ -D__SOLARIS__ -D_FILE_OFFSET_BITS=64'
104
 
                                flags << '-D__SOLARIS9__ -DBOOST__STDC_CONSTANT_MACROS_DEFINED' if RUBY_PLATFORM =~ /solaris2.9/
105
 
                        end
106
 
                        flags << '-DBOOST_HAS_STDINT_H' unless RUBY_PLATFORM =~ /solaris2.9/
107
 
                        flags << '-mcpu=ultrasparc' if RUBY_PLATFORM =~ /sparc/
108
 
                elsif RUBY_PLATFORM =~ /openbsd/
 
174
                                flags << '-D__SOLARIS9__ -DBOOST__STDC_CONSTANT_MACROS_DEFINED' if os_name =~ /solaris2\.9/
 
175
                        end
 
176
                        flags << '-DBOOST_HAS_STDINT_H' unless os_name =~ /solaris2\.9/
 
177
                        if send("#{cc_or_cxx}_is_sun_studio?")
 
178
                                flags << '-xtarget=ultra' if RUBY_PLATFORM =~ /sparc/
 
179
                        else
 
180
                                flags << '-mcpu=ultrasparc' if RUBY_PLATFORM =~ /sparc/
 
181
                        end
 
182
                elsif os_name =~ /openbsd/
109
183
                        flags << '-DBOOST_HAS_STDINT_H -D_GLIBCPP__PTHREADS'
110
 
                elsif RUBY_PLATFORM =~ /aix/
 
184
                elsif os_name =~ /aix/
111
185
                        flags << '-pthread'
112
186
                        flags << '-DOXT_DISABLE_BACKTRACES'
113
187
                elsif RUBY_PLATFORM =~ /(sparc-linux|arm-linux|^arm.*-linux|sh4-linux)/
116
190
                        # http://groups.google.com/group/phusion-passenger/browse_thread/thread/aad4bd9d8d200561
117
191
                        flags << '-DBOOST_SP_USE_PTHREADS'
118
192
                end
119
 
                
120
 
                flags << '-DHAS_ALLOCA_H' if has_alloca_h?
121
 
                flags << '-DHAS_SFENCE' if supports_sfence_instruction?
122
 
                flags << '-DHAS_LFENCE' if supports_lfence_instruction?
123
 
                
124
 
                return flags.compact.join(" ").strip
125
 
        end
126
 
        memoize :portability_cflags, true
127
193
 
128
 
        # Linker flags that should be used for linking every C/C++ program,
129
 
        # for portability reasons. These flags should be specified as last
130
 
        # when invoking the linker.
131
 
        def self.portability_ldflags
132
 
                if RUBY_PLATFORM =~ /solaris/
133
 
                        result = '-lxnet -lrt -lsocket -lnsl -lpthread'
134
 
                else
135
 
                        result = '-lpthread'
136
 
                end
137
 
                flags << ' -lmath' if has_math_library?
138
 
                return result
 
194
                return flags.compact.map{ |str| str.strip }.join(" ").strip
139
195
        end
140
 
        memoize :portability_ldflags
 
196
        private_class_method :default_extra_c_or_cxxflags
141
197
end
142
198
 
143
199
end # module PhusionPassenger