Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal load-time DLL support on Windows, support dllimport storage class #11573

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,9 @@ 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")
var.dll_storage_class = LLVM::DLLStorageClass::DLLImport
end
var.thread_local = thread_local
end
var
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("libcmt")] # For `mainCRTStartup`
@[Link({{ flag?(:preview_dll) ? "msvcrt" : "libcmt" }})] # 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("libcmt")]
@[Link({{ flag?(:preview_dll) ? "msvcrt" : "libcmt" }})]
{% end %}
lib LibC
alias Char = UInt8
Expand Down
14 changes: 12 additions & 2 deletions src/llvm/enums.cr
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,25 @@ module LLVM
Appending
Internal
Private
DLLImport
DLLExport
DLLImport # obsolete
DLLExport # obsolete
ExternalWeak
Ghost
Common
LinkerPrivate
LinkerPrivateWeak
end

enum DLLStorageClass
Default

# Function to be imported from DLL.
DLLImport

# Function to be accessible from DLL.
DLLExport
end

enum IntPredicate
EQ = 32
NE
Expand Down
1 change: 1 addition & 0 deletions src/llvm/lib_llvm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ lib LibLLVM
fun get_initializer = LLVMGetInitializer(global_var : ValueRef) : ValueRef
fun set_linkage = LLVMSetLinkage(global : ValueRef, linkage : LLVM::Linkage)
fun get_linkage = LLVMGetLinkage(global : ValueRef) : LLVM::Linkage
fun set_dll_storage_class = LLVMSetDLLStorageClass(global : ValueRef, storage_class : LLVM::DLLStorageClass)
fun set_metadata = LLVMSetMetadata(value : ValueRef, kind_id : UInt32, node : ValueRef)
fun set_target = LLVMSetTarget(mod : ModuleRef, triple : UInt8*)
fun set_thread_local = LLVMSetThreadLocal(global_var : ValueRef, is_thread_local : Int32)
Expand Down
4 changes: 4 additions & 0 deletions src/llvm/value_methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module LLVM::ValueMethods
LibLLVM.get_linkage(self)
end

def dll_storage_class=(storage_class)
LibLLVM.set_dll_storage_class(self, storage_class)
end

def call_convention=(call_convention)
LibLLVM.set_instruction_call_convention(self, call_convention)
end
Expand Down