Skip to content

Commit

Permalink
Add targets for the MSVC FMI C-runtime via wine
Browse files Browse the repository at this point in the history
Note: This requires a visual studio setup including env.variables WSDK and
W10SDK set, as well as OMDEV installed (for cmake.exe).

Belonging to [master]:
  - OpenModelica/OMCompiler#2225
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Feb 21, 2018
1 parent 70e2f00 commit 1dbe81e
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/Inst.mo
Expand Up @@ -5498,8 +5498,8 @@ algorithm
/* adrpo: reuse it if is already there! */
try
ht := getGlobalRoot(Global.instHashIndex);
ht := BaseHashTable.clear(ht);
setGlobalRoot(Global.instHashIndex, ht);
ht := BaseHashTable.clear(ht);
setGlobalRoot(Global.instHashIndex, ht);
else
setGlobalRoot(Global.instHashIndex, emptyInstHashTable());
end try;
Expand Down
42 changes: 21 additions & 21 deletions Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -3473,29 +3473,29 @@ algorithm
// create the path till packagetool
str2 := stringAppendList({omhome,pd,"lib",pd,"omc",pd,"SEMLA",pd,"packagetool",str1});
if System.regularFileExists(str2) then
// create the list of arguments for packagetool
str3 := "-librarypath \"" + System.dirname(fileName) + "\" -version \"1.0\" -language \"3.2\" -encrypt \"true\"";
call := stringAppendList({str2," ",str3});
logFile := "packagetool.log";
// remove the logFile if it already exists.
if System.regularFileExists(logFile) then
System.removeFile(logFile);
end if;
// run the packagetool command
if 0 == System.systemCall(call, logFile) then
success := true;
else
success := false;
end if;
// read the logFile
if System.regularFileExists(logFile) then
commandOutput := System.readFile(logFile);
end if;
else
Error.addMessage(Error.ENCRYPTION_NOT_SUPPORTED, {str2});
// create the list of arguments for packagetool
str3 := "-librarypath \"" + System.dirname(fileName) + "\" -version \"1.0\" -language \"3.2\" -encrypt \"true\"";
call := stringAppendList({str2," ",str3});
logFile := "packagetool.log";
// remove the logFile if it already exists.
if System.regularFileExists(logFile) then
System.removeFile(logFile);
end if;
// run the packagetool command
if 0 == System.systemCall(call, logFile) then
success := true;
else
success := false;
end if;
// read the logFile
if System.regularFileExists(logFile) then
commandOutput := System.readFile(logFile);
end if;
else
Error.addMessage(Error.ENCRYPTION_NOT_SUPPORTED, {str2});
commandOutput := "";
success := false;
end if;
end if;
else
Error.addMessage(Error.FILE_NOT_FOUND_ERROR, {fileName});
commandOutput := "";
Expand Down
40 changes: 33 additions & 7 deletions Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -751,7 +751,7 @@ algorithm
matchcontinue (inEnv, className, inFileNamePrefix, addDummy, inSimSettingsOpt, args)
local
String filenameprefix, file_dir, resstr, description;
DAE.DAElist dae;
DAE.DAElist dae, dae1;
FCore.Graph graph;
BackendDAE.BackendDAE dlow, dlow_1;
BackendDAE.Shared shared;
Expand All @@ -774,30 +774,56 @@ algorithm
ExecStat.execStatReset();
(cache, graph, odae) := CevalScriptBackend.runFrontEnd(cache, graph, className, false);
ExecStat.execStat("FrontEnd");
SOME(dae) := odae;
SOME(dae1) := odae;

if Flags.isSet(Flags.SERIALIZED_SIZE) then
serializeNotify(dae, "FrontEnd DAE");
serializeNotify(getGlobalRoot(Global.instHashIndex), "Global.instHash");
try
serializeNotify(getGlobalRoot(Global.builtinEnvIndex), "Global.builtinIndex");
else
end try;
try
serializeNotify(getGlobalRoot(Global.rewriteRulesIndex), "Global.rewriteRulesIndex");
else
end try;
try
serializeNotify(getGlobalRoot(Global.inlineHashTable), "Global.inlineHashTable");
else
end try;
try
serializeNotify(getGlobalRoot(Global.operatorOverloadingCache), "Global.operatorOverloadingCache");
else
end try;
try
serializeNotify(getGlobalRoot(Global.interactiveCache), "Global.interactiveCache");
else
end try;
serializeNotify(dae1, "FrontEnd DAE");
serializeNotify(graph, "FCore.Graph");
serializeNotify((graph,inEnv), "FCore.Graph + Old graph");
serializeNotify(cache, "FCore.Cache");
serializeNotify((cache,inCache), "FCore.Cache + Old cache");
serializeNotify(SymbolTable.get(), "Symbol Table (Absyn and SCode)");
serializeNotify((SymbolTable.get(),dae,graph,inEnv,cache,inCache), "Symbol Table, DAE, Graph, OldGraph, Cache, OldCache");
serializeNotify((SymbolTable.get(),dae1,graph,inEnv,cache,inCache), "Symbol Table, DAE, Graph, OldGraph, Cache, OldCache");
ExecStat.execStat("Serialize FrontEnd");
end if;

timeFrontend := System.realtimeTock(ClockIndexes.RT_CLOCK_FRONTEND);

System.realtimeTick(ClockIndexes.RT_CLOCK_BACKEND);
state := State.backend;
dae := DAEUtil.transformationsBeforeBackend(cache, graph, dae);
dae := DAEUtil.transformationsBeforeBackend(cache, graph, dae1);
ExecStat.execStat("Transformations before backend");

if Flags.isSet(Flags.SERIALIZED_SIZE) then
serializeNotify(dae, "FrontEnd DAE after transformations");
serializeNotify((dae,dae1), "FrontEnd DAE before+after transformations");
ExecStat.execStat("Serialize DAE (2)");
end if;
GC.free(dae1);
GC.free(odae);
odae := NONE();
dae1 := DAE.emptyDae;

generateFunctions := Flags.set(Flags.GEN, false);
// We should not need to lookup constants and classes in the backend,
Expand All @@ -810,6 +836,7 @@ algorithm
dlow := BackendDAECreate.lower(dae, cache, graph, BackendDAE.EXTRA_INFO(description,filenameprefix));

GC.free(dae);
dae := DAE.emptyDae;

if Flags.isSet(Flags.SERIALIZED_SIZE) then
serializeNotify(dlow, "BackendDAECreate.lower");
Expand Down Expand Up @@ -842,8 +869,7 @@ algorithm
serializeNotify(dlow, "BackendDAE (simulation)");
serializeNotify(initDAE, "BackendDAE (initialization)");
serializeNotify(initDAE_lambda0, "BackendDAE (lambda0)");
serializeNotify((dlow,initDAE,initDAE_lambda0), "BackendDAE (simulation+initialization+lambda0)");
serializeNotify(removedInitialEquationLst, "removedInitialEquationLst");
serializeNotify((dlow,initDAE,initDAE_lambda0,inlineData,removedInitialEquationLst), "BackendDAE (simulation+initialization+lambda0+inlineData+removedInitialEquationLst)");
ExecStat.execStat("Serialize solved system");
end if;

Expand Down
28 changes: 17 additions & 11 deletions Compiler/runtime/CMakeLists.txt
@@ -1,4 +1,9 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

# Global Variables
IF(NOT OMCTRUNCHOME)
SET(OMCTRUNCHOME ${CMAKE_CURRENT_SOURCE_DIR}/../../)
ENDIF (NOT OMCTRUNCHOME)

IF(NOT IDL)
FIND_PROGRAM(IDL idl DOC "Location of the idl executable"
Expand All @@ -7,19 +12,18 @@ IF(NOT IDL)
MESSAGE(STATUS "-- idl is " ${IDL})
ENDIF(NOT IDL)

if(WIN32)
INCLUDE_DIRECTORIES($ENV{OMDEV}/tools/rml/include/plain)
else(WIN32)
INCLUDE_DIRECTORIES(${RMLHOME}/include/plain)
endif(WIN32)
INCLUDE_DIRECTORIES(${CODEGENDIR})
INCLUDE_DIRECTORIES(${CODEGENDIR}/absyn_builder)
if(MSVC)
add_definitions(-DOMC_NO_THREADS -D_CRT_SECURE_NO_WARNINGS)
endif(MSVC)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lpsolve)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/c_runtime)

INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/meta)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/util)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/3rdParty/gc/include)

# custom command for idl
IF(IDL)
SET(IDL_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/omc_communication.cc ${CMAKE_CURRENT_BINARY_DIR}/omc_communication.h)
ADD_CUSTOM_COMMAND(OUTPUT ${IDL_OUTPUT}
COMMAND ${IDL} ${CMAKE_CURRENT_SOURCE_DIR}/omc_communication.idl
Expand All @@ -28,10 +32,11 @@ ADD_CUSTOM_COMMAND(OUTPUT ${IDL_OUTPUT}

# target fuer OM_OUTPUT
ADD_CUSTOM_TARGET(omc_communication ALL DEPENDS ${IDL_OUTPUT})
ENDIF(IDL)

# Quellen und Header
SET(CORBASRC ${IDL_OUTPUT} omc_communication_impl.cpp corbaimpl.cpp)
SET(SRC socketimpl.c printimpl.c systemimpl.c settingsimpl.c SimulationResults.c)
SET(SRC socketimpl.c printimpl.c systemimpl.c settingsimpl.c SimulationResults.c)
SET(CPPSRC unitparser.cpp unitparserext.cpp ptolemyio.cpp Dynload.cpp BackendDAEEXT.cpp ErrorMessage.cpp errorext.cpp
systemimplmisc.cpp ${CORBASRC} serializer.cpp)
SET(OBJ ${SRC} ${CPPSRC})
Expand All @@ -46,5 +51,6 @@ link_directories(${OMCTRUNCHOME}/Compiler/runtime/lpsolve/lib/win32)
# Library runtime
ADD_LIBRARY(runtime ${OBJ})
TARGET_LINK_LIBRARIES(runtime sendData c_runtime ${platform_libs} optimized lpsolve55 debug lpsolve55d)
IF(MOCH_codegen_ALL)
add_dependencies(runtime ${MOCH_codegen_ALL})

ENDIF(MOCH_codegen_ALL)
19 changes: 13 additions & 6 deletions Compiler/runtime/printimpl.c
Expand Up @@ -57,11 +57,6 @@ typedef struct print_members_s {
long* savedNfilled;
} print_members;

#include <pthread.h>

pthread_once_t printimpl_once_create_key = PTHREAD_ONCE_INIT;
pthread_key_t printimplKey;

static void free_printimpl(void *data)
{
int i;
Expand All @@ -83,19 +78,31 @@ static void free_printimpl(void *data)
free(members);
}

#if !defined(OMC_NO_THREADS)
#include <pthread.h>

pthread_once_t printimpl_once_create_key = PTHREAD_ONCE_INIT;
pthread_key_t printimplKey;

static void make_key()
{
pthread_key_create(&printimplKey,free_printimpl);
}
#endif

static print_members* getMembers(threadData_t *threadData)
{
print_members *res;
#if defined(OMC_NO_THREADS)
static
#endif
print_members *res = NULL;
if (threadData && threadData->localRoots[LOCAL_ROOT_PRINT_MO]) {
return threadData->localRoots[LOCAL_ROOT_PRINT_MO];
}
#if !defined(OMC_NO_THREADS)
pthread_once(&printimpl_once_create_key,make_key);
res = (print_members*) pthread_getspecific(printimplKey);
#endif
if (res != NULL) return res;
res = (print_members*) calloc(1,sizeof(print_members));
pthread_setspecific(printimplKey,res);
Expand Down
2 changes: 0 additions & 2 deletions Compiler/runtime/socketimpl.c
Expand Up @@ -35,7 +35,6 @@
#include <winsock2.h>
#include <ws2tcpip.h>

#include <unistd.h>
#include <errno.h>
#include <io.h>

Expand Down Expand Up @@ -81,7 +80,6 @@ fsync (int fd)

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "errorext.h"

Expand Down
2 changes: 1 addition & 1 deletion Makefile.common
@@ -1,4 +1,4 @@
inclu.PHONY : all omc release clean .testvariables mkbuilddirs
inclu.PHONY : all omc release clean .testvariables mkbuilddirs checkMSVCwine

unexport PREFIX

Expand Down
82 changes: 82 additions & 0 deletions Makefile.in
Expand Up @@ -132,3 +132,85 @@ emcc-simulationruntime:
$(MAKE) -C SimulationRuntime/c emcc

copycppheader:

OMDEV_CMAKE="$(OMDEV)/bin/cmake/bin/cmake.exe"

checkMSVCwine:
@echo 'Test for WINE, OMDEV, VS140COMNTOOLS env.vars'
test ! -z "$(WINE)"
test ! -z "$(OMDEV)"
test ! -z "$(VS140COMNTOOLS)"
"$(WINE)" cl.exe
"$(WINE)" cmd /c "if not exist $(OMDEV_CMAKE) (exit 1)"

wine_fmil_msvc: .wine_fmil_msvc
.wine_fmil_msvc: checkMSVCwine
rm -f $(OMBUILDDIR)/lib/omc/msvc/fmil*
mkdir -p $(OMBUILDDIR)/lib/omc/msvc/
# Goto build msvc directory
test -d 3rdParty/FMIL

(test -f $@ && test -d 3rdParty/FMIL/build_msvc && test -d 3rdParty/FMIL/install_msvc) || (rm -rf 3rdParty/FMIL/build_msvc && rm -rf 3rdParty/FMIL/install_msvc && mkdir -p 3rdParty/FMIL/build_msvc && cd 3rdParty/FMIL/build_msvc && "$(WINE)" "$(OMDEV_CMAKE)" -DCMAKE_VERBOSE_MAKEFILE:Bool=ON -DFMILIB_BUILD_TESTS=OFF -DFMILIB_BUILD_STATIC_LIB=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo -DFMILIB_INSTALL_PREFIX=../install_msvc ../ -G "NMake Makefiles" && $(WINE) nmake /f Makefile install)

(cp -puf 3rdParty/FMIL/install_msvc/lib/fmilib_shared.dll $(OMBUILDDIR)/lib/omc/msvc/fmilib.dll)
(cp -puf 3rdParty/FMIL/install_msvc/lib/fmilib_shared.lib $(OMBUILDDIR)/lib/omc/msvc/fmilib.lib)
# copy the dll with the actual name: fmilib_shared.dll as the fmilib.lib contains that name and the exe will look for it
(cp -puf 3rdParty/FMIL/install_msvc/lib/fmilib_shared.dll $(OMBUILDDIR)/lib/omc/msvc/fmilib_shared.dll)
(cp -puf 3rdParty/FMIL/install_msvc/lib/fmilib_shared.lib $(OMBUILDDIR)/lib/omc/msvc/fmilib_shared.lib)
@touch $@

wine_sundials_msvc: .wine_sundials_msvc
.wine_sundials_msvc: checkMSVCwine
rm -f $(OMBUILDDIR)/lib/omc/msvc/sundials*
mkdir -p $(OMBUILDDIR)/lib/omc/msvc/
# Goto build msvc directory
test -d 3rdParty/sundials

(test -f $@ && test -d 3rdParty/sundials/build_msvc) || (rm -rf 3rdParty/sundials/build_msvc && mkdir -p 3rdParty/sundials/build_msvc && cd 3rdParty/sundials/build_msvc && "$(WINE)" "$(OMDEV_CMAKE)" -DCMAKE_VERBOSE_MAKEFILE:Bool=ON -DBUILD_STATIC_LIBS=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=. ../ -G "NMake Makefiles" && $(WINE) nmake /f Makefile install)
mkdir -p $(OMBUILDDIR)/include/omc/msvc/sundials
(cp -pufr 3rdParty/sundials/build_msvc/include/* $(OMBUILDDIR)/include/omc/msvc/sundials)
(cp -puf 3rdParty/sundials/build_msvc/lib/* $(OMBUILDDIR)/lib/omc/msvc/)
@touch $@

wine_runc_msvc_release: .wine_runc_msvc_release
.wine_runc_msvc_release: mkbuilddirs checkMSVCwine wine_sundials_msvc wine_fmil_msvc wine_runc_msvc_gc
mkdir -p $(OMBUILDDIR)/include/omc/msvc/
mkdir -p $(OMBUILDDIR)/lib/omc/msvc/release
echo 'Build the MSVC RELEASE'

(test -f $@ && test -d SimulationRuntime/c/build_msvc/) || (rm -rf SimulationRuntime/c/build_msvc && mkdir -p SimulationRuntime/c/build_msvc && cd SimulationRuntime/c/build_msvc && "$(WINE)" "$(OMDEV_CMAKE)" -DCMAKE_VERBOSE_MAKEFILE:Bool=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:String=$(OMBUILDDIR) ../ -G "NMake Makefiles" && "$(WINE)" nmake /f Makefile)
@echo 'Copy the .lib files (needed for compilation) and .pdb files (needed for debugging) to $(OMBUILDDIR)/lib/omc'
(cd SimulationRuntime/c/build_msvc && find . -type f -path "*.lib" -exec cp {} $(OMBUILDDIR)/lib/omc/msvc/release/. \;)
(cd SimulationRuntime/c/build_msvc && find . -type f -path "*.pdb" -exec cp {} $(OMBUILDDIR)/lib/omc/msvc/release/. \;)
@touch $@

wine_runc_msvc_debug: .wine_runc_msvc_debug
.wine_runc_msvc_debug: mkbuilddirs checkMSVCwine wine_sundials_msvc wine_fmil_msvc wine_runc_msvc_gc
mkdir -p $(OMBUILDDIR)/include/omc/msvc/
mkdir -p $(OMBUILDDIR)/lib/omc/msvc/debug
echo 'Build the MSVC DEBUG'

(test -f $@ && test -d SimulationRuntime/c/build_msvc_debug/) || (rm -rf SimulationRuntime/c/build_msvc_debug && mkdir -p SimulationRuntime/c/build_msvc_debug && cd SimulationRuntime/c/build_msvc && "$(WINE)" "$(OMDEV_CMAKE)" -DCMAKE_VERBOSE_MAKEFILE:Bool=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX:String=$(OMBUILDDIR) ../ -G "NMake Makefiles" && "$(WINE)" nmake /f Makefile)
@echo 'Copy the .lib files (needed for compilation) and .pdb files (needed for debugging) to $(OMBUILDDIR)/lib/omc'
(cd SimulationRuntime/c/build_msvc_debug && find . -type f -path "*.lib" -exec cp {} $(OMBUILDDIR)/lib/omc/msvc/debug/. \;)
(cd SimulationRuntime/c/build_msvc_debug && find . -type f -path "*.pdb" -exec cp {} $(OMBUILDDIR)/lib/omc/msvc/debug/. \;)
@touch $@

wine_runc_msvc_gc: .wine_runc_msvc_gc
.wine_runc_msvc_gc: mkbuilddirs checkMSVCwine
# build gc as well, as it is needed for the msvc c runtime
@echo 'Build gc (RelWithDebInfo)'

(test -f $@ && test -d 3rdParty/gc/build_msvc/) || (rm -rf 3rdParty/gc/build_msvc/ && mkdir -p 3rdParty/gc/build_msvc && cd 3rdParty/gc/build_msvc && "$(WINE)" "$(OMDEV_CMAKE)" -DCMAKE_BUILD_TYPE=RelWithDebInfo ../ -G "NMake Makefiles" && "$(WINE)" nmake /f Makefile)
@echo 'Copy the .lib files (needed for compilation) and .pdb files (needed for debugging) to $(OMBUILDDIR)/lib/omc/msvc/'
cp -u 3rdParty/gc/build_msvc/gc-lib.* $(OMBUILDDIR)/lib/omc/msvc/
@touch $@

wine_run_omc_msvc: .wine_run_omc_msvc
.wine_run_omc_msvc: checkMSVCwine
@echo 'Build MSVC Compiler/runtime (RelWithDebInfo)'

(test -f $@ && test -d Compiler/runtime/build_msvc/) || (rm -rf Compiler/runtime/build_msvc/ && mkdir -p Compiler/runtime/build_msvc && cd Compiler/runtime/build_msvc && "$(WINE)" "$(OMDEV_CMAKE)" -DCMAKE_BUILD_TYPE=RelWithDebInfo ../ -G "NMake Makefiles" && "$(WINE)" nmake /f Makefile)
@echo 'Copy the .lib files (needed for compilation) and .pdb files (needed for debugging) to $(OMBUILDDIR)/lib/omc/msvc/'
# cp -u Compiler/runtime/build_msvc/gc-lib.* $(OMBUILDDIR)/lib/omc/msvc/
@touch $@
8 changes: 4 additions & 4 deletions SimulationRuntime/c/CMakeLists.txt
Expand Up @@ -60,10 +60,10 @@ IF(SUNDIALS_INCLUDE_DIR)

SET(SUNDIALS_LIBRARIES ${SUNDIALS_LIBRARY_CVODE} ${SUNDIALS_LIBRARY_IDA} ${SUNDIALS_LIBRARY_NVEC} ${SUNDIALS_KINSOL})

ENDIF(SUNDIALS_LIBRARY_CVODE AND
SUNDIALS_LIBRARY_IDA AND
SUNDIALS_LIBRARY_NVEC AND
SUNDIALS_KINSOL)
ENDIF(SUNDIALS_LIBRARY_CVODE AND
SUNDIALS_LIBRARY_IDA AND
SUNDIALS_LIBRARY_NVEC AND
SUNDIALS_KINSOL)

ENDIF(SUNDIALS_INCLUDE_DIR)

Expand Down
4 changes: 3 additions & 1 deletion SimulationRuntime/c/gc/omc_gc.h
Expand Up @@ -51,7 +51,7 @@ extern "C" {

#if defined(_MSC_VER)
#include "omc_inline.h"
#include "omc_msvc.h"
#include "util/omc_msvc.h"
#endif

typedef struct {
Expand Down Expand Up @@ -180,8 +180,10 @@ static inline void* mmc_check_out_of_memory(void *ptr)

#include <gc.h>
/* gc.h doesn't include this by default; and the actual header redirects dlopen, which does not have an implementation */
#if !defined(OMC_NO_THREADS)
int GC_pthread_create(pthread_t *,const pthread_attr_t *,void *(*)(void *), void *);
int GC_pthread_join(pthread_t, void **);
#endif

void omc_GC_set_max_heap_size(size_t);
size_t omc_GC_get_max_heap_size();
Expand Down

0 comments on commit 1dbe81e

Please sign in to comment.