Skip to content

Commit

Permalink
#16 basic function parsing support
Browse files Browse the repository at this point in the history
Add raw pointer type for (void *) C types
Some correction to jextract parser for typedefs
  • Loading branch information
DigitalSmile committed Aug 10, 2024
1 parent c00f722 commit e968f47
Show file tree
Hide file tree
Showing 18 changed files with 451 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,37 @@
import io.github.digitalsmile.annotation.structure.Enums;
import io.github.digitalsmile.annotation.structure.Struct;
import io.github.digitalsmile.annotation.structure.Structs;
import io.github.digitalsmile.gpio.libcurl.enums.CURLCode;
import io.github.digitalsmile.gpio.libcurl.enums.CURLOption;
import io.github.digitalsmile.gpio.libcurl.opaque.CurlInstance;
import io.github.digitalsmile.gpio.libcurl.enums.CURLcode;
import io.github.digitalsmile.gpio.libcurl.enums.CURLoption;
import io.github.digitalsmile.gpio.libcurl.opaque.CURL;

@NativeMemory(headers = "libcurl/curl/include/curl/curl.h")
@NativeMemoryOptions(systemIncludes = {
"/usr/lib/gcc/x86_64-linux-gnu/12/include/"
}, debugMode = true, processRootConstants = true)
//"/usr/lib/gcc/x86_64-linux-gnu/12/include/"
"/usr/lib/llvm-15/lib/clang/15.0.7/include/"
}, debugMode = true, processRootConstants = true, systemHeader = true)
@Structs({
@Struct(name = "CURL", javaName = "CurlInstance")
// @Struct(name = "CURL", javaName = "CurlInstance")
})
@Enums({
@Enum(name = "CURLcode", javaName = "CURLCode"),
@Enum(name = "CURLoption", javaName = "CURLOption")
// @Enum(name = "CURLcode", javaName = "CURLCode"),
// @Enum(name = "CURLoption", javaName = "CURLOption")
})
@NativeMemoryLibrary
public interface Libcurl {

@NativeManualFunction(name = "curl_easy_init", library = "/usr/lib/x86_64-linux-gnu/libcurl.so")
CurlInstance easyInit() throws NativeMemoryException;
CURL easyInit() throws NativeMemoryException;

@NativeManualFunction(name = "curl_global_init", library = "/usr/lib/x86_64-linux-gnu/libcurl.so")
CURLCode globalInit(long flags) throws NativeMemoryException;
CURLcode globalInit(long flags) throws NativeMemoryException;

@NativeManualFunction(name = "curl_easy_setopt", library = "/usr/lib/x86_64-linux-gnu/libcurl.so")
CURLCode easySetOpt(CurlInstance curl, CURLOption option, String value) throws NativeMemoryException;
CURLcode easySetOpt(CURL curl, CURLoption option, String value) throws NativeMemoryException;

@NativeManualFunction(name = "curl_easy_setopt", library = "/usr/lib/x86_64-linux-gnu/libcurl.so")
CURLCode easySetOpt(CurlInstance curl, CURLOption option, @ByAddress long value) throws NativeMemoryException;
CURLcode easySetOpt(CURL curl, CURLoption option, @ByAddress long value) throws NativeMemoryException;

@NativeManualFunction(name = "curl_easy_perform", library = "/usr/lib/x86_64-linux-gnu/libcurl.so")
CURLCode easyPerform(CurlInstance curl) throws NativeMemoryException;
CURLcode easyPerform(CURL curl) throws NativeMemoryException;
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
package io.github.digitalsmile.gpio.libcurl;

import io.github.digitalsmile.annotation.NativeMemoryException;
import io.github.digitalsmile.gpio.libcurl.enums.CURLCode;
import io.github.digitalsmile.gpio.libcurl.enums.CURLOption;
import io.github.digitalsmile.gpio.libcurl.enums.CURLcode;
import io.github.digitalsmile.gpio.libcurl.enums.CURLoption;
import org.junit.jupiter.api.Test;

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

public class LibcurlTest {

// @Test
// public void libcurl() throws NativeMemoryException {
// try (var libcurl = new LibcurlNative()) {
// var code = libcurl.globalInit(CurlConstants.CURL_GLOBAL_DEFAULT);
// assertEquals(code, CURLCode.CURLE_OK);
// var curl = libcurl.easyInit();
// code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_URL, "https://example.com");
// assertEquals(code, CURLCode.CURLE_OK);
// code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_FOLLOWLOCATION, 1L);
// assertEquals(code, CURLCode.CURLE_OK);
//
// code = libcurl.easyPerform(curl);
// assertEquals(code, CURLCode.CURLE_OK);
// }
// }

@Test
public void libcurl() throws NativeMemoryException {
try (var libcurl = new LibcurlNative()) {
var code = libcurl.globalInit(CurlConstants.CURL_GLOBAL_DEFAULT);
assertEquals(code, CURLCode.CURLE_OK);
assertEquals(code, CURLcode.CURLE_OK);
var curl = libcurl.easyInit();
code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_URL, "https://example.com");
assertEquals(code, CURLCode.CURLE_OK);
code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_FOLLOWLOCATION, 1L);
assertEquals(code, CURLCode.CURLE_OK);
code = libcurl.easySetOpt(curl, CURLoption.CURLOPT_URL, "https://example.com");
assertEquals(code, CURLcode.CURLE_OK);
code = libcurl.easySetOpt(curl, CURLoption.CURLOPT_FOLLOWLOCATION, 1L);
assertEquals(code, CURLcode.CURLE_OK);

code = libcurl.easyPerform(curl);
assertEquals(code, CURLCode.CURLE_OK);
assertEquals(code, CURLcode.CURLE_OK);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.digitalsmile.gpio.shared;


import io.github.digitalsmile.gpio.shared.structs.Stat;
import io.github.digitalsmile.gpio.shared.system.Stat;
import org.junit.jupiter.api.Test;

import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.github.digitalsmile.annotation.function.Returns;
import io.github.digitalsmile.annotation.structure.Struct;
import io.github.digitalsmile.annotation.structure.Structs;
import io.github.digitalsmile.gpio.shared.structs.Stat;
import io.github.digitalsmile.gpio.shared.system.Stat;

@NativeMemory(headers = "/usr/include/x86_64-linux-gnu/sys/stat.h")
@NativeMemoryOptions(systemHeader = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,25 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}

if (automaticFunctionElements != null) {
validator.validateAutomaticFunctions(parsed, automaticFunctionElements);
var functions = flatten(parsed).stream().filter(n -> n.getNodeType().isFunction()).toList();
validator.validateAutomaticFunctions(functions, automaticFunctionElements);

List<FunctionNode> autoFunctions = new ArrayList<>();
for (NativeMemoryNode node : functions) {
List<ParameterNode> parameters = new ArrayList<>();
for (NativeMemoryNode parameterNode : node.nodes()) {
parameters.add(new ParameterNode(parameterNode.getName(), parameterNode, false, false));
}

var type = ((FunctionOriginalType) node.getType()).returns();
//processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, type.typeName() + " " + type.carrierClass());
autoFunctions.add(new FunctionNode(node.getName(), null,
new NativeMemoryNode(type.typeName(), type, 0, Position.NO_POSITION), parameters, null));
}
var autoFunctionComposer = new AutoFunctionComposer(processingEnv.getMessager());
var name = rootElement.getSimpleName().toString();
var output = autoFunctionComposer.compose(packageName, PrettyName.getObjectName(name), autoFunctions);
createGeneratedFile(packageName, PrettyName.getObjectName(name + "AutoFunctions"), output);
}

processFunctions(rootElement, manualFunctions, packageName, parsed, nativeOptions);
Expand Down Expand Up @@ -214,7 +232,8 @@ private List<NativeMemoryNode> processHeaderFiles(Element element, String[] head
private void processOpaque(NativeMemoryNode node) {
var opaqueComposer = new OpaqueComposer(processingEnv.getMessager());
var output = opaqueComposer.compose(PrettyName.getObjectName(node.getName()), node);
createGeneratedFile(PackageName.getPackageName(node.getName()), PrettyName.getObjectName(node.getName()), output);
var packageName = PackageName.getPackageName(node.getName());
createGeneratedFile(packageName, PrettyName.getObjectName(node.getName()), output);
}

private void processStructs(NativeMemoryNode node) {
Expand All @@ -223,7 +242,8 @@ private void processStructs(NativeMemoryNode node) {
}
var structComposer = new StructComposer(processingEnv.getMessager());
var output = structComposer.compose(PrettyName.getObjectName(node.getName()), node);
createGeneratedFile(PackageName.getPackageName(node.getName()), PrettyName.getObjectName(node.getName()), output);
var packageName = PackageName.getPackageName(node.getName());
createGeneratedFile(packageName, PrettyName.getObjectName(node.getName()), output);
}

private void processEnums(NativeMemoryNode node, boolean rootConstants) {
Expand All @@ -236,13 +256,15 @@ private void processEnums(NativeMemoryNode node, boolean rootConstants) {
}
var enumComposer = new EnumComposer(processingEnv.getMessager());
var output = enumComposer.compose(PrettyName.getObjectName(name), node);
createGeneratedFile(PackageName.getPackageName(name), PrettyName.getObjectName(name), output);
var packageName = PackageName.getPackageName(node.getName());
createGeneratedFile(packageName, PrettyName.getObjectName(name), output);
}

private void processUnions(NativeMemoryNode node) {
var structComposer = new StructComposer(processingEnv.getMessager(), true);
var output = structComposer.compose(PrettyName.getObjectName(node.getName()), node);
createGeneratedFile(PackageName.getPackageName(node.getName()), PrettyName.getObjectName(node.getName()), output);
var packageName = PackageName.getPackageName(node.getName());
createGeneratedFile(packageName, PrettyName.getObjectName(node.getName()), output);
}

private void processFunctions(Element rootElement, List<Element> functionElements, String packageName, List<NativeMemoryNode> parsed, NativeMemoryOptions nativeOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package io.github.digitalsmile.composers;

import com.squareup.javapoet.*;
import io.github.digitalsmile.PackageName;
import io.github.digitalsmile.PrettyName;
import io.github.digitalsmile.annotation.function.NativeCall;
import io.github.digitalsmile.functions.FunctionNode;
import io.github.digitalsmile.functions.ParameterNode;
import io.github.digitalsmile.headers.mapping.*;

import javax.annotation.processing.Messager;
import javax.lang.model.element.Modifier;
import javax.tools.Diagnostic;
import java.lang.foreign.MemorySegment;
import java.util.List;
import java.util.Map;

public class AutoFunctionComposer {
private final Messager messager;

public AutoFunctionComposer(Messager messager) {
this.messager = messager;
}

public String compose(String packageName, String originalName, List<FunctionNode> nodes) {
var classBuilder = TypeSpec.interfaceBuilder(originalName + "AutoFunctions")
//.addModifiers(Modifier.PUBLIC)
.addSuperinterface(ClassName.get(packageName, originalName));

for (FunctionNode node : nodes) {
var method = MethodSpec.methodBuilder(PrettyName.getVariableName(node.functionName()));

var returnType = node.returnNode().getType();
switch (returnType) {
case ArrayOriginalType arrayOriginalType -> {
method.returns(createType(arrayOriginalType));
// if (arrayOriginalType.carrierClass().equals(String.class)) {
// method.returns(String.class);
// } else if (arrayOriginalType.carrierClass().equals(Object.class)) {
// method.returns(Object.class.arrayType());
// } else {
// var typePackageName = PackageName.getPackageName(arrayOriginalType.typeName());
// method.returns(ClassName.get(typePackageName, PrettyName.getObjectName(arrayOriginalType.typeName())));
// }
}
case ObjectOriginalType objectOriginalType -> {
method.returns(createType(objectOriginalType));
// messager.printMessage(Diagnostic.Kind.ERROR, objectOriginalType.typeName() + " " + objectOriginalType.carrierClass());
// if (objectOriginalType.carrierClass().equals(void.class)) {
// method.returns(void.class);
// } else {
// var typePackageName = PackageName.getPackageName(objectOriginalType.typeName());
// method.returns(ClassName.get(typePackageName, PrettyName.getObjectName(objectOriginalType.typeName())));
// }
}
case PrimitiveOriginalType _ -> {
method.returns(returnType.carrierClass());
}
default -> {
}
}


//messager.printMessage(Diagnostic.Kind.ERROR, "F: " + node.functionName());
for (ParameterNode parameterNode : node.functionParameters()) {
//messager.printMessage(Diagnostic.Kind.ERROR, node.functionName() + " " + parameterNode.nativeMemoryNode().getName());
var name = parameterNode.nativeMemoryNode().getName();
if (name.isEmpty()) {
name = "arg" + node.functionParameters().indexOf(parameterNode);
}
var parameterType = parameterNode.nativeMemoryNode().getType();
var parameterName = PrettyName.getVariableName(name);
switch (parameterType) {
case ArrayOriginalType arrayOriginalType -> {
method.addParameter(createType(arrayOriginalType), name);
// if (arrayOriginalType.isObjectType()) {
// if (arrayOriginalType.carrierClass().equals(String.class)) {
// method.addParameter(String.class, name);
// } else if (arrayOriginalType.carrierClass().equals(Object.class)) {
// method.addParameter(Object.class.arrayType(), name);
// } else {
// var typePackageName = PackageName.getPackageName(arrayOriginalType.typeName());
// method.addParameter(ClassName.get(typePackageName, PrettyName.getObjectName(arrayOriginalType.typeName())), name);
// }
// } else {
// method.addParameter(arrayOriginalType.carrierClass().arrayType(), name);
// }
}
case ObjectOriginalType objectOriginalType -> {
method.addParameter(createType(objectOriginalType), name);
// if (objectOriginalType.carrierClass().equals(void.class)) {
// method.addParameter(MemorySegment.class, name);
// } else {
// var typePackageName = PackageName.getPackageName(objectOriginalType.typeName());
// method.addParameter(ClassName.get(typePackageName, PrettyName.getObjectName(objectOriginalType.typeName())), parameterName);
// }
}
case PrimitiveOriginalType _ -> {
method.addParameter(parameterType.carrierClass(), parameterName);
}
default -> {
}
}

}

classBuilder.addMethod(method
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).build());
}


var builder = JavaFile.builder(packageName, classBuilder.build());
var outputFile = builder.indent("\t").skipJavaLangImports(true).build();
return outputFile.toString();
}


private TypeName createType(OriginalType type, boolean isArray) {
var typeName = type.typeName();
var clazz = type.carrierClass();
if (type.carrierClass().equals(Object.class)) {
var resolvedPackageName = PackageName.getPackageName(typeName);
if (typeName.equals(String.class.getSimpleName())) {
resolvedPackageName = "";
}
return ClassName.get(resolvedPackageName, PrettyName.getObjectName(typeName) + (isArray ? "[]" : ""));
} else {
return TypeName.get(isArray ? clazz.arrayType() : clazz);
}
}

private TypeName createType(OriginalType type) {
return createType(type, false);
}
}
Loading

0 comments on commit e968f47

Please sign in to comment.