@@ -406,8 +406,6 @@ module Crystal
406
406
bc_name = self .bc_name
407
407
object_name = self .object_name
408
408
409
- memory_buffer = llvm_mod.write_bitcode_to_memory_buffer
410
-
411
409
# To compile a file we first generate a `.bc` file and then
412
410
# create an object file from it. These `.bc` files are stored
413
411
# in the cache directory.
@@ -418,27 +416,61 @@ module Crystal
418
416
# `.bc` file is exactly the same as the old one. In that case
419
417
# the `.o` file will also be the same, so we simply reuse the
420
418
# old one. Generating an `.o` file is what takes most time.
421
- if ! compiler.emit && ! @bc_flags_changed && File .exists?(bc_name) && File .exists?(object_name)
422
- memory_io = IO ::Memory .new(memory_buffer.to_slice)
423
- changed = File .open(bc_name) { |bc_file | ! FileUtils .cmp(bc_file, memory_io) }
424
-
425
- # If the user cancelled a previous compilation
426
- # it might be that the .o file is empty
427
- if ! changed && File .size(object_name) > 0
428
- # We can skip compilation
429
- memory_buffer.dispose
430
- memory_buffer = nil
431
- else
432
- # We need to compile, so we'll write the memory buffer to file
419
+
420
+ must_compile = true
421
+ can_reuse_previous_compilation =
422
+ ! compiler.emit && ! @bc_flags_changed && File .exists?(bc_name) && File .exists?(object_name)
423
+
424
+ {% if LibLLVM ::IS_35 % }
425
+ # In LLVM 3.5 we can't write a bitcode to memory,
426
+ # so instead we write it to another file
427
+ bc_name_new = self .bc_name_new
428
+ llvm_mod.write_bitcode_to_file(bc_name_new)
429
+
430
+ if can_reuse_previous_compilation
431
+ if FileUtils .cmp(bc_name, bc_name_new)
432
+ # If the user cancelled a previous compilation it might be that
433
+ # the .o file is empty
434
+ if File .size(object_name) > 0
435
+ File .delete bc_name_new
436
+ must_compile = false
437
+ end
438
+ end
433
439
end
434
- end
435
440
436
- # If there's a memory buffer, it means we must create a .o from it
437
- if memory_buffer
438
- # Create the .bc file (for next compilations)
439
- File .write(bc_name, memory_buffer.to_slice)
440
- memory_buffer.dispose
441
+ if must_compile
442
+ # Create/overwrite the .bc file (for next compilations)
443
+ File .rename(bc_name_new, bc_name)
444
+ compiler.optimize llvm_mod if compiler.release?
445
+ compiler.target_machine.emit_obj_to_file llvm_mod, object_name
446
+ end
447
+ {% else % }
448
+ memory_buffer = llvm_mod.write_bitcode_to_memory_buffer
449
+
450
+ if can_reuse_previous_compilation
451
+ memory_io = IO ::Memory .new(memory_buffer.to_slice)
452
+ changed = File .open(bc_name) { |bc_file | ! FileUtils .cmp(bc_file, memory_io) }
453
+
454
+ # If the user cancelled a previous compilation
455
+ # it might be that the .o file is empty
456
+ if ! changed && File .size(object_name) > 0
457
+ must_compile = false
458
+ memory_buffer.dispose
459
+ memory_buffer = nil
460
+ else
461
+ # We need to compile, so we'll write the memory buffer to file
462
+ end
463
+ end
441
464
465
+ # If there's a memory buffer, it means we must create a .o from it
466
+ if memory_buffer
467
+ # Create the .bc file (for next compilations)
468
+ File .write(bc_name, memory_buffer.to_slice)
469
+ memory_buffer.dispose
470
+ end
471
+ {% end % }
472
+
473
+ if must_compile
442
474
compiler.optimize llvm_mod if compiler.release?
443
475
compiler.target_machine.emit_obj_to_file llvm_mod, object_name
444
476
end
@@ -477,6 +509,10 @@ module Crystal
477
509
" #{ @output_dir } /#{ @name } .bc"
478
510
end
479
511
512
+ def bc_name_new
513
+ " #{ @output_dir } /#{ @name } .new.bc"
514
+ end
515
+
480
516
def ll_name
481
517
" #{ @output_dir } /#{ @name } .ll"
482
518
end
0 commit comments