Skip to content

Commit

Permalink
Add pre-build-cmd to docker target property
Browse files Browse the repository at this point in the history
  • Loading branch information
lhstrh committed Mar 7, 2024
1 parent cb55ee0 commit 9d09798
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.lflang.generator.docker;

import org.eclipse.xtext.xbase.lib.IterableExtensions;
import java.util.List;
import org.lflang.generator.LFGeneratorContext;
import org.lflang.target.Target;
import org.lflang.target.property.BuildCommandsProperty;
import org.lflang.util.StringUtil;

/**
* Generate the docker file related code for the C and CCpp target.
Expand Down Expand Up @@ -34,10 +32,7 @@ public String defaultImage() {
protected String generateDockerFileContent() {
var lfModuleName = context.getFileConfig().name;
var config = context.getTargetConfig();
var compileCommand =
IterableExtensions.isNullOrEmpty(config.get(BuildCommandsProperty.INSTANCE))
? generateCompileCommand()
: StringUtil.joinObjects(config.get(BuildCommandsProperty.INSTANCE), " ");
var compileCommand = generateCompileCommand();
var compiler = config.target == Target.CCPP ? "g++" : "gcc";
var baseImage = baseImage();
return String.join(
Expand Down Expand Up @@ -83,14 +78,13 @@ protected String generateRunForBuildDependencies() {
}
}

/** Return the default compile command for the C docker container. */
protected String generateCompileCommand() {
return String.join(
"\n",
"RUN set -ex && \\",
"mkdir bin && \\",
"cmake -DCMAKE_INSTALL_BINDIR=./bin -S src-gen -B bin && \\",
"cd bin && \\",
@Override
protected List<String> defaultBuildCommands() {
return List.of(
"set -ex",
"mkdir -p bin",
"cmake -DCMAKE_INSTALL_BINDIR=./bin -S src-gen -B bin",
"cd bin",
"make all");
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.lflang.generator.docker;

import java.nio.file.Path;
import java.util.List;
import org.lflang.generator.LFGeneratorContext;
import org.lflang.target.property.BuildCommandsProperty;
import org.lflang.target.property.DockerProperty;
import org.lflang.util.StringUtil;

/**
* A class for generating docker files.
Expand Down Expand Up @@ -30,6 +33,28 @@ public DockerGenerator(LFGeneratorContext context) {
/** Return a RUN command for installing/checking build dependencies. */
protected abstract String generateRunForBuildDependencies();

/** Return the default compile commands for the C docker container. */
protected abstract List<String> defaultBuildCommands();

protected List<String> getBuildCommands() {
var customBuildCommands = context.getTargetConfig().get(BuildCommandsProperty.INSTANCE);
if (customBuildCommands != null && !customBuildCommands.isEmpty()) {
return customBuildCommands;
}
return defaultBuildCommands();
}

/** Return the default compile command for the C docker container. */
protected String generateCompileCommand() {
var preBuildCmd = preBuildCmd();
var run = "RUN ";
var del = " && ";
if (!preBuildCmd.isEmpty()) {
run = run + preBuildCmd + del;
}
return run + StringUtil.joinObjects(getBuildCommands(), del);
}

/** Return the default base image. */
public abstract String defaultImage();

Expand All @@ -42,6 +67,14 @@ public String baseImage() {
return defaultImage();
}

public String preBuildCmd() {
var preBuildCmd = context.getTargetConfig().get(DockerProperty.INSTANCE).preBuildCmd();
if (preBuildCmd != null) {
return preBuildCmd;
}
return "";
}

/**
* Produce a DockerData object, which bundles all information needed to output a Dockerfile.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lflang.generator.docker;

import java.util.List;
import org.lflang.generator.LFGeneratorContext;

/**
Expand Down Expand Up @@ -31,6 +32,11 @@ protected String generateRunForBuildDependencies() {
return "RUN which node && node --version";
}

@Override
protected List<String> defaultBuildCommands() {
return List.of("pnpm install"); // FIXME: actually build using docker, not natively.
}

@Override
public String defaultImage() {
return "node:alpine";
Expand Down
32 changes: 24 additions & 8 deletions core/src/main/java/org/lflang/target/property/DockerProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public DockerOptions fromAst(Element node, MessageReporter reporter) {
var enabled = false;
var from = "";
var rti = DockerOptions.DOCKERHUB_RTI_IMAGE;
var preBuildCmd = "";

if (node.getLiteral() != null) {
if (ASTUtils.toBoolean(node)) {
Expand All @@ -45,15 +46,23 @@ public DockerOptions fromAst(Element node, MessageReporter reporter) {
enabled = true;
for (KeyValuePair entry : node.getKeyvalue().getPairs()) {
DockerOption option = (DockerOption) DictionaryType.DOCKER_DICT.forName(entry.getName());
if (option != null && option.equals(DockerOption.FROM)) {
from = ASTUtils.elementToSingleString(entry.getValue());
if (option == null) {
continue;
}
if (option != null && option.equals(DockerOption.RTI_IMAGE)) {
rti = ASTUtils.elementToSingleString(entry.getValue());
switch (option) {
case FROM:
from = ASTUtils.elementToSingleString(entry.getValue());
break;
case RTI_IMAGE:
rti = ASTUtils.elementToSingleString(entry.getValue());
break;
case PRE_BUILD_CMD:
preBuildCmd = ASTUtils.elementToSingleString(entry.getValue());
break;
}
}
}
return new DockerOptions(enabled, from, rti);
return new DockerOptions(enabled, from, rti, preBuildCmd);
}

@Override
Expand Down Expand Up @@ -86,6 +95,12 @@ public Element toAstElement(DockerOptions value) {
}
pair.setValue(ASTUtils.toElement(value.rti));
}
if (opt == DockerOption.PRE_BUILD_CMD) {
if (!value.preBuildCmd.isEmpty()) {
continue;
}
pair.setValue(ASTUtils.toElement(value.preBuildCmd));
}
kvp.getPairs().add(pair);
}
e.setKeyvalue(kvp);
Expand All @@ -102,7 +117,7 @@ public String name() {
}

/** Settings related to Docker options. */
public record DockerOptions(boolean enabled, String from, String rti) {
public record DockerOptions(boolean enabled, String from, String rti, String preBuildCmd) {

/** Default location to pull the rti from. */
public static final String DOCKERHUB_RTI_IMAGE = "lflang/rti:rti";
Expand All @@ -111,7 +126,7 @@ public record DockerOptions(boolean enabled, String from, String rti) {
public static final String LOCAL_RTI_IMAGE = "rti:local";

public DockerOptions(boolean enabled) {
this(enabled, "", DOCKERHUB_RTI_IMAGE);
this(enabled, "", DOCKERHUB_RTI_IMAGE, "");
}
}

Expand All @@ -122,7 +137,8 @@ public DockerOptions(boolean enabled) {
*/
public enum DockerOption implements DictionaryElement {
FROM("FROM", PrimitiveType.STRING),
RTI_IMAGE("rti-image", PrimitiveType.STRING);
RTI_IMAGE("rti-image", PrimitiveType.STRING),
PRE_BUILD_CMD("pre-build-cmd", PrimitiveType.STRING);

public final PrimitiveType type;

Expand Down
3 changes: 2 additions & 1 deletion test/C/src/docker/federated/DistributedCountContainerized.lf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ target C {
logging: DEBUG,
coordination: centralized,
docker: {
rti-image: "rti:local"
rti-image: "rti:local",
pre-build-cmd: "ls"
}
}

Expand Down

0 comments on commit 9d09798

Please sign in to comment.