<?xml version="1.0" encoding="UTF-8"?>
<!--
  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. See accompanying LICENSE file.
-->
<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">
    <parent>
        <artifactId>hdds</artifactId>
        <groupId>org.apache.ozone</groupId>
        <version>1.4.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <name>Apache Ozone HDDS RocksDB Tools</name>
    <artifactId>hdds-rocks-native</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.apache.ozone</groupId>
            <artifactId>hdds-test-utils</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.ozone</groupId>
            <artifactId>hdds-managed-rocksdb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-io</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <allow.junit4>false</allow.junit4>

        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.spotbugs</groupId>
                <artifactId>spotbugs-maven-plugin</artifactId>
                <configuration>
                    <excludeFilterFile>${basedir}/dev-support/findbugsExcludeFile.xml</excludeFilterFile>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>cpu-count</id>
            <activation>
                <property>
                    <name>!system.numCores</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>get-cpu-count</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>cpu-count</goal>
                                </goals>
                                <configuration>
                                    <cpuCount>system.numCores</cpuCount>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>rocks_tools_native</id>
            <activation>
                <property>
                    <name>rocks_tools_native</name>
                </property>
            </activation>
            <properties>
                <cmake.standards>20</cmake.standards>
                <sstDump.include>true</sstDump.include>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>set-property</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                                <configuration>
                                    <mainClass>org.apache.hadoop.hdds.utils.db.managed.JniLibNamePropertyWriter</mainClass>
                                    <arguments>
                                        <argument>${project.build.directory}/propertyFile.txt</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>properties-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>read-property-from-file</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>read-project-properties</goal>
                                </goals>
                                <configuration>
                                    <files>
                                        <file>${project.build.directory}/propertyFile.txt</file>
                                    </files>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>unpack-dependency</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>org.rocksdb</groupId>
                                            <artifactId>rocksdbjni</artifactId>
                                            <type>jar</type>
                                            <overWrite>false</overWrite>
                                            <outputDirectory>${project.build.directory}/rocksdbjni</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.googlecode.maven-download-plugin</groupId>
                        <artifactId>download-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>rocksdb source download</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>wget</goal>
                                </goals>
                                <configuration>
                                    <url>https://github.com/facebook/rocksdb/archive/refs/tags/v${rocksdb.version}.tar.gz</url>
                                    <outputFileName>rocksdb-v${rocksdb.version}.tar.gz</outputFileName>
                                    <outputDirectory>${project.build.directory}/rocksdb</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-patch-plugin</artifactId>
                        <version>1.1.1</version>
                        <configuration>
                            <patchFile>${basedir}/src/main/patches/rocks-native.patch</patchFile>
                            <strip>1</strip>
                            <targetDirectory>${project.build.directory}/rocksdb/rocksdb-${rocksdb.version}</targetDirectory>
                        </configuration>
                        <executions>
                            <execution>
                                <id>patch</id>
                                <phase>process-sources</phase>
                                <goals>
                                    <goal>apply</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>${maven-antrun-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>unzip-artifact</id>
                                <phase>generate-sources</phase>
                                <configuration>
                                    <target>
                                        <untar src="${project.build.directory}/rocksdb/rocksdb-v${rocksdb.version}.tar.gz" compression="gzip" dest="${project.build.directory}/rocksdb/" />
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>build-rocksjava</id>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <target>
                                        <exec executable="chmod" failonerror="true">
                                            <arg line="-R"/>
                                            <arg line="775"/>
                                            <arg line="${project.build.directory}/rocksdb/rocksdb-${rocksdb.version}"/>
                                        </exec>
                                        <exec executable="make" dir="${project.build.directory}/rocksdb/rocksdb-${rocksdb.version}" failonerror="true">
                                            <arg line="PORTABLE=1"/>
                                            <arg line="DEBUG_LEVEL=0"/>
                                            <arg line="EXTRA_CXXFLAGS='-fPIC -D_GLIBCXX_USE_CXX11_ABI=0'"/>
                                            <arg line="-j${system.numCores}"/>
                                            <arg line="tools_lib"/>
                                        </exec>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>build-rocks-tools</id>
                                <phase>process-classes</phase>
                                <configuration>
                                    <target>
                                        <mkdir dir="${project.build.directory}/native/rocksdb"/>
                                        <exec executable="cmake" failonerror="true" dir="${project.build.directory}/native/rocksdb">
                                            <env key="CFLAGS" value="-fPIC"/>
                                            <env key="CXXFLAGS" value="-fPIC"/>
                                            <arg line="${basedir}/src"/>
                                            <arg line="-DGENERATED_JAVAH=${project.build.directory}/native/javah"/>
                                            <arg line="-DNATIVE_DIR=${basedir}/src/main/native"/>
                                            <arg line="-DSST_DUMP_INCLUDE=${sstDump.include}"/>
                                            <arg line="-DCMAKE_STANDARDS=${cmake.standards}"/>
                                            <arg line="-DROCKSDB_HEADERS=${project.build.directory}/rocksdb/rocksdb-${rocksdb.version}/include"/>
                                            <arg line="-DROCKSDB_TOOLS_LIB=${project.build.directory}/rocksdb/rocksdb-${rocksdb.version}"/>
                                            <arg line="-DROCKSDB_LIB=${project.build.directory}/rocksdbjni/${rocksdbLibName}"/>
                                        </exec>
                                        <exec executable="make" dir="${project.build.directory}/native/rocksdb" failonerror="true"/><delete dir="${project.build.directory}/rocksdb/rocksdb-${rocksdb.version}"
                            includeemptydirs="true"/>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>copy-lib-file</id>
                                <phase>process-classes</phase>
                                <configuration>
                                    <target>
                                        <copy toDir="${project.build.outputDirectory}">
                                            <fileset dir="${project.build.directory}/native/rocksdb" includes="**/lib*.*" />
                                        </copy>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>${maven-jar-plugin.version}</version>
                        <configuration>
                            <includes>
                                <include>**/*.class</include>
                                <include>**/lib*.dylib</include>
                                <include>**/lib*.so</include>
                                <include>**/lib*.jnilib</include>
                                <include>**/lib*.dll</include>
                            </includes>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <groups>native</groups>
                            <argLine>${maven-surefire-plugin.argLine} @{argLine} -Djava.library.path=${project.build.directory}/native/rocksdb</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>java-8</id>
            <activation>
                <jdk>1.8</jdk>
                <property>
                    <name>rocks_tools_native</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>native-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>compile</phase>
                                <goals>
                                    <goal>javah</goal>
                                </goals>
                                <configuration>
                                    <javahPath>${env.JAVA_HOME}/bin/javah</javahPath>
                                    <javahClassNames>
                                        <javahClassName>org.apache.hadoop.hdds.utils.db.managed.ManagedRawSSTFileReader</javahClassName>
                                        <javahClassName>org.apache.hadoop.hdds.utils.db.managed.ManagedRawSSTFileIterator</javahClassName>
                                    </javahClassNames>
                                    <javahOutputDirectory>${project.build.directory}/native/javah</javahOutputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>java-11</id>
            <activation>
                <jdk>[11,]</jdk>
                <property>
                    <name>rocks_tools_native</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-jars</id>
                                <phase>process-sources</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/dependency</outputDirectory>
                                    <includeScope>runtime</includeScope>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>javach</id>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <phase>compile</phase>
                                <configuration>
                                    <executable>${env.JAVA_HOME}/bin/javac</executable>
                                    <arguments>
                                        <argument>-cp</argument>
                                        <argument>${project.build.outputDirectory}:${project.build.directory}/dependency/*</argument>
                                        <argument>-h</argument>
                                        <argument>${project.build.directory}/native/javah</argument>
                                        <argument>${project.basedir}/src/main/java/org/apache/hadoop/hdds/utils/db/managed/ManagedRawSSTFileReader.java</argument>
                                        <argument>${project.basedir}/src/main/java/org/apache/hadoop/hdds/utils/db/managed/ManagedRawSSTFileReaderIterator.java</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
