Navigation Menu

Skip to content

Commit

Permalink
* m1: compiler adaptation -- Environment parameter
Browse files Browse the repository at this point in the history
This commit adds `Environment (Native/Simulator)` option that allows makes targets different when building for Arch.
For example build for `arm64` now could be targeted to `ios-arm64`, `macosx-arm64`, `ios-arm64-simulator`.
LLVM detects proper target from triple and it is being generated now with respect to `Environment` option.
Also linker `-miphoneos-version-min=` parameter was replaced with `--target=` one that accepts the tripple.

Changes were done to folder names: environment is being attached to every arch name where applicable. For example:
```
vm/lib/arm64/
vm/lib/arm64-simulator
vm/lib/thumbv7
vm/lib/x86-simulator
vm/lib/x86_64-simulator
```
  • Loading branch information
dkimitsa committed May 26, 2021
1 parent ef2db15 commit f933271
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 97 deletions.
Expand Up @@ -24,12 +24,8 @@
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.robovm.compiler.clazz.*;
import org.robovm.compiler.config.Arch;
import org.robovm.compiler.config.Config;
import org.robovm.compiler.config.*;
import org.robovm.compiler.config.Config.TreeShakerMode;
import org.robovm.compiler.config.ForceLinkMethodsConfig;
import org.robovm.compiler.config.OS;
import org.robovm.compiler.config.Resource;
import org.robovm.compiler.config.StripArchivesConfig.StripArchivesBuilder;
import org.robovm.compiler.log.ConsoleLogger;
import org.robovm.compiler.plugin.LaunchPlugin;
Expand Down Expand Up @@ -753,14 +749,15 @@ public static void main(String[] args) throws IOException {
if (!"auto".equals(s)) {
archs.add(Arch.valueOf(s));
}
} else if ("-env".equals(args[i])) {
String s = args[++i];
builder.env(Environment.valueOf(s));
} else if ("-archs".equals(args[i])) {
for (String s : args[++i].split(":")) {
if (!"auto".equals(s)) {
archs.add(Arch.valueOf(s));
}
}
// } else if ("-cpu".equals(args[i])) {
// builder.cpu(args[++i]);
} else if ("-target".equals(args[i])) {
String s = args[++i];
builder.targetType("auto".equals(s) ? null : s);
Expand Down Expand Up @@ -1107,9 +1104,9 @@ private static void printUsageAndExit(String errorMessage, List<Plugin> plugins)
System.err.println(" -archs <list> : separated list of archs. Used to build a fat binary which\n"
+ " includes all the specified archs. Allowed values\n"
+ " are 'x86', 'x86_64', 'thumbv7', 'arm64'.");
System.err.println(" -cpu <name> The name of the LLVM cpu to compile for. The LLVM default\n"
+ " is used if not specified. Use llc to determine allowed values.");
System.err.println(" -target <name> The target to build for. One of:\n"
System.err.println(" -env <name> The name platform environment. Allowed values\n"
+ " are 'Native', 'Simulator'. Default is 'Native'");
System.err.println(" -target <name> The target to build for. One of:\n"
+ " 'auto', '" + StringUtils.join(targets, "', '") + "'\n"
+ " The default is 'auto' which means use -os to decide.");
System.err.println(" -forcelinkclasses <list>\n"
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.robovm.compiler.clazz.MethodInfo;
import org.robovm.compiler.config.Arch;
import org.robovm.compiler.config.Config;
import org.robovm.compiler.config.Environment;
import org.robovm.compiler.config.OS;
import org.robovm.compiler.llvm.Alias;
import org.robovm.compiler.llvm.AliasRef;
Expand Down Expand Up @@ -294,9 +295,11 @@ public void compile(Clazz clazz, Executor executor, ClassCompilerListener listen

Arch arch = config.getArch();
OS os = config.getOs();
Environment env = config.getEnv();

try {
config.getLogger().info("Compiling %s (%s %s %s)", clazz, os, arch, config.isDebug() ? "debug" : "release");
config.getLogger().info("Compiling %s (%s %s%s %s)", clazz, os, arch, env.asLlvmSuffix("-"),
config.isDebug() ? "debug" : "release");
output.reset();
compile(clazz, output);
} catch (Throwable t) {
Expand Down
Expand Up @@ -87,6 +87,10 @@ public static Arch getDefaultArch() {
if (hostTriple.matches("^(x86|i\\d86).*")) {
return Arch.x86;
}
if (hostTriple.matches("^(arm-apple-darwin).*")) {
// MacOSX m1 CPU
return Arch.arm64;
}
throw new CompilerException("Unrecognized arch in host triple: " + hostTriple);
}
}
Expand Up @@ -237,6 +237,7 @@ public enum TreeShakerMode {
private transient Config configBeforeBuild;
private transient DependencyGraph dependencyGraph;
private transient Arch sliceArch;
private transient Environment env = Environment.Native;
private transient StripArchivesBuilder stripArchivesBuilder;

protected Config(UUID uuid) {
Expand Down Expand Up @@ -305,6 +306,10 @@ public File getCcBinPath() {
public OS getOs() {
return os;
}

public Environment getEnv() {
return env;
}

public Arch getArch() {
return sliceArch;
Expand All @@ -314,13 +319,23 @@ public List<Arch> getArchs() {
return archs == null ? Collections.emptyList()
: Collections.unmodifiableList(archs);
}

public String getTriple() {
return sliceArch.getLlvmName() + "-unknown-" + os.getLlvmName();
return getTriple(os.getMinVersion());
}

public String getTriple(String minVersion) {
return sliceArch.getLlvmName() + "-" + os.getVendor() + "-" + os.getLlvmName() + minVersion +
env.asLlvmSuffix("-");
}

public String getClangTriple() {
return sliceArch.getClangName() + "-unknown-" + os.getLlvmName();
return getClangTriple(os.getMinVersion());
}

public String getClangTriple(String minVersion) {
return sliceArch.getClangName() + "-" + os.getVendor() + "-" + os.getLlvmName() + minVersion +
env.asLlvmSuffix("-");
}

public DataLayout getDataLayout() {
Expand Down Expand Up @@ -995,7 +1010,7 @@ private Config build() throws IOException {
dataLayout = new DataLayout(getTriple());

osArchDepLibDir = new File(new File(home.libVmDir, os.toString()),
sliceArch.toString());
sliceArch.toString() + env.asLlvmSuffix("-"));

if (treeShakerMode != null && treeShakerMode != TreeShakerMode.none
&& os.getFamily() == Family.darwin && sliceArch == Arch.x86) {
Expand All @@ -1014,7 +1029,9 @@ private Config build() throws IOException {
this.tmpDir = ramDiskTools.getTmpDir();

File osDir = new File(cacheDir, os.toString());
File archDir = new File(osDir, sliceArch.toString());
String archName = (env != null && !env.getLlvmName().isEmpty()) ? sliceArch.toString() + "-" + env.getLlvmName() :
sliceArch.toString();
File archDir = new File(osDir, archName);
osArchCacheDir = new File(archDir, debug ? "debug" : "release");
osArchCacheDir.mkdirs();

Expand Down Expand Up @@ -1232,6 +1249,11 @@ public Builder os(OS os) {
return this;
}

public Builder env(Environment env) {
config.env = env;
return this;
}

public Builder arch(Arch arch) {
return archs(arch);
}
Expand Down
@@ -0,0 +1,24 @@
package org.robovm.compiler.config;

/**
* Specifies environment kind build/deployment is targeted
* @author dkimitsa
*/
public enum Environment {
Native(""),
Simulator("simulator");

private final String llvmName;

Environment(String llvmName) {
this.llvmName = llvmName;
}

public String getLlvmName() {
return llvmName;
}

public String asLlvmSuffix(String prefix) {
return (llvmName != null && !llvmName.isEmpty()) ? (prefix + llvmName) : "";
}
}
14 changes: 11 additions & 3 deletions compiler/compiler/src/main/java/org/robovm/compiler/config/OS.java
Expand Up @@ -23,22 +23,30 @@
*
*/
public enum OS {
linux("linux", "linux"), macosx("macosx10.9.0", "10.9"), ios("ios8.0.0", "8.0");
linux("linux", "unknown","linux"),
macosx("macosx", "apple", "10.9"),
ios("ios", "apple", "8.0");

public enum Family {linux, darwin}

private final String llvmName;
private final String vendor;
private final String minVersion;

private OS(String llvmName, String minVersion) {
private OS(String llvmName, String vendor, String minVersion) {
this.llvmName = llvmName;
this.vendor = vendor;
this.minVersion = minVersion;
}

public String getLlvmName() {
return llvmName;
}


public String getVendor() {
return vendor;
}

public String getMinVersion() {
return minVersion;
}
Expand Down
Expand Up @@ -121,7 +121,7 @@ public void beforeLaunch(Config config, LaunchParameters parameters) {
File appDir = new File(config.isSkipInstall() ? config.getTmpDir() : config.getInstallDir(), config.getExecutableName() + ".app");
builder.setAppfile(new File(appDir, config.getExecutableName()));

if (IOSTarget.isSimulatorArch(target.getArch())) {
if (IOSTarget.isSimulatorArch(config.getArch(), config.getEnv())) {
// launching on simulator, it can write down port number to file on local system
File hooksPortFile;
try {
Expand Down
Expand Up @@ -104,7 +104,7 @@ protected void doBuild(File outFile, List<String> ccArgs,
throws IOException {

if (config.getOs() == OS.macosx) {
ccArgs.add("-mmacosx-version-min=" + config.getOs().getMinVersion());
ccArgs.add("--target=" + config.getClangTriple());
if (config.getArch() == Arch.x86 || config.isDebug()) {
ccArgs.add("-Wl,-no_pie");
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.robovm.compiler.clazz.Path;
import org.robovm.compiler.config.Arch;
import org.robovm.compiler.config.Config;
import org.robovm.compiler.config.Environment;
import org.robovm.compiler.config.OS;

/**
Expand Down Expand Up @@ -56,6 +57,15 @@ public interface Target {
*/
Arch getArch();

/**
* @return the {@link Environment} this {@link Target} will build for. Used
* to differ target with same {@link Arch}.
* For ex. Arm64 iOS device and Arm64 m1 iOS simulator
*/
default Environment getEnv() {
return Environment.Native;
}

/**
* Returns a list of the default archs to build for if no archs have been
* specified in the {@link Config}. Returns an empty list if there are no
Expand Down
Expand Up @@ -7,9 +7,8 @@
import org.robovm.compiler.clazz.Path;
import org.robovm.compiler.config.Arch;
import org.robovm.compiler.config.Config;
import org.robovm.compiler.config.Environment;
import org.robovm.compiler.config.OS;
import org.robovm.compiler.log.Logger;
import org.robovm.compiler.log.LoggerProxy;
import org.robovm.compiler.target.AbstractTarget;
import org.robovm.compiler.target.ios.SDK;
import org.robovm.compiler.util.Executor;
Expand All @@ -21,12 +20,16 @@
import java.util.Collections;
import java.util.List;

import static org.robovm.compiler.target.ios.IOSTarget.isDeviceArch;
import static org.robovm.compiler.target.ios.IOSTarget.isSimulatorArch;

public class FrameworkTarget extends AbstractTarget {

public static final String TYPE = "framework";

private OS os;
private Arch arch;
private Environment env;
private SDK sdk;

public FrameworkTarget() {
Expand All @@ -47,22 +50,18 @@ public Arch getArch() {
return arch;
}

@Override
public Environment getEnv() {
return env;
}

@Override
public boolean canLaunch() {
return false;
}

public static boolean isSimulatorArch(Arch arch) {
return arch == Arch.x86 || arch == Arch.x86_64;
}

public static boolean isDeviceArch(Arch arch) {
return arch == Arch.thumbv7 || arch == Arch.arm64;
}


public List<SDK> getSDKs() {
if (isSimulatorArch(arch)) {
if (isSimulatorArch(arch, env)) {
return SDK.listSimulatorSDKs();
} else {
return SDK.listDeviceSDKs();
Expand All @@ -80,6 +79,8 @@ public void init(Config paramConfig) {
if (arch == null)
arch = Arch.getDefaultArch();

env = config.getEnv();

if (os.getFamily() != OS.Family.darwin)
throw new IllegalArgumentException("Frameworks can only be built for Darwin platforms");

Expand All @@ -92,7 +93,7 @@ public void init(Config paramConfig) {
List<SDK> sdks = getSDKs();
if (sdkVersion == null) {
if (sdks.isEmpty()) {
throw new IllegalArgumentException("No " + (isDeviceArch(arch) ? "device" : "simulator")
throw new IllegalArgumentException("No " + (isDeviceArch(arch, env) ? "device" : "simulator")
+ " SDKs installed");
}
Collections.sort(sdks);
Expand Down Expand Up @@ -133,17 +134,14 @@ protected List<String> getTargetCcArgs() {
List<String> ccArgs = new ArrayList<String>();

ccArgs.add("-stdlib=libc++");

if (isDeviceArch(arch)) {
ccArgs.add("-miphoneos-version-min=" + getMinimumOSVersion());
if (config.isEnableBitcode()) {
// tells clang to keep bitcode while linking
ccArgs.add("-fembed-bitcode");
}
} else {
ccArgs.add("-mios-simulator-version-min=" + getMinimumOSVersion());

ccArgs.add("--target=" + config.getClangTriple(getMinimumOSVersion()));

if (isDeviceArch(arch, env) && config.isEnableBitcode()) {
// tells clang to keep bitcode while linking
ccArgs.add("-fembed-bitcode");
}

ccArgs.add("-isysroot");
ccArgs.add(sdk.getRoot().getAbsolutePath());

Expand Down

0 comments on commit f933271

Please sign in to comment.