<?xml version="1.0" encoding="UTF-8"?>
<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>cloud.opencode.base</groupId>
        <artifactId>opencode-base-parent</artifactId>
        <version>1.0.4</version>
    </parent>

    <artifactId>opencode-base-image-codec</artifactId>
    <name>OpenCode Base Image Codec</name>
    <description>Pure-Java image codec library (PNG/BMP/GIF) and FFM bindings for native codecs (JPEG/WebP). Zero java.desktop, GraalVM native-image friendly.</description>

    <properties>
        <!-- Native build switches / 原生构建开关 -->
        <!-- Set to true to skip cmake build (e.g. on platforms without cmake). / 设为 true 跳过 cmake 构建（例如未装 cmake 的平台） -->
        <codec.skipNatives>false</codec.skipNatives>
        <!-- Resolved by OS-detection profiles below. Default "unknown" so cmake install fails fast on unsupported platforms. -->
        <codec.platform.token>unknown</codec.platform.token>
        <!-- Plugin version / 插件版本 -->
        <exec-maven-plugin.version>3.4.1</exec-maven-plugin.version>
    </properties>

    <dependencies>
        <!-- Core module / 核心模块 -->
        <dependency>
            <groupId>cloud.opencode.base</groupId>
            <artifactId>opencode-base-core</artifactId>
        </dependency>

        <!-- Test dependencies / 测试依赖 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <scope>test</scope>
        </dependency>

        <!--
            JMH benchmark (test scope only; industry-standard micro-benchmark
            harness; classes live under src/test/java/.../benchmark/ and are
            named *Benchmark.java so Surefire's default *Test pattern skips
            them. Run via -Pbenchmark profile.

            JMH 基准测试（仅 test scope，业界标准微基准框架；类位于
            src/test/java/.../benchmark/，命名为 *Benchmark.java，Surefire
            默认 *Test 模式不会执行。通过 -Pbenchmark profile 运行。
        -->
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--
                Tests rely on the local cmake build product under
                target/native/{platform-token}/. NativeLibraryLoader gates that
                relative-path lookup behind opencode.codec.devMode to prevent
                CWD-spoofing attacks in production; we opt in here so the test
                JVM can resolve the libraries it just built.

                测试依赖本模块 cmake 构建到 target/native/{platform-token}/ 的产物。
                NativeLibraryLoader 用 opencode.codec.devMode 守卫该相对路径回退，
                以防生产环境 CWD 被攻击者控制；此处显式开启以便测试 JVM 能解析
                刚刚编译出的库。
             -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <!--
                    combine.children="append" reserves merge semantics for any
                    future <systemPropertyVariables> the parent POM might add
                    under pluginManagement; the parent currently declares none,
                    so the directive is a no-op today and protective tomorrow.
                    combine.children="append" 为父 POM 未来在 pluginManagement
                    中新增 <systemPropertyVariables> 预留 append 合并语义；
                    当前父 POM 未声明同名属性，此处为前向兼容声明。
                -->
                <configuration>
                    <systemPropertyVariables combine.children="append">
                        <opencode.codec.devMode>true</opencode.codec.devMode>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <!--
                Configure the JMH annotation processor for test sources only.
                Without this the @Benchmark scanner does not generate the
                META-INF/BenchmarkList descriptor that JMH's runner reads.

                仅为 test 源配置 JMH 注解处理器。否则 @Benchmark 扫描器不会生成
                JMH 运行时所读的 META-INF/BenchmarkList 描述符。
             -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-testCompile</id>
                        <configuration>
                            <annotationProcessorPaths>
                                <path>
                                    <groupId>org.openjdk.jmh</groupId>
                                    <artifactId>jmh-generator-annprocess</artifactId>
                                    <version>${jmh.version}</version>
                                </path>
                            </annotationProcessorPaths>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--
                Aggregator cmake invocation. Builds all four native libraries
                (libjpeg-turbo / libwebp / lcms2 / stb_truetype wrapper) into
                target/native/{platform-token}/ during generate-resources.

                聚合 cmake 调用：在 generate-resources 阶段把四个原生库
                一次性编译并安装到 target/native/{platform-token}/。

                Skipped when -Dcodec.skipNatives=true. CMake handles up-to-date
                checks itself, so repeat invocations are cheap.
             -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${exec-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>cmake-configure</id>
                        <phase>generate-resources</phase>
                        <goals><goal>exec</goal></goals>
                        <configuration>
                            <skip>${codec.skipNatives}</skip>
                            <executable>cmake</executable>
                            <arguments>
                                <argument>-S</argument>
                                <argument>${project.basedir}/native</argument>
                                <argument>-B</argument>
                                <argument>${project.build.directory}/aggregator-build</argument>
                                <argument>-DCMAKE_BUILD_TYPE=Release</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>cmake-build</id>
                        <phase>generate-resources</phase>
                        <goals><goal>exec</goal></goals>
                        <configuration>
                            <skip>${codec.skipNatives}</skip>
                            <executable>cmake</executable>
                            <arguments>
                                <argument>--build</argument>
                                <argument>${project.build.directory}/aggregator-build</argument>
                                <argument>--parallel</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>cmake-install</id>
                        <phase>generate-resources</phase>
                        <goals><goal>exec</goal></goals>
                        <configuration>
                            <skip>${codec.skipNatives}</skip>
                            <executable>cmake</executable>
                            <arguments>
                                <argument>--install</argument>
                                <argument>${project.build.directory}/aggregator-build</argument>
                                <argument>--prefix</argument>
                                <argument>${project.build.directory}/native/${codec.platform.token}</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!--
            Platform-detection profiles. Exactly one activates per machine
            and sets ${codec.platform.token} to the matching token used by
            cloud.opencode.base.image.codec.internal.nativelib.Platform.

            平台检测 profile：每台机器恰好激活一个，将 ${codec.platform.token}
            设为与 Platform 枚举一致的 token。
         -->
        <profile>
            <id>platform-linux-x86_64</id>
            <activation>
                <os><family>unix</family><name>linux</name><arch>amd64</arch></os>
            </activation>
            <properties><codec.platform.token>linux-x86_64</codec.platform.token></properties>
        </profile>
        <profile>
            <id>platform-linux-x86_64-x86_64</id>
            <activation>
                <os><family>unix</family><name>linux</name><arch>x86_64</arch></os>
            </activation>
            <properties><codec.platform.token>linux-x86_64</codec.platform.token></properties>
        </profile>
        <profile>
            <id>platform-linux-aarch64</id>
            <activation>
                <os><family>unix</family><name>linux</name><arch>aarch64</arch></os>
            </activation>
            <properties><codec.platform.token>linux-aarch64</codec.platform.token></properties>
        </profile>
        <profile>
            <id>platform-macos-x86_64</id>
            <activation>
                <os><family>mac</family><arch>x86_64</arch></os>
            </activation>
            <properties><codec.platform.token>macos-x86_64</codec.platform.token></properties>
        </profile>
        <profile>
            <id>platform-macos-aarch64</id>
            <activation>
                <os><family>mac</family><arch>aarch64</arch></os>
            </activation>
            <properties><codec.platform.token>macos-aarch64</codec.platform.token></properties>
        </profile>
        <profile>
            <id>platform-windows-x86_64</id>
            <activation>
                <os><family>windows</family><arch>amd64</arch></os>
            </activation>
            <properties><codec.platform.token>windows-x86_64</codec.platform.token></properties>
        </profile>

        <!--
            JMH benchmark profile.
            Runs all *Benchmark classes under src/test/java/ via JMH's main
            entry point on the test classpath. Skipped by default; activate
            with `-Pbenchmark`. Output is written as JSON to
            target/jmh-result.json.

            JMH 基准测试 profile：
            通过 JMH main 入口在 test classpath 上运行 src/test/java 下的所有
            *Benchmark 类。默认不激活；通过 `-Pbenchmark` 启用。结果以 JSON
            形式写入 target/jmh-result.json。
         -->
        <profile>
            <id>benchmark</id>
            <build>
                <plugins>
                    <!--
                        Use exec:exec (not exec:java) so JMH's fork() spawns
                        a real subprocess with the complete test classpath.
                        With exec:java the forked JVM cannot resolve
                        org.openjdk.jmh.runner.ForkedMain.

                        使用 exec:exec（而非 exec:java），让 JMH 的 fork() 能以
                        完整 test classpath 启动子进程；用 exec:java 时子 VM
                        无法找到 org.openjdk.jmh.runner.ForkedMain。
                     -->
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>${exec-maven-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>jmh</id>
                                <phase>integration-test</phase>
                                <goals><goal>exec</goal></goals>
                                <configuration>
                                    <executable>java</executable>
                                    <classpathScope>test</classpathScope>
                                    <arguments>
                                        <argument>--enable-native-access=ALL-UNNAMED</argument>
                                        <argument>-classpath</argument>
                                        <classpath/>
                                        <argument>org.openjdk.jmh.Main</argument>
                                        <argument>.*Benchmark</argument>
                                        <argument>-rf</argument>
                                        <argument>json</argument>
                                        <argument>-rff</argument>
                                        <argument>${project.build.directory}/jmh-result.json</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <!--
            Assemble platform-specific native libraries into a Maven classifier
            JAR named opencode-base-image-codec-{version}-natives-{token}.jar.
            Native libs (libjpeg-turbo / libwebp / lcms2 / stb_truetype) are
            copied from target/native/{token}/ to META-INF/native/{token}/
            inside the classifier JAR, where the native loader at runtime
            looks them up.

            Activate with `-Passemble-natives`. Used by CI per platform when
            staging release artifacts so downstream consumers can opt in via
            classifier:
                <classifier>natives-macos-aarch64</classifier>

            将平台特定的原生库装配为 Maven classifier JAR
            opencode-base-image-codec-{version}-natives-{token}.jar。
            原生库（libjpeg-turbo / libwebp / lcms2 / stb_truetype）从
            target/native/{token}/ 复制到 classifier JAR 内的
            META-INF/native/{token}/ 路径，由运行时原生加载器查找。

            通过 `-Passemble-natives` 启用。CI 按平台分别激活以构建发布产物，
            下游使用方按 classifier 选用：
                <classifier>natives-macos-aarch64</classifier>
         -->
        <profile>
            <id>assemble-natives</id>
            <build>
                <plugins>
                    <!-- 1. 复制原生库到打包目录 / Copy native libs to staging dir -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-natives</id>
                                <phase>prepare-package</phase>
                                <goals><goal>copy-resources</goal></goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/native-jar/META-INF/native/${codec.platform.token}</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${project.build.directory}/native/${codec.platform.token}</directory>
                                            <includes>
                                                <include>*.so</include>
                                                <include>*.so.*</include>
                                                <include>*.dylib</include>
                                                <include>*.dll</include>
                                            </includes>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- 2. 把暂存目录打成 classifier jar / Pack staging dir into classifier jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>natives-classifier</id>
                                <phase>package</phase>
                                <goals><goal>jar</goal></goals>
                                <configuration>
                                    <classifier>natives-${codec.platform.token}</classifier>
                                    <classesDirectory>${project.build.directory}/native-jar</classesDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
