Skip to content

Commit

Permalink
#3236 fix qulice violations
Browse files Browse the repository at this point in the history
  • Loading branch information
c71n93 committed Jul 8, 2024
1 parent 1effe83 commit fb8e118
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 121 deletions.
51 changes: 42 additions & 9 deletions eo-maven-plugin/src/main/java/org/eolang/maven/CLibMojo.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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;

import java.io.File;
Expand All @@ -6,17 +30,28 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FilenameUtils;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.commons.io.FilenameUtils;

/**
* Mojo that compiles C native libraries.
*
* @since 0.38
*/
@Mojo(
name = "clib",
defaultPhase = LifecyclePhase.PROCESS_SOURCES,
threadSafe = true
)
public class CLibMojo extends SafeMojo {
public final class CLibMojo extends SafeMojo {

/**
* The directory where the target libraries will be placed.
*/
private static final Path LIB_TARGET_DIR = Paths.get("Lib/native_clib");

/**
* The directory containing sources for using C from EO (e.g. system calls).
* @checkstyle MemberNameCheck (8 lines)
Expand All @@ -27,23 +62,21 @@ public class CLibMojo extends SafeMojo {
defaultValue = "${project.basedir}/src/main/c/eo/lib"
)
@SuppressWarnings("PMD.UnusedPrivateField")
private File cEoLibDir;

private final Path LIB_TARGET_DIR = Paths.get("Lib/native_clib");
private File cLibDir;

@Override
void exec() throws IOException {
Path target = this.targetDir.toPath().resolve(LIB_TARGET_DIR);
final Path target = this.targetDir.toPath().resolve(CLibMojo.LIB_TARGET_DIR);
Files.createDirectories(target);
try (DirectoryStream<Path> files =
Files.newDirectoryStream(cEoLibDir.toPath(), "*.c")) {
Files.newDirectoryStream(this.cLibDir.toPath(), "*.{c,cpp}")) {
boolean contains = false;
for (Path source : files) {
for (final Path source : files) {
contains = true;
new NativeCLib(
source,
target.resolve(FilenameUtils.removeExtension(source.getFileName().toString()))
).build();
).compile();
}
if (!contains) {
throw new IllegalStateException("There are no C sources in the directory");
Expand Down
57 changes: 45 additions & 12 deletions eo-maven-plugin/src/main/java/org/eolang/maven/NativeCLib.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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;

import com.yegor256.Jaxec;
import java.io.IOException;
import java.nio.file.Path;
import org.eolang.maven.util.JniInfo;
import org.eolang.maven.util.CJniInfo;

/**
* Compiles C native libraries.
*
* @since 0.38
*/
public class NativeCLib {
/**
* The single C source for building shared library.
* @checkstyle MemberNameCheck (8 lines)
*/
private final Path source;

/**
* The path to the target file where the resulting native library will be compiled.
*/
private final Path target;

/**
Expand All @@ -24,22 +54,25 @@ public NativeCLib(final Path source, final Path target) {
this.target = target;
}

public void build() {
final String cc = System.getenv("CC");
/**
* Compiles C native libraries.
*/
public void compile() {
final String ccompiler = System.getenv("CC");
try {
new Jaxec(
cc,
String.format("-I%s", JniInfo.COMMON_HEADER),
String.format("-I%s", JniInfo.PLATFORM_SPECIFIC_HEADER),
source.toString(),
ccompiler,
String.format("-I%s", CJniInfo.COMMON_HEADER),
String.format("-I%s", CJniInfo.OS_SPEC_HEADER),
this.source.toString(),
"-shared",
"-o",
target.toString()
this.target.toString()
).exec();
} catch (IllegalArgumentException e) {
} catch (final IllegalArgumentException ex) {
throw new IllegalArgumentException(
"An error occurred while compiling the source code of the C native library",
e
ex
);
}
}
Expand Down
87 changes: 87 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/CJniInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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.util;

import java.util.Locale;
import java.util.Map;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;

/**
* All the information you need to build native C library that can be used with JNI.
*
* @since 0.38
*/
public final class CJniInfo {
/**
* OS name.
*/
private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);

/**
* Correspondence of OS and directory with platform specific JVM headers.
*/
private static final Map<String, String> OS_TO_DIRECTORY = new MapOf<>(
new MapEntry<>("linux", "linux"),
new MapEntry<>("mac", "darwin"),
new MapEntry<>("windows", "windows")
);

/**
* Java Home.
*/
private static final String JAVA_HOME = System.getProperty("java.home");

/**
* The jni.h header, that is common for all systems.
*/
public static final String COMMON_HEADER = String.format("%s/include", CJniInfo.JAVA_HOME);

/**
* The jni_md.h header, that is platform-specific.
*/
public static final String OS_SPEC_HEADER = String.format(
"%s/%s", CJniInfo.COMMON_HEADER, specificIncludeDirName()
);

/**
* Ctor.
*/
private CJniInfo() { }

/**
* The name of the directory that contains the platform-specific C header for JNI.
* @link <a href="https://mail.openjdk.org/pipermail/discuss/2011-June/001918.html">Where to find jni_md.h</a>
* @return The directory name.
*/
private static String specificIncludeDirName() {
for (final Map.Entry<String, String> entry : CJniInfo.OS_TO_DIRECTORY.entrySet()) {
if (CJniInfo.OS_NAME.contains(entry.getKey())) {
return entry.getValue();
}
}
throw new IllegalStateException("Unavailable OS for native C standard lib usage");
}
}
42 changes: 0 additions & 42 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/JniInfo.java

This file was deleted.

Loading

0 comments on commit fb8e118

Please sign in to comment.