1#! /bin/bash -e
2# -----------------------------------------------------------------------------
3# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
4#
5# CppAD is distributed under the terms of the
6#              Eclipse Public License Version 2.0.
7#
8# This Source Code may also be made available under the following
9# Secondary License when the conditions for such availability set forth
10# in the Eclipse Public License, Version 2.0 are satisfied:
11#       GNU General Public License, Version 2.0 or later.
12# -----------------------------------------------------------------------------
13# $begin get_cppadcg.sh$$ $newlinech #$$
14# $spell
15#   gz
16#   CppAD
17#   cppadcg
18#   Eigen
19#   doxygen
20#   html
21# $$
22#
23# $section Download and Install CppADCodeGen in Build Directory$$
24#
25# $head Syntax$$
26# $code bin/get_cppadcg.sh$$
27#
28# $head Purpose$$
29# If you are using Unix, this command will download and install
30# $href%https://github.com/joaoleal/CppADCodeGen%cppadcg%$$
31# in the CppAD $code build$$ directory.
32#
33# $head Requirements$$
34# You must first use $cref get_eigen.sh$$ to download and install Eigen.
35#
36# $head Distribution Directory$$
37# This command must be executed in the
38# $cref/distribution directory/download/Distribution Directory/$$.
39#
40# $head Source Directory$$
41# The Cppadcg source code is downloaded into the sub-directory
42# $code external/cppadcg.git$$ below the distribution directory.
43#
44# $head Prefix$$
45# The $cref/prefix/get_optional.sh/prefix/$$
46# in the file $code bin/get_optional.sh$$ is used for this install.
47#
48# $head Git Hash$$
49# This will install the commit of Cppadcg with the following git hash
50# $srccode%sh%
51git_hash='b5307ad'
52# %$$
53# The date corresponding to this commit was 20201009.
54#
55# $head Configuration$$
56# If the file
57# $codei%
58#   external/cppadcg-%git_hash%.configured
59# %$$
60# exists, the configuration will be skipped.
61# Delete this file if you want to re-run the configuration.
62#
63# $head Documentation$$
64# If you change the setting for $code CREATE_DOXYGEN_DOC$$ to $code ON$$,
65# the doxygen documentation for CppADCodeGen will be installed in the directory
66# $codei%
67#   %prefix%/share/doc/cppadcg/html
68# %$$
69# where $icode prefix$$ has the value specified in the
70# $cref/get_optional.sh/get_optional.sh/prefix/$$ file.
71#
72# $end
73# -----------------------------------------------------------------------------
74package='cppadcg'
75if [ $0 != "bin/get_$package.sh" ]
76then
77    echo "bin/get_$package.sh: must be executed from its parent directory"
78    exit 1
79fi
80# -----------------------------------------------------------------------------
81# bash function that echos and executes a command
82echo_eval() {
83    echo $*
84    eval $*
85}
86# -----------------------------------------------------------------------------
87web_page='https://github.com/joaoleal/CppADCodeGen.git'
88cppad_repo=$(pwd)
89# -----------------------------------------------------------------------------
90# prefix
91eval `grep '^prefix=' bin/get_optional.sh`
92if [[ "$prefix" =~ ^[^/] ]]
93then
94    prefix="$cppad_repo/$prefix"
95fi
96echo "prefix=$prefix"
97# -----------------------------------------------------------------------------
98configured_flag="external/$package-${git_hash}.configured"
99echo "Executing get_$package.sh"
100if [ -e "$configured_flag" ]
101then
102    echo "Skipping configuration because $configured_flag exits"
103    echo_eval cd external/$package.git/build
104    echo_eval make install
105    echo "get_$package.sh: OK"
106    exit 0
107fi
108# -----------------------------------------------------------------------------
109# Create build/cppad_lib/libcppad_lib.* to aid in cppadcg install
110if [ ! -d build ]
111then
112    echo_eval mkdir build
113fi
114echo_eval cd build
115if [ -e CMakeCache.txt ]
116then
117    echo_eval rm CMakeCache.txt
118fi
119echo_eval cmake ..
120echo_eval make
121echo_eval cd ..
122# -----------------------------------------------------------------------------
123# Change into external
124if [ ! -d external ]
125then
126    echo_eval mkdir external
127fi
128echo_eval cd external
129# -----------------------------------------------------------------------------
130# cppadcg.git
131if [ ! -e $package.git ]
132then
133    echo_eval git clone $web_page $package.git
134fi
135echo_eval cd $package.git
136# -----------------------------------------------------------------------------
137# 2DO: get following code into CppADCodeGen
138# Must modify FindCppAD.cmake so can used git repository
139# version of CppAD (not yet installed).
140cat << EOF > get_cppadcg.sed
141s|IF *( *DEFINED *CPPAD_HOME *)|IF (DEFINED CPPAD_GIT_REPO)\\
142    # This setting is used for testing before installing CppAD.\\
143    # CPPAD_GIT_REPO is the a CppAD git repository. It is assumed that\\
144    # cmake and make have been executed in CPPAD_GIT_REPO/build.\\
145    SET(CPPAD_INCLUDE_DIR "\${CPPAD_GIT_REPO}/include" )\\
146    SET(CPPAD_LIBRARIES\\
147        "\${CPPAD_GIT_REPO}/build/cppad_lib"\\
148    )\\
149    INCLUDE_DIRECTORIES(\\
150        "\${CPPAD_INCLUDE_DIR}"\\
151    )\\
152    #\\
153    IF( NOT EXISTS "\${CPPAD_INCLUDE_DIR}/cppad/cppad.hpp" )\\
154        MESSAGE(FATAL_ERROR\\
155            "Cannot find CPPAD_GIT_REPO/include/cppad/cppad.hpp"\\
156        )\\
157    ENDIF()\\
158    IF( NOT EXISTS "\${CPPAD_INCLUDE_DIR}/cppad/configure.hpp" )\\
159        MESSAGE(FATAL_ERROR\\
160            "Cannot find CPPAD_GIT_REPO/include/cppad/configure.hpp"\\
161        )\\
162    ENDIF()\\
163    #\\
164    FIND_LIBRARY( CPPAD_LIB_PATH\\
165        cppad_lib\\
166        PATHS \${CPPAD_LIBRARIES}\\
167        NO_DEFAULT_PATH\\
168    )\\
169    IF( NOT CPPAD_LIB_PATH  )\\
170        MESSAGE(FATAL_ERROR\\
171            "Cannot find \${library} library below CPPAD_GIT_REPO="\\
172            "{CPPAD_GIT_REPO}"\\
173        )\\
174    ENDIF()\\
175    #\\
176    SET(CPPAD_FOUND TRUE)\\
177\\
178ELSEIF (DEFINED CPPAD_HOME)|
179EOF
180echo_eval git checkout  cmake/FindCppAD.cmake
181echo_eval sed -i cmake/FindCppAD.cmake -f get_cppadcg.sed
182echo_eval rm get_cppadcg.sed
183# -----------------------------------------------------------------------------
184#  make install
185echo_eval git checkout --quiet $git_hash
186if [ ! -e build ]
187then
188    echo_eval mkdir build
189fi
190echo_eval cd build
191echo_eval cmake \
192    -D CPPAD_GIT_REPO="$cppad_repo" \
193    -D CMAKE_INSTALL_PREFIX=$prefix \
194    -D EIGNE_INCLUDE_DIR=$prefix/include \
195    -D GOOGLETEST_GIT=ON \
196    -D CREATE_DOXYGEN_DOC=OFF \
197    ..
198echo_eval make install
199# -----------------------------------------------------------------------------
200echo_eval touch $cppad_repo/$configured_flag
201echo "get_$package.sh: OK"
202