<?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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.apache.ozone</groupId>
    <artifactId>hdds</artifactId>
    <version>2.0.0</version>
  </parent>
  <artifactId>hdds-rocks-native</artifactId>
  <name>Apache Ozone HDDS RocksDB Tools</name>

  <dependencies>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.ozone</groupId>
      <artifactId>hdds-common</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.rocksdb</groupId>
      <artifactId>rocksdbjni</artifactId>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>

    <!-- Test dependencies -->
    <dependency>
      <groupId>org.apache.ozone</groupId>
      <artifactId>hdds-test-utils</artifactId>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>com.github.spotbugs</groupId>
        <artifactId>spotbugs-maven-plugin</artifactId>
        <configuration>
          <excludeFilterFile>${basedir}/dev-support/findbugsExcludeFile.xml</excludeFilterFile>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <proc>none</proc>
        </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>
                <goals>
                  <goal>cpu-count</goal>
                </goals>
                <phase>generate-sources</phase>
                <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>
                <goals>
                  <goal>java</goal>
                </goals>
                <phase>initialize</phase>
                <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>
                <goals>
                  <goal>read-project-properties</goal>
                </goals>
                <phase>initialize</phase>
                <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>
                <goals>
                  <goal>unpack</goal>
                </goals>
                <phase>initialize</phase>
                <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>
                <goals>
                  <goal>wget</goal>
                </goals>
                <phase>generate-sources</phase>
                <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>
            <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>
                <goals>
                  <goal>apply</goal>
                </goals>
                <phase>process-sources</phase>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>unzip-artifact</id>
                <goals>
                  <goal>run</goal>
                </goals>
                <phase>generate-sources</phase>
                <configuration>
                  <target>
                    <untar compression="gzip" dest="${project.build.directory}/rocksdb/" src="${project.build.directory}/rocksdb/rocksdb-v${rocksdb.version}.tar.gz" />
                  </target>
                </configuration>
              </execution>
              <execution>
                <id>build-rocksjava</id>
                <goals>
                  <goal>run</goal>
                </goals>
                <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 dir="${project.build.directory}/rocksdb/rocksdb-${rocksdb.version}" executable="make" 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>
              </execution>
              <execution>
                <id>build-rocks-tools</id>
                <goals>
                  <goal>run</goal>
                </goals>
                <phase>process-classes</phase>
                <configuration>
                  <target>
                    <mkdir dir="${project.build.directory}/native/rocksdb" />
                    <copy file="${project.build.directory}/rocksdbjni/${rocksdbLibName}" tofile="${project.build.directory}/native/rocksdb/${rocksdbLibName}" />
                    <exec dir="${project.build.directory}/native/rocksdb" executable="cmake" failonerror="true">
                      <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}/native/rocksdb/${rocksdbLibName}" />
                    </exec>
                    <exec dir="${project.build.directory}/native/rocksdb" executable="make" failonerror="true" />
                    <delete dir="${project.build.directory}/rocksdb/rocksdb-${rocksdb.version}" includeemptydirs="true" />
                  </target>
                </configuration>
              </execution>
              <execution>
                <id>copy-lib-file</id>
                <goals>
                  <goal>run</goal>
                </goals>
                <phase>process-classes</phase>
                <configuration>
                  <target>
                    <copy toDir="${project.build.outputDirectory}">
                      <fileset dir="${project.build.directory}/native/rocksdb" includes="**/lib*.*" />
                    </copy>
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
              <execution>
                <id>default-compile</id>
                <goals>
                  <goal>compile</goal>
                </goals>
                <phase>compile</phase>
                <configuration>
                  <compilerArgs>
                    <arg>-h</arg>
                    <arg>${project.build.directory}/native/javah</arg>
                  </compilerArgs>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <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>
  </profiles>
</project>
