<?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.plc4x</groupId>
    <artifactId>plc4x-tools</artifactId>
    <version>0.6.0</version>
  </parent>

  <artifactId>plc4x-tools-thrift</artifactId>
  <packaging>pom</packaging>

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

  <properties>
    <thrift.with.cpp>OFF</thrift.with.cpp>
    <thrift.with.python>OFF</thrift.with.python>
  </properties>

  <profiles>
    <!-- Profile for linux -->
    <profile>
      <id>os-unix</id>
      <activation>
        <os>
          <family>unix</family>
        </os>
      </activation>
      <!-- Make the cmake executable executable (Seems not to be per default) -->
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
              <execution>
                <id>make-cmake-executable</id>
                <phase>process-sources</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <basedir>${cmake.root-dir}</basedir>
                  <executable>chmod</executable>
                  <arguments>
                    <argument>+x</argument>
                    <argument>${cmake.child-dir}</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <!-- Profile for mac -->
    <profile>
      <id>os-mac</id>
      <activation>
        <os>
          <family>mac</family>
        </os>
      </activation>
      <!-- Make the cmake executable executable (Seems not to be per default) -->
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
              <execution>
                <id>make-cmake-executable</id>
                <phase>process-sources</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <basedir>${cmake.root-dir}</basedir>
                  <executable>chmod</executable>
                  <arguments>
                    <argument>+x</argument>
                    <argument>${cmake.child-dir}</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <!-- If C++ is enabled, we need to add another assembly to the build to package the libraries -->
    <profile>
      <id>with-cpp</id>
      <properties>
        <thrift.with.cpp>ON</thrift.with.cpp>
      </properties>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
              <execution>
                <id>package-thrift-cpp</id>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
                <configuration>
                  <finalName>${project.artifactId}-${project.version}</finalName>
                  <descriptors>
                    <descriptor>src/assembly/cpp.xml</descriptor>
                  </descriptors>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <!-- If Python is enabled, we need to add another assembly to the build to package the libraries -->
    <profile>
      <id>with-python</id>
      <properties>
        <thrift.with.python>ON</thrift.with.python>
      </properties>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
              <execution>
                <id>package-thrift-python</id>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
                <configuration>
                  <finalName>${project.artifactId}-${project.version}</finalName>
                  <descriptors>
                    <descriptor>src/assembly/python.xml</descriptor>
                  </descriptors>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

  <build>
    <plugins>
      <plugin>
        <groupId>com.googlecode.maven-download-plugin</groupId>
        <artifactId>download-maven-plugin</artifactId>
        <version>1.4.0</version>
        <executions>
          <!-- Download the CMake binary distribution for this platform. -->
          <execution>
            <id>get-cmake</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>wget</goal>
            </goals>
            <configuration>
              <url>${cmake.url}</url>
              <unpack>true</unpack>
              <outputDirectory>${project.build.directory}</outputDirectory>
              <overwrite>true</overwrite>
            </configuration>
          </execution>
          <!-- Download the sources for building the thrift compiler -->
          <execution>
            <id>get-thrift</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>wget</goal>
            </goals>
            <configuration>
              <url>http://www.apache.org/dyn/closer.lua?action=download&amp;filename=/thrift/${thrift.version}/thrift-${thrift.version}.tar.gz</url>
              <unpack>true</unpack>
              <outputDirectory>${project.build.directory}</outputDirectory>
              <overwrite>true</overwrite>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>unpack-boost</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.apache.plc4x</groupId>
                  <artifactId>plc4x-tools-boost</artifactId>
                  <version>${project.version}</version>
                  <type>zip</type>
                  <classifier>lib-${os.classifier}</classifier>
                  <overWrite>true</overWrite>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${project.build.directory}/boost</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>com.googlecode.cmake-maven-project</groupId>
        <artifactId>cmake-maven-plugin</artifactId>
        <version>3.7.2-b1</version>
        <executions>
          <!-- Uses a CMake generator to generate the build using the build tool of choice -->
          <execution>
            <id>cmake-generate</id>
            <phase>process-sources</phase>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <!--
                We need to use a newer version of cmake, so disable downloading
                and tell the plugin where to find that version.
              -->
              <downloadBinaries>false</downloadBinaries>
              <cmakeRootDir>${cmake.root-dir}</cmakeRootDir>
              <cmakeChildDir>${cmake.child-dir}</cmakeChildDir>

              <sourcePath>${project.build.directory}/thrift-${thrift.version}</sourcePath>
              <targetPath>${project.build.directory}/build</targetPath>
              <generator>${cmake.generator}</generator>
              <!--
                The classifier of the current platform. One of
                [windows-x86_32, windows-x86_64, linux-x86_32, linux-x86_64, linux-arm_32, mac-x86_64].
                It defines the version and type of the cmake installation to download.
              -->
              <classifier>${os.classifier}</classifier>
              <options>
                <!-- Disable testing for now -->
                <option>-DBUILD_TESTING=OFF</option>
                <option>-DBUILD_C_GLIB=OFF</option>
                <option>-DBUILD_CPP=${thrift.with.cpp}</option>
                <!-- Don't build Java, as the libs are available via Maven -->
                <option>-DBUILD_JAVA=OFF</option>
                <option>-DBUILD_PYTHON=${thrift.with.python}</option>
                <option>-DBUILD_HASKELL=OFF</option>
                <!-- Tell CMake where our boost includes and libs are located -->
                <option>-DBOOST_INCLUDEDIR=${project.build.directory}/boost/include</option>
                <option>-DBOOST_LIBRARYDIR=${project.build.directory}/boost/lib</option>
              </options>
            </configuration>
          </execution>
          <!-- Actually executes the build -->
          <execution>
            <id>cmake-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <!--
                We need to use a newer version of cmake, so disable downloading
                and tell the plugin where to find that version.
              -->
              <downloadBinaries>false</downloadBinaries>
              <cmakeRootDir>${cmake.root-dir}</cmakeRootDir>
              <cmakeChildDir>${cmake.child-dir}</cmakeChildDir>

              <!-- The directory where the "generate" step generated the build configuration -->
              <projectDirectory>${project.build.directory}/build</projectDirectory>
              <classifier>${os.classifier}</classifier>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- Add the thrift compiler executable to the build -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <executions>
          <execution>
            <id>source-release-assembly</id>
            <configuration>
              <skip>true</skip>
            </configuration>
          </execution>
          <execution>
            <id>package-thrift-compiler</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <finalName>${project.artifactId}-${project.version}</finalName>
              <descriptors>
                <descriptor>src/assembly/compiler.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <!-- Make sure boost lib is built first -->
  <dependencies>
    <dependency>
      <groupId>org.apache.plc4x</groupId>
      <artifactId>plc4x-tools-boost</artifactId>
      <version>${project.version}</version>
      <type>zip</type>
      <classifier>lib-${os.classifier}</classifier>
      <scope>provided</scope>
    </dependency>
  </dependencies>

</project>