Handling build attributes

Adriaan de Groot <adridg-FlD2LfDziEhmR6Xm/[email protected]>
Newsgroups gmane.comp.tools.aap.devel
Message-ID <[email protected]>
Suppose (this is a hypothetical recipe - I know objecttype doesn't make much 
sense in the places where I use it here):

:program a : { add_INCLUDE=-Ithing } 
	{ objecttype = frobnicated } 
	b.c 
	c.c { objecttype = discombobulate } { add_INCLUDE = -Iboo } 
	d.c { var_INCLUDE=-I/usr }

I had an earlier patch that applied build attributes ( { add_INCLUDE=-Ithing } 
{ objecttype = frobnicated } ) to sources, but it did it wrong and Bram told 
me to use spaces around = and to put the attributes in the build_attr, not in 
the nodes. So I have, and patch is attached. It's somewhat larger than 
before, because it takes the following into account:

- If a source sets an attribute that is also a build attribute, then the 
source attribute takes precedence. So c.c is compiled to a discombobulated 
object file, and not a frobnicated one. 

- If a source has an var_* attribute and the build attributes have a 
corresponding var_* or add_*, the source takes precedence.

- If a source has an add_ attribute and the build has a var_ attribute, 
compute var_ + add_ and set that as var_

- If a source has an add_ attribute and build has one too, prepend the build 
attribute to the source attribute.

This recipe shows all the combinations:

:program b : { var_INCLUDE = -I1 } { add_CFLAGS = -DD=2 }
        b.c
        c.c { add_INCLUDE = -I/usr/local/include }
        d.c { var_INCLUDE = -I/root }
        e.c { add_CFLAGS = -DDEBUG }
        f.c { var_CFLAGS = -DFROBNICATE }

with AAP output:

Aap:  cc   -I1 -DD=2   -MM b.c > build-FreeBSD4_9_RC/b.c.aap
Aap: cc   -I1 -O2 -DD=2 -DD=2 -c -o build-FreeBSD4_9_RC/b.o b.c
Aap:  cc   -I1 -I/usr/local/include -DD=2   -MM c.c > 
build-FreeBSD4_9_RC/c.c.aap
Aap: cc   -I1 -I/usr/local/include -O2 -DD=2 -DD=2 -c -o 
build-FreeBSD4_9_RC/c.o c.c
Aap:  cc   -I/root -DD=2   -MM d.c > build-FreeBSD4_9_RC/d.c.aap
Aap: cc   -I/root -O2 -DD=2 -DD=2 -c -o build-FreeBSD4_9_RC/d.o d.c
Aap:  cc   -I1 -DD=2 -DDEBUG   -MM e.c > build-FreeBSD4_9_RC/e.c.aap
Aap: cc   -I1 -O2 -DD=2 -DDEBUG -DD=2 -DDEBUG -c -o build-FreeBSD4_9_RC/e.o 
e.c
Aap:  cc   -I1 -DFROBNICATE   -MM f.c > build-FreeBSD4_9_RC/f.c.aap
Aap: cc   -I1 -O2 -DFROBNICATE -c -o build-FreeBSD4_9_RC/f.o f.c

Heck, it makes sense to me. Docs are forthcoming, unless thinking about bdir() 
gets in the way.
buildattr.diff (text/x-diff, 2.3 KB)
Index: Commands.py
===================================================================
RCS file: /cvsroot/a-a-p/Exec/Commands.py,v
retrieving revision 1.111
diff -u -3 -p -r1.111 Commands.py
--- Commands.py	29 Oct 2003 20:30:30 -0000	1.111
+++ Commands.py	30 Oct 2003 12:59:21 -0000
@@ -328,7 +328,7 @@ def aap_program(line_nr, recdict, arg, t
     if len(targetlist) != 1:
         recipe_error(rpstack, _(":%s requires one target") % cmdname)
 
-    # get any build attributes {attr = value}.
+    # get any build attributes {attr = value} from after the ':'.
     build_attr = dictlist[colonidx]
 
     # Expand the sources into dictlists.
@@ -336,6 +336,39 @@ def aap_program(line_nr, recdict, arg, t
     sourcelist = dictlist_expand(sourcelist)
     if len(sourcelist) < 1:
         recipe_error(rpstack, _(":%s requires at least one source") % cmdname)
+
+    # If there are build attributes, apply them to all the
+    # source files.
+    for ba in build_attr:
+        # Everything is named already, skip it
+        if ba == "name":
+            continue
+        value = build_attr[ba]
+        if ba.startswith("add_") or ba.startswith("var_"):
+            # Build var_ + source var_ => source takes precedence
+            # Build add_ + source var_ => source takes precedence
+            # Build var_ + source add_ => append source to build
+            # Build add_ + source add_ => append source to build ?
+            # Build var_ + no source   => use build var
+            # Build add_ + no source   => use build add
+            varname = ba[4:]
+            for source in sourcelist:
+                # Source-level var_ attributes take precedence
+                if source.has_key("var_"+varname):
+                    continue
+                # Prepend build attribute
+                if source.has_key("add_"+varname):
+                    if ba.startswith("add_"):
+                        source[ba] = value + " " + source[ba]
+                    else:
+		        source["var_"+varname]=value + " " + source["add_"+varname]
+			del source["add_"+varname]
+                else:
+                    source[ba] = value
+        else:
+            for source in sourcelist:
+                if not source.has_key(ba):
+                    source[ba] = value
 
     # ":produce abc" needs to declare "abc" as a filetype.
     if cmdname == "produce":
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.