Class: Cry::Compiler
- Inherits:
-
Object
- Object
- Cry::Compiler
- Defined in:
- lib/cry/compiler.rb
Overview
Crystal compiler command wrapper class. This class is used to compile Crystal code to WASM.
Defined Under Namespace
Classes: CompilerError, Options
Instance Attribute Summary collapse
-
#options ⇒ Struct
readonly
Return compiler options.
Instance Method Summary collapse
-
#build_command(options = @options) ⇒ String
Return build command from options.
-
#build_wasm(crystal_code, **opts) ⇒ String
Compile Crystal code to WASM.
-
#get_crystal_library_path ⇒ String
Return CRYSTAL_LIBRARY_PATH.
-
#initialize ⇒ Compiler
constructor
A new instance of Compiler.
-
#set_crystal_library_path(path = nil) ⇒ String
Set CRYSTAL_LIBRARY_PATH If path is not given, set default path.
-
#set_options(crystal_code, **opts) ⇒ Struct
Set compiler options.
Constructor Details
Instance Attribute Details
#options ⇒ Struct (readonly)
Return compiler options
21 22 23 |
# File 'lib/cry/compiler.rb', line 21 def @options end |
Instance Method Details
#build_command(options = @options) ⇒ String
Return build command from options
111 112 113 114 |
# File 'lib/cry/compiler.rb', line 111 def build_command( = @options) link_flags = "\"#{.link_flags}" + .export.map { |n| "--export #{n} " }.join + '"' "crystal build #{.input} -o #{.output} --target #{.target} --link-flags=#{link_flags}" end |
#build_wasm(crystal_code, **opts) ⇒ String
Compile Crystal code to WASM
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cry/compiler.rb', line 53 def build_wasm(crystal_code, **opts) (crystal_code, **opts) command = build_command stdout, error, status = Open3.capture3(command) @input_tempfile&.close unless status.success? warn (crystal_code, command, error) raise CompilerError, 'Failed to compile Crystal code to WASM' end if (.output = '/dev/stdout') stdout else IO.read(.output, mode: 'rb') end end |
#get_crystal_library_path ⇒ String
Return CRYSTAL_LIBRARY_PATH
39 40 41 |
# File 'lib/cry/compiler.rb', line 39 def get_crystal_library_path ENV['CRYSTAL_LIBRARY_PATH'] end |
#set_crystal_library_path(path = nil) ⇒ String
Set CRYSTAL_LIBRARY_PATH If path is not given, set default path
28 29 30 31 32 33 34 |
# File 'lib/cry/compiler.rb', line 28 def set_crystal_library_path(path = nil) if path ENV['CRYSTAL_LIBRARY_PATH'] = path else ENV['CRYSTAL_LIBRARY_PATH'] ||= File.('../../vendor/wasm32-wasi-libs', __dir__) end end |
#set_options(crystal_code, **opts) ⇒ Struct
Set compiler options
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cry/compiler.rb', line 83 def (crystal_code, **opts) .output = opts[:output] || nil .release = opts[:release] || false .export = opts[:export] || [] .target = opts[:target] || 'wasm32-wasi' .link_flags = opts[:link_flags] || '' # Set CRYSTAL_LIBRARY_PATH set_crystal_library_path # Set output (wasm bytecode) path .output = '/dev/stdout' unless .output # Set input (crystal source) path input_tempfile = nil unless .input @input_tempfile = Tempfile.create('crystal') .input = @input_tempfile.path end File.write(.input, crystal_code) end |