Error in Makefile calling sed with comment charact

2019-08-11 11:31发布

问题:

I'm trying to build lstrip, a Lua utility for compressing Lua source code. I'm trying to build for Lua 5.1.3 on OS X v10.10.3.

I have downloaded and extracted the Lua 5.1.3 source code and modified the Makefile for lstrip to point to this directory. However, when I run make, I get this output:

cc -I/usr/local/src/lua-5.1.3/src -I/usr/local/src/lua-5.1.3/src -O2 -Wall -Wextra -O2    -c -o lstrip.o lstrip.c
lstrip.c:33:14: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char* argv[])
             ^
1 warning generated.
sed '/void luaX_next/i#include "proxy.c"' /usr/local/src/lua-5.1.3/src/llex.c > llex.c
sed: 1: "/void luaX_next/i#inclu ...": command i expects \ followed by text
make: *** [llex.c] Error 1

This is what the relevant Makefile command looks like:

llex.c:
    sed '/void luaX_next/i#include "proxy.c"' $(LUASRC)/$@ > $@

I think that this is because the # in the sed command is being treated like an actual comment, but I'm not sure.

How can I fix the Makefile, or manually run the steps, to get lstrip built?


Full copy of the Makefile follows, in case it matters:

# makefile for lstrip

# change these to reflect your Lua installation (Lua 5.1!)
LUA= /usr/local/src/lua-5.1.3
LUAINC= $(LUA)/src
LUALIB= $(LUA)/src
LUASRC= $(LUA)/src

# no need to change anything below here
CFLAGS= $(INCS) $(WARN) -O2 $G
WARN= -O2 -Wall -Wextra

INCS= -I$(LUAINC) -I$(LUASRC)
LIBS= -L$(LUALIB) -llua -lm

MYNAME= lstrip
MYLIB= $(MYNAME)
T= $(MYNAME)
OBJS= $(MYNAME).o llex.o
TEST= test.lua

all:    test

test:   $T
    $T $(TEST)

$T: $(OBJS)
    $(CC) -o $@ $(OBJS) $(LIBS)

llex.c:
    sed '/void luaX_next/i#include "proxy.c"' $(LUASRC)/$@ > $@

llex.o: proxy.c

clean:
    -rm -f $(OBJS) core core.* a.out $(MYNAME)

# eof

Hand-build solution:

  1. cp /usr/local/src/lua-5.1.3/src/llex.c .
  2. Hand-edit llex.c to add the line #include "proxy.c" before the line starting with void luaX_next (line 446 for me).
  3. Now run make, which will succeed.

回答1:

This line works in Mac OS X and in Linux:

sed '/void luaX_next/{h;s/.*/#include "proxy.c"/;p;g;}' $(LUASRC)/$@ > $@


回答2:

You can find the answer in the sed manual, and it is in the Makefile lines

llex.c:
    sed '/void luaX_next/i#include "proxy.c"' $(LUASRC)/$@ > $@

Some variable expansion takes place here: $(LUASRC) is expanded to the variable set above -> $(LUA)/src. Recurse as needed. $@ is replaced with the current target (llex.c)

So this recipe says: In order to obtain the target llex.c (which will be processed later through other recipes), apply a stream editing command to the file $LUASRC/llex.c and write it to llex.c.

The stream editing command is: look for text "void luaX_next", before printing it, insert line "#include "proxy.c"". Problem is, the command to do this is not "i" but "i\(newline)", which conflicts with a requirement of Makefiles that recipes must be on a single line.

I suspect that in order to fix your Makefile you need to use a different command than sed; awk can fit the bill although it's a bit more complex.



标签: sed lua makefile