maven-nar / nar-maven-plugin

Native ARchive plugin for Maven
https://maven-nar.github.io/
Apache License 2.0
232 stars 160 forks source link

Doess nar-maven-plugin works for aarch64 linux? #397

Closed shoothzj closed 1 year ago

shoothzj commented 1 year ago

Doess nar-maven-plugin works for aarch64 linux? Thanks for your reply :)

CapitalMarkets commented 1 year ago

Your question is ambiguous to my reading so I'll answer its various possible meanings:

1. Will it run on an aarch64 linux host?

Yes, it is a plugin within Maven which is based on Java, all of which will run on aarch64 with the appropriate JRE installed.

2. Can it target aarch64 linux builds while running on an aarch64 linux host?

Yes, the compiler tools that are bootstrapped during the build process e.g. gcc, g++ will run as they do when used on the regular command line.

3. Can it target aarch64 linux cross-compiled builds while running on a X86_64/AMD64 linux host?

Yes, if you help it.

Let's presume that you are using the GNU GCC toolchain.

If you install from the distro repo the standard (native) toolchain on your X86_64/AMD64 Linux host, you will get what you need to compile binaries, shared objects, etc including standard headers and link libraries for C and C++.

[a] For example in Fedora we have: _gcc.x86_64 gcc-c++.x8664

[b] Now, if you want to build for aarch64, you install the gcc cross compilation tools for that target, again in Fedora this will be: _binutils-aarch64-linux-gnu.x86_64 gcc-aarch64-linux-gnu.x86_64 gcc-c++-aarch64-linux-gnu.x8664

[c] If we also install QEMU for aarch64, our X86_64/AMD64 Linux host will even be able to run our aarch binaries under emulation, in Fedora that would be: _qemu-system-aarch64.x86_64 qemu-system-aarch64-core.x86_64 qemu-user-static-aarch64.x8664

But let's not get too ahead - because there is an issue in some Linux distros (including Fedora):

THEY DON'T INCLUDE THE STANDARD LIBRARIES, C/C++ HEADER FILES, SHARED OBJECTS, ETC for AARCH64.

We expect to get these when we perform step [b] on our host, but some distros will only install the binary cross compilation tools, and fail to place aarch resources into the standard folders where the cross compiler and linker can find them.

To address this side of it, these AARCH64 resources can be downloaded separately from ARM, for example: Arm GNU Toolchain Downloads

In my own case I have had to do this - my last download was gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu which put a number of files on my filesystem (see pom.xml example below).

How to move forward? Presuming your question was No. 3

Don't waste time here - experiment :)

You need to download aarch64 tools for your host (step [b]), you need to find out if you can build aarch64 code out of the box (using stdio.h/iostream at least and printf/cout, etc) or whether you need to install extra files from ARM.

Along the way you need to set < architecture > and < prefix > properties in your pom.xml to get NAR to use the cross compilation tools when you do a mvn clean verify.

This should be enough if your Linux distro includes all of the AARCH64 resources needed already for you to build.

If you need to install the extra ARM tools/resources you will need to set additional compiler/link tool options in your NAR configuration to help the tools find the files you downloaded from ARM (see pom.xml example attached).

This gets a bit messy but DOES WORK. I myself am still trying to figure out how to do it in a dynamic/consistent way as the pom.xml ends up carrying build info specific to my machine/folders/username, etc.

Anyway, to help you get started look at the pom.xml attached. Note that I have two runs per maven lifecycle, one run for Linux AMD64 outputs (default config in my parent POM) then another run for Linux AARCH64 outputs (configured in the attached POM).

good luck

aarch64.example.pom.txt

   <build>
      <plugins>
         <plugin>
            <groupId>com.github.maven-nar</groupId>
            <artifactId>nar-maven-plugin</artifactId>
            <extensions>true</extensions>
            <executions>
               <!-- Enable a new lifecycle for static cross compilation to ARM Linux (inc. tests) -->
               <execution>
                  <id>arm-static-cross-compilation</id>
                  <goals>
                     <goal>nar-compile</goal>
                     <goal>nar-testCompile</goal>
                 </goals>
                  <configuration>
                     <architecture>aarch64</architecture>
                     <os>Linux</os>
                     <runtime>static</runtime>
                     <c>
                        <prefix>aarch64-linux-gnu-</prefix>
                        <options>
                           <option>--sysroot=/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc</option>
                       </options>
                        <testOptions>
                           <testOption>--sysroot=/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc</testOption>
                       </testOptions>
                     </c>
                     <cpp>
                        <prefix>aarch64-linux-gnu-</prefix>
                        <options>
                           <option>-isystem/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/include/c++/11.2.1</option>
                           <option>-isystem/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc/usr/include</option>
                       </options>
                        <testOptions>
                           <testOption>-isystem/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/include/c++/11.2.1</testOption>
                           <testOption>-isystem/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc/usr/include</testOption>
                       </testOptions>
                    </cpp>
                     <linker>
                        <prefix>aarch64-linux-gnu-</prefix>
                        <testOptions>
                           <testOption>--sysroot=/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc</testOption>
                           <testOption>-L/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/lib64</testOption>
                       </testOptions>
                    </linker>
                     <libraries>
                        <library>
                           <type>static</type>
                       </library>
                    </libraries>
                 </configuration>
              </execution>

               <!-- Enable a new lifecycle for shared cross compilation to ARM Linux -->
               <execution>
                  <id>arm-shared-cross-compilation</id>
                  <goals>
                     <goal>nar-compile</goal>
                 </goals>
                  <configuration>
                     <architecture>aarch64</architecture>
                     <os>Linux</os>
                     <runtime>dynamic</runtime>
                     <c>
                        <prefix>aarch64-linux-gnu-</prefix>
                        <options>
                           <option>--sysroot=/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc</option>
                       </options>
                    </c>
                     <cpp>
                        <prefix>aarch64-linux-gnu-</prefix>
                        <options>
                           <option>-isystem/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/include/c++/11.2.1</option>
                           <option>-isystem/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc/usr/include</option>
                       </options>
                    </cpp>
                     <linker>
                        <prefix>aarch64-linux-gnu-</prefix>
                        <options>
                           <option>--sysroot=/home/USER/Programs/arm/gcc-arm/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/libc</option>
                       </options>
                    </linker>
                     <libraries>
                        <library>
                           <type>shared</type>
                       </library>
                    </libraries>
                 </configuration>
              </execution>

               <!-- Enable a new lifecycle for cross testing to ARM Linux.
                    This only works with the 'nar-testCompile' goal in the static lifecycle. -->
               <execution>
                  <id>arm-static-cross-testing</id>
                  <goals>
                     <goal>nar-test</goal>
                 </goals>
                  <configuration>
                     <architecture>aarch64</architecture>
                     <os>Linux</os>
                 </configuration>
              </execution>
           </executions>
        </plugin>
     </plugins>
  </build>
shoothzj commented 1 year ago

@CapitalMarkets Hi, Thanks for your patient reply. I am curious that we already support aarch64 linux. Why we still need add custom aol.prorperties. Like in https://github.com/apache/bookkeeper/pull/3828 I'm sorry if I come across as inexperienced.

CapitalMarkets commented 1 year ago

When you include the nar-maven-plugin you get the default aol.properties included in the plugin.

aol.properties files provide flexiblity in that you can either override or add to the configuration (compiler/link settings and flags, custom settings needed for building for certain environments, doing things differently between the C side and the C++ sides of the build, etc, etc).

Whoever is creating those files in those projects knows why they are doing it. A quick read there for example mentions aarch64 + MacOS - I don't know much about this stack but if you look at the default aol.properties you won't see anything there for MacOS on AARCH64, but there are default entries for PowerPC (ppc.MacOSX.), x86 (i386.MacOSX.) and AMD64 (x86_64.MacOSX).

NAR supports various toolchains and recognises various OS and ARCH for configuration purposes - but you need the configuration to be inside the default aol.properties OR in custom aol.properties files you provide.

Based on that default aol.properties you would not be able to do a build for MACOS+AARCH64 but you would be able to do one for LINUX+AARCH64 or MACOS+AMD64.

This is why that project is messing with custom aol.properties files. Ignoring the defaults and taking full control using maven profiles and custom aol.properties files.

If they didn't do this they would have to either

1) Fork the NAR plugin and make/maintain their changes in their own forked default aol.properties 2) Submit the changes to this project in a PR for integration (assuming their settings are good defaults for others) and wait for them to be merged and a new release of the plugin published.

Hope that helps.

shoothzj commented 1 year ago

@CapitalMarkets As in that project bookkeeper, Before https://github.com/apache/bookkeeper/pull/3828 is merged. It has apple-m1 profile indeed. But I think if default aol.properites contains LINUX+AARCH64, I will no need to add LINUX+AARCH64 properties. It can use the default aol.properties and default profile, but I meets some problem

[ERROR] Failed to execute goal com.github.maven-nar:nar-maven-plugin:3.10.1:nar-validate (default-nar-validate) on project circe-checksum: NAR: One of two things may be wrong here:

Briefly, I think if nar-maven-plugin works for aarch64 linux build, I can try to build it on aarch linux without custom aol.properties

shoothzj commented 1 year ago

@CapitalMarkets Thanks for your former reply, looking forward for you reply. Thanks in advance.

CapitalMarkets commented 1 year ago

@Shoothzj Hi there, what are you waiting for a reply to?

If you mean the error above, you need to provide more complete error message - the one line included above doesn't say anything.