Last active
August 29, 2015 14:20
-
-
Save sfcgeorge/2a2e872ee7c3c092be17 to your computer and use it in GitHub Desktop.
Compile Crystal code from external app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
crystal run test.cr | |
#=> "foo" | |
crystal build test.cr | |
export CRYSTAL_PATH=/usr/local/Cellar/crystal/0.7.1/src | |
./test | |
#=> | |
ld: library not found for -levent | |
clang: error: linker command failed with exit code 1 (use -v to see invocation) | |
Error: execution of command failed with code: 1: `cc -o /var/folders/48/tq7k6jzd3k12q11n978s09qc0000gn/T/vector-salad-compiled.0ZfhBw .crystal/foo/main.o .crystal/foo/CFileIO.o .crystal/foo/GC.o .crystal/foo/Exception.o .crystal/foo/Pointer40UInt841.o .crystal/foo/ArgumentError.o .crystal/foo/Pointer40LibABI5858UnwindException41.o .crystal/foo/UInt64.o .crystal/foo/Array40String41.o .crystal/foo/Math.o .crystal/foo/Pointer40String41.o .crystal/foo/String.o .crystal/foo/String5858Builder.o .crystal/foo/Slice40T41.o .crystal/foo/Slice40UInt841.o .crystal/foo/IndexOutOfBounds.o .crystal/foo/DivisionByZero.o .crystal/foo/StaticArray40UInt844322241.o .crystal/foo/Array40Pointer40Void4141.o .crystal/foo/Pointer40Pointer40Void4141.o .crystal/foo/Fiber.o .crystal/foo/Pointer40Void41.o .crystal/foo/Array40Fiber41.o .crystal/foo/Pointer40Fiber41.o .crystal/foo/Event5858Base.o .crystal/foo/Scheduler.o .crystal/foo/CharReader.o .crystal/foo/InvalidByteSequenceError.o .crystal/foo/Bool.o .crystal/foo/Char.o .crystal/foo/StaticArray40UInt84432441.o .crystal/foo/Int32.o .crystal/foo/StaticArray40UInt844321241.o .crystal/foo/StringIO.o .crystal/foo/LEBReader.o .crystal/foo/Exception43.o .crystal/foo/Errno.o .crystal/foo/GlobError.o .crystal/foo/IO5858EOFError.o .crystal/foo/IO5858Error.o .crystal/foo/TimeFormat5858Error.o .crystal/foo/MissingKey.o .crystal/foo/DomainError.o .crystal/foo/EmptyEnumerable.o .crystal/foo/AtExitHandlers.o -levent -lpcl -lpcre -lgc -lpthread` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "compiler/crystal/**" | |
def tempfile(basename) | |
tempfile = Tempfile.new "vector-salad-#{basename}" | |
output_filename = tempfile.path | |
tempfile.close | |
output_filename | |
end | |
def execute(output_filename) | |
exit_status = LibC.system("#{output_filename}") | |
if exit_status != 0 | |
puts "Program terminated abnormally with error code: #{exit_status}" | |
exit 1 | |
end | |
File.delete output_filename | |
end | |
sources = Crystal::Compiler::Source.new("foo", %(p "foo")) | |
output_filename = tempfile "compiled" | |
compiler = Crystal::Compiler.new | |
compiler.compile sources, output_filename | |
execute output_filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment