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
|
# 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 'fileutils'
require 'uri'
require 'net/http'
require 'open3'
require 'pathname'
# Use Homebrew GCC if available (it uses libstdc++, avoiding
# macOS-specific libc++ incompatibilities in GCC's own sources).
class GCCConfiguration
attr_reader :target
def initialize(suffix, target, sysroot_sdk)
@suffix = suffix
@target = target
@sysroot_sdk = sysroot_sdk
end
def cc
"gcc#{@suffix}"
end
def cxx
"g++#{@suffix}"
end
def configure
@sysroot_sdk.nil? ? [] : ["--with-sysroot=#{@sysroot_sdk}"]
end
end
def gcc_verbose(gcc_binary)
read, write = IO.pipe
sh({'LC_ALL' => 'C'}, gcc_binary, '--verbose', err: write)
write.close
output = read.read
read.close
output
end
def find_build_target(gcc_version)
if RUBY_PLATFORM =~ /darwin/
suffix = '-' + gcc_version.split('.').first
mac_os_sdk = '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'
else
suffix = ''
end
build_target = gcc_verbose(ENV.fetch 'CC', "gcc#{suffix}")
.lines
.find { |line| line.start_with? 'Target: ' }
.split(' ')
.last
.strip
GCCConfiguration.new suffix, build_target, mac_os_sdk
end
def download_and_pipe(url, target, command)
target.mkpath
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_pipe URI.parse(response['location']), target, command
when Net::HTTPSuccess
Dir.chdir target.to_path do
Open3.popen2(*command) 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
end
else
response.error!
end
end
end
end
# Makes a link from the source tree in the GCC tree.
def link_frontend(source, destination)
File.symlink Pathname.new(source).relative_path_from(destination), (destination + File.basename(source))
end
# Replaces an elna-native directive in the line with its dg-* equivalent,
# escaping the directive argument for dejagnu's Tcl evaluation.
def convert_directive(line, directive, dg_name)
line.sub(/\(\*\s*#{Regexp.escape directive}\s+(.*?)\s*\*\)/) do
escaped = Regexp.last_match(1).gsub(/[\\\[\]$"]/) { |character| "\\#{character}" }
"(* { #{dg_name} \"#{escaped}\" } *)"
end
end
def convert_test(source, target, category, extra_lines = [])
File.open(target, 'wb') do |output|
File.foreach(source) do |line|
line = convert_directive(line, '@Error', 'dg-error')
line = convert_directive(line, '@Flags', 'dg-additional-options')
output.puts line
end
# Runnable tests compile, link and execute.
output.puts '(* { dg-do run } *)' if category == 'runnable'
extra_lines.each { |line| output.puts line }
# Suppress excess output so stray diagnostics do not fail the test.
output.puts '(* { dg-prune-output .* } *)'
case category
when 'compilable'
# Assert that an output file was produced.
output.puts '(* { dg-final { output-exists } } *)'
when 'fail_compilation'
# Assert that no output file was produced.
output.puts '(* { dg-final { output-exists-not } } *)'
end
end
end
namespace :gcc do
# Dependencies.
GCC_VERSION = "16.1.0"
HOST_GCC = 'build/host/gcc'
HOST_INSTALL = Pathname.new 'build/host/install'
GCC_TREE = Pathname.new "build/tools/gcc-#{GCC_VERSION}"
GCC_PATCH = 'https://raw.githubusercontent.com/Homebrew/homebrew-core/refs/heads/main/Patches/gcc/gcc-16.1.0.diff'
directory HOST_GCC
directory HOST_INSTALL.to_path
directory 'build/tools'
desc 'Download the bootstrap compiler and its prerequisites'
task download: 'build/tools' do
url = URI.parse "https://gcc.gnu.org/pub/gcc/releases/gcc-#{GCC_VERSION}/gcc-#{GCC_VERSION}.tar.xz"
download_and_pipe url, GCC_TREE.dirname, ['tar', '-Jxv']
download_and_pipe URI.parse(GCC_PATCH), GCC_TREE, ['patch', '-p1']
# GCC 16.1.0 registers 17 languages but CL_PARAMS is at bit 16,
# which collides with the 17th language class (CL_Rust). Shift
# every CL_* constant from bit 16 upward by one position.
File.open(GCC_TREE + 'gcc/opts.h', 'r+') do |opts_h|
content = opts_h.read.gsub(/\(1U << (\d+)\)/) do |m|
"(1U << #{$1.to_i + 1})"
end
opts_h.seek 0, IO::SEEK_SET
opts_h.write content
end
sh 'contrib/download_prerequisites', chdir: GCC_TREE.to_path
end
desc 'Link the frontend into the GCC source tree'
task :link do
source_destination = GCC_TREE + 'gcc/elna'
test_destination = GCC_TREE + 'gcc/testsuite/elna.dg'
rm_rf [source_destination, test_destination]
mkdir_p [source_destination, test_destination]
FileList['boot', 'include', 'COPYING3', 'README.md', 'gcc/gcc', 'gcc/*.in', 'gcc/lang*'].each do |file|
link_frontend file, source_destination
end
FileList['gcc/dg.exp', 'README.md'].each do |file|
link_frontend file, test_destination
end
destination = GCC_TREE + 'gcc/testsuite/lib'
FileList['gcc/testlib/*'].each do |file|
rm_f (destination + File.basename(file))
link_frontend file, destination
end
end
desc 'Configure the bootstrap compiler'
task configure: [HOST_GCC, HOST_INSTALL.to_path] do |t|
build_target = find_build_target GCC_VERSION
configure_options = [
"--prefix=#{File.realpath t.prerequisites.last}",
'--enable-languages=c,c++,jit,elna',
'--disable-bootstrap',
'--disable-multilib',
'--enable-host-shared',
'--with-system-zlib',
"--target=#{build_target.target}",
"--build=#{build_target.target}",
"--host=#{build_target.target}",
*build_target.configure
]
env = {
'CC' => ENV['CC'] || build_target.cc,
'CXX' => ENV['CXX'] || build_target.cxx
}
env['CFLAGS'] = env['CXXFLAGS'] = '-O0 -g -fPIC -I/opt/homebrew/opt/flex/include'
configure = GCC_TREE.relative_path_from(HOST_GCC) + 'configure'
sh env, configure.to_path, *configure_options, chdir: HOST_GCC
end
desc 'Make and install the bootstrap compiler'
task :make do
sh 'make', '-j', Etc.nprocessors.to_s, chdir: HOST_GCC
sh 'make', 'install', chdir: HOST_GCC
end
desc 'Convert the testsuite into dejagnu format'
task :convert do
destination = GCC_TREE + 'gcc/testsuite/elna.dg'
destination.mkpath
source = Pathname.new 'testsuite'
# Regenerate the category directories from scratch: this removes
# converted tests whose sources are gone and adds new ones.
%w[compilable fail_compilation runnable].each do |category|
category_destination = destination + category
category_source = source + category
rm_rf category_destination
mkdir_p category_destination
category_source.each_child do |test_path|
test_target = category_destination + test_path.basename
# Multi-module tests: directories containing a sut.elna (system under
# test) and any helper files, compiled and linked together.
if test_path.directory?
main = test_path + 'sut.elna'
raise %(multi-module test directory \"#{test_path}\" must contain sut.elna) unless main
test_target.mkdir
modules = test_path.children - [main]
modules.each do |extra|
cp_r extra, test_target
end
extra_sources = test_target.glob("**/*.elna")
.map { |extra| %("#{extra.relative_path_from test_target}") }
convert_test main, (test_target + 'sut.elna'), category, [
"(* { dg-additional-options \"-I#{test_target.expand_path}\" } *)",
"(* { dg-additional-sources #{extra_sources * ' '} } *)"
]
else
convert_test test_path, test_target, category
end
end
end
end
desc 'Run tests'
task check: 'gcc:convert' do
log_file = Pathname.new(HOST_GCC) + 'gcc/testsuite/elna/elna.log'
sh 'make', 'check-elna', chdir: File.join(HOST_GCC, 'gcc')
fail "\nSee #{log_file}." if log_file.file? and log_file.read =~ /^(FAIL|XPASS|UNRESOLVED|ERROR):/
end
desc 'Run clang-tidy'
task :tidy do
compile_db = Pathname.new 'compile_commands.json'
raise "#{compile_db} is missing. Run: bear -- rake gcc:make" unless compile_db.exist?
sources = FileList['boot/*.cc', 'gcc/gcc/*.cc', 'include/elna/**/*.h']
.reject do |file|
['/elna1.', '/elna-spec.'].any? { |pattern| file.include? pattern }
end
build_target = find_build_target GCC_VERSION
includes = ['.', build_target.target].collect do |inc|
HOST_INSTALL + 'include/c++' + GCC_VERSION + inc
end
sh 'clang-tidy', '-p', compile_db.to_path, '--extra-arg=-w', '--warnings-as-errors=*',
*includes.collect { |inc| "--extra-arg=-cxx-isystem#{inc.realpath}" },
'--extra-arg=-stdlib=libstdc++', *sources
end
desc 'Build documentation'
task :doc do
sh 'make', 'html', chdir: File.join(HOST_GCC, 'gcc')
end
desc 'Run GCC linters and tests'
task test: %w[gcc:tidy gcc:check]
end
desc 'Build the bootstrap compiler'
task gcc: %w[gcc:download gcc:link gcc:configure gcc:make]
|