OpenModelica icon indicating copy to clipboard operation
OpenModelica copied to clipboard

Bug in CMAKE leads to FMU export with missing dependency

Open casella opened this issue 1 year ago • 47 comments

From the discussion in #11604, it seems that there is a CMAKE bug that leads to missing dependencies in FMUs if using clang (not if using gcc).

@bilderbuchi notes:

This comment in the cmake issue tracker sounds quite relevant, and could be the root cause, and an easy fix (tabs vs spaces). I guess that should be tracked a separate cmake issue, but I don't have an account there. Anybody? Can we work around that somehow in the meantime, e.g. with some sed invocation in-between, or somesuch?

I would suggest to implement his proposed workarund and, if possible, to open a ticket on the CMAKE tracker addressing this issue.

casella avatar Mar 04 '24 15:03 casella

For now I have fixed this in our OMDev repos via: https://gitlab.liu.se/OpenModelica/OMDev/-/commit/e1bb0576bfb1ebb4c63fbbcc6e37fb825da16ac9 and this: https://gitlab.liu.se/OpenModelica/OMDevUCRT/-/commit/1c8bc4a63fddecee1860961381b1c731e4acd991

It seems to be working. I will try to make a PR for CMake.

adrpo avatar Mar 09 '24 23:03 adrpo

This fix will end up in v1.22.3 and of course the nightly.

adrpo avatar Mar 09 '24 23:03 adrpo

Opened a PR with cmake: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9325

adrpo avatar Mar 10 '24 00:03 adrpo

@adrpo shall we close this?

casella avatar Mar 10 '24 21:03 casella

Yes, my PR to cmake was merged: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9325

adrpo avatar Mar 11 '24 20:03 adrpo

On the today (12-3-2024) nightly build I have tested FMU export with clang. Their size is 280 kb (with euler explicit), so I suppose that is not working. It is implemented on the current nightly?

Update: The same error appears on third party simulator when try to simulate (see details in #11604)

BasilioPV avatar Mar 12 '24 15:03 BasilioPV

@adrpo maybe something is missing to get the OMDev changes into the nightly? Some submodule update?

casella avatar Mar 12 '24 16:03 casella

Update: Using today (15-3-2024) build: FMU now has 459 KB, so there is a difference between previous versions, but the erorr in third party software is the same when try to simulate (see details in #11604):

image

BasilioPV avatar Mar 15 '24 06:03 BasilioPV

@BasilioPV the FMU size isn't a good indicator. You can use tools like Dependencies too check why the DLL can't be loaded. Unzip the FMU and open FMU/binaries/win64/Test_FMU.dll with Dependencies and add the error message here. And tell us what's inside the binaries/win64 directory.

Besides the CVODE linking issue (we are linking statically now, so it should work and the increased size suggest it did) there should be some sort of pthreads DLL. If Dependencies doesn't show any errors it's probably an issue with the importing tool missing to call SetDllDirectoryA before calling LoadLibraryA, see https://github.com/OpenModelica/OMSimulator-3rdParty/commit/ca418d7768c036ac15e9894d7f00d2118b3399a6. We are in the process of updating OMSimulator to handle this.

AnHeuermann avatar Mar 15 '24 09:03 AnHeuermann

Thanks for pointing to Dependencies tool!!

FMU, with clang and CVODE and explicit Euler :

image

BasilioPV avatar Mar 15 '24 11:03 BasilioPV

That's looking good. Please open an issue with the FMU importing tool you use so that they'll add SetDllDirectoryA before loading the DLL. As far as I know there is no Windows equivalent to setting RPATH on Unix, so we can't tell Windows where to look for libwinpthread-1.dll.

AnHeuermann avatar Mar 15 '24 14:03 AnHeuermann

Ah sorry, I misread the output from your image. Did you copy Test_FMU.dll out of FMU/binaries/win64? The location matters and there should be libwinpthread-1.dll in the same directory.

AnHeuermann avatar Mar 15 '24 14:03 AnHeuermann

There are only these two files in the directory:

image

Tested with both in the same directory, with the same result:

image

BasilioPV avatar Mar 15 '24 14:03 BasilioPV

Any ideas on this?

BasilioPV avatar Mar 19 '24 16:03 BasilioPV

Can't reproduce with OpenModelica v1.23.0-dev-416-g0f1ee9aef5 (64-bit) on my Windows 11 or a clean Windows 11 virtual machine. libwinpthread-1.dll is in the binaries directory and Dependencies reports no load errors.

AnHeuermann avatar Apr 04 '24 12:04 AnHeuermann

@BasilioPV do you also see libwinphtread-1.dll in the binaries directory?

casella avatar Apr 04 '24 14:04 casella

I have installed the today's OMedit (v1.23.0-dev-420-g1cb1c69e62) (64-bit) On Simulation, this is configured:

image

On FMI: image

And the Modelica model to export is:

model Test_FMU parameter Real A=2; output Real B; equation B=A*time; end Test_FMU;

And there is no libwinphtread-1.dll in the binaries:

image

My computer is Windows 10 Pro.

BasilioPV avatar Apr 05 '24 09:04 BasilioPV

I tried again with the settings from your screenshot and here's the resulting FMU: Test_FMU.zip

The only things I can think of are some settings in OMEdit we aren't aware of or some difference between the software on Windows 10 and Windows 11. I don't have access to a Windows 10 VM, so I can't try that.

AnHeuermann avatar Apr 05 '24 09:04 AnHeuermann

@BasilioPV I have Windows 10, I tried to export your MWE and this is the content of binaries/win64 immagine I would suggest you to reset the OMEdit options. Maybe there is some setting you made somewhere that break this.

casella avatar Apr 05 '24 19:04 casella

Thanks @casella , there are settings that persist after an uninstall + install a new version? How to remove them?

BasilioPV avatar Apr 05 '24 19:04 BasilioPV

All of them are kept, settings are saved in some file or register key. You can reset them to factory settings with Tools | Options | Reset button, then close and restart OMEdit.

casella avatar Apr 05 '24 19:04 casella

Ok, after reset, the MSL 4.0.0 is loaded by default.

Now this translation error is shown:

[3] 07:58:53 Translation Error [Test_FMU: 2:3-2:19]: Parameter A has neither value nor start value, and is fixed during initialization (fixed=true).

That I solve by putting the value:

parameter Real A=0;

But the result remains the same:

image

BasilioPV avatar Apr 06 '24 06:04 BasilioPV

I have tried now with a clean W10 on a virtual machine (virtualbox). After install the latest nightly build and change only:

image image

The result still the same:

image

NOTE: If I don't change anything, with OMedit out-of-the-box, the result remains the same, no libwinpthread-1.dll

BasilioPV avatar Apr 06 '24 06:04 BasilioPV

@AnHeuermann where is the libwinpthread-1.dll fetched from? Maybe it's missing on @BasilioPV's computer?

casella avatar Apr 06 '24 18:04 casella

On a clean W10 virtual machine, after installing OM nightly build, it is placed at: image

BasilioPV avatar Apr 06 '24 19:04 BasilioPV

As @BasilioPV showed it's installed together with OpenModelica in C:\Program Files\OpenModelica1.23.0-dev-64bit\tools\msys\ucrt64\bin.

You have OMDev installed. Maybe that's the issue somehow? It also has pthreads and CMake available. But it seems to be the correct ucrt version. Also the git directory is in you PATH. That can lead to incompatibilities between UCRT and MINGW.

AnHeuermann avatar Apr 08 '24 07:04 AnHeuermann

In fact, I don't have OMDev installed, and it works for me.

casella avatar Apr 08 '24 11:04 casella

I will start again the process. On a clean W10 VM:

Before install, I don't have the file in the PC:

image

After Install OM nightly build (of today):

image

Now I open OMedit, load Test_FMU:

image

And directly right click and export to FMU:

image

Now I test changing only "Explicit Euler" to "CVODE" on FMU export settings:

image

Still the same. What I'm doing wrong?

BasilioPV avatar Apr 08 '24 13:04 BasilioPV

@AnHeuermann maybe exporting to source code FMU and manually building it can give some idea?

This is a very odd.

mahge avatar Apr 08 '24 22:04 mahge

Now I test changing only "Explicit Euler" to "CVODE" on FMU export settings:

@BasilioPV do you mean it works if you export with Euler?

mahge avatar Apr 08 '24 22:04 mahge