Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add soemJavaNativeLibrary and support cross compilation for aarch64 #18

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ ihmc {
app.entrypoint("SlaveInfo", "us.ihmc.etherCAT.master.SlaveInfo")

mainDependencies {
api("us.ihmc:soem:1.4.0-ihmc1")
api("us.ihmc:soem-platform-linux:1.4.0-ihmc1")
api("us.ihmc:soem:1.5.0")
api("us.ihmc:soem-platform-linux-x86_64:1.5.0")
api("us.ihmc:soem-platform-linux-arm64:1.5.0")
Comment on lines +20 to +22
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will have to publish these

Copy link
Member

Choose a reason for hiding this comment

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

Looks good. Looks like that's why the checks are failing. Maybe you should go ahead and publish so the tests in the PR pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, note that there is 1 failing test on develop already

api("us.ihmc:ihmc-native-library-loader:2.0.3")
api("us.ihmc:ihmc-realtime:1.6.0")
}
Expand Down
8 changes: 8 additions & 0 deletions linux-aarch64-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
set(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu)
set(CMAKE_PROGRAM_PATH /usr/aarch64-linux-gnu/bin)

set(ENV{AARCH64_CROSS} "1")
19 changes: 11 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,17 @@ The gradle build files for the SOEM wrapper and native libraries are created by

To build and publish run the following commands. Note that the gradle commands are ran from within the build directory.


- cd ihmc-ethercat-master
- mkdir build
- cd build
- cmake -DCMAKE_BUILD_TYPE=Release ..
- make
- ../gradlew publishToMavenLocal -Ptarget=JAVA
- ../gradlew publishToMavenLocal -Ptarget=PLATFORM
```
cd ihmc-ethercat-master
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
# Cross compile for arm64 using the toolchain
# cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=../linux-aarch64-toolchain.cmake ..
Copy link

Choose a reason for hiding this comment

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

I assume this means we need to run this on any arm64 machine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When running on a native arm platform you don't need to use the cross compilation toolchain

make
../gradlew publishToMavenLocal -Ptarget=JAVA
../gradlew publishToMavenLocal -Ptarget=PLATFORM
```

Note that if you want to publish multiple platform libraries you only have to run target=JAVA on a single platform

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/us/ihmc/etherCAT/master/FoEBootstrap.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package us.ihmc.etherCAT.master;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;

import us.ihmc.etherCAT.master.Mailbox.MailboxConfiguration;
import us.ihmc.etherCAT.soemJavaNativeLibrary;
import us.ihmc.soem.generated.ec_err_type;
import us.ihmc.soem.generated.ec_slavet;
import us.ihmc.soem.generated.ec_state;
import us.ihmc.soem.generated.ecx_context;
import us.ihmc.soem.generated.soem;
import us.ihmc.soem.generated.soemConstants;
import us.ihmc.tools.nativelibraries.NativeLibraryLoader;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;

/**
* Standalone implementation for File over EtherCAT in BOOT mode
Expand All @@ -24,7 +24,7 @@ public class FoEBootstrap
{
static
{
NativeLibraryLoader.loadLibrary("us.ihmc.soem.generated", "soemJava");
soemJavaNativeLibrary.load();
}

private class SlaveConfig
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/us/ihmc/etherCAT/master/Master.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import us.ihmc.etherCAT.master.EtherCATStatusCallback.TRACE_EVENT;
import us.ihmc.etherCAT.master.exception.SlavesNotConfiguredException;
import us.ihmc.etherCAT.master.exception.SlavesOfflineException;
import us.ihmc.etherCAT.soemJavaNativeLibrary;
import us.ihmc.soem.generated.ec_slavet;
import us.ihmc.soem.generated.ec_smt;
import us.ihmc.soem.generated.ec_state;
import us.ihmc.soem.generated.ecx_context;
import us.ihmc.soem.generated.ecx_portt;
import us.ihmc.soem.generated.soem;
import us.ihmc.soem.generated.soemConstants;
import us.ihmc.tools.nativelibraries.NativeLibraryLoader;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -38,7 +38,7 @@ public class Master implements MasterInterface

static
{
NativeLibraryLoader.loadLibrary("us.ihmc.soem.generated", "soemJava");
soemJavaNativeLibrary.load();
}

private static boolean initialized = false;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/us/ihmc/etherCAT/master/SlaveInfo.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package us.ihmc.etherCAT.master;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import us.ihmc.etherCAT.soemJavaNativeLibrary;
import us.ihmc.soem.generated.ec_slavet;
import us.ihmc.soem.generated.ec_smt;
import us.ihmc.soem.generated.ecx_context;
import us.ihmc.soem.generated.soem;
import us.ihmc.soem.generated.soemConstants;
import us.ihmc.tools.nativelibraries.NativeLibraryLoader;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
* Simple application to print basic information about the detected slave configuration
Expand All @@ -24,7 +24,7 @@ public class SlaveInfo

static
{
NativeLibraryLoader.loadLibrary("us.ihmc.soem.generated", "soemJava");
soemJavaNativeLibrary.load();
}

public static String hex(long n)
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/us/ihmc/etherCAT/soemJavaNativeLibrary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package us.ihmc.etherCAT;

import us.ihmc.tools.nativelibraries.NativeLibraryDescription;
import us.ihmc.tools.nativelibraries.NativeLibraryLoader;
import us.ihmc.tools.nativelibraries.NativeLibraryWithDependencies;

public class soemJavaNativeLibrary implements NativeLibraryDescription
{
@Override
public String getPackage(OperatingSystem os, Architecture arch)
{
return "us.ihmc.soem.generated";
}

@Override
public NativeLibraryWithDependencies getLibraryWithDependencies(OperatingSystem os, Architecture arch)
{
if (os == OperatingSystem.LINUX64 && arch == Architecture.x64)
{
return NativeLibraryWithDependencies.fromFilename("libsoemJava-x86_64.so");
}
else if (os == OperatingSystem.LINUX64 && arch == Architecture.arm64)
{
return NativeLibraryWithDependencies.fromFilename("libsoemJava-arm64.so");
}
throw new RuntimeException("Unsupported platform: " + os.name() + "-" + arch.name());
}

private static boolean loaded = false;

public static boolean load()
{
if (!loaded)
{
soemJavaNativeLibrary lib = new soemJavaNativeLibrary();
loaded = NativeLibraryLoader.loadLibrary(lib);
}
return loaded;
}
}
2 changes: 1 addition & 1 deletion swig/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8.4)
cmake_minimum_required(VERSION 3.10)

set(CMAKE_POSITION_INDEPENDENT_CODE true)

Expand Down
5 changes: 3 additions & 2 deletions swig/build.gradle.kts.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

ihmc {
group = "us.ihmc"
version = "1.4.0-ihmc1"
version = "1.5.0"
vcsUrl = "https://github.com/ihmcrobotics/ihmc-ethercat-master"
openSource = true

Expand Down Expand Up @@ -43,7 +43,8 @@ sourceSets {
main {
resources{
srcDir("swig/")
include("**/libsoemJava.so")
include("**/libsoemJava-x86_64.so")
include("**/libsoemJava-arm64.so")
exclude("**/CMakeFiles")
}
}
Expand Down
44 changes: 19 additions & 25 deletions swig/settings.gradle.kts.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,23 @@ if((target != "JAVA" && target != "PLATFORM"))

else
{
if(target == "JAVA")
{
rootProject.name="soem"
}
else
{

if(OperatingSystem.current().isWindows())
{
rootProject.name="soem-platform-windows"
}
else if (OperatingSystem.current().isLinux())
{
rootProject.name="soem-platform-linux"
}
else if (OperatingSystem.current().isMacOsX())
{
rootProject.name="soem-platform-mac"
}
else
{
throw GradleException("Current platform is not supported")
}

}
if (target == "JAVA")
{
rootProject.name="soem"
}
else
{
if (OperatingSystem.current().isLinux() && (System.getProperty("os.arch") == "aarch64" || System.getenv("AARCH64_CROSS") != null))
{
rootProject.name="soem-platform-linux-arm64"
}
else if (OperatingSystem.current().isLinux() && System.getProperty("os.arch") == "amd64")
{
rootProject.name="soem-platform-linux-x86_64"
}
else
{
throw GradleException("Current platform is not supported")
}
}
}
31 changes: 23 additions & 8 deletions swig/us/ihmc/soem/generated/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 3.10)

find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/swig/")
INCLUDE_DIRECTORIES("/usr/include/soem")
include_directories("${PROJECT_SOURCE_DIR}/swig/")
include_directories("/usr/include/soem")

SET_PROPERTY(SOURCE soem.i PROPERTY SWIG_FLAGS "-package" "us.ihmc.soem.generated")
set_property(SOURCE soem.i PROPERTY SWIG_FLAGS "-package" "us.ihmc.soem.generated")

SWIG_ADD_MODULE(soemJava java soem.i)
swig_link_libraries(soemJava ihmcsoemwrapper)
if(UNIX AND NOT APPLE)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" AND NOT DEFINED ENV{AARCH64_CROSS})
message(STATUS "Using target platform x86_64")
set(LIBRARY_NAME "soemJava-x86_64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" OR DEFINED ENV{AARCH64_CROSS})
message(STATUS "Using target platform arm64")
set(LIBRARY_NAME "soemJava-arm64")
else()
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
else()
message(FATAL_ERROR "This CMake configuration is only for Linux.")
endif()

# Add SWIG module with the architecture-specific library name
swig_add_module(${LIBRARY_NAME} java soem.i)
swig_link_libraries(${LIBRARY_NAME} ihmcsoemwrapper)
Loading