LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash script calling makefiles with CFLAGS prepended (https://www.linuxquestions.org/questions/linux-software-2/bash-script-calling-makefiles-with-cflags-prepended-787469/)

paulcsf 02-06-2010 05:19 PM

Bash script calling makefiles with CFLAGS prepended
 
I am trying to automate my linux build setup from a Bash script.

Most all of this works when I execute it manually, but I cannot figure out how to get a bash script to execute it automatically.

In this particular case, I am trying to build the xorg utilities. If I manually step through the process, prepending the commands with CFLAGS, LDFLAGS, etc, all of the packages build.

So I created a bash script, test.sh:
Code:

#!/bin/bash

export INITFS=/initfs
export SOURCES=/sources
export CFLAGS="-O2 -march=i586 -s -I${INITFS}/usr/include"
export CONFIG_OPTS="--disable-nls"
export XORG_PREFIX="/usr/X11"
export XORG_CONFIG="--prefix=$XORG_PREFIX --sysconfdir=/etc \
--mandir=$XORG_PREFIX/share/man --localstatedir=/var"
export XORG_DEST="DESTDIR=${INITFS}"
export PKG_CONFIG_LIBDIR="${INITFS}/usr/lib/pkgconfig"
export PKG_CONFIG_PATH="${INITFS}/usr/lib/pkgconfig"
export PKG_CONFIG_SYSROOT_DIR="${INITFS}"
export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1
export PKG_CONFIG_ALLOW_SYSTEM_LIBS=1

export S_PREPEND="CFLAGS=\"${CFLAGS}\" \
    XORG_PREFIX=\"${XORG_PREFIX}\" \
    XORG_CONFIG=\"${XORG_CONFIG}\" \
    XORG_DEST=\"${XORG_DEST}\" \
    PKG_CONFIG_LIBDIR=\"${PKG_CONFIG_LIBDIR}\" \
    PKG_CONFIG_PATH=\"${PKG_CONFIG_PATH}\" \
    PKG_CONFIG_SYSROOT_DIR=\"${PKG_CONFIG_SYSROOT_DIR}\" \
    PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=\"${PKG_CONFIG_ALLOW_SYSTEM_CFLAGS}\" \
    PKG_CONFIG_ALLOW_SYSTEM_LIBS=\"${PKG_CONFIG_ALLOW_SYSTEM_LIBS}\" \
    "

...abbreviated - for loop through packages ...

  "${S_PREPEND}" ./configure ${CONFIG_OPTS} $XORG_CONFIG
  "${S_PREPEND}" make ${XORG_DEST}
  "${S_PREPEND}" make ${XORG_DEST} install

If I set these variables manually, then echo $S_PREPEND, "$S_PREPEND", ${S_PREPEND}, or "${S_PREPEND}", I get (I manually line-wrapped these):
Code:

CFLAGS="-O2 -march=i586 -s -I/initfs/usr/include"
XORG_PREFIX="/usr/X11"
XORG_CONFIG="--prefix=/usr/X11 --sysconfdir=/etc --mandir=/usr/X11/share/man --localstatedir=/var"
XORG_DEST="DESTDIR=/initfs"
PKG_CONFIG_LIBDIR="/initfs/usr/lib/pkgconfig"
PKG_CONFIG_PATH=/initfs/usr/lib/pkgconfig"
PKG_CONFIG_PATH="/usr/X11/lib/pkgconfig" PKG_CONFIG_SYSROOT_DIR="/initfs"
PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="1" PKG_CONFIG_ALLOW_SYSTEM_LIBS="1"

which is exactly as expected.

But when the configure or make commands execute, I get various errors, in the latest configuration I get (I manually line-wrapped these):
Code:

... bash is echo'ing commands ...
+ 'CFLAGS="-O2 -march=i586 -s -I/initfs/usr/include" 
XORG_PREFIX="/usr/X11"    XORG_CONFIG="--prefix=/usr/X11 --sysconfdir=/etc --mandir=/usr/X11/share/man --localstatedir=/var"
XORG_DEST="DESTDIR=/initfs"   
PKG_CONFIG_LIBDIR="/initfs/usr/lib/pkgconfig" 
PKG_CONFIG_PATH="/initfs/usr/lib/pkgconfig"   
PKG_CONFIG_SYSROOT_DIR="/initfs"   
PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="1"   
PKG_CONFIG_ALLOW_SYSTEM_LIBS="1"    ' ./configure
--disable-nls --prefix=/usr/X11 --sysconfdir=/etc --mandir=/usr/X11/share/man --localstatedir=/var

... and here is the error
./test.sh: line 167: CFLAGS="-O2 -march=i586 -s -I/initfs
/usr/include"    XORG_PREFIX="/usr/X11"   
XORG_CONFIG="--prefix=/usr/X11 --sysconfdir=/etc --mandir=/usr
/X11/share/man --localstatedir=/var"   
XORG_DEST="DESTDIR=/initfs"    PKG_CONFIG_LIBDIR="/initfs/usr/lib
/pkgconfig"    PKG_CONFIG_PATH="/initfs/usr/lib/pkgconfig"   
PKG_CONFIG_SYSROOT_DIR="/initfs"   
PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="1"   
PKG_CONFIG_ALLOW_SYSTEM_LIBS="1"    : No such file or directory

Is this possible in a script file? I assume it's syntax that I haven't been able to find or stumble upon. Wrapping the commands in $(...) doesn't work, a single quote ' doesn't expand the $S_PREPEND and other variables, and a back tick ` seems to requote the strings in a fashion that causes errors.

Should I be creating a makefile to do all of this? I started with the bash script because I am more familiar with it, and it's creating the entire system, un-tar'ing packages, building directories, etc. I am sure a makefile can do all of this, but I don't know how.

Thanks for any help or suggestions.

ozanbaba 02-06-2010 06:16 PM

i don't think you need to export the variables to use such way as in the following exemple;

CONFIG_OPTS="--disable-multilib"
./configure $CONFIG_OPTS

however CFLAGS exported would work in make files or configure scripts which use it. but don't use it like this.

instead do it this way

SLKCFLAGS="-O2 -fPIC"

CFLAGS="$SLKFLAGS" ./configure

that's more secure and it won't intervene with other builds which may come later.

kbp 02-06-2010 06:23 PM

Maybe you're trying to do this ?

Code:

S_PREPEND="CFLAGS=\"${CFLAGS}\" \
    XORG_PREFIX=\"${XORG_PREFIX}\" \
    XORG_CONFIG=\"${XORG_CONFIG}\" \
    XORG_DEST=\"${XORG_DEST}\" \
    PKG_CONFIG_LIBDIR=\"${PKG_CONFIG_LIBDIR}\" \
    PKG_CONFIG_PATH=\"${PKG_CONFIG_PATH}\" \
    PKG_CONFIG_SYSROOT_DIR=\"${PKG_CONFIG_SYSROOT_DIR}\" \
    PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=\"${PKG_CONFIG_ALLOW_SYSTEM_CFLAGS}\" \
    PKG_CONFIG_ALLOW_SYSTEM_LIBS=\"${PKG_CONFIG_ALLOW_SYSTEM_LIBS}\" \
    "

export ${S_PREPEND}; ./configure ${CONFIG_OPTS} $XORG_CONFIG

cheers

Note the semicolon...

paulcsf 02-06-2010 06:45 PM

Thank you. Things like "missing the forest for the trees" come to mind...

I even use that syntax else where, but I got so caught up in trying to put it in a variable that I lost sight of what I was really trying to do.

For a recap, here is a working solution:
Code:

export INITFS=/initfs
export SOURCES=/sources
export S_CFLAGS="-O2 -march=i586 -s -I${INITFS}/usr/include"
export S_CPPFLAGS=${S_CFLAGS}
export S_CXXFLAGS=${S_CFLAGS}
export S_CONFIG_OPTS="--disable-nls"

export XORG_PREFIX="/usr/X11"
export XORG_CONFIG="--prefix=$XORG_PREFIX --sysconfdir=/etc \
--mandir=$XORG_PREFIX/share/man --localstatedir=/var"
export XORG_DEST="DESTDIR=${INITFS}"

export S_PKG_CONFIG_LIBDIR="${INITFS}/usr/lib/pkgconfig"
export S_PKG_CONFIG_PATH="${INITFS}/usr/lib/pkgconfig"
export S_PKG_CONFIG_SYSROOT_DIR="${INITFS}"
export S_PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1
export S_PKG_CONFIG_ALLOW_SYSTEM_LIBS=1

... abbreviated ...

  CFLAGS="${S_CFLAGS}" \
  CPPFLAGS="${S_CPPFLAGS}" \
  CXXFLAGS="${S_CXXFLAGS}" \
  PKG_CONFIG_LIBDIR="${S_PKG_CONFIG_LIBDIR}" \
  PKG_CONFIG_PATH="${S_PKG_CONFIG_PATH}" \
  PKG_CONFIG_SYSROOT_DIR="${S_PKG_CONFIG_SYSROOT_DIR}" \
  PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="${S_PKG_CONFIG_ALLOW_SYSTEM_CFLAGS}" \
  PKG_CONFIG_ALLOW_SYSTEM_LIBS="${S_PKG_CONFIG_ALLOW_SYSTEM_LIBS}" \
  ./configure ${CONFIG_OPTS} $XORG_CONFIG

  CFLAGS="${S_CFLAGS}" \
  CPPFLAGS="${S_CPPFLAGS}" \
  CXXFLAGS="${S_CXXFLAGS}" \
  PKG_CONFIG_LIBDIR="${S_PKG_CONFIG_LIBDIR}" \
  PKG_CONFIG_PATH="${S_PKG_CONFIG_PATH}" \
  PKG_CONFIG_SYSROOT_DIR="${S_PKG_CONFIG_SYSROOT_DIR}" \
  PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="${S_PKG_CONFIG_ALLOW_SYSTEM_CFLAGS}" \
  PKG_CONFIG_ALLOW_SYSTEM_LIBS="${S_PKG_CONFIG_ALLOW_SYSTEM_LIBS}" \
  make

  CFLAGS="${S_CFLAGS}" \
  CPPFLAGS="${S_CPPFLAGS}" \
  CXXFLAGS="${S_CXXFLAGS}" \
  PKG_CONFIG_LIBDIR="${S_PKG_CONFIG_LIBDIR}" \
  PKG_CONFIG_PATH="${S_PKG_CONFIG_PATH}" \
  PKG_CONFIG_SYSROOT_DIR="${S_PKG_CONFIG_SYSROOT_DIR}" \
  PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="${S_PKG_CONFIG_ALLOW_SYSTEM_CFLAGS}" \
  PKG_CONFIG_ALLOW_SYSTEM_LIBS="${S_PKG_CONFIG_ALLOW_SYSTEM_LIBS}" \
  make ${XORG_DEST}

However, it does seem there should be a way to set that into a variable/macro type of entity so that it could be managed from one place at the beginning of the file.

ozanbaba 02-06-2010 06:51 PM

Quote:

Originally Posted by paulcsf (Post 3855180)
However, it does seem there should be a way to set that into a variable/macro type of entity so that it could be managed from one place at the beginning of the file.

an example from SlackBuilds: (removed the extras and ifs)

Code:


SLKFLAGS=${SLKFLAGS:-"-O2 -fPIC" #so i can mapulate it at runtime at command line
[...]

CFLAGS="SLKFLAGS" configure

that way it won't intervene with anything at all.

paulcsf 02-06-2010 07:24 PM

Thanks for the continued input.

ozanbaba:
I don't mind setting CFLAGS, CXXFLAGS, etc, and those carry through the file just fine. The suggestion about automatically appending them to a supplied value is great, though in this instance I have a fixed set.

I was wondering looking for a way to stick all of that into one variable/macro/replacement entity that I could prepend the commands with. My updated version will work just fine, it's just going to get ugly to maintain with 8 variables already.


kbp:
That is pretty similar to what I would like to accomplish. However, trying that syntax fails, as Bash is trying to execute the middle of the CFLAGS string, giving me errors of similar to: `-march=i586': not a valid identifier.

When I execute this manually, if I double quote the export,
export "${S_PREPEND}"
the command runs, but I get the entire value of S_PREPEND as the new value for CFLAGS enclosed in single quotes. IE CFLAGS='"-O2 -march=i586 -s -I/initfs/usr/include" XORG_PREFIX="/usr/X11" XORG ...


All times are GMT -5. The time now is 05:39 PM.