summaryrefslogtreecommitdiffhomepage
path: root/tasks/mruby_build.rake
blob: b9e0d2749cff08f5f87f3446c3e377caa1642650 (plain)
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
load 'tasks/mruby_build_gem.rake'
load 'tasks/mruby_build_commands.rake'

module MRuby
  class << self
    def targets
      @targets ||= {}
    end

    def each_target(&block)
      @targets.each do |key, target|
        target.instance_eval(&block)
      end
    end
  end

  class Toolchain
    class << self
      attr_accessor :toolchains
    end

    def initialize(name, &block)
      @name, @initializer = name.to_s, block
      MRuby::Toolchain.toolchains ||= {}
      MRuby::Toolchain.toolchains[@name] = self
    end

    def setup(conf)
      conf.instance_eval(&@initializer)
    end

    def toolchain(name)
      @@toolchains[name.to_s].setup(self)
    end

    def self.load
      Dir.glob("#{File.dirname(__FILE__)}/toolchains/*.rake").each do |file|
        Kernel.load file
      end
    end
  end
  Toolchain.load

  class Build
    class << self
      attr_accessor :current
    end
    include Rake::DSL
    include LoadGems
    attr_accessor :name, :bins, :exts, :file_separator
    attr_reader :root, :libmruby, :gems

    COMPILERS = %w(cc cxx objc asm)
    COMMANDS = COMPILERS + %w(linker archiver yacc gperf git exts mrbc)
    attr_block MRuby::Build::COMMANDS

    Exts = Struct.new(:object, :executable, :library)

    def initialize(name='host', &block)
      MRuby::Build.current = self
      @name = name
      @root = File.expand_path("#{File.dirname(__FILE__)}/..")

      if ENV['OS'] == 'Windows_NT'
        @exts = Exts.new('.o', '.exe', '.a')
      else
        @exts = Exts.new('.o', '', '.a')
      end

      @file_separator = '/'
      @cc = Command::Compiler.new(self, %w(.c))
      @cxx = Command::Compiler.new(self, %w(.cc .cxx .cpp))
      @objc = Command::Compiler.new(self, %w(.m))
      @asm = Command::Compiler.new(self, %w(.S .asm))
      @linker = Command::Linker.new(self)
      @archiver = Command::Archiver.new(self)
      @yacc = Command::Yacc.new(self)
      @gperf = Command::Gperf.new(self)
      @git = Command::Git.new(self)
      @mrbc = Command::Mrbc.new(self)

      @bins = %w(mruby mrbc mirb)
      @gems, @libmruby = [], []

      MRuby.targets[name.to_s] = self

      instance_eval(&block)

      compilers.each do |compiler|
        compiler.defines -= %w(DISABLE_GEMS) if respond_to?(:enable_gems?) && enable_gems?
        compiler.define_rules build_dir
      end
    end

    def toolchain(name)
      Toolchain.toolchains[name.to_s].setup(self)
    end

    def build_dir
      "build/#{self.name}"
    end

    def mrbcfile
      exefile("build/host/bin/mrbc")
    end

    def compilers
      COMPILERS.map do |c|
        instance_variable_get("@#{c}")
      end
    end

    def filename(name)
      if name.is_a?(Array)
        name.flatten.map { |n| filename(n) }
      else
        '"%s"' % name.gsub('/', file_separator)
      end
    end

    def exefile(name)
      if name.is_a?(Array)
        name.flatten.map { |n| exefile(n) }
      else
        "#{name}#{exts.executable}"
      end
    end

    def objfile(name)
      if name.is_a?(Array)
        name.flatten.map { |n| objfile(n) }
      else
        "#{name}#{exts.object}"
      end
    end

    def libfile(name)
      if name.is_a?(Array)
        name.flatten.map { |n| libfile(n) }
      else
        "#{name}#{exts.library}"
      end
    end

    def run_test
      puts ">>> Test #{name} <<<"
      mrbtest = exefile("#{build_dir}/test/mrbtest")
      sh "#{filename mrbtest}"
      puts 
    end
  end # Build

  class CrossBuild < Build
    def run_test
      mrbtest = exefile("#{build_dir}/test/mrbtest")
      puts "You should run #{mrbtest} on target device."
      puts 
    end
  end # CrossBuild
end # MRuby