Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed replacement through file question 1

Status
Not open for further replies.

Mthales

Technical User
Feb 27, 2004
1,181
GB
I am very new at this so please forgive me if I'm being very dumb with this question but ...
I've got a makefile that contains a large number of environment variables. I want to write a script that will produce a copy of this makefile that has all the variables replaced with their values.

As a first step towards this I'm trying use the following line but it's totally not working and I'm stuck so any help you can give would be greatly appreciated. The aim of is to replace globaly from the "Makefile" all instances of $<variable name> with ${<variable name>) and put the result into a "Makefile_values".

Code:
sed 's/\$*/\${&}/gw "Makefile_values"' Makefile;

M

Trainee Chocolate Management Executive.
 
Code:
CC     = g++
# ...
CFLAGS = -g -O2  -I/usr/include -Wall --pedantic -Wstrict-prototypes

${PROG}: ${OBJS}
        ${CC} ${CFLAGS} ${OBJS} -o ${PROG} ${LDFLAGS}
how should sed be able to replace the variables?
Perhaps you may get some initial result with awk.
Getting the source of 'make' and implementing an print-variables might be more easy :)

seeking a job as java-programmer in Berlin:
 
steranwagner,
Thanks for your response.

The way the makefile is set up there is one "standard makefile" that is used in various situations. The way it works is controlled by a large number of environment variables who are set up by a complex shell script which is run before the makefile.

So for your example the "CC = g++" is set from the script and not in the top of the makefile where the "$(PROG): $(OBJS) $(CC) $(CFLAGS) $(OBJS) -o $(PROG) $(LDFLAGS)" is.

I'm hoping to get a simple script that can fit between the two and give me a file that will say what the variables throughout the makefile had as their values.

Thanks
M

Trainee Chocolate Management Executive.
 
It might help if you give a sample of what you have in the files and what you want the result to be.

As it is stated now, it is unclear to me what you want exactly.


HTH,

p5wizard
 
I'm sorry for being unclear.Part Of The standard Makefile:
Code:
.c.o:
    @echo "Building <$*.c> for [red]$(TARGET)[/red]"
    @echo "Using [red]$(MBASEINC)[/red] type is [red]$(MCTYPE)[/red]"
    @rm -f $*.o
    [red]$(REMOTE) $(SETUP_ENV) $(CC)[/red] -c [red]$(CXFLAGS) $(COMPFLAGS) $(CFLAGS) $(DEFINE) $(LOCALINC) $(MBASEINC)[/red] $*.c > [red]$(LOGDIR)[/red]/$*.o.log 2>&1
    @cat [red]$(LOGDIR)[/red]/$*.o.log
    @if [ ! -r $*.o ] ; then echo ".................................. failed" | tee -a  [red]$(LOGDIR)[/red]/$*.o.log ; else  touch $*.o ; cp $*.o [red]$(OBJDIR)[/red]/; fi

Where all the variables marked in red are set in the environment from the preceding script. So what I want output from the above sample is:

Code:
.c.o:
    @echo "Building <$*.c> for [red]mips3[/red]"
    @echo "Using [red]-Wc[/red] type is [red]mips[/red]"
    @rm -f $*.o
    [red]ksh export PROJECT=M4; WD=/home/m/test/workspace/command icpc[/red] -c [red] -DLIB_DEF -DPOSIX -D -v -O2 -g3 -mips3 -common -DLOG_MESSAGE -I/home/m/test/workspace/command/include -I/home/m/test/workspace/general_include [/red]$*.c > [red]/home/m/test/workspace/logs/[/red]$*.o.log 2>&1
    @cat [red]/home/m/test/workspace/logs[/red]/$*.o.log
    @if [ ! -r $*.o ] ; then echo ".................................. failed" | tee -a  [red]/home/m/test/workspace/logs[/red]/$*.o.log ; else  touch $*.o ; cp $*.o [red]/home/m/test/workspace/command/obj[/red]/; fi
Where all the values of the variables are marked in red.
I hope this helps to explain what I mean to be asking.
Thank you for your help
M

Trainee Chocolate Management Executive.
 
Hi,

Did you try to use your make command with -n switch so the commands are printed but not executed and redirect the result to a new file ?
 
If it helps, I believe GNU sed offers 'in-place' processing, which means that you don't have to redirect your output to another file - always rather tiresome I think.
 
I would also suggest make -m or make -p (see man page).

I tried to play around with creating sed commands from an env-file, but as your keywords and their respective values contain shell metacharacters and spaces, it is kind of hard and I gave up.


HTH,

p5wizard
 
Thank you guys very much for the pointers to these make switches. They give me another way of going about doing what I wanted and it seems much easier.
Thank you all very much for your help.
M

Trainee Chocolate Management Executive.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top