diff --git a/core/src/main/java/org/teavm/backend/c/CTarget.java b/core/src/main/java/org/teavm/backend/c/CTarget.java index 77aed11c8..5bf0c2134 100644 --- a/core/src/main/java/org/teavm/backend/c/CTarget.java +++ b/core/src/main/java/org/teavm/backend/c/CTarget.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Deque; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -719,7 +720,12 @@ public class CTarget implements TeaVMTarget, TeaVMCHost { private void generateMain(GenerationContext context, CodeWriter writer, IncludeManager includes, ListableClassHolderSource classes, List types) { - writer.println("int main(int argc, char** argv) {").indent(); + Iterator entryPointIter = controller.getEntryPoints().values().iterator(); + String mainFunctionName = entryPointIter.hasNext() ? entryPointIter.next().getPublicName() : null; + if (mainFunctionName == null) { + mainFunctionName = "main"; + } + writer.println("int " + mainFunctionName + "(int argc, char** argv) {").indent(); writer.println("teavm_beforeInit();"); writer.println("teavm_initHeap(" + minHeapSize + ");"); @@ -832,8 +838,9 @@ public class CTarget implements TeaVMTarget, TeaVMCHost { private void generateCallToMainMethod(IntrinsicContext context, InvocationExpr invocation) { NameProvider names = context.names(); - TeaVMEntryPoint entryPoint = controller.getEntryPoints().get("main"); - if (entryPoint != null) { + Iterator entryPointIter = controller.getEntryPoints().values().iterator(); + if (entryPointIter.hasNext()) { + TeaVMEntryPoint entryPoint = entryPointIter.next(); context.includes().includeClass(entryPoint.getMethod().getClassName()); String mainMethod = names.forMethod(entryPoint.getMethod()); context.writer().print(mainMethod + "("); diff --git a/tools/c-incremental/src/main/java/org/teavm/tooling/c/incremental/IncrementalCBuilder.java b/tools/c-incremental/src/main/java/org/teavm/tooling/c/incremental/IncrementalCBuilder.java index e740cf956..3aa85865b 100644 --- a/tools/c-incremental/src/main/java/org/teavm/tooling/c/incremental/IncrementalCBuilder.java +++ b/tools/c-incremental/src/main/java/org/teavm/tooling/c/incremental/IncrementalCBuilder.java @@ -67,6 +67,7 @@ public class IncrementalCBuilder { private String targetPath; private String externalTool; private String externalToolWorkingDir; + private String mainFunctionName; private IncrementalDirectoryBuildTarget buildTarget; private FileSystemWatcher watcher; @@ -126,6 +127,10 @@ public class IncrementalCBuilder { this.externalToolWorkingDir = externalToolWorkingDir; } + public void setMainFunctionName(String mainFunctionName) { + this.mainFunctionName = mainFunctionName; + } + public void addProgressHandler(ProgressHandler handler) { synchronized (progressHandlers) { progressHandlers.add(handler); @@ -329,7 +334,7 @@ public class IncrementalCBuilder { vm.installPlugins(); vm.setLastKnownClasses(lastReachedClasses); - vm.entryPoint(mainClass); + vm.entryPoint(mainClass, mainFunctionName != null ? mainFunctionName : "main"); log.info("Starting build"); progressListener.last = 0; diff --git a/tools/cli/src/main/java/org/teavm/cli/TeaVMCBuilderRunner.java b/tools/cli/src/main/java/org/teavm/cli/TeaVMCBuilderRunner.java index b47814ca2..0bfd85f3e 100644 --- a/tools/cli/src/main/java/org/teavm/cli/TeaVMCBuilderRunner.java +++ b/tools/cli/src/main/java/org/teavm/cli/TeaVMCBuilderRunner.java @@ -62,6 +62,12 @@ public class TeaVMCBuilderRunner { .hasArg() .withDescription("Minimum heap size in bytes") .create()); + options.addOption(OptionBuilder + .withLongOpt("entry-point") + .withArgName("name") + .hasArg() + .withDescription("Name of entry point function (main by default)") + .create('e')); options.addOption(OptionBuilder .withLongOpt("external-tool") .withArgName("path") @@ -108,6 +114,9 @@ public class TeaVMCBuilderRunner { builder.setLog(new ConsoleTeaVMToolLog(commandLine.hasOption('v'))); builder.setLineNumbersGenerated(commandLine.hasOption('g')); + if (commandLine.hasOption('e')) { + builder.setMainFunctionName(commandLine.getOptionValue('e')); + } String[] args = commandLine.getArgs(); if (args.length != 1) { diff --git a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java index 3d160777f..027f599cc 100644 --- a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java +++ b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java @@ -103,6 +103,7 @@ public class TeaVMTool { private Set generatedFiles = new HashSet<>(); private int minHeapSize = 32 * (1 << 20); private ReferenceCache referenceCache; + private String mainFunctionName; public File getTargetDirectory() { return targetDirectory; @@ -248,6 +249,10 @@ public class TeaVMTool { this.wasmVersion = wasmVersion; } + public void setMainFunctionName(String mainFunctionName) { + this.mainFunctionName = mainFunctionName; + } + public void setProgressListener(TeaVMProgressListener progressListener) { this.progressListener = progressListener; } @@ -389,7 +394,7 @@ public class TeaVMTool { vm.add(transformer); } if (mainClass != null) { - vm.entryPoint(mainClass, entryPointName); + vm.entryPoint(mainClass, entryPointName != null ? entryPointName : "main"); } for (String className : classesToPreserve) { vm.preserveType(className);