View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks.gcc.cross;
21  
22  import java.io.File;
23  
24  import org.apache.tools.ant.BuildException;
25  
26  import com.github.maven_nar.cpptasks.CCTask;
27  import com.github.maven_nar.cpptasks.LinkerParam;
28  import com.github.maven_nar.cpptasks.compiler.CommandLineLinkerConfiguration;
29  import com.github.maven_nar.cpptasks.compiler.LinkType;
30  import com.github.maven_nar.cpptasks.compiler.Linker;
31  import com.github.maven_nar.cpptasks.gcc.AbstractArLibrarian;
32  
33  /**
34   * Adapter for the 'ar' archiver
35   *
36   * @author Adam Murdoch
37   */
38  public final class GccLibrarian extends AbstractArLibrarian {
39    private static String[] objFileExtensions = new String[] {
40      ".o"
41    };
42    private static GccLibrarian instance = new GccLibrarian("ar", objFileExtensions, false, new GccLibrarian("ar",
43        objFileExtensions, true, null));
44  
45    public static GccLibrarian getInstance() {
46      return instance;
47    }
48  
49    private GccLibrarian(final String command, final String[] inputExtensions, final boolean isLibtool,
50        final GccLibrarian libtoolLibrarian) {
51      super(command, "V", inputExtensions, new String[0], "lib", ".a", isLibtool, libtoolLibrarian);
52    }
53  
54    @Override
55    protected Object clone() throws CloneNotSupportedException {
56      final GccLibrarian clone = (GccLibrarian) super.clone();
57      return clone;
58    }
59  
60    @Override
61    public Linker getLinker(final LinkType type) {
62      return GccLinker.getInstance().getLinker(type);
63    }
64  
65    @Override
66    public void link(final CCTask task, final File outputFile, final String[] sourceFiles,
67        final CommandLineLinkerConfiguration config) throws BuildException {
68      try {
69        final GccLibrarian clone = (GccLibrarian) this.clone();
70        final LinkerParam param = config.getParam("target");
71        if (param != null) {
72          clone.setCommand(param.getValue() + "-" + this.getCommand());
73        }
74        clone.superlink(task, outputFile, sourceFiles, config);
75      } catch (final CloneNotSupportedException e) {
76        superlink(task, outputFile, sourceFiles, config);
77      }
78    }
79  
80    private void superlink(final CCTask task, final File outputFile, final String[] sourceFiles,
81        final CommandLineLinkerConfiguration config) throws BuildException {
82      super.link(task, outputFile, sourceFiles, config);
83    }
84  }