Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Nov 29, 2023
2 parents fd370f6 + 770aba6 commit f384b1c
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.eolang.maven.rust.BuildFailureException;
import org.eolang.maven.rust.Names;

/**
* Compile binaries.
*
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @since 0.1
* @todo #2118:90min Make it more generic. Perhaps by compilation of
* RustNode instead of going and building throw directories. Maybe it
* is better to get from BinarizeParseMojo.
*/
@Mojo(
name = "binarize",
Expand Down Expand Up @@ -105,7 +109,9 @@ public final class BinarizeMojo extends SafeMojo {

@Override
public void exec() throws IOException {
new Moja<>(BinarizeParseMojo.class).copy(this).execute();
new Moja<>(BinarizeParseMojo.class).with(
"names", new Names(this.namesDir.toPath())
).copy(this).execute();
final int total = new SumOf(
new Threads<>(
Runtime.getRuntime().availableProcessors(),
Expand Down
128 changes: 32 additions & 96 deletions eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.eolang.maven;

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Shift;
Expand All @@ -33,29 +32,20 @@
import com.yegor256.xsline.Xsline;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.cactoos.map.MapOf;
import org.eolang.maven.footprint.FtDefault;
import org.eolang.maven.rust.Commented;
import org.eolang.maven.rust.Module;
import org.eolang.maven.rust.FFINode;
import org.eolang.maven.rust.Names;
import org.eolang.maven.rust.Native;
import org.eolang.maven.rust.PrimeModule;
import org.eolang.maven.rust.Project;
import org.eolang.maven.rust.RustNode;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.HmBase;
import org.eolang.parser.ParsingTrain;

/**
Expand All @@ -79,11 +69,6 @@ public final class BinarizeParseMojo extends SafeMojo {
*/
public static final Path DIR = Paths.get("binarize");

/**
* The directory with generated .rs files.
*/
public static final Path CODES = Paths.get("codes");

/**
* Parsing train with XSLs.
*/
Expand All @@ -108,16 +93,6 @@ public final class BinarizeParseMojo extends SafeMojo {
@SuppressWarnings("PMD.UnusedPrivateField")
private File generatedDir;

/**
* File where to save {@link org.eolang.maven.rust.Names} map.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
required = true,
defaultValue = "${project.build.directory}/names"
)
private File namesDir;

/**
* The directory with portal project.
* @checkstyle MemberNameCheck (8 lines)
Expand All @@ -130,60 +105,20 @@ public final class BinarizeParseMojo extends SafeMojo {
@SuppressWarnings("PMD.UnusedPrivateField")
private File eoPortalDir;

/**
* To uniquely name different ffi inerts.
*/
private Names names;

@Override
public void exec() throws IOException {
final Names names = new Names(this.namesDir.toPath());
new File(this.targetDir.toPath().resolve("Lib/").toString()).mkdirs();
for (final ForeignTojo tojo : this.scopedTojos().withOptimized()) {
final Path file = tojo.shaken();
final XML input = new XMLDocument(file);
final List<XML> nodes = this.addRust(input).nodes("/program/rusts/rust");
for (final XML node: nodes) {
final String code = BinarizeParseMojo.unhex(node.xpath("@code").get(0));
final List<String> dependencies =
node.xpath("./dependencies/dependency/attribute(name)")
.stream()
.map(BinarizeParseMojo::unhex)
.collect(Collectors.toList());
final String function = names.name(
node.xpath("@code_loc").get(0)
);
final String filename = String.format(
"%s%s",
function,
".rs"
);
final Path target = BinarizeMojo.DIR
.resolve(BinarizeParseMojo.CODES)
.resolve(filename);
new HmBase(this.targetDir.toPath()).save(
code,
target
);
Logger.info(
this,
"Binarized %s from %s:%s",
filename,
input.xpath("/program/@name").get(0),
node.xpath("@code_loc").get(0)
);
new Project(this.targetDir.toPath().resolve("Lib/".concat(function)))
.with(new Module(code, "src/foo"), dependencies)
.with(new PrimeModule(function, "src/lib"), new ArrayList<>(1))
.dependency(
"eo",
new MapOf<>("path", this.eoPortalDir.getAbsolutePath())
)
.save();
new Commented(new Native(function, "EOrust.natives"), "//")
.save(
new FtDefault(
this.generatedDir.toPath().resolve("EOrust").resolve("natives")
)
);
}
this.getFFIs(new XMLDocument(file))
.forEach(FFINode::generateUnchecked);
}
names.save();
this.names.save();
}

/**
Expand All @@ -204,29 +139,30 @@ private XML addRust(
}

/**
* Makes a text from Hexed text.
* @param txt Hexed chars separated by backspace.
* @return Normal text.
* Add ffi node via xsl transformation and return list of them.
* @param input Input xmir.
* @return FFI nodes.
* @todo #2609:90min We can make the current class more generic
* by transferring this.addRust(input) snippet to corresponding
* FFINode- {@link RustNode}. We wanna make the class independent of
* ffi-insert as a result.
* @checkstyle AbbreviationAsWordInNameCheck (8 lines)
*/
private static String unhex(final String txt) {
final StringBuilder hex = new StringBuilder(txt.length());
for (final char chr : txt.toCharArray()) {
if (chr == ' ') {
continue;
}
hex.append(chr);
}
final String result;
try {
final byte[] bytes = Hex.decodeHex(String.valueOf(hex).toCharArray());
result = new String(bytes, StandardCharsets.UTF_8);
} catch (final DecoderException exception) {
throw new IllegalArgumentException(
String.format("Invalid String %s, cannot unhex", txt),
exception
private Collection<FFINode> getFFIs(final XML input) {
final List<XML> nodes = this.addRust(input).nodes("/program/rusts/rust");
final Collection<FFINode> ret = new ArrayList<>(nodes.size());
for (final XML node : nodes) {
ret.add(
new RustNode(
node,
this.names,
this.targetDir.toPath().resolve("Lib"),
this.eoPortalDir.toPath(),
this.generatedDir.toPath().resolve("EOrust").resolve("natives")
)
);
}
return result;
return ret;
}

}
54 changes: 54 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/rust/FFINode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven.rust;

import java.io.IOException;
import java.io.UncheckedIOException;

/**
* Common interface for FFI nodes in xmir.
* @since 0.34
* @checkstyle AbbreviationAsWordInNameCheck (8 lines)
*/
public interface FFINode {

/**
* Generate necessary files in target. For example, for rust nodes we need
* to create cargo project in target/Lib and java files in
* target/generated/EOrust/natives/
* @throws IOException if any issues with IO.
*/
void generate() throws IOException;

/**
* Generate method but throw unchecked IOException.
*/
default void generateUnchecked() {
try {
this.generate();
} catch (final IOException exc) {
throw new UncheckedIOException(exc);
}
}
}
Loading

2 comments on commit f384b1c

@0pdd
Copy link

@0pdd 0pdd commented on f384b1c Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2118-1bbacef7 discovered in eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java) and submitted as #2648. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on f384b1c Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2609-cb1cc596 discovered in eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParseMojo.java) and submitted as #2649. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.