LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2012, 01:56 PM   #1
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
An eval alternative


Those that know me have seen that I am fairly anti-eval where ever possible.

So I am asking for feedback on an alternative I am looking to employ to see if anyone can poke holes in my idea

I have been labouring away to create a package manager on a source based (CLFS) system. One of the issues I have faced is that when some of the
compilations require flags to be set, such as CC or LDFLAGS, I have been dragged into the eval world and used the following:
Code:
eval ${CONF_FLAGS[*]} ./configure ${CONF_DEFAULTS[*]} ${CONF_OPTS[*]}
Now the only reason to use eval here is the CONF_FLAGS array once expanded needs to be
evaluated prior to being used or the command line will consider it to be commands and errors.

My new idea however is to remove the eval and simply sed the CONF_FLAGS into the configure script itself:
Code:
sed -i "2 i\ ${CONF_FLAGS[*]}" configure
So as stated earlier, my questions is this:

Can anyone see any inherent issue with this method?

Or, if anyone has an even better idea I am all ears

Please fire questions about anything that is too unclear
 
Old 06-12-2012, 02:50 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by grail View Post
Code:
eval ${CONF_FLAGS[*]} ./configure ${CONF_DEFAULTS[*]} ${CONF_OPTS[*]}
For my Bash build scripts, I prefer to use
Code:
( export "${CONF_FLAGS[@]}" && ./configure "${CONF_DEFAULTS[@]}" "${CONF_OPTS[@]}" || exit $? )
Is there a reason why this would not work for you?

For other interested parties: The command is run in a subshell, because then the name=value pairs in CONF_FLAGS array can simply be exported to the environment. I tend to be very careful about variable expansion, so my lists are evaluated per-element ("${var[@]}") to avoid having them be re-split. Finally, if either export or configure fails, the subshell immediately exits with the same exit status, propagating it to the parent process $?.
 
2 members found this post helpful.
Old 06-13-2012, 03:58 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Original Poster
Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Thank you NA ... valuable information as always I will give it a try. I do like it more than my current suggestion as tampering with the configure was more of a kludge than anything
 
Old 06-14-2012, 08:43 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Original Poster
Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well I am guessing by the lack of other replies that NA's solution would be the most optimum that anyone can think of (including me )

Cheers again NA
 
Old 06-14-2012, 09:59 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,783

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Actually this just occured to me, the elements in CONF_FLAGS have the form var=value, right? You can pass that to configure directly (see Defining Variables):
Code:
./configure "${CONF_FLAGS[@]}" "${CONF_DEFAULTS[@]}" "${CONF_OPTS[@]}"
 
Old 06-14-2012, 12:09 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by ntubski View Post
You can pass that to configure directly
Last time I used some packages had weird autoconf macros which would treat configure variables and environment variables differently -- specifically CC and CFLAGS; I think the environment value was appended to the configure variable value or some similar trickery. Also, not all ./configure scripts are autoconf-based; there are a few packages that use a simplified self-written version. This was a few years ago, so things may have changed (it'd take me a while to find even examples).

I'm not saying the subshell+export is the best solution, but it is the one that worked best for me.
 
Old 06-14-2012, 01:20 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Original Poster
Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Thanks for the input ntubski I guess my query to this method, not having had NA's experiences myself, would be, why then do sites like (C)LFS place the var=value prior to the ./configure?

I guess is it also possible that in some configure scripts they may possibly be interpreted as parameters? (just guessing of course)
 
Old 06-14-2012, 02:24 PM   #8
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by grail View Post
why then do sites like (C)LFS place the var=value prior to the ./configure?
Because the shell will place all NAME=VALUE pairs listed before the command into the environment variables for that command.

Most, but not all, ./configure scripts recognize NAME=VALUE pairs, and treat them as variable assignments. In my experience, some scripts are buggy or obscure, and treat the assignments differently to environment variables.

But the main point is that the shell handles all pairs listed prior to the command, automatically.
 
  


Reply



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
[SOLVED] eval puzzle dazdaz Linux - General 7 01-24-2011 04:18 PM
[SOLVED] Is eval my only choice here? grail Programming 27 10-07-2010 11:16 PM
OpenSuse Eval xbaez SUSE / openSUSE 10 12-11-2005 01:16 PM
SUSE 9.2 Eval?? rm6990 Linux - Distributions 3 10-07-2004 06:50 PM
Live eval badboyz2525 Linux - General 3 09-11-2003 08:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:52 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
Open Source Consulting | Domain Registration