From 7984f74f84b3207394d0c580fa9629599b2d52ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sj=C3=B6lund?= Date: Wed, 3 Apr 2019 08:22:50 +0200 Subject: [PATCH] Copy source-code FMU to a docker volume before compiling This avoids problems where the FMU source code is generated in a docker volume that cannot be forwarded to another container. Belonging to [master]: - OpenModelica/OMCompiler#3030 --- Compiler/Script/CevalScriptBackend.mo | 68 ++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/Compiler/Script/CevalScriptBackend.mo b/Compiler/Script/CevalScriptBackend.mo index cd35445664..44f239e736 100644 --- a/Compiler/Script/CevalScriptBackend.mo +++ b/Compiler/Script/CevalScriptBackend.mo @@ -3356,10 +3356,10 @@ protected dir=fmutmp+"/sources/", cmd="", quote="'", dquote = if isWindows then "\"" else "'", - includeDefaultFmi; + includeDefaultFmi, volumeID, cidFile, containerID; list rest; Boolean finishedBuild; - Integer uid; + Integer uid, status; algorithm CC := System.getCCompiler(); CFLAGS := "-Os "+System.stringReplace(System.getCFlags(),"${MODELICAUSERCFLAGS}",""); @@ -3427,22 +3427,66 @@ algorithm case host::"docker"::"run"::rest algorithm uid := System.getuid(); - path1 := System.realpath(fmutmp+"/.."); - path2 := path1 + "/" + System.basename(fmutmp); - for path in {path1, path2} loop - if not System.directoryExists(path) then - Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {path + " does not exist, but we should have just created it..."}); - end if; - end for; - cmd := "docker run "+(if uid<>0 then "--user " + String(uid) else "")+" --rm -w /fmu -v "+quote+path1+quote+":/fmu -v "+quote+System.realpath(includeDefaultFmi)+quote+":/fmiInclude "+stringDelimitList(rest," ")+ " sh -c " + dquote + - "(cd " + dquote + "/fmu/" + System.basename(fmutmp) + "/sources" + dquote + " || (ls -lh /fmu ; false)) && " + - "./configure --host="+quote+host+quote+" CFLAGS="+quote+"-Os"+quote+" CPPFLAGS=-I/fmiInclude LDFLAGS= && " + + // Create a docker volume for the FMU since we can't forward volumes + // to the docker run command depending on where the FMU was generated (inside another volume) + cmd := "docker volume create"; + if 0 <> System.systemCall(cmd, outFile=logfile) then + Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {cmd + " failed:\n" + System.readFile(logfile)}); + fail(); + end if; + cidFile := fmutmp+".cidfile"; + if System.regularFileExists(cidFile) then + System.removeFile(cidFile); + end if; + volumeID := System.trim(System.readFile(logfile)); + cmd := "docker run --cidfile "+cidFile+" -v "+volumeID+":/data busybox true"; + if 0 <> System.systemCall(cmd, outFile=logfile) then + Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {cmd + " failed:\n" + System.readFile(logfile)}); + // Cleanup + System.systemCall("docker volume rm " + volumeID); + fail(); + end if; + containerID := System.trim(System.readFile(cidFile)); + System.removeFile(cidFile); + // Copy the FMU contents to the container + cmd := "docker cp "+fmutmp+" "+containerID+":/data"; + if 0 <> System.systemCall(cmd, outFile=logfile) then + Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {cmd + " failed:\n" + System.readFile(logfile)}); + // Cleanup + System.systemCall("docker rm " + containerID); + System.systemCall("docker volume rm " + volumeID); + fail(); + end if; + // Copy the FMI headers to the container + cmd := "docker cp "+includeDefaultFmi+" "+containerID+":/data/fmiInclude"; + if 0 <> System.systemCall(cmd, outFile=logfile) then + Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {cmd + " failed:\n" + System.readFile(logfile)}); + // Cleanup + System.systemCall("docker rm " + containerID); + System.systemCall("docker volume rm " + volumeID); + fail(); + end if; + cmd := "docker run "+(if uid<>0 then "--user " + String(uid) else "")+" --rm -w /fmu -v "+volumeID+":/fmu "+stringDelimitList(rest," ")+ " sh -c " + dquote + + "cd " + dquote + "/fmu/" + System.basename(fmutmp) + "/sources" + dquote + " && " + + "./configure --host="+quote+host+quote+" CFLAGS="+quote+"-Os"+quote+" CPPFLAGS=-I/fmu/fmiInclude LDFLAGS= && " + nozip + dquote; if 0 <> System.systemCall(cmd, outFile=logfile) then Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {cmd + ":\n" + System.readFile(logfile)}); System.removeFile(dir + logfile); + // Cleanup + System.systemCall("docker rm " + containerID); + System.systemCall("docker volume rm " + volumeID); + fail(); + end if; + // Copy the files back from the volume (via the container) to the filesystem + cmd := "docker cp " + quote + containerID + ":/data/" + fmutmp + quote + " " + quote + fmutmp + quote; + if 0 <> System.systemCall(cmd, outFile=logfile) then + Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {cmd + ":\n" + System.readFile(logfile)}); fail(); end if; + // Cleanup + System.systemCall("docker rm " + containerID); + System.systemCall("docker volume rm " + volumeID); then true; else algorithm