<!--

    Copyright © 2015 The Gravitee team (http://gravitee.io)

    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>io.gravitee</groupId>
        <artifactId>gravitee-parent</artifactId>
        <version>25.0.0</version>
    </parent>

    <groupId>io.gravitee.llama.cpp</groupId>
    <artifactId>llamaj.cpp</artifactId>
    <version>1.2.1</version>
    <packaging>jar</packaging>

    <name>Gravitee.io - llamaj.cpp</name>
    <url>https://github.com/gravitee-io/llamaj.cpp</url>
    <description>A Java and JVM port of llama.cpp using jextract, enabling local large language model (LLM) inference through native foreign function and memory API interop. Natively supports macOS M-series and Linux x86_64 with GPU acceleration. Platform and hardware support (Windows, ARM, CUDA, etc.) can be extended through custom builds.</description>

    <scm>
        <url>https://github.com/gravitee-io/llamaj.cpp</url>
        <connection>scm:git:git://github.com/gravitee-io/llamaj.cpp</connection>
        <developerConnection>scm:git:git@github.com:gravitee-io/llamaj.cpp</developerConnection>
    </scm>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>25</maven.compiler.release>
        <junit-jupiter-api.version>6.0.3</junit-jupiter-api.version>
        <assertj-core.version>3.27.7</assertj-core.version>
        <llama.cpp.version>b9673</llama.cpp.version>
        <llama.cpp.dir>${project.basedir}/../llama.cpp</llama.cpp.dir>
        <jextract.dir>${project.basedir}/.jextract/bin</jextract.dir>
    </properties>

    <profiles>
        <profile>
            <id>linux-x86_64</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>3.6.3</version>
                        <executions>
                            <!-- Download jextract before generating sources for Linux -->
                            <execution>
                                <id>download-jextract-linux</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>scripts/download-jextract.sh</executable>
                                    <arguments>
                                        <argument>-o</argument>
                                        <argument>linux</argument>
                                        <argument>-p</argument>
                                        <argument>x86_64</argument>
                                        <argument>-d</argument>
                                        <argument>${project.basedir}/.jextract</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>download-llama.cpp-shared-objects</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>scripts/download-native-libraries.sh</executable>
                                    <arguments>
                                        <argument>-o</argument>
                                        <argument>linux</argument>
                                        <argument>-p</argument>
                                        <argument>x86_64</argument>
                                        <argument>-v</argument>
                                        <argument>${llama.cpp.version}</argument>
                                        <argument>-d</argument>
                                        <argument>${project.basedir}/src/main/resources</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>generate-llama.cpp-sources-linux</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>${project.basedir}/.jextract/bin/jextract</executable>
                                    <arguments>
                                        <argument>-t</argument>
                                        <argument>io.gravitee.llama.cpp.linux.x86_64</argument>
                                        <argument>--include-dir</argument>
                                        <argument>${llama.cpp.dir}/ggml/include</argument>
                                        <argument>--include-dir</argument>
                                        <argument>${llama.cpp.dir}/include</argument>
                                        <argument>--output</argument>
                                        <argument>${project.build.directory}/generated-sources</argument>
                                        <argument>--header-class-name</argument>
                                        <argument>llama_h</argument>
                                        <argument>${llama.cpp.dir}/tools/mtmd/mtmd.h</argument>
                                        <argument>${llama.cpp.dir}/tools/mtmd/mtmd-helper.h</argument>
                                        <argument>${llama.cpp.dir}/include/llama.h</argument>
                                        <argument>${llama.cpp.dir}/ggml/include/ggml-rpc.h</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.google.code.maven-replacer-plugin</groupId>
                        <artifactId>replacer</artifactId>
                        <version>1.5.3</version>
                        <executions>
                            <execution>
                                <id>fix-ggml-layout-linux</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>replace</goal>
                                </goals>
                                <configuration>
                                    <file>${project.build.directory}/generated-sources/io/gravitee/llama/cpp/linux/x86_64/llama_h.java</file>
                                    <replacements>
                                        <replacement>
                                            <token>ggml_backend_graph_copy\.layout\(\)</token>
                                            <value>io.gravitee.llama.cpp.linux.x86_64.ggml_backend_graph_copy.layout()</value>
                                        </replacement>
                                    </replacements>
                                </configuration>
                            </execution>
                            <execution>
                                <id>fix-visibility-llama_h_1-linux</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>replace</goal>
                                </goals>
                                <configuration>
                                    <file>${project.build.directory}/generated-sources/io/gravitee/llama/cpp/linux/x86_64/llama_h_1.java</file>
                                    <replacements>
                                        <replacement>
                                            <token>^class llama_h_1 extends</token>
                                            <value>public class llama_h_1 extends</value>
                                        </replacement>
                                    </replacements>
                                    <regex>true</regex>
                                    <regexFlags>MULTILINE</regexFlags>
                                    <quiet>true</quiet>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>macosx-aarch64</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>3.6.3</version>
                        <executions>
                            <!-- Download jextract before generating sources for macOS -->
                            <execution>
                                <id>download-jextract-macos</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>scripts/download-jextract.sh</executable>
                                    <arguments>
                                        <argument>-o</argument>
                                        <argument>macosx</argument>
                                        <argument>-p</argument>
                                        <argument>aarch64</argument>
                                        <argument>-d</argument>
                                        <argument>${project.basedir}/.jextract</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>download-llama.cpp-dylibs</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>scripts/download-native-libraries.sh</executable>
                                    <arguments>
                                        <argument>-o</argument>
                                        <argument>macosx</argument>
                                        <argument>-p</argument>
                                        <argument>aarch64</argument>
                                        <argument>-v</argument>
                                        <argument>${llama.cpp.version}</argument>
                                        <argument>-d</argument>
                                        <argument>${project.basedir}/src/main/resources</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>generate-llama.cpp-sources-macosx</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>${project.basedir}/.jextract/bin/jextract</executable>
                                    <arguments>
                                        <argument>-t</argument>
                                        <argument>io.gravitee.llama.cpp.macosx.aarch64</argument>
                                        <argument>--include-dir</argument>
                                        <argument>${llama.cpp.dir}/ggml/include</argument>
                                        <argument>--include-dir</argument>
                                        <argument>${llama.cpp.dir}/include</argument>
                                        <argument>--output</argument>
                                        <argument>${project.build.directory}/generated-sources</argument>
                                        <argument>--header-class-name</argument>
                                        <argument>llama_h</argument>
                                        <argument>${llama.cpp.dir}/tools/mtmd/mtmd.h</argument>
                                        <argument>${llama.cpp.dir}/tools/mtmd/mtmd-helper.h</argument>
                                        <argument>${llama.cpp.dir}/include/llama.h</argument>
                                        <argument>${llama.cpp.dir}/ggml/include/ggml-rpc.h</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.google.code.maven-replacer-plugin</groupId>
                        <artifactId>replacer</artifactId>
                        <version>1.5.3</version>
                        <executions>
                            <execution>
                                <id>fix-ggml-layout-macos</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>replace</goal>
                                </goals>
                                <configuration>
                                    <file>${project.build.directory}/generated-sources/io/gravitee/llama/cpp/macosx/aarch64/llama_h_1.java</file>
                                    <replacements>
                                        <replacement>
                                            <token>ggml_backend_graph_copy\.layout\(\)</token>
                                            <value>io.gravitee.llama.cpp.macosx.aarch64.ggml_backend_graph_copy.layout()</value>
                                        </replacement>
                                    </replacements>
                                </configuration>
                            </execution>
                            <execution>
                                <id>fix-visibility-llama_h_1-macos</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>replace</goal>
                                </goals>
                                <configuration>
                                    <file>${project.build.directory}/generated-sources/io/gravitee/llama/cpp/macosx/aarch64/llama_h_1.java</file>
                                    <replacements>
                                        <replacement>
                                            <token>^class llama_h_1 extends</token>
                                            <value>public class llama_h_1 extends</value>
                                        </replacement>
                                    </replacements>
                                    <regex>true</regex>
                                    <regexFlags>MULTILINE</regexFlags>
                                </configuration>
                            </execution>
                            <execution>
                                <id>fix-visibility-llama_h_2-macos</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>replace</goal>
                                </goals>
                                <configuration>
                                    <file>${project.build.directory}/generated-sources/io/gravitee/llama/cpp/macosx/aarch64/llama_h_2.java</file>
                                    <replacements>
                                        <replacement>
                                            <token>^class llama_h_2 extends</token>
                                            <value>public class llama_h_2 extends</value>
                                        </replacement>
                                    </replacements>
                                    <regex>true</regex>
                                    <regexFlags>MULTILINE</regexFlags>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit-jupiter-api.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-jupiter-api.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter-api.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit-jupiter-api.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>${assertj-core.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.mycila</groupId>
                <artifactId>license-maven-plugin</artifactId>
                <configuration>
                    <properties>
                        <owner>The Gravitee team</owner>
                        <email>http://gravitee.io</email>
                    </properties>
                    <licenseSets>
                        <licenseSet>
                            <header>com/mycila/maven/plugin/license/templates/APACHE-2.txt</header>
                            <excludes>
                                <exclude>CONTRIBUTING.adoc</exclude>
                                <exclude>LICENSE.txt</exclude>
                                <exclude>.circleci/config.yml</exclude>
                                <exclude>README.md</exclude>
                                <exclude>SECURITY.md</exclude>
                                <exclude>.gravitee.settings.xml</exclude>
                                <exclude>.jextract/**</exclude>
                            </excludes>
                        </licenseSet>
                    </licenseSets>
                    <mapping>
                        <ts>SLASHSTAR_STYLE</ts>
                        <js>SLASHSTAR_STYLE</js>
                    </mapping>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>25</source>
                    <target>25</target>
                    <compilerArgs>
                        <arg>--enable-preview</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>io.gravitee.llama.cpp.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.5.5</version>
                <configuration>
                    <argLine>--enable-native-access=ALL-UNNAMED</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <source>25</source>
                    <additionalOptions>--enable-preview</additionalOptions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.6.1</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated-sources/</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!--
                  Inherited from gravitee-parent. ArchUnit 1.2.1 bundles a
                  shaded ASM that cannot parse Java 25 (class-file major 69)
                  bytecode, so scanning the jextract-generated bindings spams
                  "Unsupported class file major version 69". The rule only logs
                  (it never fails the build) and cannot read the generated FFM
                  bindings anyway, so skip it until the parent ships an
                  ArchUnit new enough for class v69.
                -->
                <groupId>io.gravitee.maven</groupId>
                <artifactId>gravitee-archrules-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
