]> sigrok.org Git - sigrok-androidutils.git/blobdiff - ant/src/org/sigrok/androidutils/ant/CopyLibsTask.java
device_filter.xml: Update.
[sigrok-androidutils.git] / ant / src / org / sigrok / androidutils / ant / CopyLibsTask.java
index f2f93954adfbea32566f30dbde608d250458f1c5..38e5e0d97f7b3cfeefbf4d3791e12a73829d1f13 100644 (file)
@@ -35,6 +35,7 @@ import java.util.Queue;
 import java.util.TreeSet;
 import java.util.Vector;
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.DynamicAttribute;
 import org.apache.tools.ant.Task;
 import org.apache.tools.ant.types.FileSet;
 import org.apache.tools.ant.types.PatternSet;
@@ -43,8 +44,16 @@ import org.apache.tools.ant.types.ResourceCollection;
 import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.types.selectors.SelectorUtils;
 
-public class CopyLibsTask extends Task
+public class CopyLibsTask extends Task implements DynamicAttribute
 {
+       private static final HashMap<String,String> blacklist;
+
+       static {
+               HashMap<String,String> bl = new HashMap<String,String>();
+               bl.put("libpcre.so", "libercp.so");
+               blacklist = bl;
+       }
+
        private static BuildException buildException(Exception e)
        {
                if (e instanceof BuildException)
@@ -70,9 +79,13 @@ public class CopyLibsTask extends Task
                int i = s.lastIndexOf(".so");
 
                if (i >= 0 && i < (l - 3))
-                       return s.substring(0, i + 3);
-               else
-                       return s;
+                       s = s.substring(0, i + 3);
+
+               String bl = blacklist.get(s);
+               if (bl != null)
+                       s = bl;
+
+               return s;
        }
 
        protected class Library implements Comparable<Library>
@@ -90,6 +103,7 @@ public class CopyLibsTask extends Task
                protected class Range implements Comparable<Range>
                {
                        public final long start, end;
+                       public final byte[] replacement;
 
                        public int compareTo(Range r)
                        {
@@ -105,10 +119,16 @@ public class CopyLibsTask extends Task
                                        return 0;
                        }
 
-                       public Range(long start, long end)
+                       public Range(long start, long end, byte[] replacement)
                        {
                                this.start = start;
                                this.end = end;
+                               this.replacement = replacement;
+                       }
+
+                       public Range(long start, long end)
+                       {
+                               this(start, end, null);
                        }
                }
 
@@ -142,7 +162,8 @@ public class CopyLibsTask extends Task
                        String name = new String(s, offs, nul-offs, "US-ASCII");
                        offs += base;
 
-                       if (d.d_tag == ElfFile.DT_RPATH) {
+                       if (d.d_tag == ElfFile.DT_RPATH ||
+                           d.d_tag == ElfFile.DT_RUNPATH) {
                                // Zap rpath,
                                fixups.add(new Range(offs, offs + name.length()));
                        } else {
@@ -151,6 +172,9 @@ public class CopyLibsTask extends Task
                                        fixups.add(new Range(offs + fix.length(),
                                                offs + name.length()));
                                }
+                               if (!fix.equals(name.substring(0, fix.length()))) {
+                                       fixups.add(new Range(offs, offs + fix.length(), fix.getBytes("US-ASCII")));
+                               }
                        }
                        return name;
                }
@@ -169,7 +193,8 @@ public class CopyLibsTask extends Task
                                        soname = getDynstr(d, strs, strsh.sh_offset);
                                else if (d.d_tag == ElfFile.DT_NEEDED)
                                        addNeeded(getDynstr(d, strs, strsh.sh_offset));
-                               else if (d.d_tag == ElfFile.DT_RPATH)
+                               else if (d.d_tag == ElfFile.DT_RPATH ||
+                                        d.d_tag == ElfFile.DT_RUNPATH)
                                        addRpath(getDynstr(d, strs, strsh.sh_offset));
                        }
                }
@@ -184,7 +209,7 @@ public class CopyLibsTask extends Task
 
                protected File getDestName(File dest)
                {
-                       File d = new File(dest, subdir);
+                       File d = (subdir == null? dest : new File(dest, subdir));
                        File f = new File(d, destname);
                        return f;
                }
@@ -222,7 +247,10 @@ public class CopyLibsTask extends Task
                                                        if (r < 0)
                                                                break outer;
                                                        if (r > 0) {
-                                                               Arrays.fill(buf, 0, r, (byte)0);
+                                                               if (rg.replacement == null)
+                                                                       Arrays.fill(buf, 0, r, (byte)0);
+                                                               else
+                                                                       System.arraycopy(rg.replacement, (int)(offs-rg.start), buf, 0, r);
                                                                os.write(buf, 0, r);
                                                                chunk -= r;
                                                        }
@@ -299,7 +327,7 @@ public class CopyLibsTask extends Task
 
                protected void setDependency(Library l1, Library l2)
                {
-                       if (l2 == null) // Dependancy on external lib.
+                       if (l2 == null) // Dependency on external lib.
                                return;
                        l1.dependencies.add(l2);
                        l2.dependedUpon = true;
@@ -326,16 +354,22 @@ public class CopyLibsTask extends Task
                        if (l != null)
                                return l;
                        boolean include = false;
-                       for (String patt : patterns.getIncludePatterns(getProject())) {
-                               if (SelectorUtils.match(patt, s)) {
-                                       include = true;
-                                       break;
+                       String[] includePatterns = patterns.getIncludePatterns(getProject());
+                       if (includePatterns != null) {
+                               for (String patt : includePatterns) {
+                                       if (SelectorUtils.match(patt, s)) {
+                                               include = true;
+                                               break;
+                                       }
                                }
                        }
                        if (!include) {
-                               for (String patt : patterns.getExcludePatterns(getProject())) {
-                                       if (SelectorUtils.match(patt, s))
-                                               return null;
+                               String[] excludePatterns = patterns.getExcludePatterns(getProject());
+                               if (excludePatterns != null) {
+                                       for (String patt : excludePatterns) {
+                                               if (SelectorUtils.match(patt, s))
+                                                       return null;
+                                       }
                                }
                        }
                        l = findLibInRpath(s, subdir);
@@ -348,7 +382,7 @@ public class CopyLibsTask extends Task
                protected void process(Library l) throws Exception
                {
                        if (processedLibs.contains(l))
-                               return; // Already processed
+                               return; // Already processed.
                        processedLibs.add(l);
                        addRpath(l.rpath);
                        for (String need : l.needed)
@@ -390,7 +424,7 @@ public class CopyLibsTask extends Task
                        }
                        if (property != null) {
                                Vector<Library> order =
-                                       topoSort(new HashSet<Library>(processedLibs));;
+                                       topoSort(new HashSet<Library>(processedLibs));
                                StringBuilder sb = new StringBuilder();
                                for (Library l : order) {
                                        String name = l.destname;
@@ -439,16 +473,25 @@ public class CopyLibsTask extends Task
 
        };
 
-       protected File destDir = null;  // The destination directory.
+       protected File destDir = null; // The destination directory.
        protected Vector<ResourceCollection> rcs = new Vector<ResourceCollection>();
        protected PatternSet patterns = new PatternSet();
        protected String property = null;
+       protected Vector<String> rpath = new Vector<String>();
 
        public void setTodir(File destDir)
        {
                this.destDir = destDir;
        }
 
+       public void setDynamicAttribute(String name, String value)
+       {
+               if ("rpath-link".equals(name))
+                       this.rpath.add(value);
+               else
+                       throw new BuildException("copylibs doesn't support the \"" + name + "\" attribute");
+       }
+
        public void addFileset(FileSet set)
        {
                add(set);
@@ -495,8 +538,10 @@ public class CopyLibsTask extends Task
                                }
                                Integer m = new Integer(l.elf.header.e_machine);
                                Worker w = workers.get(m);
-                               if (w == null)
+                               if (w == null) {
                                        workers.put(m, (w = new Worker(m.intValue())));
+                                       w.addRpath(rpath);
+                               }
                                w.addWork(l);
                        }
                }