LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-06-2010, 05:19 PM   #1
paulcsf
LQ Newbie
 
Registered: Jan 2009
Distribution: Ubuntu / LFS/BLFS/CLFS
Posts: 20

Rep: Reputation: 1
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.
 
Old 02-06-2010, 06:16 PM   #2
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 673

Rep: Reputation: 94
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.
 
Old 02-06-2010, 06:23 PM   #3
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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...

Last edited by kbp; 02-06-2010 at 06:24 PM.
 
Old 02-06-2010, 06:45 PM   #4
paulcsf
LQ Newbie
 
Registered: Jan 2009
Distribution: Ubuntu / LFS/BLFS/CLFS
Posts: 20

Original Poster
Rep: Reputation: 1
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.
 
Old 02-06-2010, 06:51 PM   #5
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 673

Rep: Reputation: 94
Quote:
Originally Posted by paulcsf View Post
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.
 
Old 02-06-2010, 07:24 PM   #6
paulcsf
LQ Newbie
 
Registered: Jan 2009
Distribution: Ubuntu / LFS/BLFS/CLFS
Posts: 20

Original Poster
Rep: Reputation: 1
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 ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling director, bash script Spinoz Linux - Newbie 6 09-30-2009 08:23 PM
calling user input in bash script dbmacartney Linux - General 3 05-19-2008 07:55 AM
bash - order of commands when calling remote script? babag Programming 2 04-05-2008 06:10 PM
bash script calling mplayer afrodocter Programming 13 08-23-2005 11:41 AM
What is CFLAGS variable used in Makefiles for? Nerox Programming 3 12-28-2004 06:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration