<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -13,6 +13,7 @@ class Compiler
 
   def initialize(argv)
     @mode = :normal
+    @archs = []
     @frameworks = []
     @internal = argv.delete('--internal')
 
@@ -24,6 +25,7 @@ class Compiler
       opts.on('--mode [MODE]', &quot;Select compilation mode (normal or full)&quot;) { |mode| @mode = mode.intern }
       #opts.on('--framework &lt;framework&gt;', 'Link against &lt;framework&gt;') { |path| @frameworks &lt;&lt; path }
       opts.on('-C', 'Compile, assemble and link a loadable object file') { @bundle = true }
+      opts.on('-a', '--arch &lt;ARCH&gt;', 'Compile for specified CPU architecture') { |arch| @archs &lt;&lt; arch }
       opts.on('-v', '--version', 'Display the version') { puts RUBY_DESCRIPTION; exit 1 }
       opts.on('-V', '--verbose', 'Print every command line executed') { @verbose = true }
       opts.on('-h', '--help', 'Display this information') { die opts }
@@ -37,14 +39,17 @@ class Compiler
       if @mode != :normal and @mode != :full
         die &quot;invalid mode `#{@mode}' (possible choices are: normal, full)&quot;
       end
+      @archs.uniq!
+      @archs &lt;&lt; RUBY_ARCH if @archs.empty?
     end
 
     # Locate necessary programs.
     @macruby = locate(@internal ? './miniruby' : 'macruby')
-    @llc = locate('llc')    
+    @llc = locate('llc')
     @gcc = locate('gcc')
     @gcxx = locate('g++')
     @nm = locate('nm')
+    @lipo = locate('lipo')
 
     # Misc.
     @tmpdir = (ENV['TMPDIR'] or '/tmp')
@@ -98,16 +103,24 @@ class Compiler
     # Generate init function (must be unique).
     init_func = &quot;MREP_#{File.read(path).hash.abs}&quot;
 
-    # Compile the file into LLVM bitcode.
-    bc = gen_tmpfile(base, 'bc')
-    execute(&quot;#{@macruby} --emit-llvm \&quot;#{bc}\&quot; #{init_func} \&quot;#{path}\&quot;&quot;)
+    tmp_objs = []
+    @archs.each do |arch|
+      # Compile the file into LLVM bitcode.
+      bc = gen_tmpfile(base + arch, 'bc')
+      execute(&quot;arch -#{arch} #{@macruby} --emit-llvm \&quot;#{bc}\&quot; #{init_func} \&quot;#{path}\&quot;&quot;)
 
-    # Compile the bitcode as assembly.
-    asm = gen_tmpfile(base, 's')
-    execute(&quot;#{@llc} -f #{bc} -o=#{asm} -march=x86-64 -enable-eh&quot;)
+      # Compile the bitcode as assembly.
+      asm = gen_tmpfile(base + arch, 's')
+      execute(&quot;#{@llc} -f #{bc} -o=#{asm} -march=#{llc_arch(arch)} -enable-eh&quot;)
 
-    # Finally compile the assembly.
-    execute(&quot;#{@gcc} -c -arch x86_64 #{asm} -o #{output}&quot;)
+      # Compile the assembly.
+      tmp_obj = gen_tmpfile(base + arch, 'o')
+      execute(&quot;#{@gcc} -c -arch #{arch} #{asm} -o #{tmp_obj}&quot;)
+      tmp_objs &lt;&lt; tmp_obj
+    end
+
+    # Link the architecture objects.
+    execute(&quot;#{@lipo} -create #{tmp_objs.join(' ')} -output #{output}&quot;)
 
     [output, init_func]
   end
@@ -131,8 +144,9 @@ EOS
 
     main = gen_tmpfile('main', 'c')
     File.open(main, 'w') { |io| io.write(main_txt) }
-    link = @internal ? &quot;-L. -lmacruby&quot; : &quot;-framework MacRuby&quot;
-    execute(&quot;#{@gcxx} #{main} -dynamic -bundle -undefined suppress -flat_namespace -arch x86_64 #{link} #{obj} -o #{output}&quot;)
+    linkf = @internal ? &quot;-L. -lmacruby&quot; : &quot;-framework MacRuby&quot;
+    archf = @archs.map { |x| &quot;-arch #{x}&quot; }.join(' ')
+    execute(&quot;#{@gcxx} #{main} -dynamic -bundle -undefined suppress -flat_namespace #{archf} #{linkf} #{obj} -o #{output}&quot;)
   end
 
   def compile_executable(objs_data, output)
@@ -184,11 +198,12 @@ EOS
     main = gen_tmpfile('main', 'cpp')
     File.open(main, 'w') { |io| io.write(main_txt) }
     main_o = gen_tmpfile('main', 'o')
-    execute(&quot;#{@gcxx} #{main} -c -arch x86_64 -o #{main_o}&quot;)
+    archf = @archs.map { |x| &quot;-arch #{x}&quot; }.join(' ')
+    execute(&quot;#{@gcxx} #{main} -c #{archf} -o #{main_o}&quot;)
     objs.unshift(main_o)
 
     # Link all objects into executable.
-    line = &quot;#{@gcxx} -o #{output} -L#{RbConfig::CONFIG['libdir']} -lmacruby-static -arch x86_64 -framework Foundation -lobjc -lauto -I/usr/include/libxml2 -lxml2 &quot;
+    line = &quot;#{@gcxx} -o #{output} -L#{RbConfig::CONFIG['libdir']} -lmacruby-static #{archf} -framework Foundation -lobjc -lauto -I/usr/include/libxml2 -lxml2 &quot;
     @frameworks.each { |f| line &lt;&lt; &quot;-framework #{f} &quot; }
     line &lt;&lt; execute(&quot;llvm-config --ldflags --libs core jit nativecodegen interpreter bitwriter&quot;).gsub(/\n/, '')
     objs.each { |o| line &lt;&lt; &quot; #{o}&quot; }
@@ -208,6 +223,15 @@ EOS
     path
   end
 
+  def llc_arch(arch)
+    # LLVM uses a different convention for architecture names.
+    case arch
+      when 'i386'; 'x86'
+      when 'x86_64'; 'x86-64'
+      else; arch
+    end
+  end
+
   def gen_tmpfile(base, ext)
     file = File.join(@tmpdir, &quot;#{base}.#{ext}&quot;)
     @tmpfiles &lt;&lt; file</diff>
      <filename>bin/rubyc</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b79d3d38dd39937c70015088b14cfb5093df9cc8</id>
    </parent>
  </parents>
  <author>
    <name>lsansonetti@apple.com</name>
    <email>lsansonetti@apple.com@23306eb0-4c56-4727-a40e-e92c0eb68959</email>
  </author>
  <url>http://github.com/dj2/MacRuby/commit/ee756b11ac214ef1c63aba77818ce6f1c4eb4b87</url>
  <id>ee756b11ac214ef1c63aba77818ce6f1c4eb4b87</id>
  <committed-date>2009-10-14T15:32:30-07:00</committed-date>
  <authored-date>2009-10-14T15:32:30-07:00</authored-date>
  <message>macrubyc is now able to build fat binaries

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@2796 23306eb0-4c56-4727-a40e-e92c0eb68959</message>
  <tree>858c6ce638644094e72a308d03c90d4af078e76e</tree>
  <committer>
    <name>lsansonetti@apple.com</name>
    <email>lsansonetti@apple.com@23306eb0-4c56-4727-a40e-e92c0eb68959</email>
  </committer>
</commit>
