2 * This file is part of the sigrok-androidutils project.
4 * Copyright (C) 2014 Marcus Comstedt <marcus@mc.pp.se>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package org.sigrok.androidutils.ant;
23 import java.io.IOException;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.LinkedList;
34 import java.util.Queue;
35 import java.util.TreeSet;
36 import java.util.Vector;
37 import org.apache.tools.ant.BuildException;
38 import org.apache.tools.ant.Task;
39 import org.apache.tools.ant.types.FileSet;
40 import org.apache.tools.ant.types.PatternSet;
41 import org.apache.tools.ant.types.Resource;
42 import org.apache.tools.ant.types.ResourceCollection;
43 import org.apache.tools.ant.types.resources.FileProvider;
44 import org.apache.tools.ant.types.selectors.SelectorUtils;
46 public class CopyLibsTask extends Task
48 private static final HashMap<String,String> blacklist;
51 HashMap bl = new HashMap<String,String>();
52 bl.put("libpcre.so", "libercp.so");
56 private static BuildException buildException(Exception e)
58 if (e instanceof BuildException)
59 return (BuildException)e;
61 return new BuildException(e);
64 private static int indexOf(byte[] data, byte v, int start)
67 for (int i = start; i < data.length; i++) {
75 private static String fixSoname(String s)
78 int i = s.lastIndexOf(".so");
80 if (i >= 0 && i < (l - 3))
81 s = s.substring(0, i + 3);
83 String bl = blacklist.get(s);
90 protected class Library implements Comparable<Library>
92 protected final File file;
93 protected final ElfFile elf;
94 protected final HashSet<String> needed;
95 protected final Vector<String> rpath;
96 protected final TreeSet<Range> fixups;
97 protected final String subdir;
98 protected String soname, destname;
99 protected final HashSet<Library> dependencies;
100 protected boolean dependedUpon;
102 protected class Range implements Comparable<Range>
104 public final long start, end;
105 public final byte[] replacement;
107 public int compareTo(Range r)
111 else if (start > r.start)
113 else if (end < r.end)
115 else if (end > r.end)
121 public Range(long start, long end, byte[] replacement)
125 this.replacement = replacement;
128 public Range(long start, long end)
130 this(start, end, null);
134 public int compareTo(Library l)
136 return destname.compareTo(l.destname);
139 protected void addNeeded(String so)
144 protected void addRpath(String rp)
146 for (String p : rp.split(":")) {
147 if (!rpath.contains(p))
152 private String getDynstr(ElfFile.Dynamic d, byte[] s,
153 long base) throws Exception
155 int offs = (int)d.d_val;
156 int nul = indexOf(s, (byte)0, offs);
159 throw new Exception("Invalid dynamic string");
161 String name = new String(s, offs, nul-offs, "US-ASCII");
164 if (d.d_tag == ElfFile.DT_RPATH ||
165 d.d_tag == ElfFile.DT_RUNPATH) {
167 fixups.add(new Range(offs, offs + name.length()));
169 String fix = fixSoname(name);
170 if (fix.length() < name.length()) {
171 fixups.add(new Range(offs + fix.length(),
172 offs + name.length()));
174 if (!fix.equals(name.substring(0, fix.length()))) {
175 fixups.add(new Range(offs, offs + fix.length(), fix.getBytes("US-ASCII")));
181 private void checkDynamic(ElfFile.SectionHeader dynsh,
182 ElfFile.SectionHeader strsh) throws Exception
184 byte strs[] = new byte[(int)strsh.sh_size];
186 elf.read(strsh, strs);
188 for (ElfFile.Dynamic d : elf.readDynamic(dynsh)) {
189 if (d.d_tag == ElfFile.DT_NULL)
191 else if (d.d_tag == ElfFile.DT_SONAME)
192 soname = getDynstr(d, strs, strsh.sh_offset);
193 else if (d.d_tag == ElfFile.DT_NEEDED)
194 addNeeded(getDynstr(d, strs, strsh.sh_offset));
195 else if (d.d_tag == ElfFile.DT_RPATH ||
196 d.d_tag == ElfFile.DT_RUNPATH)
197 addRpath(getDynstr(d, strs, strsh.sh_offset));
201 private void checkElf() throws Exception
203 for (ElfFile.SectionHeader sh : elf.secHeaders) {
204 if (sh.sh_type == ElfFile.SHT_DYNAMIC)
205 checkDynamic(sh, elf.secHeaders[sh.sh_link]);
209 protected File getDestName(File dest)
211 File d = new File(dest, subdir);
212 File f = new File(d, destname);
216 protected void writeTo(File dest) throws IOException
218 FileInputStream is = new FileInputStream(file);
219 FileOutputStream os = new FileOutputStream(dest);
220 byte[] buf = new byte[65536];
221 TreeSet<Range> ranges = new TreeSet<Range>(fixups);
225 long next = offs + buf.length;
226 if (!ranges.isEmpty())
227 next = ranges.first().start;
229 long chunk = next - offs;
230 if (chunk > buf.length)
232 int r = is.read(buf, 0, (int)chunk);
239 while (!ranges.isEmpty() && ranges.first().start <= offs) {
240 Range rg = ranges.pollFirst();
242 long chunk = rg.end - offs;
244 int slice = (chunk > buf.length ? buf.length : (int)chunk);
245 int r = is.read(buf, 0, slice);
249 if (rg.replacement == null)
250 Arrays.fill(buf, 0, r, (byte)0);
252 System.arraycopy(rg.replacement, (int)(offs-rg.start), buf, 0, r);
266 protected Library(File f, String s) throws Exception
270 elf = new ElfFile(file);
271 needed = new HashSet<String>();
272 rpath = new Vector<String>();
273 fixups = new TreeSet<Range>();
274 soname = f.getName();
275 dependencies = new HashSet<Library>();
276 dependedUpon = false;
278 destname = fixSoname(soname);
281 protected Library(Resource r) throws Exception
283 this(r.as(FileProvider.class).getFile(),
284 new File(r.getName()).getParent());
287 public String toString()
289 return "Library(" + file + ")";
294 protected class Worker
296 protected final int machine;
297 protected final Queue<Library> workQueue;
298 protected final HashMap<String,Library> knownLibs;
299 protected final HashSet<Library> processedLibs;
300 protected final HashSet<String> allDests;
301 protected final Vector<String> rpath;
303 protected void addWork(Library l)
307 Library kl = knownLibs.get(l.soname);
309 return; // Already processed.
311 throw new BuildException("Multiple libs with the same soname " + l.soname);
312 knownLibs.put(l.soname, l);
313 if (allDests.contains(l.destname))
314 throw new BuildException("Multiple libs with simplified soname " + l.destname);
315 allDests.add(l.destname);
319 protected void addRpath(Vector<String> rp)
321 for (String p : rp) {
322 if (!rpath.contains(p))
327 protected void setDependency(Library l1, Library l2)
329 if (l2 == null) // Dependency on external lib.
331 l1.dependencies.add(l2);
332 l2.dependedUpon = true;
335 protected Library findLibInRpath(String s, String subdir)
338 for (String p : rpath) {
339 File f = new File(p, s);
341 Library l = new Library(f, subdir);
342 if (l.elf.header.e_machine == machine)
349 protected Library getLibForSoname(String s, String subdir)
352 Library l = knownLibs.get(s);
355 boolean include = false;
356 String[] includePatterns = patterns.getIncludePatterns(getProject());
357 if (includePatterns != null) {
358 for (String patt : includePatterns) {
359 if (SelectorUtils.match(patt, s)) {
366 String[] excludePatterns = patterns.getExcludePatterns(getProject());
367 if (excludePatterns != null) {
368 for (String patt : excludePatterns) {
369 if (SelectorUtils.match(patt, s))
374 l = findLibInRpath(s, subdir);
376 throw new Exception("Library " + s + " not found");
381 protected void process(Library l) throws Exception
383 if (processedLibs.contains(l))
384 return; // Already processed.
385 processedLibs.add(l);
387 for (String need : l.needed)
388 setDependency(l, getLibForSoname(need, l.subdir));
391 protected Vector<Library> topoSort(HashSet<Library> libs)
393 Vector<Library> order = new Vector<Library>();
394 for (Library chk : new HashSet<Library>(libs)) {
395 if (!chk.dependedUpon)
398 while (!libs.isEmpty()) {
399 HashSet<Library> leafs = new HashSet<Library>();
400 for (Library chk : new HashSet<Library>(libs)) {
401 if (chk.dependencies.isEmpty())
405 throw new BuildException("Circular dependency found");
406 ArrayList<Library> llist = new ArrayList<Library>(leafs);
407 Collections.sort(llist);
409 libs.removeAll(leafs);
410 for (Library l : libs)
411 l.dependencies.removeAll(leafs);
416 protected void execute() throws BuildException
419 while (!workQueue.isEmpty())
420 process(workQueue.remove());
421 } catch (Exception e) {
422 throw buildException(e);
424 if (property != null) {
425 Vector<Library> order =
426 topoSort(new HashSet<Library>(processedLibs));
427 StringBuilder sb = new StringBuilder();
428 for (Library l : order) {
429 String name = l.destname;
430 if (name.startsWith("lib"))
431 name = name.substring(3);
432 if (name.endsWith(".so"))
433 name = name.substring(0, name.length() - 3);
434 sb.append(" <item>");
436 sb.append("</item>\n");
438 String orderedLibs = sb.toString();
439 getProject().setNewProperty(property, orderedLibs);
441 for (Library chk : new HashSet<Library>(processedLibs)) {
442 File dest = chk.getDestName(destDir);
444 dest.lastModified() >= chk.file.lastModified())
445 processedLibs.remove(chk);
446 dest = dest.getParentFile();
450 if (processedLibs.isEmpty())
452 log("Copying " + processedLibs.size() + " libraries into " + destDir);
453 ArrayList<Library> libs = new ArrayList<Library>(processedLibs);
454 Collections.sort(libs);
456 for (Library l : libs)
457 l.writeTo(l.getDestName(destDir));
458 } catch (Exception e) {
459 throw buildException(e);
463 protected Worker(int mach)
466 workQueue = new LinkedList<Library>();
467 knownLibs = new HashMap<String,Library>();
468 processedLibs = new HashSet<Library>();
469 allDests = new HashSet<String>();
470 rpath = new Vector<String>();
475 protected File destDir = null; // The destination directory.
476 protected Vector<ResourceCollection> rcs = new Vector<ResourceCollection>();
477 protected PatternSet patterns = new PatternSet();
478 protected String property = null;
480 public void setTodir(File destDir)
482 this.destDir = destDir;
485 public void addFileset(FileSet set)
490 public void add(ResourceCollection res)
495 public PatternSet.NameEntry createExclude()
497 return patterns.createExclude();
500 public PatternSet.NameEntry createInclude()
502 return patterns.createInclude();
505 public void setProperty(String prop)
510 public void execute() throws BuildException
512 HashMap<Integer,Worker> workers = new HashMap<Integer,Worker>();
513 final int size = rcs.size();
515 for (int i = 0; i < size; i++) {
516 ResourceCollection rc = rcs.elementAt(i);
517 for (Resource r : rc) {
519 String message = "Could not find library "
520 + r.toLongString() + " to copy.";
521 throw new BuildException(message);
526 } catch (Exception e) {
527 throw buildException(e);
529 Integer m = new Integer(l.elf.header.e_machine);
530 Worker w = workers.get(m);
532 workers.put(m, (w = new Worker(m.intValue())));
536 ArrayList<Integer> machines = new ArrayList<Integer>(workers.keySet());
537 Collections.sort(machines);
538 for (Integer m : machines)
539 workers.get(m).execute();