From fda2dde7662c6ded8cee87c8d304e1b5fac2a9cc Mon Sep 17 00:00:00 2001 From: Mahder Gebremedhin Date: Mon, 7 Feb 2022 13:49:38 +0100 Subject: [PATCH] Build static and shared versions of ModelicaExternalC - 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//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. --- OMCompiler/Compiler/runtime/CMakeLists.txt | 4 +- OMCompiler/Makefile.common | 57 ++++--- OMCompiler/Makefile.in | 6 +- OMCompiler/Makefile.omdev.mingw | 38 ++--- .../ModelicaExternalC/CMakeLists.txt | 143 +++++++++++------ .../MEC_standalone_2.8.cmake | 150 ++++++++++++++++++ .../OMSICpp/omcWrapper/omcCAPI/CMakeLists.txt | 2 +- 7 files changed, 304 insertions(+), 96 deletions(-) create mode 100644 OMCompiler/SimulationRuntime/ModelicaExternalC/MEC_standalone_2.8.cmake diff --git a/OMCompiler/Compiler/runtime/CMakeLists.txt b/OMCompiler/Compiler/runtime/CMakeLists.txt index d6de108358f..248a92c354a 100644 --- a/OMCompiler/Compiler/runtime/CMakeLists.txt +++ b/OMCompiler/Compiler/runtime/CMakeLists.txt @@ -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) @@ -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}) diff --git a/OMCompiler/Makefile.common b/OMCompiler/Makefile.common index 3cb4f637cd6..5f4177fe6c5 100644 --- a/OMCompiler/Makefile.common +++ b/OMCompiler/Makefile.common @@ -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) @@ -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* \ @@ -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) diff --git a/OMCompiler/Makefile.in b/OMCompiler/Makefile.in index 59ed6e858c5..c410aa55787 100644 --- a/OMCompiler/Makefile.in +++ b/OMCompiler/Makefile.in @@ -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@ @@ -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) diff --git a/OMCompiler/Makefile.omdev.mingw b/OMCompiler/Makefile.omdev.mingw index 32d9a436140..36b24c2d5ff 100644 --- a/OMCompiler/Makefile.omdev.mingw +++ b/OMCompiler/Makefile.omdev.mingw @@ -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 @@ -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 @@ -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 diff --git a/OMCompiler/SimulationRuntime/ModelicaExternalC/CMakeLists.txt b/OMCompiler/SimulationRuntime/ModelicaExternalC/CMakeLists.txt index 1e12ea5a9d6..d43fd0f4244 100644 --- a/OMCompiler/SimulationRuntime/ModelicaExternalC/CMakeLists.txt +++ b/OMCompiler/SimulationRuntime/ModelicaExternalC/CMakeLists.txt @@ -1,98 +1,137 @@ -cmake_minimum_required(VERSION 3.14) -project(OMModelicaExternalC) - - -# zlib -# We have decided to use zlib from here. We could have used the system zlib. However, -# modelica annotations request for "zlib" while the system zlib is OFTEN (but not always) called "libz" -# which means it should be used as "z". We can modify the annotations to use "z" but then -# it will be the same issue on systems that call it "zlib". So we need to find a solution. -# Originally I was creating a sym link to the system zlib in our lib directories. However, -# that might be confusing for others. So it might be better to explicitly -# build it and use it from here. The one advantage of this is that we can compile it with -fpic so -# that we can link it into our static FMUs. -file(GLOB libzlib_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/C-Sources/zlib/*.c) -add_library(zlib STATIC ${libzlib_SOURCES}) -add_library(omc::simrt::Modelica::zlib ALIAS zlib) - - -target_include_directories(zlib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/C-Sources/zlib) - -## Check if we have unistd.h and define accordingly -## We can not use the omc macro omc_check_header_exists_and_define() because -## this file is processed for the omc-msvc targets right now. Do it manually. -include(CheckIncludeFile) -check_include_file(unistd.h HAVE_UNISTD_H) -if(HAVE_UNISTD_H) - target_compile_definitions(zlib PRIVATE -DHAVE_UNISTD_H=1) +# We now exclusively use CMake to build ModelicaExternalC. That is, even for +# the autotools+Makefiles build system OpenModelica uses, CMake is used to +# compile ModelicaExternalC. +# If we get here from autotools+Makefiles build we use a standalone CMake project file +# to compile the ModelicaExternalC libs. This is signified by setting MODELICA_EXTERNAL_C_STANDALONE +# when configuring this file. +if(MODELICA_EXTERNAL_C_STANDALONE) + include(MEC_standalone_2.8.cmake) + return() endif() -install(TARGETS zlib) -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/C-Sources/zlib/zlib.h - ${CMAKE_CURRENT_SOURCE_DIR}/C-Sources/zlib/zconf.h - TYPE INCLUDE) - +cmake_minimum_required(VERSION 3.14) +project(OMModelicaExternalC) -# ModelicaExternalC +## ModelicaExternalC ######################################################################### set(libModelicaExternalC_SOURCES C-Sources/ModelicaFFT.c C-Sources/ModelicaInternal.c C-Sources/ModelicaRandom.c C-Sources/ModelicaStrings.c) +# Static version add_library(ModelicaExternalC STATIC ${libModelicaExternalC_SOURCES}) add_library(omc::simrt::Modelica::ExternalC ALIAS ModelicaExternalC) target_link_libraries(ModelicaExternalC PUBLIC m) +target_link_libraries(ModelicaExternalC PUBLIC omc::simrt::runtime) + +# Shared version. +add_library(ModelicaExternalC_shared SHARED ${libModelicaExternalC_SOURCES}) +add_library(omc::simrt::Modelica::ExternalC::shared ALIAS ModelicaExternalC_shared) +set_target_properties(ModelicaExternalC_shared + PROPERTIES OUTPUT_NAME ModelicaExternalC CLEAN_DIRECT_OUTPUT 1) + +target_link_libraries(ModelicaExternalC_shared PUBLIC m) +target_link_libraries(ModelicaExternalC_shared PUBLIC omc::simrt::runtime) +if(WIN32) + target_link_options(ModelicaExternalC_shared PRIVATE -Wl,--export-all-symbols) +endif() + -# ModelicaMatIO +## ModelicaMatIO ######################################################################### set(libModelicaMatIO_SOURCES C-Sources/ModelicaMatIO.c C-Sources/snprintf.c) +# Static version add_library(ModelicaMatIO STATIC ${libModelicaMatIO_SOURCES}) add_library(omc::simrt::Modelica::MatIO ALIAS ModelicaMatIO) target_compile_definitions(ModelicaMatIO PRIVATE HAVE_ZLIB) -target_link_libraries(ModelicaMatIO PUBLIC zlib) +target_link_libraries(ModelicaMatIO PUBLIC omc::3rd::zlib) target_link_libraries(ModelicaMatIO PUBLIC omc::simrt::runtime) -# find_package(ZLIB) -# if(ZLIB_FOUND) -# target_link_libraries(ModelicaMatIO PUBLIC ZLIB::ZLIB) -# target_compile_definitions(ModelicaMatIO PRIVATE HAVE_ZLIB) -# endif() - -# find_package(HDF5) -# if(HDF5_FOUND) -# target_include_directories(ModelicaMatIO PRIVATE ${HDF5_INCLUDE_DIRS}) -# target_link_libraries(ModelicaMatIO PUBLIC ${HDF5_LIBRARIES}) -# target_compile_definitions(ModelicaMatIO PRIVATE HAVE_HDF5) -# endif() +# Shared version +add_library(ModelicaMatIO_shared SHARED ${libModelicaMatIO_SOURCES}) +add_library(omc::simrt::Modelica::MatIO::shared ALIAS ModelicaMatIO_shared) +set_target_properties(ModelicaMatIO_shared + PROPERTIES OUTPUT_NAME ModelicaMatIO CLEAN_DIRECT_OUTPUT 1) + +target_compile_definitions(ModelicaMatIO_shared PUBLIC HAVE_ZLIB) +target_link_libraries(ModelicaMatIO_shared PUBLIC omc::3rd::zlib) +target_link_libraries(ModelicaMatIO_shared PUBLIC omc::simrt::runtime) +if(WIN32) + target_link_options(ModelicaMatIO_shared PRIVATE -Wl,--export-all-symbols) +endif() -# ModelicaIO +## ModelicaIO ######################################################################### set(libModelicaIO_SOURCES C-Sources/ModelicaIO.c) +# Static version add_library(ModelicaIO STATIC ${libModelicaIO_SOURCES}) add_library(omc::simrt::Modelica::IO ALIAS ModelicaIO) target_link_libraries(ModelicaIO PUBLIC ModelicaMatIO) +# Shared version +add_library(ModelicaIO_shared SHARED ${libModelicaIO_SOURCES}) +add_library(omc::simrt::Modelica::IO::shared ALIAS ModelicaIO_shared) +set_target_properties(ModelicaIO_shared + PROPERTIES OUTPUT_NAME ModelicaIO CLEAN_DIRECT_OUTPUT 1) + +target_link_libraries(ModelicaIO_shared PUBLIC ModelicaMatIO_shared) +if(WIN32) + target_link_options(ModelicaIO_shared PRIVATE -Wl,--export-all-symbols) +endif() + -# ModelicaStandardTables +## ModelicaStandardTables ######################################################################### set(ModelicaStandardTables_SOURCES C-Sources/ModelicaStandardTables.c C-Sources/ModelicaStandardTablesUsertab.c) +# Static version. add_library(ModelicaStandardTables STATIC ${ModelicaStandardTables_SOURCES}) add_library(omc::simrt::Modelica::StandardTables ALIAS ModelicaStandardTables) -# This seems to be needed. Otherwise we get undefined referenes to function 'usertab' +# This seems to be needed. Otherwise we get undefined references to function 'usertab' target_compile_definitions(ModelicaStandardTables PRIVATE -DDUMMY_FUNCTION_USERTAB) -target_link_libraries(ModelicaStandardTables INTERFACE ModelicaMatIO) +target_link_libraries(ModelicaStandardTables PUBLIC ModelicaIO) target_link_libraries(ModelicaStandardTables PUBLIC m) +# Shared version +add_library(ModelicaStandardTables_shared SHARED ${ModelicaStandardTables_SOURCES}) +add_library(omc::simrt::Modelica::StandardTables::shared ALIAS ModelicaStandardTables_shared) +set_target_properties(ModelicaStandardTables_shared + PROPERTIES OUTPUT_NAME ModelicaStandardTables CLEAN_DIRECT_OUTPUT 1) + +# This seems to be needed. Otherwise we get undefined references to function 'usertab' +target_compile_definitions(ModelicaStandardTables_shared PRIVATE -DDUMMY_FUNCTION_USERTAB) + +target_link_libraries(ModelicaStandardTables_shared PUBLIC ModelicaIO_shared) +target_link_libraries(ModelicaStandardTables_shared PUBLIC m) +if(WIN32) + target_link_options(ModelicaStandardTables_shared PRIVATE -Wl,--export-all-symbols) +endif() + +## Install +# Install the static libs to the normal lib dir. install(TARGETS ModelicaExternalC ModelicaMatIO ModelicaIO - ModelicaStandardTables) + ModelicaStandardTables + ) + +# Install the shared libs to a directory 'ffi' within the lib dir. +# This is so that they are not on the normal link path of simulation executables. +# We do not want to have them for anything other than FFI based for constant +# evaluation by omc (They are only loaded never linked) +install(TARGETS ModelicaExternalC_shared + ModelicaMatIO_shared + ModelicaIO_shared + ModelicaStandardTables_shared + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/ffi + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/ffi + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/ffi + ) diff --git a/OMCompiler/SimulationRuntime/ModelicaExternalC/MEC_standalone_2.8.cmake b/OMCompiler/SimulationRuntime/ModelicaExternalC/MEC_standalone_2.8.cmake new file mode 100644 index 00000000000..4a0a8ad5c04 --- /dev/null +++ b/OMCompiler/SimulationRuntime/ModelicaExternalC/MEC_standalone_2.8.cmake @@ -0,0 +1,150 @@ + +# We now use this CMake file for standalone compilation of ModelicaExternalC. +# That is, even for the autotools+Makefiles build system OpenModelica uses, this files +# is used to compile ModelicaExternalC. +# So we need to know where some of the OpenModelica libraries are to successfully +# compile DLLs for these files (i.e, no undefined references in the library) + +cmake_minimum_required(VERSION 3.4) +project(OMModelicaExternalC) + +# Use the OpenModelica lib and bin dirs as linking directories. +# The dependencies (libzlib and libOpenModelicaCompilerC) should have +# been built and installed to this dirs before we can build the ModelicaExternalC libs. +link_directories(${CMAKE_INSTALL_LIBDIR} ${CMAKE_INSTALL_BINDIR}) + +# Set the rpath to the same dir as the destination of the libs +# when installing. This should be enough since the dependencies +# (libOpenModelicaRuntimeC and libomcgc) should end up in the same +# dir as well (lib//omc/ on linux or bin/ on Windows). +set(CMAKE_INSTALL_RPATH $ORIGIN) + +## ModelicaExternalC ######################################################################### +set(libModelicaExternalC_SOURCES C-Sources/ModelicaFFT.c + C-Sources/ModelicaInternal.c + C-Sources/ModelicaRandom.c + C-Sources/ModelicaStrings.c) + +# Static version +add_library(ModelicaExternalC STATIC ${libModelicaExternalC_SOURCES}) +add_library(omc::simrt::Modelica::ExternalC ALIAS ModelicaExternalC) + +target_link_libraries(ModelicaExternalC PUBLIC m) +target_link_libraries(ModelicaExternalC PUBLIC OpenModelicaRuntimeC) +target_link_libraries(ModelicaExternalC PUBLIC omcgc) + +# Shared version. +add_library(ModelicaExternalC_shared SHARED ${libModelicaExternalC_SOURCES}) +add_library(omc::simrt::Modelica::ExternalC::shared ALIAS ModelicaExternalC_shared) +set_target_properties(ModelicaExternalC_shared + PROPERTIES OUTPUT_NAME ModelicaExternalC CLEAN_DIRECT_OUTPUT 1) + +target_link_libraries(ModelicaExternalC_shared PUBLIC m) +target_link_libraries(ModelicaExternalC_shared PUBLIC OpenModelicaRuntimeC) +target_link_libraries(ModelicaExternalC_shared PUBLIC omcgc) + +if(WIN32) + set_target_properties(ModelicaExternalC_shared PROPERTIES LINK_FLAGS "-Wl,--export-all-symbols") +endif() + + + +## ModelicaMatIO ######################################################################### +set(libModelicaMatIO_SOURCES C-Sources/ModelicaMatIO.c C-Sources/snprintf.c) +# Static version +add_library(ModelicaMatIO STATIC ${libModelicaMatIO_SOURCES}) +add_library(omc::simrt::Modelica::MatIO ALIAS ModelicaMatIO) + +target_compile_definitions(ModelicaMatIO PRIVATE HAVE_ZLIB) +target_link_libraries(ModelicaMatIO PUBLIC zlib) +target_link_libraries(ModelicaMatIO PUBLIC OpenModelicaRuntimeC) +target_link_libraries(ModelicaMatIO PUBLIC omcgc) + +# Shared version +add_library(ModelicaMatIO_shared SHARED ${libModelicaMatIO_SOURCES}) +add_library(omc::simrt::Modelica::MatIO::shared ALIAS ModelicaMatIO_shared) +set_target_properties(ModelicaMatIO_shared + PROPERTIES OUTPUT_NAME ModelicaMatIO CLEAN_DIRECT_OUTPUT 1) + +target_compile_definitions(ModelicaMatIO_shared PUBLIC HAVE_ZLIB) +target_link_libraries(ModelicaMatIO_shared PUBLIC zlib) +target_link_libraries(ModelicaMatIO_shared PUBLIC OpenModelicaRuntimeC) +target_link_libraries(ModelicaMatIO_shared PUBLIC omcgc) +if(WIN32) + set_target_properties(ModelicaMatIO_shared PROPERTIES LINK_FLAGS "-Wl,--export-all-symbols") +endif() + + +## ModelicaIO ######################################################################### +set(libModelicaIO_SOURCES C-Sources/ModelicaIO.c) +# Static version +add_library(ModelicaIO STATIC ${libModelicaIO_SOURCES}) +add_library(omc::simrt::Modelica::IO ALIAS ModelicaIO) + +target_link_libraries(ModelicaIO PUBLIC ModelicaMatIO) + +# Shared version +add_library(ModelicaIO_shared SHARED ${libModelicaIO_SOURCES}) +add_library(omc::simrt::Modelica::IO::shared ALIAS ModelicaIO_shared) +set_target_properties(ModelicaIO_shared + PROPERTIES OUTPUT_NAME ModelicaIO CLEAN_DIRECT_OUTPUT 1) + +target_link_libraries(ModelicaIO_shared PUBLIC ModelicaMatIO_shared) +if(WIN32) + set_target_properties(ModelicaIO_shared PROPERTIES LINK_FLAGS "-Wl,--export-all-symbols") +endif() + + + +## ModelicaStandardTables ######################################################################### +set(ModelicaStandardTables_SOURCES C-Sources/ModelicaStandardTables.c + C-Sources/ModelicaStandardTablesUsertab.c) +# Static version. +add_library(ModelicaStandardTables STATIC ${ModelicaStandardTables_SOURCES}) +add_library(omc::simrt::Modelica::StandardTables ALIAS ModelicaStandardTables) + +# This seems to be needed. Otherwise we get undefined references to function 'usertab' +target_compile_definitions(ModelicaStandardTables PRIVATE -DDUMMY_FUNCTION_USERTAB) + +target_link_libraries(ModelicaStandardTables PUBLIC ModelicaIO) +target_link_libraries(ModelicaStandardTables PUBLIC m) + +# Shared version +add_library(ModelicaStandardTables_shared SHARED ${ModelicaStandardTables_SOURCES}) +add_library(omc::simrt::Modelica::StandardTables::shared ALIAS ModelicaStandardTables_shared) +set_target_properties(ModelicaStandardTables_shared + PROPERTIES OUTPUT_NAME ModelicaStandardTables CLEAN_DIRECT_OUTPUT 1) + +# This seems to be needed. Otherwise we get undefined references to function 'usertab' +target_compile_definitions(ModelicaStandardTables_shared PRIVATE -DDUMMY_FUNCTION_USERTAB) + +target_link_libraries(ModelicaStandardTables_shared PUBLIC ModelicaIO_shared) +target_link_libraries(ModelicaStandardTables_shared PUBLIC m) +if(WIN32) + set_target_properties(ModelicaStandardTables_shared PROPERTIES LINK_FLAGS "-Wl,--export-all-symbols") +endif() + + +## Install +# Install the static libs to the normal lib dir. +install(TARGETS ModelicaExternalC + ModelicaMatIO + ModelicaIO + ModelicaStandardTables + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ) + +# Install the shared libs to a directory 'ffi' within the lib dir. +# This is so that they are not on the normal link path of simulation executables. +# We do not want to have them for anything other than FFI based for constant +# evaluation by omc (They are only loaded never linked) +install(TARGETS ModelicaExternalC_shared + ModelicaMatIO_shared + ModelicaIO_shared + ModelicaStandardTables_shared + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/ffi + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/ffi + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/ffi + ) diff --git a/OMCompiler/SimulationRuntime/OMSICpp/omcWrapper/omcCAPI/CMakeLists.txt b/OMCompiler/SimulationRuntime/OMSICpp/omcWrapper/omcCAPI/CMakeLists.txt index 114f21f694f..1912bbbe33b 100644 --- a/OMCompiler/SimulationRuntime/OMSICpp/omcWrapper/omcCAPI/CMakeLists.txt +++ b/OMCompiler/SimulationRuntime/OMSICpp/omcWrapper/omcCAPI/CMakeLists.txt @@ -36,7 +36,7 @@ MESSAGE(STATUS ${OMC_LIB_PATH}) INCLUDE_DIRECTORIES(./include) #search for ModelicaExtrenal C library -FIND_LIBRARY(MODELICAEX_LIB "ModelicaExternalC" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${OMC_LIB_PATH}) +FIND_LIBRARY(MODELICAEX_LIB NAMES libModelicaExternalC.a ModelicaExternalC.lib NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${OMC_LIB_PATH}) IF(NOT MODELICAEX_LIB) MESSAGE(FATAL_ERROR "Could not find ModelicaExternalC library!") ENDIF(NOT MODELICAEX_LIB)