Skip to content

Commit

Permalink
preparations for linux support, integration of new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThummeTo committed Dec 9, 2024
1 parent f3d23ed commit c50dab8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
4 changes: 2 additions & 2 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 <[email protected]>", "LM <[email protected]>"]
version = "0.3.2"
version = "0.3.3"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -12,6 +12,6 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
[compat]
Dates = "1"
FMIBase = "1.0.0"
PackageCompiler = "2.1.9"
PackageCompiler = "2.1.9, 2.2.0"
Pkg = "1"
julia = "1.6"
6 changes: 3 additions & 3 deletions src/FMIBuild.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ function saveFMU(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Strin

# [note] 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
if isnothing(default_fmiexportPath) # the environment 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
else # the environment 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."
Expand Down Expand Up @@ -326,7 +326,7 @@ function saveFMU(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Strin
cp(key, joinpath(bin_dir, "..", "..", "resources", val); force=true)
@info "[Build FMU] \t $val"
end
@info "[Build FMU] ... adding resoruce files done."
@info "[Build FMU] ... adding resource files done."
end

@info "[Build FMU] Patching libjulia.$(libext) @ `$(bin_dir)`..."
Expand Down
54 changes: 48 additions & 6 deletions template/ME/header/FMU2_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,31 @@ void shutdown_julia(int retcode) { jl_atexit_hook(retcode); }

// custom

void constructor(short unsigned int* path)
{
init_julia(0, NULL);

char* ptr = (char*)&path;
init_FMU(ptr);
}

void destructor(void)
{
shutdown_julia(0);
}

#ifdef _WIN32
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
init_julia(0, NULL);

// get DLL path for resource location
char path[MAX_PATH];
short unsigned int path[MAX_PATH];
HMODULE hm = NULL;

if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &init_julia, &hm) == 0)
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCWSTR) &init_julia, &hm) == 0)
{
int ret = GetLastError();
fprintf(stderr, "GetModuleHandle failed, error = %d\n", ret);
Expand All @@ -109,12 +122,41 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
return (FALSE);
}

char* ptr = (char*)&path;
init_FMU(ptr);
constructor(path);
break;
case DLL_PROCESS_DETACH:
shutdown_julia(0);
destructor();
break;
}
return (TRUE);
}

#else
CP_BEGIN_EXTERN_C
__attribute__((constructor))
/**
* initializer of the dylib.
*/
static void Initializer(int argc, char** argv, char** envp)
{
char pid[20];
char path[PATH_MAX + 1] = {0};

sprintf(pid, "/proc/%d/exe", getpid());
readlink(pid, path, sizeof(path));

constructor(string(path));
}

__attribute__((destructor))
/**
* It is called when dylib is being unloaded.
*
*/
static void Finalizer()
{
destructor();
}

CP_END_EXTERN_C
#endif

0 comments on commit c50dab8

Please sign in to comment.