<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.apache.bookkeeper</groupId>
    <artifactId>bookkeeper</artifactId>
    <version>4.18.0</version>
    <relativePath>..</relativePath>
  </parent>

  <artifactId>native-io</artifactId>
  <packaging>jar</packaging>
  <name>Apache BookKeeper :: Native IO Library</name>
  <description>Native IO Library</description>

  <properties>
    <rust.dir>${project.basedir}/src/main/native-io-jni/rust</rust.dir>
    <rust.target.dir>${rust.dir}/target</rust.target.dir>
    <rust.executable>cargo</rust.executable>
    <rust.release.lib.relative.path>release/libnative_io.so</rust.release.lib.relative.path>
    <rust.zig.target.amd64.gnu>x86_64-unknown-linux-gnu</rust.zig.target.amd64.gnu>
    <rust.zig.target.arm64.gnu>aarch64-unknown-linux-gnu</rust.zig.target.arm64.gnu>
    <rust.lib.dir>${project.build.directory}/classes/lib</rust.lib.dir>
    <native.io.pure.rust>false</native.io.pure.rust>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.bookkeeper</groupId>
      <artifactId>native-library-common</artifactId>
      <version>${project.parent.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.rat</groupId>
        <artifactId>apache-rat-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/rust/target/**</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <!--
        Cross-compile for Linux amd64 + arm64 (glibc) via cargo-zigbuild.
        Produces a single JAR containing two variants:
          lib/linux-x86_64-gnu/libnative-io.so   (glibc, amd64)
          lib/linux-aarch64-gnu/libnative-io.so  (glibc, arm64)

        Prerequisites: zig, cargo-zigbuild, and both Rust targets
        added via `rustup target add`.
      -->
      <id>cargo-zigbuild</id>
      <activation>
        <property>
          <name>native.io.pure.rust</name>
          <value>!true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>cargo-zigbuild-amd64-gnu</id>
                <phase>compile</phase>
                <goals><goal>exec</goal></goals>
                <configuration>
                  <executable>${rust.executable}</executable>
                  <workingDirectory>${rust.dir}</workingDirectory>
                  <arguments>
                    <argument>zigbuild</argument>
                    <argument>--release</argument>
                    <argument>--target</argument>
                    <argument>${rust.zig.target.amd64.gnu}</argument>
                  </arguments>
                </configuration>
              </execution>
              <execution>
                <id>cargo-zigbuild-arm64-gnu</id>
                <phase>compile</phase>
                <goals><goal>exec</goal></goals>
                <configuration>
                  <executable>${rust.executable}</executable>
                  <workingDirectory>${rust.dir}</workingDirectory>
                  <arguments>
                    <argument>zigbuild</argument>
                    <argument>--release</argument>
                    <argument>--target</argument>
                    <argument>${rust.zig.target.arm64.gnu}</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>copy-zig-libs</id>
                <phase>process-classes</phase>
                <goals><goal>run</goal></goals>
                <configuration>
                  <target>
                    <mkdir dir="${rust.lib.dir}/linux-x86_64-gnu" />
                    <mkdir dir="${rust.lib.dir}/linux-aarch64-gnu" />
                    <copy file="${rust.target.dir}/${rust.zig.target.amd64.gnu}/${rust.release.lib.relative.path}" tofile="${rust.lib.dir}/linux-x86_64-gnu/libnative-io.so" failonerror="true" verbose="true" />
                    <copy file="${rust.target.dir}/${rust.zig.target.arm64.gnu}/${rust.release.lib.relative.path}" tofile="${rust.lib.dir}/linux-aarch64-gnu/libnative-io.so" failonerror="true" verbose="true" />
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <!--
        Opt-in local build that uses plain cargo for the current Linux host.
        Enable with: mvn -pl native-io -Dnative.io.pure.rust=true test
      -->
      <id>pure-rust-build</id>
      <activation>
        <property>
          <name>native.io.pure.rust</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>build-and-copy-pure-rust-lib</id>
                <phase>process-classes</phase>
                <goals><goal>run</goal></goals>
                <configuration>
                  <target>
                    <fail message="Pure Rust native-io builds are supported on Linux hosts only.">
                      <condition>
                        <not>
                          <equals arg1="${os.detected.name}" arg2="linux" />
                        </not>
                      </condition>
                    </fail>
                    <condition property="rust.current.jar.directory" value="linux-x86_64-gnu">
                      <or>
                        <equals arg1="${os.detected.arch}" arg2="x86_64" />
                        <equals arg1="${os.detected.arch}" arg2="amd64" />
                      </or>
                    </condition>
                    <condition property="rust.current.jar.directory" value="linux-aarch64-gnu">
                      <or>
                        <equals arg1="${os.detected.arch}" arg2="aarch64" />
                        <equals arg1="${os.detected.arch}" arg2="arm64" />
                        <equals arg1="${os.detected.arch}" arg2="aarch_64" />
                      </or>
                    </condition>
                    <fail unless="rust.current.jar.directory" message="Pure Rust native-io builds are supported on Linux amd64/aarch64 hosts only." />
                    <mkdir dir="${rust.lib.dir}/${rust.current.jar.directory}" />
                    <exec executable="${rust.executable}" dir="${rust.dir}" failonerror="true">
                      <arg value="build" />
                      <arg value="--release" />
                    </exec>
                    <copy file="${rust.target.dir}/${rust.release.lib.relative.path}" tofile="${rust.lib.dir}/${rust.current.jar.directory}/libnative-io.so" failonerror="true" verbose="true" />
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
