Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7385,6 +7385,28 @@ public void usesClassMethod(String cls, String method) {
throw new BuildException("Failed to update infoplist file", ex);
}

// ios.optimizationLevel: the clang level of the optimized configurations.
// See XcodeOptimizationLevel; the cloud builder does the same.
String optimizationLevel = request.getArg("ios.optimizationLevel", null);
if (optimizationLevel != null && optimizationLevel.trim().length() > 0) {
optimizationLevel = optimizationLevel.trim();
if (!XcodeOptimizationLevel.isValid(optimizationLevel)) {
throw new BuildException("ios.optimizationLevel must be one of 0, 1, 2, 3, s, z or fast, not "
+ optimizationLevel);
}
try {
java.nio.file.Path pbxPath = new File(tmpFile, "dist/" + request.getMainClass()
+ ".xcodeproj/project.pbxproj").toPath();
String before = new String(java.nio.file.Files.readAllBytes(pbxPath), "UTF-8");
String after = XcodeOptimizationLevel.apply(before, optimizationLevel);
if (!after.equals(before)) {
java.nio.file.Files.write(pbxPath, after.getBytes("UTF-8"));
}
} catch (IOException ex) {
throw new BuildException("Failed to apply ios.optimizationLevel", ex);
}
}

try {
normalizeAssetCatalogs(request);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.builders;

/// The `ios.optimizationLevel` build hint: the clang optimization level of the
/// generated project's optimized configurations.
///
/// Every `GCC_OPTIMIZATION_LEVEL` the project sets to something other than `0`
/// is replaced, so Release (and any other optimized configuration) changes
/// while Debug stays unoptimized. `z` is the one that matters in practice: the
/// device runtime, whose thousands of generated interpreter shims pushed its
/// executable past the App Store's 500 MB limit, builds with it. It is slower
/// code, which an app that spends its time elsewhere never notices.
///
/// Editing the project, rather than asking the translator, means it works with
/// whichever ParparVM produced the project.
final class XcodeOptimizationLevel {
private static final String KEY = "GCC_OPTIMIZATION_LEVEL = ";

private XcodeOptimizationLevel() {
}

/// The levels clang accepts as `-O<level>`.
static boolean isValid(String level) {
return "0".equals(level) || "1".equals(level) || "2".equals(level) || "3".equals(level)
|| "s".equals(level) || "z".equals(level) || "fast".equals(level);
}

/// Returns the project text with every non-zero `GCC_OPTIMIZATION_LEVEL`
/// set to `level`.
static String apply(String pbx, String level) {
StringBuilder out = new StringBuilder(pbx.length());
int from = 0;
while (true) {
int at = pbx.indexOf(KEY, from);
if (at < 0) {
out.append(pbx, from, pbx.length());
return out.toString();
}
int valueStart = at + KEY.length();
int valueEnd = pbx.indexOf(';', valueStart);
if (valueEnd < 0) {
out.append(pbx, from, pbx.length());
return out.toString();
}
String value = pbx.substring(valueStart, valueEnd).trim();
out.append(pbx, from, valueStart);
out.append("0".equals(value) ? value : level);
from = valueEnd;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.builders;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class XcodeOptimizationLevelTest {
private static final String PROJECT = "\t\t\t\tGCC_OPTIMIZATION_LEVEL = 0;\n"
+ "\t\t\t\tGCC_PREPROCESSOR_DEFINITIONS = \"DEBUG=1\";\n"
+ "\t\t\t\tGCC_OPTIMIZATION_LEVEL = 3;\n"
+ "\t\t\t\tGCC_OPTIMIZATION_LEVEL = s;\n";

@Test
void optimizedConfigurationsTakeTheLevelAndDebugStaysUnoptimized() {
assertEquals("\t\t\t\tGCC_OPTIMIZATION_LEVEL = 0;\n"
+ "\t\t\t\tGCC_PREPROCESSOR_DEFINITIONS = \"DEBUG=1\";\n"
+ "\t\t\t\tGCC_OPTIMIZATION_LEVEL = z;\n"
+ "\t\t\t\tGCC_OPTIMIZATION_LEVEL = z;\n",
XcodeOptimizationLevel.apply(PROJECT, "z"));
}

@Test
void applyingTwiceChangesNothing() {
String once = XcodeOptimizationLevel.apply(PROJECT, "z");
assertEquals(once, XcodeOptimizationLevel.apply(once, "z"));
}

@Test
void aProjectWithoutTheSettingIsUnchanged() {
String pbx = "\t\t\t\tSDKROOT = iphoneos;\n";
assertEquals(pbx, XcodeOptimizationLevel.apply(pbx, "z"));
}

@Test
void onlyClangLevelsAreValid() {
assertTrue(XcodeOptimizationLevel.isValid("z"));
assertTrue(XcodeOptimizationLevel.isValid("s"));
assertTrue(XcodeOptimizationLevel.isValid("3"));
assertTrue(XcodeOptimizationLevel.isValid("fast"));
assertFalse(XcodeOptimizationLevel.isValid("Oz"));
assertFalse(XcodeOptimizationLevel.isValid("4"));
assertFalse(XcodeOptimizationLevel.isValid(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,15 @@ static void register(List<Hint> h) {
+ "on-device interpreter app (scripts/cn1-device-runtime). A regular app "
+ "leaves this false and gets a smaller binary."));

h.add(new Hint("ios.optimizationLevel")
.group(HintGroup.IOS)
.type(HintType.STRING)
.platform("ios")
.doc("The clang optimization level of the release build: 0, 1, 2, 3, s, z or fast. "
+ "Debug builds stay unoptimized. Set z for an app whose executable is too "
+ "large, such as the device runtime: the code is smaller and somewhat "
+ "slower."));

h.add(new Hint("ios.featureScanExclude")
.group(HintGroup.IOS)
.type(HintType.STRING)
Expand Down
8 changes: 8 additions & 0 deletions maven/javase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,17 @@
time from the iPhoneX.skin theme. -->
<echo>Generating foldable skins</echo>
<mkdir dir="${project.build.directory}/foldable-skins-classes"/>
<!-- Java 8 class files whatever JDK compiles them: this
module is built on JDK 8 and on JDK 17 (the guide
snippets), and ant skips an up-to-date class, so a
JDK 17 compile followed by a JDK 8 build ran the
generator from version-61 class files and failed
with UnsupportedClassVersionError. -->
<javac srcdir="${project.basedir}/../../tools/foldable-skins"
destdir="${project.build.directory}/foldable-skins-classes"
includeantruntime="false"
source="1.8"
target="1.8"
fork="true"
nowarn="true"/>
<java classname="GenerateFoldableSkins" fork="true" failonerror="true">
Expand Down
8 changes: 8 additions & 0 deletions scripts/cn1-device-runtime/INTERNALS.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ A generated shim overrides the framework method and asks the interpreter for it.

That's what the `super_` bridges on every shim are for: `super.paint(g)` from interpreted code is dispatched to `super_paint`, which is the only path to the framework implementation. The same applies to any `super.` call on a host superclass.

=== Shim methods are a call, not a template

Every shim overrides every method its class inherits, so the shims are most of the app: about 1,650 classes and 104,000 overrides. When each override carried its own argument array, its own `try`/`catch` and its own exception unwrapping, they averaged 2.3 KB of machine code each. Together they were two thirds of the executable and pushed it past the App Store's 500 MB limit. Each new framework method also grew every shim that inherits it.

An override now calls a helper in the generated `InterpCalls` class, one helper per parameter shape (about 260 shapes cover the framework). It tests the result with `InterpCalls.miss` and calls `super` itself, because Java allows a `super` call only inside the subclass. Only a method that declares a checked exception keeps a handler, so that the exception reaches its caller by its own type. Keep new shim behavior in the helpers, not in the emitted method body.

The device runtime also sets the `ios.optimizationLevel=z` build hint, so the whole iOS release build is compiled for size. The code is somewhat slower, which a development tool can afford.

=== Pushing an application, not a file

`cn1:push` sends the module's whole compiled tree with all of its source roots,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# builds; nothing here calls Play billing or FCM at runtime.
codename1.arg.android.useAndroidX=true
codename1.arg.ios.interpHost=true
# Compiled for size: the interpreter shims are two thirds of the executable, and
# at the default speed optimization it passed the App Store's 500 MB limit.
codename1.arg.ios.optimizationLevel=z
# A pushed program gets the native theme its own ios.themeMode /
# ios.themeGeneration ask for, so every iOS native theme has to be in the
# bundle; the builder otherwise ships only the one this app's mode loads.
Expand Down
Loading
Loading