* config-ml.in (ml_realsrcdir): New, to account for ${subdir}.
[external/binutils.git] / config-ml.in
1 # Configure fragment invoked in the post-target section for subdirs
2 # wanting multilib support.
3 #
4 # It is advisable to support a few --enable/--disable options to let the
5 # user select which libraries s/he really wants.
6 #
7 # Subdirectories wishing to use multilib should put the following lines
8 # in the "post-target" section of configure.in.
9 #
10 # if [ "${srcdir}" = "." ] ; then
11 #   if [ "${with_target_subdir}" != "." ] ; then
12 #     . ${with_multisrctop}../../config-ml.in
13 #   else
14 #     . ${with_multisrctop}../config-ml.in
15 #   fi
16 # else
17 #   . ${srcdir}/../config-ml.in
18 # fi
19 #
20 # See librx/configure.in in the libg++ distribution for an example of how
21 # to handle autoconf'd libraries.
22 #
23 # Things are complicated because 6 separate cases must be handled:
24 # 2 (native, cross) x 3 (absolute-path, relative-not-dot, dot) = 6.
25 #
26 # srcdir=. is special.  It must handle make programs that don't handle VPATH.
27 # To implement this, a symlink tree is built for each library and for each
28 # multilib subdir.
29 #
30 # The build tree is layed out as
31 #
32 # ./
33 #   libg++
34 #   newlib
35 #   m68020/
36 #          libg++
37 #          newlib
38 #          m68881/
39 #                 libg++
40 #                 newlib
41 #
42 # The nice feature about this arrangement is that inter-library references
43 # in the build tree work without having to care where you are.  Note that
44 # inter-library references also work in the source tree because symlink trees
45 # are built when srcdir=.
46 #
47 # Unfortunately, trying to access the libraries in the build tree requires
48 # the user to manually choose which library to use as GCC won't be able to
49 # find the right one.  This is viewed as the lesser of two evils.
50 #
51 # Configure variables:
52 # ${with_target_subdir} = "." for native, or ${target_alias} for cross.
53 # Set by top level Makefile.
54 # ${with_multisrctop} = how many levels of multilibs there are in the source
55 # tree.  It exists to handle the case of configuring in the source tree:
56 # ${srcdir} is not constant.
57 # ${with_multisubdir} = name of multilib subdirectory (eg: m68020/m68881).
58 #
59 # Makefile variables:
60 # MULTISRCTOP = number of multilib levels in source tree (+1 if cross)
61 # (FIXME: note that this is different than ${with_multisrctop}.  Check out.).
62 # MULTIBUILDTOP = number of multilib levels in build tree
63 # MULTIDIRS = list of multilib subdirs (eg: m68000 m68020 ...)
64 # (only defined in each library's main Makefile).
65 # MULTISUBDIR = installed subdirectory name with leading '/' (eg: /m68000)
66 # (only defined in each multilib subdir).
67
68 # FIXME: Multilib is currently disabled by default for everything other than
69 # newlib.  It is up to each target to turn on multilib support for the other
70 # libraries as desired.
71
72 # We have to handle being invoked by both Cygnus configure and Autoconf.
73 #
74 # Cygnus configure incoming variables:
75 # srcdir, subdir, target, arguments
76 #
77 # Autoconf incoming variables:
78 # srcdir, target, ac_configure_args
79 #
80 # We *could* figure srcdir and target out, but we'd have to do work that
81 # our caller has already done to figure them out and requiring these two
82 # seems reasonable.
83
84 if [ -n "${ac_configure_args}" ]; then
85   Makefile=${ac_file-Makefile}
86   ml_config_shell=${CONFIG_SHELL-/bin/sh}
87   ml_arguments="${ac_configure_args}"
88   ml_realsrcdir=${srcdir}
89 else
90   Makefile=${Makefile-Makefile}
91   ml_config_shell=${config_shell-/bin/sh}
92   ml_arguments="${arguments}"
93   if [ -n "${subdir}" -a "${subdir}" != "." ] ; then
94     ml_realsrcdir=${srcdir}/${subdir}
95   else
96     ml_realsrcdir=${srcdir}
97   fi
98 fi
99
100 # Scan all the arguments and set all the ones we need.
101
102 for option in ${ml_arguments}
103 do
104   case $option in
105   --*) ;;
106   -*) option=-$option ;;
107   esac
108
109   case $option in
110   --*=*)
111         optarg=`echo $option | sed -e 's/^[^=]*=//'`
112         ;;
113   esac
114
115   case $option in
116   --disable-*)
117         enableopt=`echo ${option} | sed 's:^--disable-:enable_:;s:-:_:g'`
118         eval $enableopt=no
119         ;;
120   --enable-*)
121         case "$option" in
122         *=*)    ;;
123         *)      optarg=yes ;;
124         esac
125         enableopt=`echo ${option} | sed 's:^--::;s:=.*$::;s:-:_:g'`
126         eval $enableopt="$optarg"
127         ;;
128   --norecursion | --no*)
129         ml_norecursion=yes
130         ;;
131   --verbose | --v | --verb*)
132         ml_verbose=--verbose
133         ;;
134   --with-*)
135         case "$option" in
136         *=*)    ;;
137         *)      optarg=yes ;;
138         esac
139         withopt=`echo ${option} | sed 's:^--::;s:=.*$::;s:-:_:g'`
140         eval $withopt="$optarg"
141         ;;
142   --without-*)
143         withopt=`echo ${option} | sed 's:^--::;s:out::;s:-:_:g'`
144         eval $withopt=no
145         ;;
146   esac
147 done
148
149 # Only do this if --enable-multilib.
150 if [ "${enable_multilib}" = yes ]; then
151
152 # Compute whether this is the library's top level directory
153 # (ie: not a multilib subdirectory, and not a subdirectory like libg++/src).
154 # ${with_multisubdir} tells us we're in the right branch, but we could be
155 # in a subdir of that.
156 # ??? The previous version could void this test by separating the process into
157 # two files: one that only the library's toplevel configure.in ran (to
158 # configure the multilib subdirs), and another that all configure.in's ran to
159 # update the Makefile.  It seemed reasonable to collapse all multilib support
160 # into one file, but it does leave us with having to perform this test.
161 ml_toplevel_p=no
162 if [ -z "${with_multisubdir}" ]; then
163   if [ "${srcdir}" = "." ]; then
164     # Use ${ml_realsrcdir} instead of ${srcdir} here to account for ${subdir}.
165     # ${with_target_subdir} = "." for native, otherwise target alias.
166     if [ "${with_target_subdir}" = "." ]; then
167       if [ -f ${ml_realsrcdir}/../config-ml.in ]; then
168         ml_toplevel_p=yes
169       fi
170     else
171       if [ -f ${ml_realsrcdir}/../../config-ml.in ]; then
172         ml_toplevel_p=yes
173       fi
174     fi
175   else
176     # Use ${ml_realsrcdir} instead of ${srcdir} here to account for ${subdir}.
177     if [ -f ${ml_realsrcdir}/../config-ml.in ]; then
178       ml_toplevel_p=yes
179     fi
180   fi
181 fi
182
183 # If this is the library's top level directory, set multidirs to the
184 # multilib subdirs to support.  This lives at the top because we need
185 # `multidirs' set right away.
186
187 if [ "${ml_toplevel_p}" = yes ]; then
188
189 multidirs=
190 for i in `${CC-gcc} --print-multi-lib 2>/dev/null`; do
191   dir=`echo $i | sed -e 's/;.*$//'`
192   if [ "${dir}" = "." ]; then
193     true
194   else
195     if [ -z "${multidirs}" ]; then
196       multidirs="${dir}"
197     else
198       multidirs="${multidirs} ${dir}"
199     fi
200   fi
201 done
202
203 case "${target}" in
204 # start-sanitize-arc
205 arc-sbp-elf*)
206         if [ x$enable_biendian != xyes ]
207         then
208           old_multidirs=${multidirs}
209           multidirs=
210           for x in ${old_multidirs}; do
211             case "${x}" in
212               *be*) ;;
213               *) if [ x"$multidirs" = x ]; then
214                    multidirs="$x";
215                  else
216                    multidirs="${multidirs} ${x}"; 
217                  fi
218                  ;;
219             esac
220           done
221         fi
222         ;;
223 # end-sanitize-arc
224 mips*-*-*)
225         if [ x$enable_single_float = xno ]
226         then
227           old_multidirs="${multidirs}"
228           multidirs=""
229           for x in ${old_multidirs}; do
230             case "$x" in
231               *single* ) : ;;
232               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
233             esac
234           done
235         fi
236         if [ x$enable_biendian = xno ]
237         then
238           old_multidirs="${multidirs}"
239           multidirs=""
240           for x in ${old_multidirs}; do
241             case "$x" in
242               *el* ) : ;;
243               *eb* ) : ;;
244               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
245             esac
246           done
247         fi
248         if [ x$enable_softfloat = xno ]
249         then
250           old_multidirs="${multidirs}"
251           multidirs=""
252           for x in ${old_multidirs}; do
253             case "$x" in
254               *soft-float* ) : ;;
255               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
256             esac
257           done
258         fi
259         ;;
260 powerpc*-*-* | rs6000*-*-*)
261         if [ x$enable_softfloat = xno ]
262         then
263           old_multidirs="${multidirs}"
264           multidirs=""
265           for x in ${old_multidirs}; do
266             case "$x" in
267               *soft-float* ) : ;;
268               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
269             esac
270           done
271         fi
272         if [ x$enable_commoncpu = xno ]
273         then
274           old_multidirs="${multidirs}"
275           multidirs=""
276           for x in ${old_multidirs}; do
277             case "$x" in
278               *common* ) : ;;
279               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
280             esac
281           done
282         fi
283         if [ x$enable_powercpu = xno ]
284         then
285           old_multidirs="${multidirs}"
286           multidirs=""
287           for x in ${old_multidirs}; do
288             case "$x" in
289               power | */power | */power/* ) : ;;
290               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
291             esac
292           done
293         fi
294         if [ x$enable_power2cpu = xno ]
295         then
296           old_multidirs="${multidirs}"
297           multidirs=""
298           for x in ${old_multidirs}; do
299             case "$x" in
300               *power2* ) : ;;
301               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
302             esac
303           done
304         fi
305         if [ x$enable_powerpccpu = xno ]
306         then
307           old_multidirs="${multidirs}"
308           multidirs=""
309           for x in ${old_multidirs}; do
310             case "$x" in
311               *powerpc* ) : ;;
312               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
313             esac
314           done
315         fi
316         if [ x$enable_601cpu = xno ]
317         then
318           old_multidirs="${multidirs}"
319           multidirs=""
320           for x in ${old_multidirs}; do
321             case "$x" in
322               *601* ) : ;;
323               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
324             esac
325           done
326         fi
327         if [ x$enable_biendian = xno ]
328         then
329           old_multidirs="${multidirs}"
330           multidirs=""
331           for x in ${old_multidirs}; do
332             case "$x" in
333               *endian* ) : ;;
334               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
335             esac
336           done
337         fi
338         if [ x$enable_relocatable = xno ]
339         then
340           old_multidirs="${multidirs}"
341           multidirs=""
342           for x in ${old_multidirs}; do
343             case "$x" in
344               *relocatable* ) : ;;
345               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
346             esac
347           done
348         fi
349         if [ x$enable_sysv = xno ]
350         then
351           old_multidirs="${multidirs}"
352           multidirs=""
353           for x in ${old_multidirs}; do
354             case "$x" in
355               *sysv* ) : ;;
356               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
357             esac
358           done
359         fi
360         if [ x$enable_aix = xno ]
361         then
362           old_multidirs="${multidirs}"
363           multidirs=""
364           for x in ${old_multidirs}; do
365             case "$x" in
366               *aix* ) : ;;
367               *) if [ x"$multidirs" = x ]; then multidirs="$x"; else multidirs="${multidirs} ${x}"; fi ;;
368             esac
369           done
370         fi
371         ;;
372 esac
373
374 # Add code to library's top level makefile to handle building the multilib
375 # subdirs.
376
377 cat > Multi.tem <<\EOF
378
379 # FIXME: There should be an @-sign in front of the `if'.
380 # Leave out until this is tested a bit more.
381 multi-do:
382         if [ -z "$(MULTIDIRS)" ]; then \
383           true; \
384         else \
385           rootpre=`pwd`/; export rootpre; \
386           srcrootpre=`cd $(srcdir); pwd`/; export srcrootpre; \
387           lib=`echo $${rootpre} | sed -e 's,^.*/\([^/][^/]*\)/$$,\1,'`; \
388           compiler="$(CC)"; \
389           for i in `$${compiler} --print-multi-lib 2>/dev/null`; do \
390             dir=`echo $$i | sed -e 's/;.*$$//'`; \
391             if [ "$${dir}" = "." ]; then \
392               true; \
393             else \
394               if [ -d ../$${dir}/$${lib} ]; then \
395                 flags=`echo $$i | sed -e 's/^[^;]*;//' -e 's/@/ -/g'`; \
396                 if (cd ../$${dir}/$${lib}; $(MAKE) $(FLAGS_TO_PASS) \
397                                 CFLAGS="$(CFLAGS) $${flags}" \
398                                 CXXFLAGS="$(CXXFLAGS) $${flags}" \
399                                 LIBCFLAGS="$(LIBCFLAGS) $${flags}" \
400                                 LIBCXXFLAGS="$(LIBCXXFLAGS) $${flags}" \
401                                 $(DO)); then \
402                   true; \
403                 else \
404                   exit 1; \
405                 fi; \
406               else true; \
407               fi; \
408             fi; \
409           done; \
410         fi
411
412 # FIXME: There should be an @-sign in front of the `if'.
413 # Leave out until this is tested a bit more.
414 multi-clean:
415         if [ -z "$(MULTIDIRS)" ]; then \
416           true; \
417         else \
418           lib=`pwd | sed -e 's,^.*/\([^/][^/]*\)$$,\1,'`; \
419           for dir in Makefile $(MULTIDIRS); do \
420             if [ -f ../$${dir}/$${lib}/Makefile ]; then \
421               if (cd ../$${dir}/$${lib}; $(MAKE) $(FLAGS_TO_PASS) $(DO)); \
422               then true; \
423               else exit 1; \
424               fi; \
425             else true; \
426             fi; \
427           done; \
428         fi
429 EOF
430
431 cat ${Makefile} Multi.tem > Makefile.tem
432 rm -f ${Makefile} Multi.tem
433 mv Makefile.tem ${Makefile}
434
435 fi # ${ml_toplevel_p} = yes
436
437 if [ "${ml_verbose}" = --verbose ]; then
438   echo "Adding multilib support to Makefile in ${ml_realsrcdir}"
439   if [ "${ml_toplevel_p}" = yes ]; then
440     echo "multidirs=${multidirs}"
441   fi
442   echo "with_multisubdir=${with_multisubdir}"
443 fi
444
445 if [ "${srcdir}" = "." ]; then
446   if [ "${with_target_subdir}" != "." ]; then
447     ml_srcdotdot="../"
448   else
449     ml_srcdotdot=""
450   fi
451 else
452   ml_srcdotdot=""
453 fi
454
455 if [ -z "${with_multisubdir}" ]; then
456   ml_subdir=
457   ml_builddotdot=
458   : # ml_srcdotdot= # already set
459 else
460   ml_subdir="/${with_multisubdir}"
461   # The '[^/][^/]*' appears that way to work around a SunOS sed bug.
462   ml_builddotdot=`echo ${with_multisubdir} | sed -e 's:[^/][^/]*:..:g'`/
463   if [ "$srcdir" = "." ]; then
464     ml_srcdotdot=${ml_srcdotdot}${ml_builddotdot}
465   else
466     : # ml_srcdotdot= # already set
467   fi
468 fi
469
470 if [ "${ml_toplevel_p}" = yes ]; then
471   ml_do='$(MAKE)'
472   ml_clean='$(MAKE)'
473 else
474   ml_do=true
475   ml_clean=true
476 fi
477
478 # TOP is used by newlib and should not be used elsewhere for this purpose.
479 # MULTI{SRC,BUILD}TOP are the proper ones to use.  MULTISRCTOP is empty
480 # when srcdir != builddir.  MULTIBUILDTOP is always some number of ../'s.
481 # FIXME: newlib needs to be updated to use MULTI{SRC,BUILD}TOP so we can
482 # delete TOP.  Newlib may wish to continue to use TOP for its own purposes
483 # of course.
484 # MULTIDIRS is non-empty for the cpu top level Makefile (eg: newlib/Makefile)
485 # and lists the subdirectories to recurse into.
486 # MULTISUBDIR is non-empty in each cpu subdirectory's Makefile
487 # (eg: newlib/h8300h/Makefile) and is the installed subdirectory name with
488 # a leading '/'.
489 # MULTIDO is used for targets like all, install, and check where
490 # $(FLAGS_TO_PASS) augmented with the subdir's compiler option is needed.
491 # MULTICLEAN is used for the *clean targets.
492 #
493 # ??? It is possible to merge MULTIDO and MULTICLEAN into one.  They are
494 # currently kept separate because we don't want the *clean targets to require
495 # the existence of the compiler (which MULTIDO currently requires) and
496 # therefore we'd have to record the directory options as well as names
497 # (currently we just record the names and use --print-multi-lib to get the
498 # options).
499
500 sed -e "s:^TOP[         ]*=[    ]*\([./]*\)[    ]*$:TOP = ${ml_builddotdot}\1:" \
501     -e "s:^MULTISRCTOP[         ]*=.*$:MULTISRCTOP = ${ml_srcdotdot}:" \
502     -e "s:^MULTIBUILDTOP[       ]*=.*$:MULTIBUILDTOP = ${ml_builddotdot}:" \
503     -e "s:^MULTIDIRS[   ]*=.*$:MULTIDIRS = ${multidirs}:" \
504     -e "s:^MULTISUBDIR[         ]*=.*$:MULTISUBDIR = ${ml_subdir}:" \
505     -e "s:^MULTIDO[     ]*=.*$:MULTIDO = $ml_do:" \
506     -e "s:^MULTICLEAN[  ]*=.*$:MULTICLEAN = $ml_clean:" \
507         ${Makefile} > Makefile.tem
508 rm -f ${Makefile}
509 mv Makefile.tem ${Makefile}
510
511 # If this is the library's top level, configure each multilib subdir.
512 # This is done at the end because this is the loop that runs configure
513 # in each multilib subdir and it seemed reasonable to finish updating the
514 # Makefile before going on to configure the subdirs.
515
516 if [ "${ml_toplevel_p}" = yes ]; then
517
518 # We must freshly configure each subdirectory.  This bit of code is
519 # actually partially stolen from the main configure script.  FIXME.
520
521 if [ -n "${multidirs}" ] && [ -z "${ml_norecursion}" ]; then
522
523   if [ "${ml_verbose}" = --verbose ]; then
524     echo "Running configure in multilib subdirs ${multidirs}"
525     echo "pwd: `pwd`"
526   fi
527
528   ml_origdir=`pwd`
529   ml_libdir=`echo $ml_origdir | sed -e 's,^.*/,,'`
530   # cd to top-level-build-dir/${with_target_subdir}
531   cd ..
532
533   for ml_dir in ${multidirs}; do
534
535     if [ "${ml_verbose}" = --verbose ]; then
536       echo "Running configure in multilib subdir ${ml_dir}"
537       echo "pwd: `pwd`"
538     fi
539
540     if [ -d ${ml_dir} ]; then true; else mkdir ${ml_dir}; fi
541     if [ -d ${ml_dir}/${ml_libdir} ]; then true; else mkdir ${ml_dir}/${ml_libdir}; fi
542
543     # Eg: if ${ml_dir} = m68000/m68881, dotdot = ../../
544     dotdot=../`echo ${ml_dir} | sed -e 's|[^/]||g' -e 's|/|../|g'`
545
546     case ${srcdir} in
547     ".")
548       echo Building symlink tree in `pwd`/${ml_dir}/${ml_libdir}
549       if [ "${with_target_subdir}" != "." ]; then
550         ml_unsubdir="../"
551       else
552         ml_unsubdir=""
553       fi
554       (cd ${ml_dir}/${ml_libdir};
555        ../${dotdot}${ml_unsubdir}symlink-tree ../${dotdot}${ml_unsubdir}${ml_libdir} "")
556       ml_newsrcdir="."
557       ml_srcdiroption=
558       multisrctop=${dotdot}
559       ;;
560     *)
561       case "${srcdir}" in
562       /*) # absolute path
563         ml_newsrcdir=${srcdir}
564         ;;
565       *) # otherwise relative
566         ml_newsrcdir=${dotdot}${srcdir}
567         ;;
568       esac
569       ml_srcdiroption="-srcdir=${ml_newsrcdir}"
570       multisrctop=
571       ;;
572     esac
573
574     case "${progname}" in
575     /*)     ml_recprog=${progname} ;;
576     *)      ml_recprog=${dotdot}${progname} ;;
577     esac
578
579     # FIXME: POPDIR=${PWD=`pwd`} doesn't work here.
580     ML_POPDIR=`pwd`
581     cd ${ml_dir}/${ml_libdir}
582
583     if [ -f ${ml_newsrcdir}/configure ]; then
584       ml_recprog=${ml_newsrcdir}/configure
585     fi
586     if eval ${ml_config_shell} ${ml_recprog} \
587         --with-multisubdir=${ml_dir} --with-multisrctop=${multisrctop} \
588         ${ml_arguments} ${ml_srcdiroption} ; then
589       true
590     else
591       exit 1
592     fi
593
594     cd ${ML_POPDIR}
595
596   done
597
598   cd ${ml_origdir}
599 fi
600
601 fi # ${ml_toplevel_p} = yes
602 fi # ${enable_multilib} = yes