Makefile error calling sed with comment character

I am trying to create lstrip

, a Lua utility to compress Lua source code. I am trying to build for Lua 5.1.3 OS X v10.10.3.

I downloaded and extracted the Lua 5.1.3 source code and modified the Makefile 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 corresponding Makefile command looks like:

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

      

I think this is because #

sed is being treated like an actual comment in the command, but I'm not sure.

How can I fix the Makefile or manually follow the steps to get it lstrip

?


A complete 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

      


Manual assembly solution:

  • cp /usr/local/src/lua-5.1.3/src/llex.c .

  • Hand-modify llex.c to add a line #include "proxy.c"

    before the line starting with void luaX_next

    (line 446 for me).
  • Now run make

    that will be successful.
+1


source to share


2 answers


This line works on Mac OS X and Linux:



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

      

+1


source


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

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

      

This is where some variable expansion happens: $ (LUASRC) expands to the above variable -> $ (LUA) / src. Be careful if necessary. $ @ is replaced with the current target (llex.c)



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

Stream edit command: find the text "void luaX_next", before printing it, insert the line "#include" proxy.c "". The problem is that the command for this is not "i" but "i \ (newline)", which contradicts the Makefile's requirement that the recipes must be on one line.

I suspect that you need to use a different command than sed to fix your Makefile; awk can fit the score, although it's a little more complex.

+2


source







All Articles