<?xml version="1.0" encoding="UTF-8"?>
<!--
  Copyright (c) 2024-2025, NVIDIA CORPORATION.

  Licensed 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>com.nvidia</groupId>
        <artifactId>rapids-4-spark-hybrid-parent_2.12</artifactId>
        <version>25.04.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>rapids-4-spark-hybrid_2.12</artifactId>
    <name>Hybrid Execution Adaptor for RAPIDS Accelerator</name>
    <description>Hybrid Execution Adaptor for RAPIDS Accelerator</description>

    <properties>
        <rapids.module>rapids-hybrid</rapids.module>
        <gluten.clean.skip>true</gluten.clean.skip>
        <main.basedir>${project.basedir}/..</main.basedir>
        <gluten.base.path>${main.basedir}/third_party/incubator-gluten</gluten.base.path>
        <gluten.build.path>${gluten.base.path}/cpp/build</gluten.build.path>
        <gluten.build.force>false</gluten.build.force>
        <native.build.path>${project.basedir}/src/main/cpp/build</native.build.path>
        <CPP_PARALLEL_LEVEL>4</CPP_PARALLEL_LEVEL>
        <build.info.path>${project.build.outputDirectory}/rapids4spark-hybrid-version-info.properties</build.info.path>
        <nonfail.errors.quiet>false</nonfail.errors.quiet>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.nvidia</groupId>
            <artifactId>spark-rapids-jni</artifactId>
            <version>${spark.rapids.jni.version}</version>
            <classifier>${spark.rapids.jni.classifier}</classifier>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_${scala.binary.version}</artifactId>
        </dependency>
        <dependency>
            <groupId>com.nvidia</groupId>
            <artifactId>gluten-velox-bundle</artifactId>
	    <version>${gluten.version}</version>
            <classifier>${gluten.classifier}</classifier>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <profiles>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>clean gluten</id>
                        <phase>clean</phase>
                        <configuration>
                            <skip>${gluten.clean.skip}</skip>
                            <target>
                                <delete dir="${gluten.build.path}" includeemptydirs="true"/>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>build rapids-hybrid</id>
                        <phase>validate</phase>
                        <configuration>
                            <target>
                                <mkdir dir="${native.build.path}"/>
                                <exec dir="${native.build.path}/.."
                                      failonerror="true"
                                      executable="cmake">
                                    <arg value="-DGLUTEN_HOME=${gluten.base.path}"/>
                                    <arg value="-S"/>
                                    <arg value="."/>
                                    <arg value="-B"/>
                                    <arg value="./build"/>
                                </exec>
                                <exec dir="${native.build.path}/.."
                                      failonerror="true"
                                      executable="cmake">
                                    <arg value="--build"/>
                                    <arg value="./build"/>
                                    <arg value="-j${CPP_PARALLEL_LEVEL}"/>
                                    <arg value="-v"/>
                                </exec>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate-build-info</id>
                        <phase>generate-resources</phase>
                        <configuration>
                            <!-- Execute the shell script to generate the plugin build information. -->
                            <target name="build-info">
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <exec executable="git"
                                      outputproperty="git.head.revision"
                                      discardError="${nonfail.errors.quiet}">
                                    <arg value="rev-parse"/>
                                    <arg value="HEAD"/>
                                </exec>
                                <ac:if xmlns:ac="antlib:net.sf.antcontrib">
                                    <available file="${build.info.path}" type="file"/>
                                    <then>
                                        <property file="${build.info.path}" prefix="saved.build-info"/>
                                    </then>
                                    <else>
                                        <property name="saved.build-info.revision" value="N/A"/>
                                    </else>
                                </ac:if>
                                <echo>
                                    Comparing git revisions:
                                    previous=${saved.build-info.revision}
                                    current=${git.head.revision}
                                </echo>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <ac:if xmlns:ac="antlib:net.sf.antcontrib">
                                    <equals arg1="${git.head.revision}" arg2="${saved.build-info.revision}"/>
                                    <then>
                                        <echo>
                                            Git revisions unchanged: skipping version info file generation.
                                            Delete ${build.info.path} or mvn clean if regeneration desired.
                                            This will force full Scala code rebuild in downstream modules.
                                        </echo>
                                    </then>
                                    <else>
                                        <echo>Generating new version info file</echo>
                                        <mkdir dir="${project.build.outputDirectory}"/>
                                        <exec executable="bash"
                                              output="${build.info.path}"
                                              resultproperty="build-info.exitCode"
                                              errorproperty="build-info.errorMsg"
                                              failonerror="false">
                                            <arg value="${spark.rapids.source.basedir}/build/build-info"/>
                                            <arg value="${project.version}"/>
                                        </exec>
                                        <fail message="exec build-info.sh failed, exit code is ${build-info.exitCode}, error msg is ${build-info.errorMsg}">
                                            <condition>
                                                <not>
                                                    <equals arg1="${build-info.exitCode}" arg2="0"/>
                                                </not>
                                            </condition>
                                        </fail>
                                    </else>
                                </ac:if>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant</artifactId>
                        <version>1.10.12</version>
                    </dependency>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>1.0b3</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                        <configuration>
                            <classifier>tests</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <!-- Remove the double quote characters from os-release and generate a new property file
                     e.g.:
                        ID=ubuntu
                        VERSION_ID="24.04"
                     ==>>
                        ID=ubuntu
                        VERSION_ID=24.04
                     If not remove the double quote, the Spark-Rapids packaging will report error
                -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>remove-double-quote-from-os-release-file</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>/bin/bash</executable>
                            <arguments>
                                <argument>-c</argument>
                                <argument>cat /etc/os-release | sed 's/\"//g' > target/os-release.properties</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- This plugin reads properties in os-release.properties which is from/etc/os-release to
                     get Linux OS ID and OS version,
                         For both Ubuntu and CentOS, there is a /etc/os-release file.
                         e.g., Ubuntu gets:
                            ID=ubuntu
                            VERSION_ID=24.04
                         e.g., CentOS gets:
                            ID=rocky
                            VERSION_ID=8.8
                         The copy-native-libs execution will copy so files into subdirectories according to
                         ID and VERSION_ID
                -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>read-properties-in-os-release</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>target/os-release.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <!-- downgrade version so symlinks are followed -->
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-native-libs</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <overwrite>true</overwrite>
                            <!-- Put lib file into path: arch/OS/OS-version, e.g.: x86_64/ubuntu/22.04 -->
                            <outputDirectory>${project.build.outputDirectory}/${os.arch}/${ID}/${VERSION_ID}
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${native.build.path}/releases</directory>
                                    <includes>
                                        <include>librapids_hybrid.so</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- Copy LICENSE and NOTICE into the jar, path in jar is: jar:/META-INF -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>add-license-notice-resources</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>add-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>${spark.rapids.source.basedir}</directory>
                                    <targetPath>META-INF</targetPath>
                                    <includes>
                                        <include>LICENSE</include>
                                        <include>NOTICE</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

