#!/usr/bin/env ruby # Copyright 2024 Kyrylo Shyshko # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'kompiler' # Print the help screen if requested if (ARGV.size == 1) && ["--help", "-h"].include?(ARGV[0]) puts """Usage: kompile [options] | [ [architecture]] Available options: --help, -h Prints this screen --list-architectures Prints available architectures -L Alias to --list-architectures To compile, run: kompile [architecture] Arguments: The path to the input file The path where the compiled output will be saved [architecture] (Optional) One of the installed architectures to use for compilation (defaults to \"armv8a\") """ exit # Exit end # Print the available architectures if requested if (ARGV.size == 1) && ["--list-architectures", "-L"].include?(ARGV[0]) Kompiler::ArchManager.load_all_entries() puts "Available architectures:" Kompiler::ArchManager.entries.each do |entry| puts " #{entry[:name]}" end exit # Exit end in_filename = ARGV[0] out_filename = ARGV[1] arch_name = ARGV[2] || "armv8a" if !in_filename puts "Error: No input file provided" exit end if !out_filename puts "Error: No output file provided" exit end if !File.exist?(in_filename) puts "Error: #{in_filename} doesn't exist" exit end # Load all the architecture entries Kompiler::ArchManager.load_all_entries() # Find the right architecture arch = Kompiler::ArchManager.get_arch(arch_name) if arch == nil puts "Error: Architecture \"#{arch_name}\" not found." exit end # Get the include path for the architecture arch_include_path = arch[:include_path] # Load the architecture begin require arch_include_path rescue LoadError => e puts "Error: Could not load #{arch_name} architecture's configuration." exit end code = File.read(in_filename) compiled_bytes_str = Kompiler::CompilerFunctions.compile(code, [in_filename]) File.open(out_filename, "wb") do |file| file.write compiled_bytes_str end