File: common_library.rb

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (452 lines) | stat: -rw-r--r-- 12,142 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#  Phusion Passenger - https://www.phusionpassenger.com/
#  Copyright (c) 2012 Phusion
#
#  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

# This file lists all the Phusion Passenger C++ library files and contains
# code for calculating how to compile and how to link them into executables.
# It's used by the build system (build/*.rb) and
# lib/phusion_passenger/standalone/runtime_installer.rb.

class CommonLibraryBuilder
	include Rake::DSL if defined?(Rake::DSL)

	attr_reader :all_components, :selected_components, :output_dir

	def initialize(&block)
		@all_components = {}
		@all_ordered_components = []
		@selected_components = {}
		@namespace = "common"
		if defined?(COMMON_OUTPUT_DIR)
			@output_dir = COMMON_OUTPUT_DIR + "libpassenger_common"
		else
			@output_dir = "."
		end
		instance_eval(&block) if block
	end

	def initialize_copy(other)
		[:all_components, :all_ordered_components, :selected_components, :namespace, :output_dir].each do |name|
			var_name = "@#{name}"
			instance_variable_set(var_name, other.instance_variable_get(var_name).dup)
		end
	end

	def define_component(object_name, options)
		options[:deps] ||= []
		@all_components[object_name] = options
		@all_ordered_components << object_name
		@selected_components[object_name] = options
	end

	def only(*selector)
		return dup.send(:only!, *selector)
	end

	def exclude(*selector)
		return dup.send(:exclude!, *selector)
	end

	def set_namespace(namespace)
		return dup.send(:set_namespace!, namespace)
	end

	def set_output_dir(dir)
		return dup.send(:set_output_dir!, dir)
	end

	def link_objects
		result = []

		selected_categories.each do |category|
			if category_complete?(category) && false
				# Feature disabled: we don't want to waste too much space when
				# packaging the runtime ('passenger package-runtime') so we
				# never generate static libraries.
				if aggregate_sources?
					result << "#{@output_dir}/#{category}.o"
				else
					result << "#{@output_dir}/#{category}.a"
				end
			else
				object_names = selected_objects_beloging_to_category(category)
				result.concat(object_filenames_for(object_names))
			end
		end

		return result
	end

	def link_objects_as_string
		return link_objects.join(' ')
	end

	def define_tasks(extra_compiler_flags = nil)
		flags =  "-Iext -Iext/common #{LIBEV_CFLAGS} -I/usr/include/jsoncpp #{extra_compiler_flags} "
		cflags = (flags + EXTRA_CFLAGS).strip
		cxxflags = (flags + EXTRA_CXXFLAGS).strip

		group_all_components_by_category.each_pair do |category, object_names|
			define_category_tasks(category, object_names, cflags, cxxflags)
		end

		task("#{@namespace}:clean") do
			sh "rm -rf #{@output_dir}"
		end

		return self
	end

private
	def define_category_tasks(category, object_names, cflags, cxxflags)
		object_filenames = object_filenames_for(object_names)

		object_names.each do |object_name|
			options     = @all_components[object_name]
			source_file = "ext/common/#{options[:source]}"
			object_file = "#{@output_dir}/#{object_name}"

			file(object_file => dependencies_for(options)) do
				ensure_directory_exists(File.dirname(object_file))
				if source_file =~ /\.c$/
					compile_c(source_file, "#{cflags} -o #{object_file}")
				else
					compile_cxx(source_file, "#{cxxflags} -o #{object_file}")
				end
			end
		end

		task "#{@namespace}:clean" do
			sh "rm -f #{object_filenames.join(' ')}"
		end

		if aggregate_sources?
			aggregate_source = "#{@output_dir}/#{category}.cpp"
			aggregate_object = "#{@output_dir}/#{category}.o"

			file(aggregate_object => dependencies_for(object_names)) do
				ensure_directory_exists(File.dirname(aggregate_source))
				ensure_directory_exists(File.dirname(aggregate_object))

				File.open(aggregate_source, "w") do |f|
					f.puts %q{
						#ifndef _GNU_SOURCE
							#define _GNU_SOURCE
						#endif
					}
					object_names.each do |object_name|
						options = @all_components[object_name]
						source_file = options[:source].sub(%r(^ext/common), '')
						f.puts "#include \"#{source_file}\""
					end
				end

				compile_cxx(aggregate_source, "#{flags} -o #{aggregate_object}")
			end

			task "#{@namespace}:clean" do
				sh "rm -f #{aggregate_source} #{aggregate_object}"
			end
		elsif false
			# Feature disabled: we don't want to waste too much space when
			# packaging the runtime ('passenger package-runtime') so we
			# never generate static libraries.
			library = "#{@output_dir}/#{category}.a"
			
			file(library => object_filenames) do
				create_static_library(library, object_filenames.join(' '))
			end

			task "#{@namespace}:clean" do
				sh "rm -f #{library}"
			end
		end
	end

	def set_namespace!(namespace)
		@namespace = namespace
		return self
	end

	def set_output_dir!(dir)
		@output_dir = dir
		return self
	end

	def only!(*selector)
		new_components = apply_selector(*selector)
		@selected_components = new_components
		return self
	end

	def exclude!(*selector)
		apply_selector(*selector).each_key do |object_name|
			@selected_components.delete(object_name)
		end
		return self
	end

	def apply_selector(*selector)
		result = {}
		selector = [selector].flatten
		selector.each do |condition|
			@selected_components.each do |object_name, options|
				if component_satisfies_condition?(object_name, options, condition)
					result[object_name] = options
				end
			end
		end
		return result
	end

	def component_satisfies_condition?(object_name, options, condition)
		case condition
		when Symbol
			return condition == :all || options[:category] == condition
		when String
			return object_name == condition
		else
			raise ArgumentError, "Invalid condition #{condition.inspect}"
		end
	end

	def ensure_directory_exists(dir)
		sh("mkdir -p #{dir}") if !File.directory?(dir)
	end

	def selected_categories
		categories = {}
		@selected_components.each_value do |options|
			categories[options[:category]] = true
		end
		return categories.keys
	end

	def category_complete?(category)
		expected = 0
		actual   = 0
		@all_components.each_value do |options|
			if options[:category] == category
				expected += 1
			end
		end
		@selected_components.each_value do |options|
			if options[:category] == category
				actual += 1
			end
		end
		return expected == actual
	end

	def selected_objects_beloging_to_category(category)
		result = []
		@selected_components.each_pair do |object_name, options|
			if options[:category] == category
				result << object_name
			end
		end
		return result
	end

	def dependencies_for(component_options_or_object_names)
		result = nil
		case component_options_or_object_names
		when Hash
			component_options = component_options_or_object_names
			result = ["ext/common/#{component_options[:source]}"]
			component_options[:deps].each do |dependency|
				result << "ext/common/#{dependency}"
			end
		when Array
			result = []
			object_names = component_options_or_object_names
			object_names.each do |object_name|
				options = @all_components[object_name]
				result.concat(dependencies_for(options))
			end
			result.uniq!
		end
		return result
	end

	def object_filenames_for(object_names)
		return object_names.map { |name| "#{@output_dir}/#{name}" }
	end

	def group_all_components_by_category
		categories = {}
		@all_ordered_components.each do |object_name|
			options  = @all_components[object_name]
			category = options[:category]
			categories[category] ||= []
			categories[category] << object_name
		end
		return categories
	end

	def aggregate_sources?
		# Feature disabled: it's too hard to make it work because
		# lots of executables have to be linked to individual objects
		# anyway.
		return false
	end
end


COMMON_LIBRARY = CommonLibraryBuilder.new do
	define_component 'Logging.o',
		:source   => 'Logging.cpp',
		:category => :base,
		:deps     => %w(
			Logging.cpp
			Logging.h
		)
	define_component 'Exceptions.o',
		:source   => 'Exceptions.cpp',
		:category => :base,
		:deps     => %w(
			Exceptions.h
		)
	define_component 'Utils/SystemTime.o',
		:source   => 'Utils/SystemTime.cpp',
		:category => :base,
		:deps     => %w(
			Utils/SystemTime.h
		)
	define_component 'Utils/StrIntUtils.o',
		:source   => 'Utils/StrIntUtils.cpp',
		:category => :base,
		:deps     => %w(
			Utils/StrIntUtils.h
		)
	define_component 'Utils/IOUtils.o',
		:source   => 'Utils/IOUtils.cpp',
		:category => :base,
		:deps     => %w(
			Utils/IOUtils.h
		)
	define_component 'Utils.o',
		:source   => 'Utils.cpp',
		:category => :base,
		:deps     => %w(
			Utils.h
			Utils/Base64.h
			Utils/StrIntUtils.h
			ResourceLocator.h
		)

	define_component 'Utils/Base64.o',
		:source   => 'Utils/Base64.cpp',
		:category => :other,
		:deps     => %w(
			Utils/Base64.h
		)
	define_component 'Utils/CachedFileStat.o',
		:source   => 'Utils/CachedFileStat.cpp',
		:category => :other,
		:deps     => %w(
			Utils/CachedFileStat.h
			Utils/CachedFileStat.hpp
		)
	define_component 'Utils/LargeFiles.o',
		:source   => 'Utils/LargeFiles.cpp',
		:category => :other,
		:deps     => %w(
			Utils/LargeFiles.h
		)
	define_component 'ApplicationPool2/Implementation.o',
		:source   => 'ApplicationPool2/Implementation.cpp',
		:category => :other,
		:deps     => %w(
			ApplicationPool2/Spawner.h
			ApplicationPool2/Common.h
			ApplicationPool2/Pool.h
			ApplicationPool2/SuperGroup.h
			ApplicationPool2/Group.h
			ApplicationPool2/Process.h
			ApplicationPool2/Socket.h
			ApplicationPool2/Session.h
			ApplicationPool2/Options.h
			ApplicationPool2/PipeWatcher.h
			ApplicationPool2/AppTypes.h
			ApplicationPool2/Spawner.h
			ApplicationPool2/SpawnerFactory.h
			ApplicationPool2/SmartSpawner.h
			ApplicationPool2/DirectSpawner.h
			ApplicationPool2/DummySpawner.h
		)
	define_component 'ApplicationPool2/AppTypes.o',
		:source   => 'ApplicationPool2/AppTypes.cpp',
		:category => :other,
		:deps     => %w(
			ApplicationPool2/AppTypes.h
			Utils/StrIntUtils.h
			Utils/CachedFileStat.h
		)
	define_component 'AgentsStarter.o',
		:source   => 'AgentsStarter.cpp',
		:category => :other,
		:deps     => %w(
			AgentsStarter.h
			ResourceLocator.h
			MessageClient.h
			ServerInstanceDir.h
			Utils/IniFile.h
			Utils/VariantMap.h
		)
	define_component 'AgentsBase.o',
		:source   => 'agents/Base.cpp',
		:category => :other,
		:deps     => %w(
			agents/Base.h
			Utils/VariantMap.h
		)
	define_component 'agents/LoggingAgent/FilterSupport.o',
		:source   => 'agents/LoggingAgent/FilterSupport.cpp',
		:category => :logging_agent,
		:deps     => %w(
			agents/LoggingAgent/FilterSupport.h
		)
	define_component 'Utils/MD5.o',
		:source   => 'Utils/MD5.cpp',
		:category => :other,
		:deps     => %w(
			Utils/MD5.h
		)
	define_component 'Utils/fib.o',
		:source   => 'Utils/fib.c',
		:category => :other,
		:deps     => %w(
			Utils/fib.h
			Utils/fibpriv.h
		)

	#'BCrypt.o' => %w(
	#	BCrypt.cpp
	#	BCrypt.h
	#	Blowfish.h
	#	Blowfish.c)
end

# Objects that must be linked into the Nginx binary.
NGINX_LIBS_SELECTOR = [:base, 'AgentsStarter.o', 'ApplicationPool2/AppTypes.o',
	'Utils/CachedFileStat.o', 'Utils/Base64.o', 'agents/LoggingAgent/FilterSupport.o']