1#!/bin/sh
2
3# $Id: create_flat_makefile.sh 633243 2021-06-15 18:16:21Z ivanov $
4# Author:  Andrei Gourianov, NCBI (gouriano@ncbi.nlm.nih.gov)
5
6#-----------------------------------------------------------------------------
7#set -xv
8#set -x
9
10# defaults
11solution="Makefile.flat"
12logfile="Flat.configuration_log"
13relroot="/net/snowman/vol/export2/win-coremake/App/Ncbi/cppcore"
14if [ \! -d $relroot -a -d /Volumes/win-coremake ]; then
15    relroot=/Volumes/win-coremake/App/Ncbi/cppcore
16fi
17
18ptbname="project_tree_builder"
19# release path to project_tree_builder
20relptbpath="$relroot/ptb/"
21# release path to datatool
22reldatatoolpath="$relroot/datatool/"
23# Configuration GUI
24ptbgui="src/build-system/project_tree_builder_gui/bin/ptbgui.jar"
25
26# dependencies
27ptbdep="corelib util util/regexp util/xregexp build-system/project_tree_builder"
28#-----------------------------------------------------------------------------
29
30initial_dir=`pwd`
31script_name=`basename $0`
32script_dir=`dirname $0`
33script_dir=`(cd "${script_dir}" ; pwd)`
34
35. ${script_dir}/../common.sh
36
37
38# has one optional argument: error message
39Usage()
40{
41    cat <<EOF 1>&2
42USAGE: $script_name BuildDir [-s SrcDir] [-p ProjectList] [-b] [-remoteptb] [-cfg] [-saved SavedCfg]
43SYNOPSIS:
44 Create flat makefile for a given build tree.
45ARGUMENTS:
46  BuildDir   -- mandatory. Root dir of the build tree (eg ~/c++/GCC340-Debug)
47  -s         -- optional.  Root dir of the source tree (eg ~/c++)
48  -p         -- optional.  List of projects: subtree of the source tree, or LST file
49  -b         -- optional.  Build project_tree_builder locally
50  -remoteptb -- optional.  Use prebuilt project_tree_builder only; do not attempt to build it locally
51  -cfg       -- optional.  Use Configuration GUI application.
52  -saved     -- optional.  Use saved configuration settings from SavedCfg file
53EOF
54    test -z "$1"  ||  echo ERROR: $1 1>&2
55    exit 1
56}
57
58#-----------------------------------------------------------------------------
59# analyze script arguments
60
61test $# -lt 1 && Usage "Mandatory argument is missing"
62COMMON_Exec cd $initial_dir
63COMMON_Exec cd $1
64a1=`pwd`
65builddir="$a1/build"
66srcdir="$a1/.."
67projectlist="src"
68buildptb="no"
69remoteptbonly="no"
70req_gui_cfg="no"
71savedcfg=""
72PLATFORM=`COMMON_DetectPlatform`
73shift
74
75dest=""
76for cmd_arg in "$@"; do
77  case "$dest" in
78    src  )  dest="";  srcdir="$cmd_arg"     ;  continue ;;
79    prj  )  dest="";  projectlist="$cmd_arg";  continue ;;
80    cfg  )  dest="";  savedcfg="$cmd_arg";     continue ;;
81    *    )  dest=""                                     ;;
82  esac
83  case "$cmd_arg" in
84    -s )  dest="src" ;;
85    -p )  dest="prj" ;;
86    -b )  dest="";    buildptb="yes" ;;
87    -remoteptb )  dest="";    remoteptbonly="yes" ;;
88    -cfg       )  dest="";    req_gui_cfg="yes" ;;
89    -saved     )  dest="cfg" ;;
90    *  )  Usage "Invalid command line argument:  $cmd_arg"
91  esac
92done
93
94test -d "$builddir"  || Usage "$builddir is not a directory"
95test -d "$srcdir"    || Usage "$srcdir is not a directory"
96case "$projectlist" in
97  /* ) abs_projectlist=$projectlist ;;
98  *  ) abs_projectlist=$srcdir/$projectlist ;;
99esac
100if test ! -f "$abs_projectlist"; then
101  test -d "$abs_projectlist" || Usage "$abs_projectlist not found"
102fi
103if test -n "$savedcfg"; then
104  if test ! -f "$savedcfg"; then
105    if test -f "$initial_dir/$savedcfg"; then
106      savedcfg="$initial_dir/$savedcfg"
107    else
108      Usage "$savedcfg not found"
109    fi
110  fi
111fi
112
113#-----------------------------------------------------------------------------
114# get required version of PTB
115ptbreqver=""
116ptbver="$srcdir/src/build-system/ptb_version.txt"
117if test -r "$ptbver"; then
118  ptbreqver=`cat "$ptbver" | sed -e 's/ //'`
119  if test "$ptbreqver" = 2.5.0 -a -f $a1/status/DLL_BUILD.enabled; then
120    echo "Forcing use of local project_tree builder."
121    buildptb=yes
122  fi
123fi
124
125#-----------------------------------------------------------------------------
126# find PTB
127if test $buildptb = "no"; then
128  if test "$PREBUILT_PTB_EXE" = "bootstrap"; then
129    ptb="$builddir/build-system/project_tree_builder/$ptbname"
130    if test ! -x "$ptb"; then
131      echo "$ptbname is not found at $ptb"
132      echo "Will build $ptbname locally"
133      buildptb="yes"
134    fi
135  elif test -n "$PREBUILT_PTB_EXE"; then
136    if test -x "$PREBUILT_PTB_EXE"; then
137      ptb="$PREBUILT_PTB_EXE"
138      echo "Using $ptbname at $ptb"
139    else
140      echo "ERROR: $PREBUILT_PTB_EXE not found"
141      exit 1
142    fi
143  else
144    ptb="$relptbpath$PLATFORM/$ptbreqver/$ptbname"
145    if "$ptb" -version >/dev/null 2>&1; then
146      echo "Using $ptbname at $ptb"
147    else
148      if test $remoteptbonly = "yes"; then
149        echo "Prebuilt $ptbname not found"
150	exit 0
151      fi
152      echo "$ptbname is not found at $ptb"
153      echo "Will build $ptbname locally"
154      buildptb="yes"
155    fi
156  fi
157fi
158
159COMMON_Exec cd $builddir
160dll=""
161test -f "../status/DLL.enabled" && dll="-dll"
162ptbini="$srcdir/src/build-system/$ptbname.ini"
163test -f "$ptbini" || Usage "$ptbini not found"
164
165#-----------------------------------------------------------------------------
166# build project_tree_builder
167
168COMMON_Exec cd $builddir
169if test "$buildptb" = "yes"; then
170  for dep in $ptbdep; do
171    if test ! -d "$dep"; then
172      echo "WARNING: $builddir/$dep not found"
173      buildptb="no"
174      break;
175    fi
176    if test ! -f "$dep/Makefile"; then
177      echo "WARNING: $builddir/$dep/Makefile not found"
178      buildptb="no"
179      break;
180    fi
181  done
182fi
183
184if test "$buildptb" = "yes"; then
185  echo "**********************************************************************"
186  echo "Building $ptbname"
187  echo "**********************************************************************"
188  for dep in $ptbdep; do
189    COMMON_Exec cd $builddir
190    COMMON_Exec cd $dep
191    COMMON_Exec make
192  done
193  COMMON_Exec cd $builddir
194  ptb="./build-system/project_tree_builder/$ptbname"
195  test -x "$ptb" || Usage "$builddir/$ptb not found"
196  COMMON_AddRunpath $builddir/../lib
197fi
198
199test -x "$ptb" || Usage "$ptbname not found at $ptb"
200
201#-----------------------------------------------------------------------------
202# get version of project_tree_builder
203
204$ptb -version >/dev/null 2>&1
205if test $? -ne 0; then
206  echo "ERROR: $ptb does not work"
207  exit 1
208fi
209#ptbver=`$ptb -version | grep ^$ptbname | sed -e s/$ptbname:// | sed -e 's/ //g'`
210ptbver=`$ptb -version | sed -ne "s/^$ptbname: *//p"`
211
212verno=`echo $ptbver | sed -e 's/[.]/ /g'`
213for v in $verno; do
214  ptb_ver_major=$v
215  break
216done
217verno=`echo $ptbver | sed -e 's/[.]//g'`
218
219# see if we can use GUI
220use_gui_cfg="no"
221if test "$req_gui_cfg" = "yes"; then
222  if test $ptb_ver_major -ge 2; then
223    if test -e "$srcdir/$ptbgui"; then
224      java -version >/dev/null 2>&1
225      if test $? -ne 0; then
226        echo "WARNING: Java not found, cannot run configuration GUI"
227      else
228        use_gui_cfg="yes"
229      fi
230    else
231      echo WARNING: $srcdir/$ptbgui not found
232    fi
233  fi
234fi
235
236# see if we can use saved settings
237ptb_saved_cfg=""
238if test -n "$savedcfg"; then
239  if test $ptb_ver_major -ge 2; then
240    if test $verno -ge 220; then
241      ptb_saved_cfg="-args $savedcfg"
242# PTB will read projectlist from the saved settings
243      projectlist="\"\""
244    fi
245  fi
246fi
247
248#-----------------------------------------------------------------------------
249# find datatool
250ptb251="no"
251dtfound="no"
252dtdep=""
253if test $ptb_ver_major -ge 2 -a $verno -gt 250; then
254  ptb251="yes"
255  if test "$PREBUILT_DATATOOL_EXE" = "bootstrap"; then
256    echo "WARNING: Using in-tree datatool"
257  else
258    dtreqver="."
259    dtver="$srcdir/src/build-system/datatool_version.txt"
260    if test -r "$dtver"; then
261      dtreqver=`cat "$dtver" | sed -e 's/ //'`
262    fi
263    datatool="$reldatatoolpath$PLATFORM/$dtreqver/datatool"
264    if test -x "$datatool"; then
265      $datatool -version >/dev/null 2>&1
266      if test $? -eq 0; then
267        dtfound="yes"
268      else
269        echo "WARNING: $datatool does not work"
270      fi
271    fi
272  fi
273fi
274if test "$dtfound" = "no" -a $ptb251 = "yes"; then
275  dtdep="-dtdep"
276fi
277
278#-----------------------------------------------------------------------------
279# run project_tree_builder
280
281COMMON_Exec cd $builddir
282echo "**********************************************************************"
283echo "Running $ptbname. Please wait."
284echo "**********************************************************************"
285echo $ptb $dll $dtdep $ptb_saved_cfg -conffile $ptbini -logfile $logfile $srcdir $projectlist $solution
286if test "$use_gui_cfg" = "yes"; then
287  COMMON_Exec java -jar $srcdir/$ptbgui $ptb -i $dll $dtdep $ptb_saved_cfg -conffile $ptbini -logfile $logfile $srcdir $projectlist $solution
288else
289  COMMON_Exec $ptb $dll $dtdep $ptb_saved_cfg -conffile $ptbini -logfile $logfile $srcdir $projectlist $solution
290fi
291
292#-----------------------------------------------------------------------------
293# generate sources
294if test "$dtfound" = "yes"; then
295  if test -f "$builddir/../status/objects.enabled"; then
296    if test -r "$solution"; then
297      echo "**********************************************************************"
298      echo "Generating objects source code. Please wait."
299      echo "**********************************************************************"
300      echo make -f $solution all_files
301      make -f $solution all_files >/dev/null 2>&1
302    fi
303  fi
304fi
305
306echo "Done"
307