Skip to content

Commit

Permalink
Resolve the always-present Windows linker's warnings (crystal-lang#9307)
Browse files Browse the repository at this point in the history
* Resolve the always-present Windows linker's warnings

Currently when building on Windows, the version of MSVC linker is always printed first, then the list of all inputs, then a warning for every object file because it ends with .o, not .obj.

So, use the .obj extension and disable the default outputs. So errors are still printed, but normally the output is empty.

* Also switch the file extension for llvm_ext.obj
  • Loading branch information
oprypin authored May 16, 2020
1 parent d49375b commit 60bdcf0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ jobs:
- name: Cross-compile Crystal
run: |
LLVM_TARGETS=X86 bin/crystal build --cross-compile --target x86_64-pc-windows-msvc src/compiler/crystal.cr -Dwithout_playground
mv crystal.o crystal.obj || true # TODO: Remove this after 0.35.0
- name: Upload Crystal object file
uses: actions/upload-artifact@v1
with:
name: objs
path: crystal.o
path: crystal.obj

windows-job:
needs: linux-job
Expand Down Expand Up @@ -146,10 +147,10 @@ jobs:
name: objs
- name: Build LLVM extensions
run: |
cl /MT /c src\llvm\ext\llvm_ext.cc -I llvm\include /Fosrc\llvm\ext\llvm_ext.o
cl /MT /c src\llvm\ext\llvm_ext.cc -I llvm\include /Fosrc\llvm\ext\llvm_ext.obj
- name: Link Crystal executable
run: |
Invoke-Expression "cl objs\crystal.o /Fecrystal-cross src\llvm\ext\llvm_ext.o $(llvm\bin\llvm-config.exe --libs) libs\pcre.lib libs\gc.lib advapi32.lib libcmt.lib legacy_stdio_definitions.lib /F10000000"
Invoke-Expression "cl objs\crystal.obj /Fecrystal-cross src\llvm\ext\llvm_ext.obj $(llvm\bin\llvm-config.exe --libs) libs\pcre.lib libs\gc.lib advapi32.lib libcmt.lib legacy_stdio_definitions.lib /F10000000"
- name: Re-build Crystal
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ all_spec
/tmp
/docs/
/src/llvm/ext/llvm_ext.o
/src/llvm/ext/llvm_ext.obj
/src/llvm/ext/llvm_ext.dwo
/src/ext/*.o
/src/ext/libcrystal.a
4 changes: 4 additions & 0 deletions src/compiler/crystal/codegen/link.cr
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ module Crystal
end

class Program
def object_extension
has_flag?("windows") ? ".obj" : ".o"
end

def lib_flags
has_flag?("windows") ? lib_flags_windows : lib_flags_posix
end
Expand Down
17 changes: 10 additions & 7 deletions src/compiler/crystal/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ module Crystal
units = llvm_modules.map do |type_name, info|
llvm_mod = info.mod
llvm_mod.target = target_triple
CompilationUnit.new(self, type_name, llvm_mod, output_dir, bc_flags_changed)
CompilationUnit.new(self, program, type_name, llvm_mod, output_dir, bc_flags_changed)
end

if @cross_compile
Expand Down Expand Up @@ -304,7 +304,7 @@ module Crystal
private def cross_compile(program, units, output_filename)
unit = units.first
llvm_mod = unit.llvm_mod
object_name = "#{output_filename}.o"
object_name = output_filename + program.object_extension

optimize llvm_mod if @release

Expand All @@ -327,7 +327,7 @@ module Crystal
# Execute and expand `subcommands`.
lib_flags = lib_flags.gsub(/`(.*?)`/) { `#{$1}` } if expand

args = %(#{object_names.join(" ")} "/Fe#{output_filename}" #{lib_flags} #{@link_flags})
args = %(/nologo #{object_names.join(" ")} "/Fe#{output_filename}" #{lib_flags} #{@link_flags})
cmd = "#{CL} #{args}"

if cmd.to_utf16.size > 32000
Expand Down Expand Up @@ -597,9 +597,10 @@ module Crystal
getter original_name
getter llvm_mod
getter? reused_previous_compilation = false
@object_extension : String

def initialize(@compiler : Compiler, @name : String, @llvm_mod : LLVM::Module,
@output_dir : String, @bc_flags_changed : Bool)
def initialize(@compiler : Compiler, program : Program, @name : String,
@llvm_mod : LLVM::Module, @output_dir : String, @bc_flags_changed : Bool)
@name = "_main" if @name == ""
@original_name = @name
@name = String.build do |str|
Expand All @@ -621,6 +622,8 @@ module Crystal
# 17 chars from name + 1 (dash) + 32 (md5) = 50
@name = "#{@name[0..16]}-#{Digest::MD5.hexdigest(@name)}"
end

@object_extension = program.object_extension
end

def compile
Expand Down Expand Up @@ -721,7 +724,7 @@ module Crystal
llvm_mod.print_to_file "#{output_filename}.ll"
end
if emit_target.obj?
FileUtils.cp(object_name, "#{output_filename}.o")
FileUtils.cp(object_name, output_filename + @object_extension)
end
end

Expand All @@ -730,7 +733,7 @@ module Crystal
end

def object_filename
"#{@name}.o"
@name + @object_extension
end

def temporary_object_name
Expand Down
6 changes: 5 additions & 1 deletion src/llvm/lib_llvm_ext.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require "./lib_llvm"
@[Link(ldflags: "#{__DIR__}/ext/llvm_ext.o")]
{% if flag?(:win32) %}
@[Link(ldflags: "#{__DIR__}/ext/llvm_ext.obj")]
{% else %}
@[Link(ldflags: "#{__DIR__}/ext/llvm_ext.o")]
{% end %}
lib LibLLVMExt
alias Char = LibC::Char
alias Int = LibC::Int
Expand Down

0 comments on commit 60bdcf0

Please sign in to comment.