konsoletyper / teavm

Compiles Java bytecode to JavaScript, WebAssembly and C
https://teavm.org
Apache License 2.0
2.55k stars 260 forks source link

error while compiling #877

Closed radmanplays closed 5 months ago

radmanplays commented 5 months ago

when i try to set the optimization level in build.gradle I get this error :

./gradlew generateJavaScript
OpenJDK 64-Bit Server VM warning: Ignoring option --illegal-access=permit; support was removed in 17.0

FAILURE: Build failed with an exception.

* Where:
Build file '/workspaces/Eaglercraft-1.12-Project/build.gradle' line: 37

* What went wrong:
A problem occurred evaluating root project 'teavm'.
> Could not get unknown property 'OptimizationLevel' for root project 'teavm' of type org.gradle.api.Project.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 489ms

this is my build.gradle :

plugins {
    id "java"
    id "war"
    id "org.teavm" version "0.9.2"
}
repositories {
    mavenCentral()
}

// This is optional, but for real-world applications you need to interact with browser.
// This dependency provides useful wrappers.
dependencies {
    implementation teavm.libs.jsoApis
}

apply plugin: 'eclipse'

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir 'src/teavm/java'
        }
    }
}

teavm.js {
    addedToWebApp = true
    mainClass = "Main"
    outputDir = file("javascript");
    targetFileName = "classes.js"
    obfuscated = false;
    sourceMap = true;
    entryPointName = 'main';
    properties = null;
    optimization = OptimizationLevel.AGGRESSIVE;
    minHeapSize = 4;
    maxHeapSize = 128;
}
konsoletyper commented 5 months ago

This is a configuration problem in your Gradle script, it has nothing to do with TeaVM

xvik commented 5 months ago

@radmanplays configuration relies on enum, but gradle couldn't recognize it without full package. So you should use:

optimization = org.teavm.gradle.api.OptimizationLevel.AGGRESSIVE;

@konsoletyper you can map property values in plugin to simplify configuration, like this:

final ExtraPropertiesExtension extraProps = project.getExtensions().getExtraProperties();
Arrays.asList(OptimizationLevel.values()).forEach(type -> extraProps.set(type.name(), type));

Enum value configuration becomes:

optimization = AGGRESSIVE

(AGGRESSIVE here would be a known gradle property and so would be replaced by complete enum path)

Or longer variant could be used to just avoid the package:

Arrays.asList(OptimizationLevel.values()).forEach(type -> extraProps.set(type.getSimpleName()+"."+type.name(), type));

(for optimization = OptimizationLevel.AGGRESSIVE)

EDIT: the last variant (longer path) is incorrect, it should be enouth to just map the enum:

extraProps.set(OptimizationLevel.class.getSimpleName(), OptimizationLevel.class.getName());
konsoletyper commented 5 months ago

@xvik, nope, I won't do this trick. First, it pollutes global namespace. Second, it's Groovy-only. Third, users are expected to rarely configure this property manually. Optimization levels are carefully chosen for each particular platform. If a developer wants better debugging experience in their development machine, they can create separate run configuration for compiling debug JS by specifying -P command line. Another option is to create teavm-local.properties and exclude it from VCS, then specify all options for building locally. See more in documentation