aboutsummaryrefslogtreecommitdiff
path: root/bin/cross_toolchain.rb
blob: 42b18659d83063e39ddff6d970b50bfb55d3ac68 (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
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
#!/usr/bin/env ruby
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/. -}

# frozen_string_literal: true

require 'uri'
require 'net/http'
require 'open3'
require 'etc'
require 'fileutils'
require 'pathname'
require 'term/ansicolor'

BINUTILS_VERSION = '2.45'
GLIBC_VERSION = '2.42'
KERNEL_VERSION = '6.1.150'
GCC_VERSION = "15.2.0"

TMP = Pathname.new('./tmp')

class BuildTarget
  def initialize(architecture)
    case architecture
    when /^r(isc)?v32$/
      @architecture = :rv32
    else
      raise "Unsupported architecture '#{architecture}'."
    end
  end

  def sysroot
    TMP + 'sysroot'
  end

  def rootfs
    TMP + 'rootfs'
  end

  def target
    case @architecture
    when :rv32
      'riscv32-unknown-linux-gnu'
    end
  end

  def build
    @build ||= gcc_verbose(ENV['CC']).lines
      .find { |line| line.start_with? 'Target: ' }
      .split(' ').last.strip
  end

  def configuration(prefix)
    case @architecture
    when :rv32
      ['arch=rv32imafdc', 'abi=ilp32d', 'tune=rocket', 'isa-spec=20191213'].map do |option|
        "-#{prefix}#{option}"
      end
    else
      []
    end
  end
end

def gcc_verbose(gcc_binary)
  read, write = IO.pipe
  system({'LANG' => 'C'}, gcc_binary, '--verbose', err: write)
  write.close
  output = read.read
  read.close
  output
end

def download_and_unarchive(url)
  puts Term::ANSIColor.green "Downloading #{url}."

  case File.extname url.path
  when '.bz2'
    archive_type = '-j'
    root_directory = File.basename url.path, '.tar.bz2'
  when '.xz'
    archive_type = '-J'
    root_directory = File.basename url.path, '.tar.xz'
  else
    raise "Unsupported archive type #{url.path}."
  end
  target = TMP + 'tools'
  Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') do |http|
    request = Net::HTTP::Get.new url.request_uri

    http.request request do |response|
      case response
      when Net::HTTPRedirection
        download_and_unarchive URI.parse(response['location'])
      when Net::HTTPSuccess
        Open3.popen2 'tar', '-C', target.to_path, archive_type, '-xv' do |stdin, stdout, wait_thread|
          Thread.new do
            stdout.each { |line| puts line }
          end

          response.read_body do |chunk|
            stdin.write chunk
          end
          stdin.close

          wait_thread.value
        end
      else
        response.error!
      end
    end
  end
  target + root_directory
end

def configure_make_install(source_directory, configure_options, env, cwd)
  configure = source_directory.relative_path_from(cwd) + 'configure'
  system env, configure.to_path, *configure_options, chdir: cwd.to_path, exception: true
  system 'make', '-j', Etc.nprocessors.to_s, chdir: cwd.to_path, exception: true
  system env, 'make', 'install', chdir: cwd.to_path, exception: true
end

def major(version)
  version.split('.').first
end

def environment
  binary_suffix = ''
  output = gcc_verbose 'gcc'

  if output.start_with? 'Apple clang'
    binary_suffix = "-#{major(GCC_VERSION)}"
  end

  ENV['CC'] = "gcc#{binary_suffix}"
  ENV['CXX'] = "g++#{binary_suffix}"
end

# Build cross binutils.
def binutils(options)
  source_directory = download_and_unarchive(
    URI.parse("https://ftpmirror.gnu.org/gnu/binutils/binutils-#{BINUTILS_VERSION}.tar.xz"))

  cwd = source_directory.dirname + 'build-binutils'
  cwd.mkpath
  options.rootfs.mkpath
  options.sysroot.mkpath

  env = ENV.slice 'CC', 'CXX'
  configure_options = [
    "--prefix=#{options.rootfs.realpath}",
    "--target=#{options.target}",
    "--with-sysroot=#{options.sysroot.realpath}",
    '--disable-nls',
    '--enable-gprofng=no',
    '--disable-werror',
    '--enable-default-hash-style=gnu',
    '--disable-libquadmath'
  ]
  configure_make_install source_directory, configure_options, env, cwd
end

# Build stage 1 GCC.
def gcc1(options)
  source_directory = download_and_unarchive(
    URI.parse("https://gcc.gnu.org/pub/gcc/releases/gcc-#{GCC_VERSION}/gcc-#{GCC_VERSION}.tar.xz"))

  cwd = source_directory.dirname + 'build-gcc'
  cwd.mkpath
  options.rootfs.mkpath

  system 'contrib/download_prerequisites', chdir: source_directory.to_path, exception: true
  configure_options = options.configuration('-with-') + [
    "--prefix=#{options.rootfs.realpath}",
    "--with-sysroot=#{options.sysroot.realpath}",
    '--enable-languages=c,c++',
    '--disable-shared',
    '--disable-bootstrap',
    '--disable-multilib',
    '--disable-libmudflap',
    '--disable-libssp',
    '--disable-libquadmath',
    '--disable-libsanitizer',
    '--disable-threads',
    '--disable-libatomic',
    '--disable-libgomp',
    '--disable-libvtv',
    '--disable-libstdcxx',
    '--disable-nls',
    '--with-newlib',
    '--without-headers',
    "--target=#{options.target}",
    "--build=#{options.build}",
    "--host=#{options.build}"
  ]
  flags = '-O2 -fPIC'
  env = {
    'CC' => ENV['CC'],
    'CXX' => ENV['CXX'],
    'CFLAGS' => flags,
    'CXXFLAGS' => flags,
    'PATH' => "#{options.rootfs.realpath + 'bin'}:#{ENV['PATH']}"
  }
  configure_make_install source_directory, configure_options, env, cwd
end

# Copy glibc headers.
def headers(options)
  source_directory = download_and_unarchive(
    URI.parse("https://ftpmirror.gnu.org/gnu/glibc/glibc-#{GLIBC_VERSION}.tar.xz"))
  include_directory = TMP + 'tools/include'

  include_directory.mkpath
  FileUtils.cp (source_directory + 'elf/elf.h'), (include_directory + 'elf.h')
end

# Build linux kernel.
def kernel(options)
  cwd = download_and_unarchive(
    URI.parse("https://cdn.kernel.org/pub/linux/kernel/v#{major(KERNEL_VERSION)}.x/linux-#{KERNEL_VERSION}.tar.xz"))

  env = {
    'CROSS_COMPILE' => "#{options.target}-",
    'ARCH' => 'riscv',
    'PATH' => "#{options.rootfs.realpath + 'bin'}:#{ENV['PATH']}",
    'HOSTCFLAGS' => "-D_UUID_T -D__GETHOSTUUID_H -I#{TMP + 'tools/include'}"
  }
  system env, 'make', 'rv32_defconfig', chdir: cwd.to_path, exception: true
  # system env, 'make', '-j', Etc.nprocessors.to_s, chdir: cwd.to_path, exception: true
  system env, 'make', 'headers', chdir: cwd.to_path, exception: true

  user_directory = options.sysroot + 'usr'

  user_directory.mkpath
  FileUtils.cp_r (cwd + 'usr/include'), (user_directory + 'include')
end

# Build glibc.
def glibc(options)
  source_directory = TMP + "tools/glibc-#{GLIBC_VERSION}"
  configure_options = [
    '--prefix=/usr',
    "--host=#{options.target}",
    "--target=#{options.target}",
    "--build=#{options.build}",
    "--enable-kernel=#{KERNEL_VERSION}",
    "--with-headers=#{options.sysroot.realpath + 'usr/include'}",
    '--disable-nscd',
    '--disable-libquadmath',
    '--disable-libitm',
    '--disable-werror',
    'libc_cv_forced_unwind=yes'
  ]
  bin = options.rootfs.realpath + 'bin'
  env = {
    'PATH' => "#{bin}:#{ENV['PATH']}",
    'MAKE' => 'make', # Otherwise it uses gnumake which can be different and too old.
    'CC' => "#{options.target}-gcc",
    'CXX' => "#{options.target}-g++"
  }
  cwd = source_directory.dirname + 'build-glibc'
  cwd.mkpath

  configure = source_directory.relative_path_from(cwd) +'./configure'
  system env, configure.to_path, *configure_options, chdir: cwd.to_path, exception: true
  system env, 'make', '-j', Etc.nprocessors.to_s, chdir: cwd.to_path, exception: true
  system env, 'make', "install_root=#{options.sysroot.realpath}", 'install', chdir: cwd.to_path, exception: true
end

# Build stage 2 GCC.
def gcc2(options)
  source_directory = TMP + "tools/gcc-#{GCC_VERSION}"
  cwd = TMP + 'tools/build-gcc'

  FileUtils.rm_rf cwd
  cwd.mkpath

  configure_options = options.configuration('-with-') + [
    "--prefix=#{options.rootfs.realpath}",
    "--with-sysroot=#{options.sysroot.realpath}",
    '--enable-languages=c,c++,lto',
    '--enable-lto',
    '--enable-shared',
    '--disable-bootstrap',
    '--disable-multilib',
    '--enable-checking=release',
    '--disable-libssp',
    '--disable-libquadmath',
    '--enable-threads=posix',
    '--with-default-libstdcxx-abi=new',
    '--disable-nls',
    "--target=#{options.target}",
    "--build=#{options.build}",
    "--host=#{options.build}"
  ]
  flags = '-O2 -fPIC -I/opt/homebrew/opt/flex/include'
  env = {
    'CFLAGS' => flags,
    'CXXFLAGS' => flags,
    'PATH' => "#{options.rootfs.realpath + 'bin'}:#{ENV['PATH']}"
  }
  configure_make_install source_directory, configure_options, env, cwd
end

target = ARGV.fetch 0, 'riscv32'
options = BuildTarget.new target

(TMP + 'tools').mkpath
environment

binutils options
gcc1 options
headers options
kernel options
glibc options
gcc2 options