<?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>dev.onvoid.webrtc</groupId>
		<artifactId>webrtc-java-parent</artifactId>
		<version>0.20.0</version>
	</parent>

	<artifactId>webrtc-java</artifactId>

	<name>webrtc-java</name>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<!-- add-exports: dev.onvoid.webrtc.internal is not exported to
					     applications, but the tests cover NativeApi, which native
					     extension modules use to reach the native sources. -->
					<argLine>
						--add-exports webrtc.java/dev.onvoid.webrtc.internal=ALL-UNNAMED
						--add-opens webrtc.java/dev.onvoid.webrtc=ALL-UNNAMED
						--add-opens webrtc.java/dev.onvoid.webrtc.logging=ALL-UNNAMED
						--add-opens webrtc.java/dev.onvoid.webrtc.media=ALL-UNNAMED
						--add-opens webrtc.java/dev.onvoid.webrtc.media.audio=ALL-UNNAMED
						--add-opens webrtc.java/dev.onvoid.webrtc.media.video=ALL-UNNAMED
					</argLine>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<profiles>
		<!--
			Opt-in JNI reference-discipline check: runs the same tests under
			-Xcheck:jni, which validates every JNI call's local/global refs and
			aborts the JVM with "FATAL ERROR in native method: ..." on a real
			violation (e.g. a double-freed or already-deleted local ref):
			exactly the class of bug JNI_*.cpp changes are prone to introducing.
			Not part of the default test run: -Xcheck:jni also emits a
			"WARNING in native method: JNI call made without checking
			exceptions" for every native call in this codebase that doesn't
			immediately check for a pending exception (thousands of them,
			pre-existing, harmless on their own), which would drown out
			everything else if left on by default.

			The TCP forkNode is required, not optional: -Xcheck:jni writes its
			diagnostics straight to the process's native stdout, which is the
			same stream Surefire's default (pipe-based) fork communication
			uses for its own protocol; without switching to TCP, that
			corrupts the channel and the run fails with "Corrupted channel by
			directly writing to native stream" instead of reporting test
			results.

			Run with: mvn -pl webrtc test -Pjni-check
			Then check for real bugs with: grep "FATAL ERROR" -r target/surefire-reports
		-->
		<profile>
			<id>jni-check</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-surefire-plugin</artifactId>
						<configuration>
							<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory" />
							<argLine>
								-Xcheck:jni
								--add-exports webrtc.java/dev.onvoid.webrtc.internal=ALL-UNNAMED
								--add-opens webrtc.java/dev.onvoid.webrtc=ALL-UNNAMED
								--add-opens webrtc.java/dev.onvoid.webrtc.logging=ALL-UNNAMED
								--add-opens webrtc.java/dev.onvoid.webrtc.media=ALL-UNNAMED
								--add-opens webrtc.java/dev.onvoid.webrtc.media.audio=ALL-UNNAMED
								--add-opens webrtc.java/dev.onvoid.webrtc.media.video=ALL-UNNAMED
							</argLine>
						</configuration>
					</plugin>
				</plugins>
			</build>
		</profile>

		<!--
			Default: attach the native library jar that webrtc-jni has just built for
			the host platform. Active unless "natives.dir" is set, so local builds and
			the per-platform CI builds behave as they always have.
		-->
		<profile>
			<id>attach-native-host</id>
			<activation>
				<property>
					<name>!natives.dir</name>
				</property>
			</activation>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-antrun-plugin</artifactId>
						<executions>
							<execution>
								<id>attach-artifact</id>
								<phase>package</phase>
								<goals>
									<goal>run</goal>
								</goals>
								<configuration>
									<target>
										<attachartifact file="../webrtc-jni/target/${project.name}-${project.version}-${platform.classifier}.jar" classifier="${platform.classifier}" />
									</target>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>

		<!--
			Release: attach the native library jars of all supported platforms, which
			the release workflow has collected into "natives.dir" from the per-platform
			build jobs. Maven Central publishes a version as one atomic bundle, hence
			all classifiers must be attached to a single deployment.

			Run with: mvn deploy -pl .,webrtc,webrtc-java-media -Prelease -Dnatives.dir=/abs/path
		-->
		<profile>
			<id>attach-natives-all</id>
			<activation>
				<property>
					<name>natives.dir</name>
				</property>
			</activation>
			<build>
				<plugins>
					<plugin>
						<groupId>org.codehaus.mojo</groupId>
						<artifactId>build-helper-maven-plugin</artifactId>
						<executions>
							<execution>
								<id>attach-natives</id>
								<phase>package</phase>
								<goals>
									<goal>attach-artifact</goal>
								</goals>
								<configuration>
									<artifacts>
										<artifact>
											<file>${natives.dir}/${project.name}-${project.version}-windows-x86_64.jar</file>
											<type>jar</type>
											<classifier>windows-x86_64</classifier>
										</artifact>
										<artifact>
											<file>${natives.dir}/${project.name}-${project.version}-windows-aarch64.jar</file>
											<type>jar</type>
											<classifier>windows-aarch64</classifier>
										</artifact>
										<artifact>
											<file>${natives.dir}/${project.name}-${project.version}-linux-x86_64.jar</file>
											<type>jar</type>
											<classifier>linux-x86_64</classifier>
										</artifact>
										<artifact>
											<file>${natives.dir}/${project.name}-${project.version}-linux-aarch64.jar</file>
											<type>jar</type>
											<classifier>linux-aarch64</classifier>
										</artifact>
										<artifact>
											<file>${natives.dir}/${project.name}-${project.version}-linux-aarch32.jar</file>
											<type>jar</type>
											<classifier>linux-aarch32</classifier>
										</artifact>
										<artifact>
											<file>${natives.dir}/${project.name}-${project.version}-macos-x86_64.jar</file>
											<type>jar</type>
											<classifier>macos-x86_64</classifier>
										</artifact>
										<artifact>
											<file>${natives.dir}/${project.name}-${project.version}-macos-aarch64.jar</file>
											<type>jar</type>
											<classifier>macos-aarch64</classifier>
										</artifact>
									</artifacts>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>

	<dependencies>
		<dependency>
			<groupId>${project.groupId}</groupId>
			<artifactId>${project.artifactId}</artifactId>
			<version>${project.version}</version>
			<classifier>${platform.classifier}</classifier>
		</dependency>
	</dependencies>
</project>
