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 {
49 private static BuildException buildException(Exception e)
51 if (e instanceof BuildException)
52 return (BuildException)e;
54 return new BuildException(e);
57 private static int indexOf(byte[] data, byte v, int start)
60 for (int i=start; i<data.length; i++)
66 private static String fixSoname(String s)
69 int i = s.lastIndexOf(".so");
70 if (i >= 0 && i < l-3)
71 return s.substring(0, i+3);
76 protected class Library implements Comparable<Library> {
78 protected final File file;
79 protected final ElfFile elf;
80 protected final HashSet<String> needed;
81 protected final Vector<String> rpath;
82 protected final TreeSet<Range> fixups;
83 protected final String subdir;
84 protected String soname, destname;
85 protected final HashSet<Library> dependencies;
86 protected boolean dependedUpon;
88 protected class Range implements Comparable<Range> {
90 public final long start, end;
92 public int compareTo(Range r)
96 else if (start > r.start)
100 else if (end > r.end)
106 public Range(long start, long end)
113 public int compareTo(Library l)
115 return destname.compareTo(l.destname);
118 protected void addNeeded(String so)
123 protected void addRpath(String rp)
125 for (String p : rp.split(":"))
126 if (!rpath.contains(p))
130 private String getDynstr(ElfFile.Dynamic d, byte[] s,
131 long base) throws Exception
133 int offs = (int)d.d_val;
134 int nul = indexOf(s, (byte)0, offs);
136 throw new Exception("Invalid dynamic string");
137 String name = new String(s, offs, nul-offs, "US-ASCII");
139 if (d.d_tag == ElfFile.DT_RPATH) {
141 fixups.add(new Range(offs, offs+name.length()));
143 String fix = fixSoname(name);
144 if (fix.length() < name.length()) {
145 fixups.add(new Range(offs+fix.length(),
146 offs+name.length()));
152 private void checkDynamic(ElfFile.SectionHeader dynsh,
153 ElfFile.SectionHeader strsh) throws Exception
155 byte strs[] = new byte[(int)strsh.sh_size];
156 elf.read(strsh, strs);
157 for (ElfFile.Dynamic d : elf.readDynamic(dynsh))
158 if (d.d_tag == ElfFile.DT_NULL)
160 else if (d.d_tag == ElfFile.DT_SONAME)
161 soname = getDynstr(d, strs, strsh.sh_offset);
162 else if (d.d_tag == ElfFile.DT_NEEDED)
163 addNeeded(getDynstr(d, strs, strsh.sh_offset));
164 else if (d.d_tag == ElfFile.DT_RPATH)
165 addRpath(getDynstr(d, strs, strsh.sh_offset));
168 private void checkElf() throws Exception
170 for (ElfFile.SectionHeader sh : elf.secHeaders)
171 if (sh.sh_type == ElfFile.SHT_DYNAMIC)
172 checkDynamic(sh, elf.secHeaders[sh.sh_link]);
175 protected File getDestName(File dest)
177 File d = new File(dest, subdir);
178 File f = new File(d, destname);
182 protected void writeTo(File dest) throws IOException
184 FileInputStream is = new FileInputStream(file);
185 FileOutputStream os = new FileOutputStream(dest);
186 byte[] buf = new byte[65536];
187 TreeSet<Range> ranges = new TreeSet<Range>(fixups);
190 long next = offs + buf.length;
191 if (!ranges.isEmpty()) {
192 next = ranges.first().start;
195 long chunk = next-offs;
196 if (chunk > buf.length)
198 int r = is.read(buf, 0, (int)chunk);
205 while (!ranges.isEmpty() &&
206 ranges.first().start <= offs) {
207 Range rg = ranges.pollFirst();
209 long chunk = rg.end-offs;
211 int slice = (chunk > buf.length?
212 buf.length : (int)chunk);
213 int r = is.read(buf, 0, slice);
217 Arrays.fill(buf, 0, r, (byte)0);
230 protected Library(File f, String s) throws Exception
234 elf = new ElfFile(file);
235 needed = new HashSet<String>();
236 rpath = new Vector<String>();
237 fixups = new TreeSet<Range>();
238 soname = f.getName();
239 dependencies = new HashSet<Library>();
240 dependedUpon = false;
242 destname = fixSoname(soname);
245 protected Library(Resource r) throws Exception
247 this(r.as(FileProvider.class).getFile(),
248 new File(r.getName()).getParent());
251 public String toString() { return "Library("+file+")"; }
254 protected class Worker
256 protected final int machine;
257 protected final Queue<Library> workQueue;
258 protected final HashMap<String,Library> knownLibs;
259 protected final HashSet<Library> processedLibs;
260 protected final HashSet<String> allDests;
261 protected final Vector<String> rpath;
263 protected void addWork(Library l)
267 Library kl = knownLibs.get(l.soname);
269 return; // Already processed
271 throw new BuildException("Multiple libs with the same soname "+
273 knownLibs.put(l.soname, l);
274 if (allDests.contains(l.destname))
275 throw new BuildException("Multiple libs with simplified soname "+
277 allDests.add(l.destname);
281 protected void addRpath(Vector<String> rp)
284 if (!rpath.contains(p))
288 protected void setDependency(Library l1, Library l2)
290 if (l2 == null) // Dependancy on external lib
292 l1.dependencies.add(l2);
293 l2.dependedUpon = true;
296 protected Library findLibInRpath(String s, String subdir)
299 for (String p : rpath) {
300 File f = new File(p, s);
302 Library l = new Library(f, subdir);
303 if (l.elf.header.e_machine == machine)
310 protected Library getLibForSoname(String s, String subdir)
313 Library l = knownLibs.get(s);
316 boolean include = false;
317 for (String patt : patterns.getIncludePatterns(getProject()))
318 if (SelectorUtils.match(patt, s)) {
323 for (String patt : patterns.getExcludePatterns(getProject()))
324 if (SelectorUtils.match(patt, s))
327 l = findLibInRpath(s, subdir);
329 throw new Exception("Library "+s+" not found");
334 protected void process(Library l) throws Exception
336 if (processedLibs.contains(l))
337 return; // Already processed
338 processedLibs.add(l);
340 for (String need : l.needed)
341 setDependency(l, getLibForSoname(need, l.subdir));
344 protected Vector<Library> topoSort(HashSet<Library> libs)
346 Vector<Library> order = new Vector<Library>();
347 for (Library chk : new HashSet<Library>(libs)) {
348 if (!chk.dependedUpon)
351 while (!libs.isEmpty()) {
352 HashSet<Library> leafs = new HashSet<Library>();
353 for (Library chk : new HashSet<Library>(libs)) {
354 if (chk.dependencies.isEmpty())
358 throw new BuildException("Circular dependency found");
359 ArrayList<Library> llist = new ArrayList<Library>(leafs);
360 Collections.sort(llist);
362 libs.removeAll(leafs);
363 for (Library l : libs)
364 l.dependencies.removeAll(leafs);
369 protected void execute() throws BuildException
372 while (!workQueue.isEmpty())
373 process(workQueue.remove());
374 } catch (Exception e) {
375 throw buildException(e);
377 if (property != null) {
378 Vector<Library> order =
379 topoSort(new HashSet<Library>(processedLibs));;
380 StringBuilder sb = new StringBuilder();
381 for (Library l : order) {
382 String name = l.destname;
383 if (name.startsWith("lib"))
384 name = name.substring(3);
385 if (name.endsWith(".so"))
386 name = name.substring(0, name.length()-3);
387 sb.append(" <item>");
389 sb.append("</item>\n");
391 String orderedLibs = sb.toString();
392 getProject().setNewProperty(property, orderedLibs);
394 for (Library chk : new HashSet<Library>(processedLibs)) {
395 File dest = chk.getDestName(destDir);
397 dest.lastModified() >= chk.file.lastModified())
398 processedLibs.remove(chk);
399 dest = dest.getParentFile();
403 if (processedLibs.isEmpty())
405 log("Copying "+processedLibs.size()+" libraries into "+destDir);
406 ArrayList<Library> libs = new ArrayList<Library>(processedLibs);
407 Collections.sort(libs);
409 for (Library l : libs)
410 l.writeTo(l.getDestName(destDir));
411 } catch (Exception e) {
412 throw buildException(e);
416 protected Worker(int mach)
419 workQueue = new LinkedList<Library>();
420 knownLibs = new HashMap<String,Library>();
421 processedLibs = new HashSet<Library>();
422 allDests = new HashSet<String>();
423 rpath = new Vector<String>();
427 protected File destDir = null; // the destination directory
428 protected Vector<ResourceCollection> rcs = new Vector<ResourceCollection>();
429 protected PatternSet patterns = new PatternSet();
430 protected String property = null;
432 public void setTodir(File destDir) {
433 this.destDir = destDir;
436 public void addFileset(FileSet set) {
440 public void add(ResourceCollection res) {
444 public PatternSet.NameEntry createExclude()
446 return patterns.createExclude();
449 public PatternSet.NameEntry createInclude()
451 return patterns.createInclude();
454 public void setProperty(String prop)
459 public void execute() throws BuildException {
460 HashMap<Integer,Worker> workers = new HashMap<Integer,Worker>();
461 final int size = rcs.size();
462 for (int i = 0; i < size; i++) {
463 ResourceCollection rc = rcs.elementAt(i);
464 for (Resource r : rc) {
466 String message = "Could not find library "
467 + r.toLongString() + " to copy.";
468 throw new BuildException(message);
473 } catch (Exception e) {
474 throw buildException(e);
476 Integer m = new Integer(l.elf.header.e_machine);
477 Worker w = workers.get(m);
479 workers.put(m, (w = new Worker(m.intValue())));
483 ArrayList<Integer> machines = new ArrayList<Integer>(workers.keySet());
484 Collections.sort(machines);
485 for (Integer m : machines)
486 workers.get(m).execute();