Skip to content

Commit

Permalink
fix: make cbLibPath relocatable on sysimages, with RelocatableFolders.
Browse files Browse the repository at this point in the history
  • Loading branch information
ven-k committed Oct 23, 2023
1 parent 35112f1 commit 1059825
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 50 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
FMICore = "8af89139-c281-408e-bce2-3005eb87462f"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"

[compat]
EzXML = "1.1.0"
FMICore = "0.18.0"
RelocatableFolders = "1"
ZipFile = "0.10.0"
julia = "1.6"
92 changes: 47 additions & 45 deletions src/FMI2/ext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ using Libdl
using ZipFile
import Downloads

const CB_LIB_PATH = @path joinpath(dirname(@__FILE__), "callbackFunctions", "binaries")

"""
fmi2Unzip(pathToFMU::String; unpackPath=nothing, cleanup=true)
Expand Down Expand Up @@ -105,8 +107,8 @@ end

"""
fmi2Load(pathToFMU::String;
unpackPath=nothing,
type=nothing,
unpackPath=nothing,
type=nothing,
cleanup=true)
Sets the properties of the fmu by reading the modelDescription.xml.
Expand All @@ -132,9 +134,9 @@ function fmi2Load(pathToFMU::String; unpackPath::Union{String, Nothing}=nothing,
# Create uninitialized FMU

if isa(logLevel, Symbol)
if logLevel == :info
logLevel = FMULogLevelInfo
elseif logLevel == :warn
if logLevel == :info
logLevel = FMULogLevelInfo
elseif logLevel == :warn
logLevel = FMULogLevelWarn
elseif logLevel == :error
logLevel = FMULogLevelError
Expand Down Expand Up @@ -329,7 +331,7 @@ function loadBinary(fmu::FMU2)
end

function unloadBinary(fmu::FMU2)

# retrieve functions
fmu.cInstantiate = @cfunction(FMICore.unload_fmi2Instantiate, fmi2Component, (fmi2String, fmi2Type, fmi2String, fmi2String, Ptr{fmi2CallbackFunctions}, fmi2Boolean, fmi2Boolean))
fmu.cGetTypesPlatform = @cfunction(FMICore.unload_fmi2GetTypesPlatform, fmi2String, ())
Expand Down Expand Up @@ -397,18 +399,18 @@ end

lk_fmi2Instantiate = ReentrantLock()
"""
fmi2Instantiate!(fmu::FMU2;
instanceName::String=fmu.modelName,
type::fmi2Type=fmu.type,
pushComponents::Bool = true,
visible::Bool = false,
loggingOn::Bool = fmu.executionConfig.loggingOn,
fmi2Instantiate!(fmu::FMU2;
instanceName::String=fmu.modelName,
type::fmi2Type=fmu.type,
pushComponents::Bool = true,
visible::Bool = false,
loggingOn::Bool = fmu.executionConfig.loggingOn,
externalCallbacks::Bool = fmu.executionConfig.externalCallbacks,
logStatusOK::Bool=true,
logStatusWarning::Bool=true,
logStatusDiscard::Bool=true,
logStatusError::Bool=true,
logStatusFatal::Bool=true,
logStatusOK::Bool=true,
logStatusWarning::Bool=true,
logStatusDiscard::Bool=true,
logStatusError::Bool=true,
logStatusFatal::Bool=true,
logStatusPending::Bool=true)
Create a new instance of the given fmu, adds a logger if logginOn == true.
Expand Down Expand Up @@ -438,18 +440,18 @@ Create a new instance of the given fmu, adds a logger if logginOn == true.
See also [`fmi2Instantiate`](#@ref).
"""
function fmi2Instantiate!(fmu::FMU2;
instanceName::String=fmu.modelName,
type::fmi2Type=fmu.type,
pushComponents::Bool = true,
visible::Bool = false,
loggingOn::Bool = fmu.executionConfig.loggingOn,
function fmi2Instantiate!(fmu::FMU2;
instanceName::String=fmu.modelName,
type::fmi2Type=fmu.type,
pushComponents::Bool = true,
visible::Bool = false,
loggingOn::Bool = fmu.executionConfig.loggingOn,
externalCallbacks::Bool = fmu.executionConfig.externalCallbacks,
logStatusOK::Bool=true,
logStatusWarning::Bool=true,
logStatusDiscard::Bool=true,
logStatusError::Bool=true,
logStatusFatal::Bool=true,
logStatusOK::Bool=true,
logStatusWarning::Bool=true,
logStatusDiscard::Bool=true,
logStatusError::Bool=true,
logStatusFatal::Bool=true,
logStatusPending::Bool=true)

compEnv = FMU2ComponentEnvironment()
Expand All @@ -465,7 +467,7 @@ function fmi2Instantiate!(fmu::FMU2;
if fmu.callbackLibHandle == C_NULL
@assert Sys.WORD_SIZE == 64 "`externalCallbacks=true` is only supported for 64-bit."

cbLibPath = joinpath(dirname(@__FILE__), "callbackFunctions", "binaries")
cbLibPath = CB_LIB_PATH
if Sys.iswindows()
cbLibPath = joinpath(cbLibPath, "win64", "callbackFunctions.dll")
elseif Sys.islinux()
Expand Down Expand Up @@ -496,9 +498,9 @@ function fmi2Instantiate!(fmu::FMU2;
guidStr = "$(fmu.modelDescription.guid)"

global lk_fmi2Instantiate

lock(lk_fmi2Instantiate) do

component = nothing
compAddr = fmi2Instantiate(fmu.cInstantiate, pointer(instanceName), type, pointer(guidStr), pointer(fmu.fmuResourceLocation), Ptr{fmi2CallbackFunctions}(pointer_from_objref(callbackFunctions)), fmi2Boolean(visible), fmi2Boolean(loggingOn))

Expand All @@ -523,7 +525,7 @@ function fmi2Instantiate!(fmu::FMU2;
component.callbackFunctions = callbackFunctions
component.instanceName = instanceName
component.type = type

if pushComponents
push!(fmu.components, component)
end
Expand All @@ -533,7 +535,7 @@ function fmi2Instantiate!(fmu::FMU2;
component.loggingOn = loggingOn
component.visible = visible

# Jacobians
# Jacobians

# smpFct = (mtx, ∂f_refs, ∂x_refs) -> fmi2SampleJacobian!(mtx, component, ∂f_refs, ∂x_refs)
# updFct = nothing
Expand All @@ -546,7 +548,7 @@ function fmi2Instantiate!(fmu::FMU2;
# component.∂ẋ_∂x = FMICore.FMU2Jacobian{fmi2Real, fmi2ValueReference}(component, updFct)
# component.∂ẋ_∂u = FMICore.FMU2Jacobian{fmi2Real, fmi2ValueReference}(component, updFct)
# component.∂ẋ_∂p = FMICore.FMU2Jacobian{fmi2Real, fmi2ValueReference}(component, updFct)

# component.∂y_∂x = FMICore.FMU2Jacobian{fmi2Real, fmi2ValueReference}(component, updFct)
# component.∂y_∂u = FMICore.FMU2Jacobian{fmi2Real, fmi2ValueReference}(component, updFct)
# component.∂y_∂p = FMICore.FMU2Jacobian{fmi2Real, fmi2ValueReference}(component, updFct)
Expand All @@ -558,7 +560,7 @@ function fmi2Instantiate!(fmu::FMU2;
# register component for current thread
fmu.threadComponents[Threads.threadid()] = component
end

return getCurrentComponent(fmu)
end

Expand Down Expand Up @@ -589,7 +591,7 @@ Free the allocated memory, close the binaries and remove temporary zip and unzip
- `fmu::FMU2`: Mutable struct representing a FMU and all it instantiated instances in the FMI 2.0.2 Standard.
- `cleanUp::Bool= true`: Defines if the file and directory should be deleted.
# Keywords
# Keywords
- `secure_pointers=true` whether pointers to C-functions should be overwritten with dummies with Julia assertions, instead of pointing to dead memory (slower, but more user safe)
"""
function fmi2Unload(fmu::FMU2, cleanUp::Bool = true; secure_pointers::Bool=true)
Expand Down Expand Up @@ -934,11 +936,11 @@ function fmi2GetJacobian!(jac::AbstractMatrix{fmi2Real},
# end

if length(sensitive_rdx) > 0

fmi2GetDirectionalDerivative!(comp, sensitive_rdx, [rx[i]], view(jac, sensitive_rdx_inds, i))

# jac[sensitive_rdx_inds, i] = fmi2GetDirectionalDerivative(comp, sensitive_rdx, [rx[i]])

end
end

Expand Down Expand Up @@ -1131,9 +1133,9 @@ end


"""
fmi2Set(comp::FMU2Component,
vrs::fmi2ValueReferenceFormat,
srcArray::AbstractArray;
fmi2Set(comp::FMU2Component,
vrs::fmi2ValueReferenceFormat,
srcArray::AbstractArray;
filter=nothing)
Stores the specific value of `fmi2ScalarVariable` containing the modelVariables with the identical fmi2ValueReference and returns an array that indicates the Status.
Expand Down Expand Up @@ -1379,7 +1381,7 @@ end
"""
fmi2GetUnit(st::fmi2SimpleType)
Returns the `unit` entry (a string) of the corresponding simple type `st` if it has the
Returns the `unit` entry (a string) of the corresponding simple type `st` if it has the
attribute `Real` and `nothing` otherwise.
# Source
Expand Down Expand Up @@ -1423,7 +1425,7 @@ end
"""
fmi2GetDeclaredType(md::fmi2ModelDescription, mv::fmi2ScalarVariable)
Returns the `fmi2SimpleType` of the corresponding model variable `mv` as defined in
Returns the `fmi2SimpleType` of the corresponding model variable `mv` as defined in
`md.typeDefinitions`.
If `mv` does not have a declared type, return `nothing`.
If `mv` has a declared type, but it is not found, issue a warning and return `nothing`.
Expand Down Expand Up @@ -1456,7 +1458,7 @@ end
fmi2GetSimpleTypeAttributeStruct(st::fmi2SimpleType)
Returns the attribute structure for the simple type `st`.
Depending on definition, this is either `st.Real`, `st.Integer`, `st.String`,
Depending on definition, this is either `st.Real`, `st.Integer`, `st.String`,
`st.Boolean` or `st.Enumeration`.
# Arguments
Expand Down
12 changes: 7 additions & 5 deletions src/FMIImport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ using FMICore: fast_copy!
using FMICore.Requires
import FMICore.ChainRulesCore: ignore_derivatives

# functions that have (currently) no better place
using RelocatableFolders

# functions that have (currently) no better place

# Receives one or an array of values and converts it into an Array{typeof(value)} (if not already).
prepareValue(value) = [value]
Expand Down Expand Up @@ -58,7 +60,7 @@ export fmi2CompletedIntegratorStep
export fmi2Unzip, fmi2Load, loadBinary, fmi2Reload, fmi2Unload, fmi2Instantiate!
export fmi2SampleJacobian!
export fmi2GetJacobian, fmi2GetJacobian!, fmi2GetFullJacobian, fmi2GetFullJacobian!
export fmi2Get, fmi2Get!, fmi2Set
export fmi2Get, fmi2Get!, fmi2Set
export fmi2GetUnit, fmi2GetInitial, fmi2GetStartValue, fmi2SampleJacobian
export fmi2GetContinuousStates

Expand All @@ -74,7 +76,7 @@ export fmi2GetValueReferencesAndNames, fmi2GetNamesAndDescriptions, fmi2GetNames
# FMI2_fmu_to_md.jl
# everything exported in `FMI2_md.jl`

# FMI2_sens.jl
# FMI2_sens.jl
export eval!

### FMI3 ###
Expand Down Expand Up @@ -117,14 +119,14 @@ export fmi3GetStartValue, fmi3SampleDirectionalDerivative, fmi3CompletedIntegrat

# FMI3_ext.jl
export fmi3Unzip, fmi3Load, fmi3Unload, fmi3InstantiateModelExchange!, fmi3InstantiateCoSimulation!, fmi3InstantiateScheduledExecution!
export fmi3Get, fmi3Get!, fmi3Set
export fmi3Get, fmi3Get!, fmi3Set
export fmi3SampleDirectionalDerivative!
export fmi3GetJacobian, fmi3GetJacobian!, fmi3GetFullJacobian, fmi3GetFullJacobian!

# FMI3_md.jl
export fmi3LoadModelDescription
export fmi3GetModelName, fmi3GetInstantiationToken, fmi3GetGenerationTool, fmi3GetGenerationDateAndTime, fmi3GetVariableNamingConvention
export fmi3IsCoSimulation, fmi3IsModelExchange, fmi3IsScheduledExecution
export fmi3IsCoSimulation, fmi3IsModelExchange, fmi3IsScheduledExecution
export fmi3GetDefaultStartTime, fmi3GetDefaultStopTime, fmi3GetDefaultTolerance, fmi3GetDefaultStepSize
export fmi3GetModelIdentifier, fmi3CanGetSetState, fmi3CanSerializeFMUState, fmi3ProvidesDirectionalDerivatives, fmi3ProvidesAdjointDerivatives

Expand Down

0 comments on commit 1059825

Please sign in to comment.