Re: [PATCH] Makefile: Remove excess backslashes from sed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Apr 08, 2010 at 10:57:56PM -0700, Junio C Hamano wrote:
> Brian Gernhardt <brian@xxxxxxxxxxxxxxxxxxxxx> writes:
> 
> > The sed script that was intended to add lines altering the sys.path
> > had extra backslashes in them.  Instead resulting in
> >
> >   import sys;  import os;  sys.path.insert( ... )
> >
> > It output
> >
> >   import sys; \ import os; \ sys.path.insert( ... )
> >
> > Unfortunately this caused python (2.6.1 on OS X 10.6.3) to error
> >
> >   SyntaxError: unexpected character after line continuation character
> >
> > Removing two of the backslashes solves this problem.
> 
> Traditionally, multi-line sed statements written in the Makefile were
> portability nightmare, as the line folding rules (especially how the
> backslash is treated in the output) in make implementations were subtly
> different, and implementations of sed also got multi-line statement often
> wrong.  These days, things might have gotten much better, but in olden
> days (back when BSD vs SysV war was still raging), the trick to write
> things like this portably was to invoke a shell script that has multi-line
> sed statement from the Makefile.  It was painful.
> 
> I wonder if we can make this a lot simpler to avoid multi-line sed script.
> For example, we could write the source Python script to always begin with:
> 
> 	#!/usr/bin/python
>         import sys;
>         import os;
>         sys.path.insert(0, os.getenv("GITPYTHONLIB","."));
> 
> and then do something like this in the Makefile:
> 
> 	INSTLIBDIR=... && \
> 	sed -e '1s|#!.*python|#!$(PYTHON_PATH_SQ)|' \
>         -e 's|\(os\.getenv("GITPYTHONLIB"\)[^)]*)|\1,"'"$$INSTLIBDIR"'")|' \
>         <$@.py >$@+
> 	mv $@+ $@
> 
> Contributors can then run things in-place while developing the scripts,
> perhaps setting GITPYTHONLIB to the source area.

Here's what I cooked up so far...:

$(patsubst %.py,%,$(SCRIPT_PYTHON)): GIT-CFLAGS
$(patsubst %.py,%,$(SCRIPT_PYTHON)): % : %.py
	$(QUIET_GEN)$(RM) $@ $@+ && \
	INSTLIBDIR=`MAKEFLAGS= $(MAKE) -C git_remote_helpers -s \
		--no-print-directory prefix='$(prefix_SQ)' DESTDIR='$(DESTDIR_SQ)' \
		instlibdir` && \
	sed -e '1{' \
	    -e '	s|#!.*python|#!$(PYTHON_PATH_SQ)\n@@STARTUP@@\n|' \
	    -e '}' \
	    -e 's|@@STARTUP@@|import os, sys; sys.path.insert(0, @@ENV@@)|' \
	    -e 's|@@ENV@@|os.getenv("GITPYTHONLIB", "@@INSTLIBDIR@@")|' \
	    -e 's|@@INSTLIBDIR@@|'"$$INSTLIBDIR"'|g' \
	    $@.py >$@+ && \
	chmod +x $@+ && \
	mv $@+ $@

It avoids the multi-line portability concerns by substituting variables
along each step.

This is also nicer in that we don't need to start scripts with
"import sys" or os.getenv() anything...  all we require is a
shebang line.  We inject @@STARTUP@@ when we rewrite the
shebang.

That seems nicer then the old expression which searched for
"import sys.*" when doing the replacements.

Is the \n@@STARTUP\n thing portable?

-- 
		David
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]