Class: DyCI::ClangRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/dyci-compiler/clang_runner.rb

Instance Method Summary collapse

Instance Method Details

#cleanupObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dyci-compiler/clang_runner.rb', line 79

def cleanup
  files = []
  # TODO: move to utils module
  begin
  index_dir = Dir.new File.expand_path('~/.dyci')
  rescue
    return false
  end
  index_dir.each do |file_entry|
    files << "#{index_dir.path}/#{file_entry}" if file_entry =~ /.*\.dylib|.*resource/ 
  end 

  FileUtils.rm files, :force => true 
end

#compile(args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dyci-compiler/clang_runner.rb', line 6

def compile args
  developer_path = %x[ xcode-select -print-path ].chomp
  clang_path = "#{developer_path}/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
  if File.exists? "#{clang_path}.backup" 
    clang_path = "#{clang_path}.backup"
  else # this mean that we use DyCi as proxy
    args = args.delete_if { |arg| arg =~ /\A-mmacosx-version-min/ }
  end

  args = args.map do |arg|
    arg.gsub /(.+)/, '"\0"'
  end
  
  cmd = "#{clang_path} #{args.join(" ")}" 
  Kernel.exec(cmd)
end

#copy_resource(resource, root) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dyci-compiler/clang_runner.rb', line 94

def copy_resource resource, root
  bundle_path = nil
  File.open("#{root}/bundle", 'r') do |file|
    bundle_path = file.read
  end
  FileUtils.cp resource, bundle_path if bundle_path
  
  File.open("#{root}/resource", 'w') do |file|
    file.write(resource)
  end
end

#recompile(class_name) ⇒ Object



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
# File 'lib/dyci-compiler/clang_runner.rb', line 23

def recompile class_name
  if class_name =~ /\.jp?g|\.png/
    copy_resource class_name, File.expand_path("~/.dyci")
    exit 0
  end

  class_name = class_name.gsub /\.h\z/, '.m'
  # TODO: refactor this shit too
  storage = ClangStorage.new
  arguments = storage.load_arguments class_name
  # we store working directory at first argument
  Dir.chdir arguments.shift.chomp

  fork {
    self.compile arguments
  }
  params = ClangParser.params arguments
  self.cleanup
  lib_name = "dyci_#{Time.now.to_i}.dylib"

  recompile_params = []
  recompile_params << "-arch"
  recompile_params << "#{params[:arch]}"
  recompile_params << "-dynamiclib"
  recompile_params << "-isysroot"
  recompile_params << "#{params[:i_sys_root]}"
  recompile_params + params[:l_params]
  recompile_params + params[:f_params]
  recompile_params << params[:object_compilation]
  recompile_params << "-install_name"
  recompile_params << "/usr/local/lib/#{lib_name}"
  recompile_params << "-Xlinker"
  recompile_params << "-objc_abi_version"
  recompile_params << "-Xlinker"
  recompile_params << "2"
  recompile_params << "-ObjC"
  recompile_params << "-undefined"
  recompile_params << "dynamic_lookup"
  recompile_params << "-fobjc-arc"
  recompile_params << "-fobjc-link-runtime"
  recompile_params << "-Xlinker"
  recompile_params << "-no_implicit_dylibs"
  recompile_params << params[:min_os_param]
  recompile_params << "-single_module"
  recompile_params << "-compatibility_version"
  recompile_params << "5"
  recompile_params << "-current_version"
  recompile_params << "5"
  recompile_params << "-v"
  # TODO: fix that shit
  recompile_params << "-o"
  recompile_params << "#{File.expand_path('~/.dyci')}/#{lib_name}"

  self.compile recompile_params
end