<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you 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>org.apache</groupId>
        <artifactId>apache</artifactId>
        <version>32</version>
    </parent>

    <groupId>org.apache.iotdb.tools</groupId>
    <artifactId>iotdb-tools-thrift</artifactId>
    <!--
        Setting the base-version to exactly the same as ${thrift.version} as this module
        contains nothing else as the compiled thrift binary without any modification.
        The fourth segment allows us to re-deploy, if we need to change the config.
    -->
    <version>0.23.0.0</version>
    <packaging>pom</packaging>

    <name>IoTDB-Tools: Thrift</name>
    <description>Local build of the Apache Thrift compiler.</description>

    <properties>
        <thrift.version>0.23.0</thrift.version>
        <cmake.generator>Unix Makefiles</cmake.generator>
        <cmake.build.type>Release</cmake.build.type>
        <prebuilt.artifacts.dir>${project.build.directory}/prebuilt-artifacts</prebuilt.artifacts.dir>
    </properties>

    <repositories>
        <repository>
            <id>conatype-oss</id>
            <url>https://oss.sonatype.org/service/local/repositories/releases/content</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>com.googlecode.maven-download-plugin</groupId>
                <artifactId>download-maven-plugin</artifactId>
                <executions>
                    <!-- Download the sources for building the thrift compiler -->
                    <execution>
                        <id>get-thrift</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>https://archive.apache.org/dist/thrift/${thrift.version}/thrift-${thrift.version}.tar.gz</url>
                            <unpack>true</unpack>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.googlecode.cmake-maven-project</groupId>
                <artifactId>cmake-maven-plugin</artifactId>
                <executions>
                    <!-- Uses a CMake generator to generate the build using the build tool of choice -->
                    <execution>
                        <id>cmake-generate</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generator>${cmake.generator}</generator>
                            <sourcePath>${project.build.directory}/thrift-${thrift.version}</sourcePath>
                            <targetPath>${project.build.directory}/build</targetPath>
                            <options>
                                <!-- Disable testing for now -->
                                <option>-DBUILD_TESTING=OFF</option>
                                <!-- The release artifact only packages the compiler binary -->
                                <option>-DBUILD_LIBRARIES=OFF</option>
                                <option>-DBUILD_TUTORIALS=OFF</option>
                                <!-- Enable and disable the building of code generators for different languages -->
                                <option>-DTHRIFT_COMPILER_C_GLIB=OFF</option>
                                <option>-DTHRIFT_COMPILER_CL=OFF</option>
                                <option>-DTHRIFT_COMPILER_CPP=OFF</option>
                                <option>-DTHRIFT_COMPILER_D=OFF</option>
                                <option>-DTHRIFT_COMPILER_DART=OFF</option>
                                <option>-DTHRIFT_COMPILER_DELPHI=OFF</option>
                                <option>-DTHRIFT_COMPILER_ERL=OFF</option>
                                <option>-DTHRIFT_COMPILER_GO=ON</option>
                                <option>-DTHRIFT_COMPILER_GV=OFF</option>
                                <option>-DTHRIFT_COMPILER_HAXE=OFF</option>
                                <option>-DTHRIFT_COMPILER_HTML=OFF</option>
                                <option>-DTHRIFT_COMPILER_JAVA=ON</option>
                                <option>-DTHRIFT_COMPILER_JAVAME=OFF</option>
                                <option>-DTHRIFT_COMPILER_JS=ON</option>
                                <option>-DTHRIFT_COMPILER_JSON=OFF</option>
                                <option>-DTHRIFT_COMPILER_KOTLIN=OFF</option>
                                <option>-DTHRIFT_COMPILER_LUA=OFF</option>
                                <option>-DTHRIFT_COMPILER_MARKDOWN=OFF</option>
                                <option>-DTHRIFT_COMPILER_NETSTD=ON</option>
                                <option>-DTHRIFT_COMPILER_OCAML=OFF</option>
                                <option>-DTHRIFT_COMPILER_PERL=OFF</option>
                                <option>-DTHRIFT_COMPILER_PHP=OFF</option>
                                <option>-DTHRIFT_COMPILER_PY=ON</option>
                                <option>-DTHRIFT_COMPILER_RB=OFF</option>
                                <option>-DTHRIFT_COMPILER_RS=ON</option>
                                <option>-DTHRIFT_COMPILER_ST=OFF</option>
                                <option>-DTHRIFT_COMPILER_SWIFT=OFF</option>
                                <option>-DTHRIFT_COMPILER_XML=OFF</option>
                                <option>-DTHRIFT_COMPILER_XSD=OFF</option>
                            </options>
                        </configuration>
                    </execution>
                    <!-- Actually executes the build -->
                    <execution>
                        <id>cmake-compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <config>${cmake.build.type}</config>
                            <!-- The directory where the "generate" step generated the build configuration -->
                            <projectDirectory>${project.build.directory}/build</projectDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--
                Bundle up the needed parts into an archive for this architecture
                The bundle contains the Thrift executable.
            -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>binary-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <appendAssemblyId>true</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/main/assembly/thrift.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-deploy</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy</goal>
                            <goal>rc-open</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.googlecode.maven-download-plugin</groupId>
                    <artifactId>download-maven-plugin</artifactId>
                    <version>1.7.1</version>
                </plugin>
                <plugin>
                    <groupId>com.googlecode.cmake-maven-project</groupId>
                    <artifactId>cmake-maven-plugin</artifactId>
                    <version>3.29.3-b2</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>3.6.0</version>
                </plugin>
                <plugin>
                    <groupId>org.sonatype.plugins</groupId>
                    <artifactId>nexus-staging-maven-plugin</artifactId>
                    <version>1.6.13</version>
                    <executions>
                        <execution>
                            <id>default-deploy</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>deploy</goal>
                                <goal>rc-open</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <serverId>apache.releases.https</serverId>
                        <nexusUrl>https://repository.apache.org</nexusUrl>
                        <skipStagingRepositoryClose>true</skipStagingRepositoryClose>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>reuse-staging-repository</id>
            <activation>
                <property>
                    <name>stagingRepositoryId</name>
                </property>
            </activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.sonatype.plugins</groupId>
                            <artifactId>nexus-staging-maven-plugin</artifactId>
                            <configuration>
                                <stagingRepositoryId>${stagingRepositoryId}</stagingRepositoryId>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>

        <profile>
            <id>.os-unix</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>Linux</name>
                    <arch>!aarch64</arch>
                </os>
            </activation>
            <properties>
                <os.classifier>linux-x86_64</os.classifier>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.googlecode.cmake-maven-project</groupId>
                        <artifactId>cmake-maven-plugin</artifactId>
                        <executions>
                            <!-- Uses a CMake generator to generate the build using the build tool of choice -->
                            <execution>
                                <id>cmake-generate</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                                <configuration>
                                    <generator>${cmake.generator}</generator>
                                    <sourcePath>${project.build.directory}/thrift-${thrift.version}</sourcePath>
                                    <targetPath>${project.build.directory}/build</targetPath>
                                    <options combine.children="append">
                                        <!-- Tell the linker to build statically linked binaries -->
                                        <option>-DCMAKE_EXE_LINKER_FLAGS=-static</option>
                                        <option>-DZLIB_USE_STATIC_LIBS=ON</option>
                                        <option>-DOPENSSL_USE_STATIC_LIBS=ON</option>
                                    </options>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>.os-unix-arm</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>Linux</name>
                    <arch>aarch64</arch>
                </os>
            </activation>
            <properties>
                <os.classifier>linux-aarch64</os.classifier>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.googlecode.cmake-maven-project</groupId>
                        <artifactId>cmake-maven-plugin</artifactId>
                        <executions>
                            <!-- Uses a CMake generator to generate the build using the build tool of choice -->
                            <execution>
                                <id>cmake-generate</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                                <configuration>
                                    <generator>${cmake.generator}</generator>
                                    <sourcePath>${project.build.directory}/thrift-${thrift.version}</sourcePath>
                                    <targetPath>${project.build.directory}/build</targetPath>
                                    <options combine.children="append">
                                        <!-- Tell the linker to build statically linked binaries -->
                                        <option>-DCMAKE_EXE_LINKER_FLAGS=-static</option>
                                        <option>-DZLIB_USE_STATIC_LIBS=ON</option>
                                        <option>-DOPENSSL_USE_STATIC_LIBS=ON</option>
                                    </options>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>.os-mac</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>!aarch64</arch>
                </os>
            </activation>
            <properties>
                <os.classifier>mac-x86_64</os.classifier>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.googlecode.cmake-maven-project</groupId>
                        <artifactId>cmake-maven-plugin</artifactId>
                        <executions>
                            <!-- Uses a CMake generator to generate the build using the build tool of choice -->
                            <execution>
                                <id>cmake-generate</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                                <configuration>
                                    <generator>${cmake.generator}</generator>
                                    <sourcePath>${project.build.directory}/thrift-${thrift.version}</sourcePath>
                                    <targetPath>${project.build.directory}/build</targetPath>
                                    <options combine.children="append">
                                        <!-- Specify new version Bison path for Mac-OS, as the default version is too old -->
                                        <option>-DBISON_EXECUTABLE=/usr/local/opt/bison/bin/bison</option>
                                    </options>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>.os-mac-arm</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>aarch64</arch>
                </os>
            </activation>
            <properties>
                <os.classifier>mac-aarch64</os.classifier>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.googlecode.cmake-maven-project</groupId>
                        <artifactId>cmake-maven-plugin</artifactId>
                        <executions>
                            <!-- Uses a CMake generator to generate the build using the build tool of choice -->
                            <execution>
                                <id>cmake-generate</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                                <configuration>
                                    <generator>${cmake.generator}</generator>
                                    <sourcePath>${project.build.directory}/thrift-${thrift.version}</sourcePath>
                                    <targetPath>${project.build.directory}/build</targetPath>
                                    <options combine.children="append">
                                        <!-- Specify new version Bison path for Mac-OS, as the default version is too old -->
                                        <option>-DBISON_EXECUTABLE=/opt/homebrew/opt/bison/bin/bison</option>
                                    </options>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>.os-windows</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>!aarch64</arch>
                </os>
            </activation>
            <properties>
                <os.classifier>windows-x86_64</os.classifier>
                <!-- We're using Mingw everywhere else, but it seems boost is not supported for that -->
                <cmake.generator>Visual Studio 17 2022</cmake.generator>
            </properties>
        </profile>

        <profile>
            <id>.os-windows-arm</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>aarch64</arch>
                </os>
            </activation>
            <properties>
                <os.classifier>windows-aarch64</os.classifier>
                <!-- We're using Mingw everywhere else, but it seems boost is not supported for that -->
                <cmake.generator>Visual Studio 17 2022</cmake.generator>
            </properties>
        </profile>

        <profile>
            <id>prebuilt-artifacts</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.googlecode.maven-download-plugin</groupId>
                        <artifactId>download-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>get-thrift</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.googlecode.cmake-maven-project</groupId>
                        <artifactId>cmake-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>cmake-generate</id>
                                <phase>none</phase>
                            </execution>
                            <execution>
                                <id>cmake-compile</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>binary-assembly</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>attach-prebuilt-artifacts</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>attach-artifact</goal>
                                </goals>
                                <configuration>
                                    <artifacts>
                                        <artifact>
                                            <file>${prebuilt.artifacts.dir}/${project.artifactId}-${project.version}-linux-x86_64.zip</file>
                                            <type>zip</type>
                                            <classifier>linux-x86_64</classifier>
                                        </artifact>
                                        <artifact>
                                            <file>${prebuilt.artifacts.dir}/${project.artifactId}-${project.version}-linux-aarch64.zip</file>
                                            <type>zip</type>
                                            <classifier>linux-aarch64</classifier>
                                        </artifact>
                                        <artifact>
                                            <file>${prebuilt.artifacts.dir}/${project.artifactId}-${project.version}-mac-x86_64.zip</file>
                                            <type>zip</type>
                                            <classifier>mac-x86_64</classifier>
                                        </artifact>
                                        <artifact>
                                            <file>${prebuilt.artifacts.dir}/${project.artifactId}-${project.version}-mac-aarch64.zip</file>
                                            <type>zip</type>
                                            <classifier>mac-aarch64</classifier>
                                        </artifact>
                                        <artifact>
                                            <file>${prebuilt.artifacts.dir}/${project.artifactId}-${project.version}-windows-x86_64.zip</file>
                                            <type>zip</type>
                                            <classifier>windows-x86_64</classifier>
                                        </artifact>
                                        <artifact>
                                            <file>${prebuilt.artifacts.dir}/${project.artifactId}-${project.version}-windows-aarch64.zip</file>
                                            <type>zip</type>
                                            <classifier>windows-aarch64</classifier>
                                        </artifact>
                                    </artifacts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
