Class: ChildProcess::Tools::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/childprocess/tools/generator.rb

Constant Summary collapse

EXE_NAME =
"childprocess-sizeof-generator"
TMP_PROGRAM =
"childprocess-sizeof-generator.c"
DEFAULT_INCLUDES =
%w[stdio.h stddef.h]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



15
16
17
18
19
20
# File 'lib/childprocess/tools/generator.rb', line 15

def initialize
  @cc        = ENV['CC'] || 'gcc'
  @out       = File.expand_path("../../unix/platform/#{ChildProcess.platform_name}.rb", __FILE__)
  @sizeof    = {}
  @constants = {}
end

Class Method Details

.generateObject



11
12
13
# File 'lib/childprocess/tools/generator.rb', line 11

def self.generate
  new.generate
end

Instance Method Details

#execute(src, opts) ⇒ Object



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
# File 'lib/childprocess/tools/generator.rb', line 78

def execute(src, opts)

  program = Array(opts[:define]).map do |key, value|
    "#ifndef \#{key}\n#define \#{key} \#{value}\n#endif\n    SRC\n  end.join(\"\\n\")\n  program << \"\\n\"\n\n  includes = Array(opts[:include]) + DEFAULT_INCLUDES\n  program << includes.map { |include| \"#include <\#{include}>\" }.join(\"\\n\")\n  program << \"\\n\#{src}\"\n\n  File.open(TMP_PROGRAM, 'w') do |file|\n    file << program\n  end\n\n  cmd = \"\#{@cc} \#{TMP_PROGRAM} -o \#{EXE_NAME}\"\n  system cmd\n  unless $?.success?\n    raise \"failed to compile program: \#{cmd.inspect}\\n\#{program}\"\n  end\n\n  output = `./\#{EXE_NAME} 2>&1`\n\n  unless $?.success?\n    raise \"failed to run program: \#{cmd.inspect}\\n\#{output}\"\n  end\n\n  output.chomp\nensure\n  File.delete TMP_PROGRAM if File.exist?(TMP_PROGRAM)\n  File.delete EXE_NAME if File.exist?(EXE_NAME)\nend\n"

#fetch_constant(name, opts) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/childprocess/tools/generator.rb', line 65

def fetch_constant(name, opts)
  src = "int main() {\n  printf(\"%d\", (unsigned int)\#{name});\n  return 0;\n}\n  EOF\n\n  output = execute(src, opts)\n  @constants[name] = Integer(output)\nend\n"

#fetch_size(type_name, opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/childprocess/tools/generator.rb', line 48

def fetch_size(type_name, opts = {})
  src = "int main() {\n  printf(\"%d\", (unsigned int)sizeof(\#{type_name}));\n  return 0;\n}\n  EOF\n\n  output = execute(src, opts)\n\n  if output.to_i < 1\n    raise \"sizeof(\#{type_name}) == \#{output.to_i} (output=\#{output})\"\n  end\n\n  @sizeof[type_name] = output.to_i\nend\n"

#generateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/childprocess/tools/generator.rb', line 22

def generate
  fetch_size 'posix_spawn_file_actions_t', :include => "spawn.h"
  fetch_size 'posix_spawnattr_t', :include => "spawn.h"
  fetch_size 'sigset_t', :include => "signal.h"

  fetch_constant 'POSIX_SPAWN_RESETIDS',   :include  => 'spawn.h'
  fetch_constant 'POSIX_SPAWN_SETPGROUP',  :include  => 'spawn.h'
  fetch_constant 'POSIX_SPAWN_SETSIGDEF',  :include  => 'spawn.h'
  fetch_constant 'POSIX_SPAWN_SETSIGMASK', :include  => 'spawn.h'

  if ChildProcess.linux?
    fetch_constant 'POSIX_SPAWN_USEVFORK', :include => 'spawn.h', :define => {'_GNU_SOURCE' => nil}
  end

  write
end

#resultObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/childprocess/tools/generator.rb', line 115

def result
  if @sizeof.empty?
    raise "no sizes collected, nothing to do"
  end

  out =  ['module ChildProcess::Unix::Platform']
  out << '  SIZEOF = {'

  max = @sizeof.keys.map { |e| e.length }.max
  @sizeof.each_with_index do |(type, size), idx|
    out << "     :#{type.ljust max} => #{size}#{',' unless idx == @sizeof.size - 1}"
  end
  out << '  }'

  max = @constants.keys.map { |e| e.length }.max
  @constants.each do |name, val|
    out << "  #{name.ljust max} = #{val}"
  end
  out << 'end'

  out.join "\n"
end

#writeObject



39
40
41
42
43
44
45
46
# File 'lib/childprocess/tools/generator.rb', line 39

def write
  FileUtils.mkdir_p(File.dirname(@out))
  File.open(@out, 'w') do |io|
    io.puts result
  end

  puts "wrote #{@out}"
end