summaryrefslogtreecommitdiff
path: root/rakelib
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-12-02 10:22:06 +0100
committerEugen Wissner <belka@caraus.de>2025-12-02 17:14:18 +0100
commit23b6f074c7f560d701e9a1fa5713a965af3a18a3 (patch)
tree87549a4eba3da8d8ed6e3fbb2e337e152a8bc96a /rakelib
parent5f7d83974114c73327ce9fff3635927df050b5e4 (diff)
downloadelna-23b6f074c7f560d701e9a1fa5713a965af3a18a3.tar.gz
Merge GCC frontend into the branch
Diffstat (limited to 'rakelib')
-rw-r--r--rakelib/gcc.rake113
-rw-r--r--rakelib/modula.rake36
2 files changed, 149 insertions, 0 deletions
diff --git a/rakelib/gcc.rake b/rakelib/gcc.rake
new file mode 100644
index 0000000..39b4442
--- /dev/null
+++ b/rakelib/gcc.rake
@@ -0,0 +1,113 @@
+# 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 'pathname'
+
+def gcc_verbose(gcc_binary)
+ read, write = IO.pipe
+ sh({'LANG' => 'C'}, gcc_binary, '--verbose', err: write)
+ write.close
+ output = read.read
+ read.close
+ output
+end
+
+def find_build_target
+ gcc_verbose(ENV.fetch 'CC', 'gcc')
+ .lines
+ .find { |line| line.start_with? 'Target: ' }
+ .split(' ')
+ .last
+ .strip
+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
+
+namespace :gcc do
+ # Dependencies.
+ GCC_VERSION = "15.2.0"
+ HOST_GCC = 'build/host/gcc'
+ HOST_INSTALL = 'build/host/install'
+ GCC_PATCH = 'https://raw.githubusercontent.com/Homebrew/formula-patches/575ffcaed6d3112916fed77d271dd3799a7255c4/gcc/gcc-15.1.0.diff'
+
+ directory HOST_GCC
+ directory HOST_INSTALL
+ directory 'build/tools'
+
+ desc 'Download and configure the bootstrap compiler'
+ task configure: ['build/tools', HOST_GCC, HOST_INSTALL] do
+ url = URI.parse "https://gcc.gnu.org/pub/gcc/releases/gcc-#{GCC_VERSION}/gcc-#{GCC_VERSION}.tar.xz"
+ build_target = find_build_target
+ source_directory = Pathname.new "build/tools/gcc-#{GCC_VERSION}"
+ frontend_link = source_directory + 'gcc'
+
+ download_and_pipe url, source_directory.dirname, ['tar', '-Jxv']
+ download_and_pipe URI.parse(GCC_PATCH), source_directory, ['patch', '-p1']
+
+ sh 'contrib/download_prerequisites', chdir: source_directory.to_path
+ File.symlink Pathname.new('.').relative_path_from(frontend_link), (frontend_link + 'elna')
+
+ configure_options = [
+ "--prefix=#{File.realpath HOST_INSTALL}",
+ '--enable-languages=c,c++,elna',
+ '--disable-bootstrap',
+ '--disable-multilib',
+ '--with-system-zlib',
+ "--target=#{build_target}",
+ "--build=#{build_target}",
+ "--host=#{build_target}"
+ ]
+ if File.symlink? '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'
+ configure_options << '--with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'
+ end
+ env = ENV.slice 'CC', 'CXX'
+ env['CFLAGS'] = env['CXXFLAGS'] = '-O0 -g -fPIC -I/opt/homebrew/opt/flex/include'
+
+ configure = source_directory.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
+end
+
+desc 'Build the bootstrap compiler'
+task gcc: %w[gcc:configure gcc:make]
diff --git a/rakelib/modula.rake b/rakelib/modula.rake
new file mode 100644
index 0000000..746ebb8
--- /dev/null
+++ b/rakelib/modula.rake
@@ -0,0 +1,36 @@
+# 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 'pathname'
+require 'rake/clean'
+
+CLEAN.include 'build/gcc'
+
+task source: ['source/main.elna', 'build/gcc/elna'] do |t|
+ sources, compiler = t.prerequisites.partition { |f| f.end_with? '.elna' }
+
+ sh *compiler, '--parse', *sources
+end
+
+rule(/gcc\/.+\.o$/ => ->(file) {
+ source = Pathname.new('source') +
+ Pathname.new(file).relative_path_from('build/gcc').sub_ext('.elna')
+
+ ['build/host/install/bin/gelna', source]
+}) do |t|
+ Pathname.new(t.name).dirname.mkpath
+ sources, compiler = t.prerequisites.partition { |source| source.end_with? '.elna' }
+
+ sh *compiler, '-c', '-O0', '-g', '-o', t.name, *sources
+end
+
+file 'build/gcc/elna' => FileList['source/**/*.elna'].reject { |file|
+ file != file.downcase
+}.map { |file|
+ Pathname.new('build/gcc') +
+ Pathname.new(file).relative_path_from('source').sub_ext('.o')
+} do |t|
+ sh 'build/host/install/bin/gcc', '-o', t.name, *t.prerequisites
+end