Skip to content

Commit

Permalink
Merge pull request #42336 from gabilang/import-rt-mgt-mod
Browse files Browse the repository at this point in the history
Add 'remoteManagement' compiler option through build-options of Ballerina.toml
  • Loading branch information
hasithaa authored Apr 5, 2024
2 parents 7782a44 + 5c32e37 commit 3b1191c
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public BuildCommand() {
@CommandLine.Option(names = "--cloud", description = "Enable cloud artifact generation")
private String cloud;

@CommandLine.Option(names = "--remote-management", description = "enable service management tools in " +
"the executable JAR file(s).")
private Boolean remoteManagement;

@CommandLine.Option(names = "--list-conflicted-classes",
description = "list conflicted classes when generating executable")
private Boolean listConflictedClasses;
Expand Down Expand Up @@ -302,6 +306,7 @@ private BuildOptions constructBuildOptions() {
.setOffline(offline)
.setObservabilityIncluded(observabilityIncluded)
.setCloud(cloud)
.setRemoteManagement(remoteManagement)
.setDumpBir(dumpBIR)
.setDumpBirFile(dumpBIRFile)
.setDumpGraph(dumpGraph)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public class RunCommand implements BLauncherCmd {
"when run is used with a source file or a module.")
private Boolean observabilityIncluded;

@CommandLine.Option(names = "--remote-management", description = "enable management service in the " +
"executable when run is used with a source file or a module.")
private Boolean remoteManagement;

@CommandLine.Option(names = "--sticky", description = "stick to exact versions locked (if exists)")
private Boolean sticky;

Expand Down Expand Up @@ -282,6 +286,7 @@ private BuildOptions constructBuildOptions() {
.setSkipTests(true)
.setTestReport(false)
.setObservabilityIncluded(observabilityIncluded)
.setRemoteManagement(remoteManagement)
.setSticky(sticky)
.setDumpGraph(dumpGraph)
.setDumpRawGraphs(dumpRawGraphs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ OPTIONS
--graalvm-build-options
Additional build options to be passed to the GraalVM native image.

--remote-management
Include the dependencies that are required to enable remote package
management service.


EXAMPLES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public String cloud() {
return this.compilationOptions.getCloud();
}

public boolean remoteManagement() {
return this.compilationOptions.remoteManagement();
}

CompilationOptions compilationOptions() {
return this.compilationOptions;
}
Expand Down Expand Up @@ -195,6 +199,7 @@ public BuildOptions acceptTheirs(BuildOptions theirOptions) {
buildOptionsBuilder.setExportOpenAPI(compilationOptions.exportOpenAPI);
buildOptionsBuilder.setExportComponentModel(compilationOptions.exportComponentModel);
buildOptionsBuilder.setEnableCache(compilationOptions.enableCache);
buildOptionsBuilder.setRemoteManagement(compilationOptions.remoteManagement);

return buildOptionsBuilder.build();
}
Expand Down Expand Up @@ -387,6 +392,11 @@ public BuildOptionsBuilder setNativeImage(Boolean value) {
return this;
}

public BuildOptionsBuilder setRemoteManagement(Boolean value) {
compilationOptionsBuilder.setRemoteManagement(value);
return this;
}

public BuildOptions build() {
CompilationOptions compilationOptions = compilationOptionsBuilder.build();
return new BuildOptions(testReport, codeCoverage, dumpBuildTime, skipTests, compilationOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public class CompilationOptions {
Boolean exportComponentModel;
Boolean enableCache;
Boolean disableSyntaxTree;
Boolean remoteManagement;

CompilationOptions(Boolean offlineBuild, Boolean observabilityIncluded, Boolean dumpBir,
Boolean dumpBirFile, String cloud, Boolean listConflictedClasses, Boolean sticky,
Boolean dumpGraph, Boolean dumpRawGraphs, Boolean withCodeGenerators,
Boolean withCodeModifiers, Boolean configSchemaGen, Boolean exportOpenAPI,
Boolean exportComponentModel, Boolean enableCache, Boolean disableSyntaxTree) {
Boolean exportComponentModel, Boolean enableCache, Boolean disableSyntaxTree,
Boolean remoteManagement) {
this.offlineBuild = offlineBuild;
this.observabilityIncluded = observabilityIncluded;
this.dumpBir = dumpBir;
Expand All @@ -61,6 +63,7 @@ public class CompilationOptions {
this.exportComponentModel = exportComponentModel;
this.enableCache = enableCache;
this.disableSyntaxTree = disableSyntaxTree;
this.remoteManagement = remoteManagement;
}

public boolean offlineBuild() {
Expand Down Expand Up @@ -123,6 +126,10 @@ public boolean enableCache() {
return toBooleanDefaultIfNull(this.enableCache);
}

boolean remoteManagement() {
return toBooleanDefaultIfNull(this.remoteManagement);
}

/**
* Merge the given compilation options by favoring theirs if there are conflicts.
*
Expand Down Expand Up @@ -206,6 +213,11 @@ CompilationOptions acceptTheirs(CompilationOptions theirOptions) {
} else {
compilationOptionsBuilder.setEnableCache(this.enableCache);
}
if (theirOptions.remoteManagement != null) {
compilationOptionsBuilder.setRemoteManagement(theirOptions.remoteManagement);
} else {
compilationOptionsBuilder.setRemoteManagement(this.remoteManagement);
}
return compilationOptionsBuilder.build();
}

Expand Down Expand Up @@ -260,6 +272,7 @@ public static class CompilationOptionsBuilder {
private Boolean exportComponentModel;
private Boolean enableCache;
private Boolean disableSyntaxTree;
private Boolean remoteManagement;

public CompilationOptionsBuilder setOffline(Boolean value) {
offline = value;
Expand Down Expand Up @@ -341,11 +354,16 @@ public CompilationOptionsBuilder setEnableCache(Boolean value) {
return this;
}

public CompilationOptionsBuilder setRemoteManagement(Boolean value) {
remoteManagement = value;
return this;
}

public CompilationOptions build() {
return new CompilationOptions(offline, observabilityIncluded, dumpBir,
dumpBirFile, cloud, listConflictedClasses, sticky, dumpGraph, dumpRawGraph,
withCodeGenerators, withCodeModifiers, configSchemaGen, exportOpenAPI,
exportComponentModel, enableCache, disableSyntaxTree);
exportComponentModel, enableCache, disableSyntaxTree, remoteManagement);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ public void performCodeGen(ModuleContext moduleContext, CompilationCache compila
if (bLangPackage.getErrorCount() > 0) {
return;
}
CompiledJarFile compiledJarFile = jvmCodeGenerator.generate(bLangPackage);
boolean isRemoteMgtEnabled = moduleContext.project().buildOptions().compilationOptions().remoteManagement();
CompiledJarFile compiledJarFile = jvmCodeGenerator.generate(bLangPackage, isRemoteMgtEnabled);
if (compiledJarFile == null) {
throw new IllegalStateException("Missing generated jar, module: " + moduleContext.moduleName());
}
Expand All @@ -355,7 +356,8 @@ public void performCodeGen(ModuleContext moduleContext, CompilationCache compila
}

String testJarFileName = jarFileName + TEST_JAR_FILE_NAME_SUFFIX;
CompiledJarFile compiledTestJarFile = jvmCodeGenerator.generateTestModule(bLangPackage.testablePkgs.get(0));
CompiledJarFile compiledTestJarFile = jvmCodeGenerator.generateTestModule(bLangPackage.testablePkgs.get(0),
isRemoteMgtEnabled);
try {
ByteArrayOutputStream byteStream = JarWriter.write(compiledTestJarFile, getAllResources(moduleContext));
compilationCache.cachePlatformSpecificLibrary(this, testJarFileName, byteStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static org.ballerinalang.compiler.CompilerOptionName.CLOUD;
import static org.ballerinalang.compiler.CompilerOptionName.DUMP_BIR;
import static org.ballerinalang.compiler.CompilerOptionName.DUMP_BIR_FILE;
import static org.ballerinalang.compiler.CompilerOptionName.REMOTE_MANAGEMENT;
import static org.ballerinalang.compiler.CompilerOptionName.OBSERVABILITY_INCLUDED;
import static org.ballerinalang.compiler.CompilerOptionName.OFFLINE;

Expand Down Expand Up @@ -87,6 +88,7 @@ private void setCompilerOptions(CompilationOptions compilationOptions) {
options.put(DUMP_BIR, Boolean.toString(compilationOptions.dumpBir()));
options.put(DUMP_BIR_FILE, Boolean.toString(compilationOptions.dumpBirFile()));
options.put(CLOUD, compilationOptions.getCloud());
options.put(REMOTE_MANAGEMENT, Boolean.toString(compilationOptions.remoteManagement()));
}

static PackageCompilation from(PackageContext rootPackageContext, CompilationOptions compilationOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ PackageCompilation getPackageCompilation(CompilationOptions compilationOptions)
.setListConflictedClasses(this.compilationOptions.listConflictedClasses())
.setConfigSchemaGen(this.compilationOptions.configSchemaGen())
.setEnableCache(this.compilationOptions.enableCache())
.setRemoteManagement(this.compilationOptions.remoteManagement())
.build();
CompilationOptions mergedOptions = options.acceptTheirs(compilationOptions);
return PackageCompilation.from(this, mergedOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ private LinkedHashSet<ModuleLoadRequest> getModuleLoadRequestsOfDirectDependenci
PackageDependencyScope.DEFAULT, DependencyResolutionType.COMPILER_PLUGIN);
allModuleLoadRequests.add(c2cModuleLoadReq);
}

return allModuleLoadRequests;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ private BuildOptions setBuildOptions(TomlTableNode tomlTableNode) {
BuildOptions.OptionName.EXPORT_COMPONENT_MODEL.toString());
String graalVMBuildOptions = getStringFromBuildOptionsTableNode(tableNode,
BuildOptions.OptionName.GRAAL_VM_BUILD_OPTIONS.toString());

Boolean remoteManagement = getBooleanFromBuildOptionsTableNode(tableNode,
CompilerOptionName.REMOTE_MANAGEMENT.toString());

buildOptionsBuilder
.setOffline(offline)
Expand All @@ -705,7 +706,8 @@ private BuildOptions setBuildOptions(TomlTableNode tomlTableNode) {
.setEnableCache(enableCache)
.setNativeImage(nativeImage)
.setExportComponentModel(exportComponentModel)
.setGraalVMBuildOptions(graalVMBuildOptions);
.setGraalVMBuildOptions(graalVMBuildOptions)
.setRemoteManagement(remoteManagement);

if (targetDir != null) {
buildOptionsBuilder.targetDir(targetDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public enum CompilerOptionName {
STICKY("sticky"),

ENABLE_CACHE("enableCache"),
REMOTE_MANAGEMENT("remoteManagement"),

/**
* We've introduced this temporary option to support old-project structure and the new package structure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ public static CodeGenerator getInstance(CompilerContext context) {
return codeGenerator;
}

public CompiledJarFile generate(BLangPackage bLangPackage) {
public CompiledJarFile generate(BLangPackage bLangPackage, boolean isRemoteMgtEnabled) {
// generate module
return generate(bLangPackage.symbol);
return generate(bLangPackage.symbol, isRemoteMgtEnabled);
}

public CompiledJarFile generateTestModule(BLangPackage bLangTestablePackage) {
return generate(bLangTestablePackage.symbol);
public CompiledJarFile generateTestModule(BLangPackage bLangTestablePackage, boolean isRemoteMgtEnabled) {
return generate(bLangTestablePackage.symbol, isRemoteMgtEnabled);
}

private CompiledJarFile generate(BPackageSymbol packageSymbol) {
private CompiledJarFile generate(BPackageSymbol packageSymbol, boolean isRemoteMgtEnabled) {
// Desugar BIR to include the observations
JvmObservabilityGen jvmObservabilityGen = new JvmObservabilityGen(packageCache, symbolTable);
jvmObservabilityGen.instrumentPackage(packageSymbol.bir);
Expand All @@ -77,7 +77,8 @@ private CompiledJarFile generate(BPackageSymbol packageSymbol) {
BIRGenUtils.rearrangeBasicBlocks(packageSymbol.bir);

dlog.setCurrentPackageId(packageSymbol.pkgID);
final JvmPackageGen jvmPackageGen = new JvmPackageGen(symbolTable, packageCache, dlog, types);
final JvmPackageGen jvmPackageGen = new JvmPackageGen(symbolTable, packageCache, dlog, types,
isRemoteMgtEnabled);

//Rewrite identifier names with encoding special characters
HashMap<String, String> originalIdentifierMap = JvmDesugarPhase.encodeModuleIdentifiers(packageSymbol.bir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ public class JvmPackageGen {
private final Set<PackageID> dependentModules;
private final BLangDiagnosticLog dlog;
private final Types types;
private final boolean isRemoteMgtEnabled;

JvmPackageGen(SymbolTable symbolTable, PackageCache packageCache, BLangDiagnosticLog dlog, Types types) {
JvmPackageGen(SymbolTable symbolTable, PackageCache packageCache, BLangDiagnosticLog dlog, Types types,
boolean isRemoteMgtEnabled) {
birFunctionMap = new HashMap<>();
globalVarClassMap = new HashMap<>();
dependentModules = new LinkedHashSet<>();
this.symbolTable = symbolTable;
this.packageCache = packageCache;
this.dlog = dlog;
this.types = types;
this.isRemoteMgtEnabled = isRemoteMgtEnabled;
methodGen = new MethodGen(this, types);
initMethodGen = new InitMethodGen(symbolTable);
configMethodGen = new ConfigMethodGen();
Expand Down Expand Up @@ -757,7 +760,8 @@ CompiledJarFile generate(BIRPackage module, boolean isEntry) {
rewriteRecordInits(module.typeDefs);

// generate object/record value classes
JvmValueGen valueGen = new JvmValueGen(module, this, methodGen, typeHashVisitor, types);
JvmValueGen valueGen = new JvmValueGen(module, this, methodGen, typeHashVisitor, types,
isRemoteMgtEnabled);
JvmCastGen jvmCastGen = new JvmCastGen(symbolTable, jvmTypeGen, types);
valueGen.generateValueClasses(jarEntries, jvmConstantsGen, jvmTypeGen);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ public class JvmValueGen {
private final Types types;

JvmValueGen(BIRNode.BIRPackage module, JvmPackageGen jvmPackageGen, MethodGen methodGen,
TypeHashVisitor typeHashVisitor, Types types) {
TypeHashVisitor typeHashVisitor, Types types, boolean isRemoteMgtEnabled) {
this.module = module;
this.jvmPackageGen = jvmPackageGen;
this.methodGen = methodGen;
this.booleanType = jvmPackageGen.symbolTable.booleanType;
this.jvmRecordGen = new JvmRecordGen(jvmPackageGen.symbolTable);
this.jvmObjectGen = new JvmObjectGen();
this.jvmObjectGen = new JvmObjectGen(isRemoteMgtEnabled);
this.typeHashVisitor = typeHashVisitor;
this.types = types;
}
Expand Down Expand Up @@ -205,8 +205,8 @@ void generateValueClasses(Map<String, byte[]> jarEntries, JvmConstantsGen jvmCon
if (optionalTypeDef.type.tag == TypeTags.OBJECT &&
Symbols.isFlagOn(optionalTypeDef.type.tsymbol.flags, Flags.CLASS)) {
BObjectType objectType = (BObjectType) optionalTypeDef.type;
this.createObjectValueClasses(objectType, className, optionalTypeDef, jvmConstantsGen
, asyncDataCollector, jarEntries);
this.createObjectValueClasses(objectType, className, optionalTypeDef, jvmConstantsGen,
asyncDataCollector, jarEntries);
} else if (bType.tag == TypeTags.RECORD) {
BRecordType recordType = (BRecordType) bType;
byte[] bytes = this.createRecordValueClass(recordType, className, optionalTypeDef, jvmConstantsGen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
*/
public class JvmObjectGen {

private final boolean isRemoteMgtEnabled;

public JvmObjectGen(boolean isRemoteMgtEnabled) {
this.isRemoteMgtEnabled = isRemoteMgtEnabled;
}

static final FieldNameHashComparator FIELD_NAME_HASH_COMPARATOR = new FieldNameHashComparator();

public void createAndSplitCallMethod(ClassWriter cw, List<BIRNode.BIRFunction> functions, String objClassName,
Expand Down Expand Up @@ -196,8 +202,9 @@ public void createAndSplitCallMethod(ClassWriter cw, List<BIRNode.BIRFunction> f
}
}

private static boolean isListenerAttach(BIRNode.BIRFunction func) {
return func.name.value.equals("attach") && Symbols.isFlagOn(func.parameters.get(0).type.flags, Flags.SERVICE);
private boolean isListenerAttach(BIRNode.BIRFunction func) {
return isRemoteMgtEnabled && func.name.value.equals("attach")
&& Symbols.isFlagOn(func.parameters.get(0).type.flags, Flags.SERVICE);
}

public void createAndSplitGetMethod(ClassWriter cw, Map<String, BField> fields, String className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ private static BuildOptions constructDefaultBuildOptions() {
.setSticky(false)
.setDumpGraph(false)
.setDumpRawGraphs(false)
.setConfigSchemaGen(false);
.setConfigSchemaGen(false)
.setRemoteManagement(false);

return buildOptionsBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ public void testLoadingBuildOptionsFromToml() {
Assert.assertFalse(project.buildOptions().codeCoverage());
Assert.assertFalse(project.buildOptions().offlineBuild());
Assert.assertFalse(project.buildOptions().testReport());
Assert.assertTrue(project.buildOptions().remoteManagement());
}

@Test(description = "tests loading a valid build project with build options from toml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ version = "0.1.0"
b7aConfigFile="/tmp/ballerina.conf"
observabilityIncluded = true
skipTests=true
remoteManagement=true

0 comments on commit 3b1191c

Please sign in to comment.