Skip to content

Commit

Permalink
v0.2.0 (#27)
Browse files Browse the repository at this point in the history
* restored compatibility with FMICore update

* minor changes
  • Loading branch information
ThummeTo authored Nov 6, 2023
1 parent 8c39e40 commit 1665634
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 66 deletions.
8 changes: 5 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FMIBuild"
uuid = "226f0e26-6dd6-4589-ada7-1d32f6e1d800"
authors = ["TT <tobias.thummerer@informatik.uni-augsburg.de>", "LM <lars.mikelsons@informatik.uni-augsburg.de>"]
version = "0.1.16"
version = "0.2.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -12,8 +12,10 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"

[compat]
Dates = "1"
EzXML = "1.1.0"
FMICore = "0.17"
PackageCompiler = "2.1.5"
FMICore = "0.18.0 - 0.19.0"
PackageCompiler = "2.1.9"
Pkg = "1"
ZipFile = "0.9, 0.10"
julia = "1.6"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ To keep dependencies nice and clean, the original package *FMI.jl* had been spli
- [*FMIImport.jl*](https://github.com/ThummeTo/FMIImport.jl): Importing FMUs into Julia
- [*FMIExport.jl*](https://github.com/ThummeTo/FMIExport.jl): Exporting stand-alone FMUs from Julia Code
- [*FMICore.jl*](https://github.com/ThummeTo/FMICore.jl): C-code wrapper for the FMI-standard
- [*FMISensitivity.jl*](https://github.com/ThummeTo/FMISensitivity.jl): Static and dynamic sensitivities over FMUs
- [*FMIBuild.jl*](https://github.com/ThummeTo/FMIBuild.jl): Compiler/Compilation dependencies for FMIExport.jl
- [*FMIFlux.jl*](https://github.com/ThummeTo/FMIFlux.jl): Machine Learning with FMUs (differentiation over FMUs)
- [*FMIZoo.jl*](https://github.com/ThummeTo/FMIZoo.jl): A collection of testing and example FMUs
Expand Down
102 changes: 45 additions & 57 deletions src/FMIBuild.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri
md_path = joinpath(target_dir, fmu_name, "modelDescription.xml")

bin_dir = joinpath(target_dir, fmu_name, "binaries")
libext = ""
libext = nothing

# checking architecture
juliaArch = Sys.WORD_SIZE
Expand All @@ -164,7 +164,7 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri
end
end

@assert length(libext) > 0 "fmiBuild(...): Unsupported target platform. Supporting Windows (64-, 32-bit), Linux (64-bit) and MacOS (64-bit). Please open an issue online if you need further architectures."
@assert !isnothing(libext) "fmiBuild(...): Unsupported target platform. Supporting Windows (64-, 32-bit), Linux (64-bit) and MacOS (64-bit). Please open an issue online if you need further architectures."

mkpath(bin_dir)

Expand Down Expand Up @@ -221,67 +221,56 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri

@info "[Build FMU] Adding/removing dependencies ..."
currentEnv = Base.active_project()
currentCompState = 1
try
currentCompState = ENV["JULIA_PKG_PRECOMPILE_AUTO"]
catch e
currentCompState = 1
end
currentCompState = get(ENV, "JULIA_PKG_PRECOMPILE_AUTO", 1)
ENV["JULIA_PKG_PRECOMPILE_AUTO"] = 0

# check package dependencies
# Pkg.activate(source_pkf_dir)
# buf = IOBuffer()
# Pkg.status(;io=buf)
# installedPkgs = split(String(take!(buf)), "\n")
# installedPkgs = installedPkgs[3:end] # skip header

fmiexportPath = packagePath("FMIExport")
fmicorePath = packagePath("FMICore")

defaultEnv = get(ENV, "FMIExport_DefaultEnv", nothing)
if !isnothing(defaultEnv)
@info "[Build FMU] > Using default environment `$(defaultEnv)` from environment variable `FMIExport_DefaultEnv`."
else
defaultEnv = Base.active_project()
@info "[Build FMU] > Using active environment `$(defaultEnv)`."
end
Pkg.activate(defaultEnv)
default_fmiexportPath = packagePath("FMIExport")
default_fmicorePath = packagePath("FMICore")

# adding Pkgs
Pkg.activate(merge_dir)
# for pkg in installedPkgs
# pkgname = pkg[14:end]
# Pkg.add(pkgname)
# @info "[Build FMU] > Added `$(pkgname)`"
# end

# redirect FMIExport.jl package (if locally checked out, this is necessary for Github-CI to use the current version from a PR)
if !isnothing(fmiexportPath)
old_fmiexportPath = packagePath("FMIExport")
if isnothing(old_fmiexportPath)
@info "[Build FMU] > `FMIExport` not installed, adding at `$(fmiexportPath)`."
Pkg.add(path=fmiexportPath)
elseif lowercase(old_fmiexportPath) == lowercase(fmiexportPath)
@info "[Build FMU] > Most recent version of `FMIExport` already checked out, is `$(fmiexportPath)`."
else
@info "[Build FMU] > Replacing `FMIExport` at `$(old_fmiexportPath)` with the current installation at `$(fmiexportPath)`."
Pkg.add(path=fmiexportPath)
end
else
@info "[Build FMU] > FMU has no dependency on `FMIExport`."
end

if !isnothing(fmicorePath)
if isnothing(default_fmicorePath)
@info "[Build FMU] > Default environment `$(defaultEnv)` has no dependency on `FMICore`, adding `FMICore` from registry."
Pkg.add("FMICore")
else
old_fmicorePath = packagePath("FMICore")
if isnothing(old_fmicorePath)
@info "[Build FMU] > `FMICore` not installed, adding at `$(fmicorePath)`."
Pkg.add(path=fmicorePath)
elseif lowercase(old_fmicorePath) == lowercase(fmicorePath)
@info "[Build FMU] > Most recent version of `FMICore` already checked out, is `$(fmicorePath)`."
@info "[Build FMU] > `FMICore` not installed, adding at `$(default_fmicorePath)`, adding `FMICore` from default environment."
Pkg.add(path=default_fmicorePath)
elseif lowercase(old_fmicorePath) == lowercase(default_fmicorePath)
@info "[Build FMU] > Most recent version (as in default environment) of `FMICore` already checked out, is `$(default_fmicorePath)`."
else
@info "[Build FMU] > Replacing `FMICore` at `$(old_fmicorePath)` with the current installation at `$(fmicorePath)`."
Pkg.add(path=fmicorePath)
end
else
@info "[Build FMU] > Adding `FMICore` with the current installation from registrator."
Pkg.add("FMICore")
@info "[Build FMU] > Replacing `FMICore` at `$(old_fmicorePath)` with the default environment installation at `$(default_fmicorePath)`."
Pkg.add(path=default_fmicorePath)
end
end

# redirect FMIExport.jl package in case the active environment (the env the installer is called from)
# has a *more recent* version of FMIExport.jl than the registry (necessary for Github-CI to use the current version from a PR)
if isnothing(default_fmiexportPath) # the environemnt the exporter is called from *has no* FMIExport.jl installed
@info "[Build FMU] > Default environment `$(defaultEnv)` has no dependency on `FMIExport`."
else # the environemnt the exporter is called from *has* FMIExport.jl installed
old_fmiexportPath = packagePath("FMIExport")
if isnothing(old_fmiexportPath) # the FMU has no dependency to FMIExport.jl
@info "[Build FMU] > `FMIExport` for FMU not installed, adding at `$(default_fmiexportPath)`, adding `FMIExport` from default environment."
Pkg.add(path=default_fmiexportPath)
elseif lowercase(old_fmiexportPath) == lowercase(default_fmiexportPath) # the FMU is already using the most recent version of FMIExport.jl
@info "[Build FMU] > Most recent version of `FMIExport` already checked out for FMU, is `$(default_fmiexportPath)`."
else
@info "[Build FMU] > Replacing `FMIExport` at `$(old_fmiexportPath)` with the current installation at `$(default_fmiexportPath)` for FMU ."
Pkg.add(path=default_fmiexportPath)
end
end

#@info "[Build FMU] > Added LLVMExtra_jll"
#Pkg.add("LLVMExtra_jll")
#Pkg.add(name="FMICore", version="0.7.1")
if removeLibDependency
cdata = replace(cdata, r"(using|import) FMIBuild" => "")
try
Expand Down Expand Up @@ -345,12 +334,11 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri
startPacking = time()

@info "[Build FMU] Building model description ..."
#buildModelDescription(md_path, fmu_name, fmu_src_file)
fmi2SaveModelDescription(fmu.modelDescription, md_path)
@info "[Build FMU] ... building model description done."

@info "[Build FMU] Zipping FMU ..."
# parse and zip directories
@info "[Build FMU] Zipping FMU ..."
zipfile = joinpath(target_dir, fmu_name * ".zip")
zdir = ZipFile.Writer(zipfile)
for (root, dirs, files) in walkdir(joinpath(target_dir, fmu_name))
Expand All @@ -367,20 +355,20 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri
end
end
close(zdir)

# Rename ZIP-file to FMU-file
cp(joinpath(target_dir, fmu_name * ".zip"), joinpath(fmu_dir, fmu_name * ".fmu"); force=true)
@info "[Build FMU] ... zipping FMU done."

if cleanup
@info "[Build FMU] Clean up ..."
# Clean-up is done by saving in a temporary directory (which may be deleted by the OS)
# ToDo: Clean-up is done by saving in a temporary directory (which may be deleted by the OS)
@info "[Build FMU] ... clean up done."
end

stopPacking = time()

# output message

dt = stopPacking-startCompilation
per = (stopPacking-startPacking) / dt * 100.0
mins = 0
Expand Down
34 changes: 33 additions & 1 deletion template/ME/FMU2/src/FMU2_content.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import FMICore

using FMICore: fmi2CallbackFunctions, fmi2Component, fmi2ComponentEnvironment, fmi2EventInfo, fmi2ValueReference
using FMICore: fmi2Real, fmi2Integer, fmi2Boolean, fmi2String, fmi2True, fmi2False, fmi2StatusError, fmi2StatusFatal
using FMICore: fmi2Real, fmi2Integer, fmi2Boolean, fmi2String, fmi2True, fmi2False, fmi2StatusOK, fmi2StatusWarning, fmi2StatusError, fmi2StatusFatal
using FMICore: fmi2Status, fmi2Type, fmi2StatusToString
using FMICore: FMU2Component
import FMICore: logInfo, logWarning, logError

##############

Expand All @@ -17,6 +18,37 @@ global FMIBUILD_CONSTRUCTOR = nothing
global FMIBUILD_LOGGING = true
global FMIBUILD_INSTANCES = []

##############

function dereferenceInstance(address::fmi2Component)
global FMIBUILD_FMU
for component in FMIBUILD_FMU.components
if component.compAddr == address
return component
end
end

@warn "Unknown instance at $(address)."
return nothing
end

function logInfo(_component::fmi2Component, message, status::fmi2Status=fmi2StatusOK)
component = dereferenceInstance(_component)
logInfo(component, message, status)
end

function logWarning(_component::fmi2Component, message, status::fmi2Status=fmi2StatusWarning)
component = dereferenceInstance(_component)
logWarning(component, message, status)
end

function logError(_component::fmi2Component, message, status::fmi2Status=fmi2StatusError)
component = dereferenceInstance(_component)
logError(component, message, status)
end

##############

Base.@ccallable function init_FMU(_dllLoc::Ptr{Cchar})::Cvoid
dllLoc = unsafe_string(_dllLoc)
comps = splitpath(dllLoc)
Expand Down
37 changes: 32 additions & 5 deletions template/ME/FMU2/src/FMU2_content_debug.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import FMICore

using FMICore: fmi2CallbackFunctions, fmi2Component, fmi2ComponentEnvironment, fmi2EventInfo, fmi2ValueReference
using FMICore: fmi2Real, fmi2Integer, fmi2Boolean, fmi2String, fmi2True, fmi2False, fmi2StatusError, fmi2StatusFatal
using FMICore: fmi2Real, fmi2Integer, fmi2Boolean, fmi2String, fmi2True, fmi2False, fmi2StatusOK, fmi2StatusWarning, fmi2StatusError, fmi2StatusFatal
using FMICore: fmi2Status, fmi2Type, fmi2StatusToString
using FMICore: FMU2Component
import FMICore: logInfo, logWarning, logError

##############

Expand All @@ -17,6 +18,35 @@ global FMIBUILD_CONSTRUCTOR = nothing
global FMIBUILD_LOGGING = true
global FMIBUILD_INSTANCES = []

##############

function dereferenceInstance(address::fmi2Component)
global FMIBUILD_FMU
for component in FMIBUILD_FMU.components
if component.compAddr == address
return component
end
end

@warn "Unknown instance at $(address)."
return nothing
end

function logInfo(_component::fmi2Component, message, status::fmi2Status=fmi2StatusOK)
component = dereferenceInstance(_component)
logInfo(component, message, status)
end

function logWarning(_component::fmi2Component, message, status::fmi2Status=fmi2StatusWarning)
component = dereferenceInstance(_component)
logWarning(component, message, status)
end

function logError(_component::fmi2Component, message, status::fmi2Status=fmi2StatusError)
component = dereferenceInstance(_component)
logError(component, message, status)
end

Base.@ccallable function init_FMU(_dllLoc::Ptr{Cchar})::Cvoid
dllLoc = unsafe_string(_dllLoc)
comps = splitpath(dllLoc)
Expand Down Expand Up @@ -62,10 +92,7 @@ Base.@ccallable function fmi2Instantiate(_instanceName::fmi2String,
logInfo(_component, "fmi2Instantiate($(_instanceName), $(fmuType), $(_fmuGUID), $(_fmuResourceLocation), $(_functions), $(visible), $(loggingOn))\n\t-> $(_component)")
end
catch e
if _component == C_NULL
_component = nothing
end


logError(_component, "Exception thrown:\tIn function: fmi2Instantiate($(_instanceName), $(fmuType), $(_fmuGUID), $(_fmuResourceLocation), $(_functions), $(visible), $(loggingOn))\n\tMessage: $(e)\n\tStack:")
for s in stacktrace()
logError(_component, "$(s)")
Expand Down

2 comments on commit 1665634

@ThummeTo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/94829

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 1665634f999416bd691db96c39e6f650f2a04356
git push origin v0.2.0

Please sign in to comment.