Skip to content

Commit

Permalink
Respect --static on Windows (#14292)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Feb 20, 2024
1 parent 9828371 commit ee2d34c
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 13 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/win_build_portable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ jobs:
- name: Build Crystal
run: |
bin/crystal.bat env
make -f Makefile.win -B ${{ inputs.release && 'release=1' || '' }}
# TODO: the 1.11.2 compiler only understands `-Dpreview_dll`; remove this once the
# base compiler is updated
make -f Makefile.win -B FLAGS=-Dpreview_dll ${{ inputs.release && 'release=1' || '' }}
# TODO: 1.11.2 comes with LLVM 17's DLL and copies it to the output directory, but a
# dynamically linked compiler requires LLVM 18, so we must overwrite it; remove this
# line once the base compiler is updated
cp dlls/LLVM-C.dll .build/
- name: Download shards release
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ LLVM_VERSION := $(if $(LLVM_CONFIG),$(shell $(LLVM_CONFIG) --version))
LLVM_EXT_DIR = src\llvm\ext
LLVM_EXT_OBJ = $(LLVM_EXT_DIR)\llvm_ext.obj
DEPS = $(LLVM_EXT_OBJ)
CXXFLAGS += $(if $(debug),/MTd /Od,/MT)
CXXFLAGS += $(if $(static),$(if $(debug),/MTd /Od ,/MT ),$(if $(debug),/MDd /Od ,/MD ))
CRYSTAL_VERSION ?= $(shell type src\VERSION)

prefix ?= $(or $(ProgramW6432),$(ProgramFiles))\crystal
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ module Crystal
unless var
var = llvm_mod.globals.add(llvm_c_return_type(type), name)
var.linkage = LLVM::Linkage::External
if @program.has_flag?("win32") && @program.has_flag?("preview_dll")
if @program.has_flag?("win32") && !@program.has_flag?("static")
var.dll_storage_class = LLVM::DLLStorageClass::DLLImport
end
var.thread_local = thread_local
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ module Crystal
{% end %}

{% if flag?(:windows) %}
copy_dlls(program, output_filename) if program.has_flag?("preview_dll")
copy_dlls(program, output_filename) unless static?
{% end %}
end

Expand Down Expand Up @@ -442,7 +442,7 @@ module Crystal

{% if flag?(:msvc) %}
unless @cross_compile
extra_suffix = program.has_flag?("preview_dll") ? "-dynamic" : "-static"
extra_suffix = static? ? "-static" : "-dynamic"
search_result = Loader.search_libraries(Process.parse_arguments_windows(link_args.join(' ').gsub('\n', ' ')), extra_suffix: extra_suffix)
if not_found = search_result.not_found?
error "Cannot locate the .lib files for the following libraries: #{not_found.join(", ")}"
Expand Down
3 changes: 0 additions & 3 deletions src/compiler/crystal/interpreter/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class Crystal::Repl::Context

def initialize(@program : Program)
@program.flags << "interpreted"
{% if flag?(:win32) %}
@program.flags << "preview_dll"
{% end %}

@gc_references = [] of Void*

Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/wmain.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "c/stdlib"

{% begin %}
# we have both `main` and `wmain`, so we must choose an unambiguous entry point
@[Link({{ flag?(:preview_dll) ? "msvcrt" : "libcmt" }}, ldflags: "/ENTRY:wmainCRTStartup")]
@[Link({{ flag?(:static) ? "libcmt" : "msvcrt" }}, ldflags: "/ENTRY:wmainCRTStartup")]
{% end %}
lib LibCrystalMain
end
Expand Down
2 changes: 1 addition & 1 deletion src/empty.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "primitives"

{% if flag?(:win32) %}
@[Link({{ flag?(:preview_dll) ? "msvcrt" : "libcmt" }})] # For `mainCRTStartup`
@[Link({{ flag?(:static) ? "libcmt" : "msvcrt" }})] # For `mainCRTStartup`
{% end %}
lib LibCrystalMain
@[Raises]
Expand Down
2 changes: 1 addition & 1 deletion src/lib_c.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if flag?(:win32) %}
@[Link({{ flag?(:preview_dll) ? "ucrt" : "libucrt" }})]
@[Link({{ flag?(:static) ? "libucrt" : "ucrt" }})]
{% end %}
lib LibC
alias Char = UInt8
Expand Down
2 changes: 1 addition & 1 deletion src/llvm/lib_llvm.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% begin %}
{% if flag?(:win32) && flag?(:preview_dll) %}
{% if flag?(:win32) && !flag?(:static) %}
{% config = nil %}
{% for dir in Crystal::LIBRARY_PATH.split(';') %}
{% config ||= read_file?("#{dir.id}/llvm_VERSION") %}
Expand Down
2 changes: 1 addition & 1 deletion src/raise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end
require "exception/lib_unwind"

{% begin %}
@[Link({{ flag?(:preview_dll) ? "vcruntime" : "libvcruntime" }})]
@[Link({{ flag?(:static) ? "libvcruntime" : "vcruntime" }})]
{% end %}
lib LibC
fun _CxxThrowException(ex : Void*, throw_info : Void*) : NoReturn
Expand Down

0 comments on commit ee2d34c

Please sign in to comment.