Skip to content

Commit

Permalink
Build static and shared versions of ModelicaExternalC
Browse files Browse the repository at this point in the history
  - We now exclusively use CMake to build ModelicaExternalC. This means
    even for the autotools + Makefile build system, we use CMake to build
    ModelicaExternalC.

    This has made the CMakeLists.txt for ModelicaExternalC a tad bit
    complicated. It is now used to compile just ModelicaExternalC as a
    standalone project when asked by the autotools + Makefile build. This
    is signified by setting MODELICA_EXTERNAL_C_STANDALONE=ON when configuring
    it. In this mode it will first look for find and setup the required
    libraries (zlib and libOpenModelicaRuntimeC).

    In the other mode (OpenModelica's new CMake build) it just acts as any
    other CMakeLists.txt file and uses the required libraries from the OpenModelica
    project.

    The shared version of the Modelica libs are isntalled to a specific
    directory lib/<arch>/omc/ffi so that they are not in the link path
    for Simulation executables. They are only intended for libFFI based
    loading by omc and not for actual linking.

  - The ModelicaExternalC builds have been adjusted for MSVC as well.
  • Loading branch information
mahge committed Feb 11, 2022
1 parent 29cce9d commit fda2dde
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 96 deletions.
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/runtime/CMakeLists.txt
Expand Up @@ -77,7 +77,7 @@ target_link_libraries(omcruntime PUBLIC Iconv::Iconv)
target_link_libraries(omcruntime PUBLIC omc::simrt::runtime)
target_link_libraries(omcruntime PUBLIC omc::3rd::ffi)
target_link_libraries(omcruntime PUBLIC omc::3rd::libzmq)
target_link_libraries(omcruntime PUBLIC omc::simrt::Modelica::zlib)
target_link_libraries(omcruntime PUBLIC omc::3rd::zlib)

if(OM_OMC_USE_LPSOLVE)
target_link_libraries(omcruntime PUBLIC omc::3rd::lpsolve55)
Expand Down Expand Up @@ -155,7 +155,7 @@ target_link_libraries(omcbackendruntime PUBLIC ${Intl_LIBRARIES})
target_link_libraries(omcbackendruntime PUBLIC omc::simrt::runtime)
target_link_libraries(omcbackendruntime PUBLIC omc::3rd::metis)
target_link_libraries(omcbackendruntime PUBLIC omc::3rd::fmilib)
target_link_libraries(omcbackendruntime PUBLIC omc::simrt::Modelica::zlib)
target_link_libraries(omcbackendruntime PUBLIC omc::3rd::zlib)

target_include_directories(omcbackendruntime PUBLIC ${Intl_INCLUDE_DIRS})
target_include_directories(omcbackendruntime PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
57 changes: 35 additions & 22 deletions OMCompiler/Makefile.common
Expand Up @@ -111,7 +111,7 @@ $(builddir_share)/omc/omc_communication.idl: Compiler/runtime/omc_communication.
cp -a "$<" "$@"
idl: $(builddir_share)/omc/omc_communication.idl

interactive-common: .testvariables graphstream boehm-gc idl ModelicaExternalC antlr-copy libffi $(MINGW_EXTRA_LIBS)
interactive-common: .testvariables graphstream boehm-gc idl antlr-copy libffi $(MINGW_EXTRA_LIBS)

interactive-short: .testvariables interactive-common
$(MAKE) -C SimulationRuntime/c -f $(defaultMakefileTarget) bootstrap-dependencies OMBUILDDIR=$(OMBUILDDIR)
Expand Down Expand Up @@ -487,13 +487,11 @@ openblas-clean:
# OpenBLAS make clean actually gives error-messages. Let it do it, but silently
test ! -d 3rdParty/OpenBLAS-0.2.8 || $(MAKE) -C 3rdParty/OpenBLAS-0.2.8 clean > /dev/null 2>&1

clean: $(SEMLA_CLEAN) fmil-clean opencl_rt_clean gc-clean lis-clean runtimeCPPclean CMinpack-clean metis-clean Cdaskr-clean bootstrap-clean msgpack-clean graphstream-clean openblas-clean suitesparse-clean OMSI-clean libffi-clean ipopt-clean
clean: $(SEMLA_CLEAN) fmil-clean opencl_rt_clean gc-clean lis-clean runtimeCPPclean CMinpack-clean metis-clean Cdaskr-clean bootstrap-clean msgpack-clean graphstream-clean openblas-clean suitesparse-clean OMSI-clean libffi-clean ipopt-clean ModelicaExternalC-clean zlib-clean
(cd SimulationRuntime/c && $(MAKE) -f $(defaultMakefileTarget) clean OMBUILDDIR=$(OMBUILDDIR))
(cd Compiler && $(MAKE) -f $(defaultMakefileTarget) clean OMBUILDDIR=$(OMBUILDDIR))
(cd Parser && $(MAKE) -f $(defaultMakefileTarget) clean OMBUILDDIR=$(OMBUILDDIR))
$(MAKE) -C Compiler/Template -f $(defaultMakefileTarget) clean OMBUILDDIR=$(OMBUILDDIR)
# remove ModelicaExternalC stuff
if test -f SimulationRuntime/ModelicaExternalC/BuildProjects/autotools/Makefile; then $(MAKE) -C SimulationRuntime/ModelicaExternalC/BuildProjects/autotools/ distclean OMBUILDDIR=$(OMBUILDDIR); fi
(rm -rf SimulationRuntime/build_c)
rm -rf 3rdParty/sundials-5.4.0/build
rm -rf $(OMBUILDDIR)/share $(OMBUILDDIR)/lib $(OMBUILDDIR)/include $(OMBUILDDIR)/bin/OMShell* $(OMBUILDDIR)/bin/OMNotebook* \
Expand Down Expand Up @@ -689,26 +687,41 @@ libffi-clean:
sanity-check: omc
$(OMBUILDDIR)/bin/omc Examples/SanityCheck.mos

MODELICAEXTERNALC=SimulationRuntime/ModelicaExternalC
ModelicaExternalC: $(LIBMODELICAEXTERNALC) $(LIBMODELICASTANDARDTABLES) $(LIBMODELICAIO) $(LIBMODELICAMATIO) $(LIBMODELICAZLIB)
$(LIBMODELICAEXTERNALC):
(cd "$(MODELICAEXTERNALC)/BuildProjects/autotools" && (test -f Makefile || ./autogen.sh))
(cd "$(MODELICAEXTERNALC)/BuildProjects/autotools" && (test -f Makefile || (./configure --prefix="`pwd`" ${MSL_EXTRA_ARGS} --enable-shared --disable-hdf5 --enable-static-zlib "--host=$(host)" --libdir="$(OMBUILDDIR)/$(LIB_OMC)/" CC="$(CC)" CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" && $(MAKE) clean)))
$(MAKE) -C "$(MODELICAEXTERNALC)/BuildProjects/autotools"
$(MAKE) -C "$(MODELICAEXTERNALC)/BuildProjects/autotools" install
rm -f $(OMBUILDDIR)/$(LIB_OMC)/libzlib.la $(OMBUILDDIR)/$(LIB_OMC)/libModelica*.la
cp -pPR "$(MODELICAEXTERNALC)/C-Sources/zlib/zlib.h" $(builddir_inc)/
cp -pPR "$(MODELICAEXTERNALC)/C-Sources/zlib/zconf.h" $(builddir_inc)/
# test ! `uname` = Darwin || install_name_tool -id @rpath/libModelicaExternalC.0.dylib "$(LIBMODELICAEXTERNALC)"
# test ! `uname` = Darwin || (install_name_tool -id @rpath/libModelicaStandardTables.0.dylib "$(LIBMODELICASTANDARDTABLES)" && install_name_tool -change "$(LIBMODELICAMATIO:%.dylib=%.0.dylib)" @rpath/libModelicaMatIO.0.dylib "$(LIBMODELICASTANDARDTABLES)" && install_name_tool -change "$(LIBMODELICAZLIB:%.dylib=%.0.dylib)" @rpath/libzlib.0.dylib "$(LIBMODELICASTANDARDTABLES)")
# test ! `uname` = Darwin || (install_name_tool -id @rpath/libzlib.0.dylib "$(LIBMODELICAZLIB)" && install_name_tool -change "$(LIBMODELICAZLIB:%.dylib=%.0.dylib)" @rpath/libzlib.0.dylib "$(LIBMODELICAZLIB)")
# test ! `uname` = Darwin || (install_name_tool -id @rpath/libModelicaIO.0.dylib "$(LIBMODELICAIO)" && install_name_tool -change "$(LIBMODELICAMATIO:%.dylib=%.0.dylib)" @rpath/libModelicaMatIO.0.dylib "$(LIBMODELICAIO)" && install_name_tool -change "$(LIBMODELICAZLIB:%.dylib=%.0.dylib)" @rpath/libzlib.0.dylib "$(LIBMODELICAIO)")
# test ! `uname` = Darwin || (install_name_tool -id @rpath/libModelicaMatIO.0.dylib "$(LIBMODELICAMATIO)" && install_name_tool -change "$(LIBMODELICAZLIB:%.dylib=%.0.dylib)" @rpath/libzlib.0.dylib "$(LIBMODELICAMATIO)")
@test -f "$@"
$(LIBMODELICAZLIB):
mkdir -p $(ZLIB_DIR)/build
cd $(ZLIB_DIR)/build && $(CMAKE) .. \
-G $(CMAKE_TARGET) \
-DCMAKE_VERBOSE_MAKEFILE:Bool=ON \
-DCMAKE_INSTALL_PREFIX:PATH="$(top_builddir)" \
-DCMAKE_INSTALL_LIBDIR="$(builddir_lib_omc)" \
-DCMAKE_INSTALL_INCLUDEDIR="$(builddir_inc)" \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
$(MAKE) -C $(ZLIB_DIR)/build/ install

zlib-clean:
if test -f $(ZLIB_DIR)/build/Makefile; then $(MAKE) -C $(ZLIB_DIR)/build/ clean; fi
rm -rf $(ZLIB_DIR)/build

MODELICAEXTERNALC_DIR=SimulationRuntime/ModelicaExternalC
ModelicaExternalC: interactive $(LIBMODELICAZLIB)
mkdir -p $(MODELICAEXTERNALC_DIR)/build
cd $(MODELICAEXTERNALC_DIR)/build && $(CMAKE) .. \
-G $(CMAKE_TARGET) \
-DCMAKE_VERBOSE_MAKEFILE:Bool=ON \
-DCMAKE_INSTALL_PREFIX:PATH="$(top_builddir)" \
-DCMAKE_INSTALL_LIBDIR="$(builddir_lib_omc)" \
-DCMAKE_INSTALL_BINDIR="$(builddir_bin)" \
-DCMAKE_INSTALL_INCLUDEDIR="$(builddir_inc)" \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-DMODELICA_EXTERNAL_C_STANDALONE=ON
$(MAKE) -C $(MODELICAEXTERNALC_DIR)/build/ install

ModelicaExternalC-clean:
if test -f $(MODELICAEXTERNALC_DIR)/build/Makefile; then $(MAKE) -C $(MODELICAEXTERNALC_DIR)/build/ clean; fi
rm -rf $(MODELICAEXTERNALC_DIR)/build

$(LIBMODELICASTANDARDTABLES): $(LIBMODELICAEXTERNALC)
@test -f "$@"
$(LIBMODELICAZLIB): $(LIBMODELICAEXTERNALC)
@test -f "$@"
$(LIBMODELICAIO): $(LIBMODELICAEXTERNALC)
@test -f "$@"
$(LIBMODELICAMATIO): $(LIBMODELICAEXTERNALC)
Expand Down
6 changes: 5 additions & 1 deletion OMCompiler/Makefile.in
Expand Up @@ -78,9 +78,12 @@ endif
LIB_OMC=lib/@host_short@/omc
LIBMODELICAEXTERNALC=@OMBUILDDIR@/lib/@host_short@/omc/libModelicaExternalC$(STAEXT)
LIBMODELICASTANDARDTABLES=@OMBUILDDIR@/lib/@host_short@/omc/libModelicaStandardTables$(STAEXT)
LIBMODELICAZLIB=@OMBUILDDIR@/lib/@host_short@/omc/libzlib$(STAEXT)
LIBMODELICAIO=@OMBUILDDIR@/lib/@host_short@/omc/libModelicaIO$(STAEXT)
LIBMODELICAMATIO=@OMBUILDDIR@/lib/@host_short@/omc/libModelicaMatIO$(STAEXT)

ZLIB_DIR=3rdParty/zlib
LIBMODELICAZLIB=@OMBUILDDIR@/lib/@host_short@/omc/libzlib$(STAEXT)

OMENCRYPTION=@OMENCRYPTION@
LVE_EXE_SUFFIX=@MODELICA_SPEC_PLATFORM@
MINGW_EXTRA_LIBS=@MINGW_EXTRA_LIBS@
Expand Down Expand Up @@ -113,6 +116,7 @@ $(OMBUILDDIR)/$(LIB_OMC)/libomcgc.so: 3rdParty/gc/.libs/libomcgc.so

omc-bootstrapped: $(SEMLA_LIB)
$(MAKE) interactive sim-dependencies install-openturns
$(MAKE) ModelicaExternalC
$(MAKE) -C Compiler/boot OMBUILDDIR=$(OMBUILDDIR)
$(MAKE) -f $(defaultMakefileTarget) -C Compiler install_scripts OMBUILDDIR=$(OMBUILDDIR)
@# See if any library is linked directly into the build dirs (we should use @rpath everywhere)
Expand Down
38 changes: 20 additions & 18 deletions OMCompiler/Makefile.omdev.mingw
Expand Up @@ -100,11 +100,13 @@ IDLFILE=$(top_builddir)/Compiler/runtime/omc_communication.idl
LIB_OMC=lib/omc
LIBMODELICAEXTERNALC=$(OMBUILDDIR)/lib/omc/libModelicaExternalC$(STAEXT)
LIBMODELICASTANDARDTABLES=$(OMBUILDDIR)/lib/omc/libModelicaStandardTables$(STAEXT)
LIBMODELICAZLIB=$(OMBUILDDIR)/lib/omc/libzlib$(STAEXT)
LIBMODELICAZLIB_MSVC=$(OMBUILDDIR_MSVC)\lib\omc\msvc\zlib.lib
LIBMODELICAIO=$(OMBUILDDIR)/lib/omc/libModelicaIO$(STAEXT)
LIBMODELICAMATIO=$(OMBUILDDIR)/lib/omc/libModelicaMatIO$(STAEXT)

ZLIB_DIR=3rdParty/zlib
LIBMODELICAZLIB=$(OMBUILDDIR)/lib/omc/libzlib$(STAEXT)
LIBMODELICAZLIB_MSVC=$(OMBUILDDIR_MSVC)\lib\omc\msvc\zlib.lib

SUITESPARSE_LIBS=

defaultMakefileTarget = Makefile.omdev.mingw
Expand Down Expand Up @@ -322,7 +324,7 @@ copycppmsvcheader_old: getMSVCversion
(cp -puf $(BOOST_PATH_MSVC)/boost/*.hpp $(OMBUILDDIR)/include/omc/cpp/3rdParty/boost/msvc/boost)
(cp -pufr $(BOOST_PATH_MSVC)/boost/* $(OMBUILDDIR)/include/omc/cpp/3rdParty/boost/msvc/boost)

omc: interactive fmil omdev_extra_dlls breakprocess opencl_rt CMinpack metis Cdaskr $(IPOPT_TARGET) graphstream $(SEMLA_LIB) OMSI parmodauto
omc: interactive fmil omdev_extra_dlls breakprocess opencl_rt CMinpack metis Cdaskr $(IPOPT_TARGET) graphstream $(SEMLA_LIB) OMSI parmodauto ModelicaExternalC
(time $(MAKE) -f $(defaultMakefileTarget) CFLAGS="$(CFLAGS)" omc-bootstrapped OMBUILDDIR=$(OMBUILDDIR))

boehm-gc-lib: $(OMBUILDDIR)/$(LIB_OMC)/libomcgc.a
Expand All @@ -331,30 +333,30 @@ boehm-gc-lib: $(OMBUILDDIR)/$(LIB_OMC)/libomcgc.a
zlib_msvc: $(LIBMODELICAZLIB_MSVC)
$(LIBMODELICAZLIB_MSVC): getMSVCversion
rm -f $(OMBUILDDIR)/lib/omc/msvc/zlib.*
rm -rf SimulationRuntime/ModelicaExternalC/build_msvc
rm -rf SimulationRuntime/ModelicaExternalC/install_msvc
rm -rf $(ZLIB_DIR)/build_msvc
rm -rf $(ZLIB_DIR)/install_msvc
mkdir -p $(OMBUILDDIR)/lib/omc/msvc/
# Goto build msvc directory
test -d SimulationRuntime/ModelicaExternalC
mkdir -p SimulationRuntime/ModelicaExternalC/build_msvc
test -d $(ZLIB_DIR)
mkdir -p $(ZLIB_DIR)/build_msvc

# Create batch file, that builds with CMake and Visual Studio
test -f """$(VSCOMNTOOLS)/../../VC/vcvarsall.bat"""
echo 'Building ModelicaExternalC/zlib with MSVC'
echo @echo on > SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo call '"$(VSCOMNTOOLS)\\..\\..\\VC\\vcvarsall.bat" ${VCVARS_PARAMS}' >> SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo echo Calling CMake >> SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo 'cmake -DCMAKE_VERBOSE_MAKEFILE:Bool=ON -DSHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=../install_msvc ../ -G "NMake Makefiles JOM" -D"CMAKE_MAKE_PROGRAM:PATH=%OMDEV%\\tools\\jom\\jom.exe"' >> SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo echo Building ModelicaExternalC/zlib >> SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo @echo on >> SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo set MAKE= >> SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo set MAKEFLAGS= >> SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo %OMDEV%\\tools\\jom\\jom.exe /f Makefile install >> SimulationRuntime/ModelicaExternalC/build_msvc/build.bat
echo @echo on > $(ZLIB_DIR)/build_msvc/build.bat
echo call '"$(VSCOMNTOOLS)\\..\\..\\VC\\vcvarsall.bat" ${VCVARS_PARAMS}' >> $(ZLIB_DIR)/build_msvc/build.bat
echo echo Calling CMake >> $(ZLIB_DIR)/build_msvc/build.bat
echo 'cmake -DCMAKE_VERBOSE_MAKEFILE:Bool=ON -DSHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=../install_msvc -DCMAKE_INSTALL_INCLUDEDIR="$(builddir_inc_msvc)" -DCMAKE_INSTALL_LIBDIR="$(LIB_OMC)" ../ -G "NMake Makefiles JOM" -D"CMAKE_MAKE_PROGRAM:PATH=%OMDEV%\\tools\\jom\\jom.exe"' >> $(ZLIB_DIR)/build_msvc/build.bat
echo echo Building ModelicaExternalC/zlib >> $(ZLIB_DIR)/build_msvc/build.bat
echo @echo on >> $(ZLIB_DIR)/build_msvc/build.bat
echo set MAKE= >> $(ZLIB_DIR)/build_msvc/build.bat
echo set MAKEFLAGS= >> $(ZLIB_DIR)/build_msvc/build.bat
echo %OMDEV%\\tools\\jom\\jom.exe /f Makefile install >> $(ZLIB_DIR)/build_msvc/build.bat
# Change into directory and run batch file
cd ./SimulationRuntime/ModelicaExternalC/build_msvc; echo "change to SimulationRuntime/ModelicaExternalC/build_msvc";\
cd ./$(ZLIB_DIR)/build_msvc; echo "change to $(ZLIB_DIR)/build_msvc";\
cmd /c build.bat
# copy dll to bin folder
(cp -puf SimulationRuntime/ModelicaExternalC/install_msvc/lib/zlib.lib $(OMBUILDDIR)/lib/omc/msvc/)
(cp -puf $(ZLIB_DIR)/install_msvc/lib/omc/zlib.lib $(OMBUILDDIR)/lib/omc/msvc/)

fmil_msvc: $(OMBUILDDIR)/bin/msvc/fmilib_shared.dll
$(OMBUILDDIR)/bin/msvc/fmilib_shared.dll: getMSVCversion zlib_msvc
Expand Down

5 comments on commit fda2dde

@AnHeuermann
Copy link
Member

Choose a reason for hiding this comment

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

@mahge I'm getting errors like

$ cmake -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++  ..
[...]
CMake Error at OMCompiler/Compiler/boot/CMakeLists.txt:11 (add_executable):
  Target "bomc" links to target "omc::3rd::zlib" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

for the Windows CMake build after updating to the latest master commit. I guess this commit caused the problem.

@mahge
Copy link
Contributor Author

@mahge mahge commented on fda2dde Feb 14, 2022

Choose a reason for hiding this comment

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

I think you need to update 3rdparty submodule.

@AnHeuermann
Copy link
Member

Choose a reason for hiding this comment

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

@mahge
Copy link
Contributor Author

@mahge mahge commented on fda2dde Feb 14, 2022

Choose a reason for hiding this comment

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

Ahhh I forgot to merge the PR 🤦‍♂️. I will merge it.

You can just do

git submodule update

In OpenModelica in the meantime. Instead of getting in 3rdParty and checking out master. I think it should work.

@mahge
Copy link
Contributor Author

@mahge mahge commented on fda2dde Feb 14, 2022

Choose a reason for hiding this comment

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

It is now fixed with #8542.

Please sign in to comment.