Esempio n. 1
0
 /** @return The standard Linker configuration with 'testOptions' added to the argument list. */
 public final LinkerDef getTestLinker(
     final AbstractCompileMojo mojo,
     final CCTask task,
     final String os,
     final String prefix,
     final String type)
     throws MojoFailureException, MojoExecutionException {
   final LinkerDef linker = getLinker(mojo, task, os, prefix, type);
   if (this.testOptions != null) {
     for (final Iterator i = this.testOptions.iterator(); i.hasNext(); ) {
       final LinkerArgument arg = new LinkerArgument();
       arg.setValue((String) i.next());
       linker.addConfiguredLinkerArg(arg);
     }
   }
   return linker;
 }
Esempio n. 2
0
  private void addLibraries(
      final String libraryList,
      final LinkerDef linker,
      final Project antProject,
      final boolean isSystem) {

    if (libraryList == null) {
      return;
    }

    final String[] lib = libraryList.split(",");

    for (final String element : lib) {

      final String[] libInfo = element.trim().split(":", 3);

      LibrarySet librarySet = new LibrarySet();

      if (isSystem) {
        librarySet = new SystemLibrarySet();
      }

      librarySet.setProject(antProject);
      librarySet.setLibs(new CUtil.StringArrayBuilder(libInfo[0]));

      if (libInfo.length > 1) {

        final LibraryTypeEnum libType = new LibraryTypeEnum();

        libType.setValue(libInfo[1]);
        librarySet.setType(libType);

        if (!isSystem && libInfo.length > 2) {
          librarySet.setDir(new File(libInfo[2]));
        }
      }

      if (!isSystem) {
        linker.addLibset(librarySet);
      } else {
        linker.addSyslibset((SystemLibrarySet) librarySet);
      }
    }
  }
  private void createLibrary(final Project antProject, final Library library)
      throws MojoExecutionException, MojoFailureException {
    getLog().debug("Creating Library " + library);
    // configure task
    final CCTask task = new CCTask();
    task.setCommandLogLevel(this.commandLogLevel);
    task.setProject(antProject);

    task.setDecorateLinkerOptions(this.decorateLinkerOptions);

    // subsystem
    final SubsystemEnum subSystem = new SubsystemEnum();
    subSystem.setValue(library.getSubSystem());
    task.setSubsystem(subSystem);

    // set max cores
    task.setMaxCores(getMaxCores(getAOL()));

    // outtype
    final OutputTypeEnum outTypeEnum = new OutputTypeEnum();
    final String type = library.getType();
    outTypeEnum.setValue(type);
    task.setOuttype(outTypeEnum);

    // stdc++
    task.setLinkCPP(library.linkCPP());

    // fortran
    task.setLinkFortran(library.linkFortran());
    task.setLinkFortranMain(library.linkFortranMain());

    // outDir
    File outDir;
    if (type.equals(Library.EXECUTABLE)) {
      outDir =
          getLayout()
              .getBinDirectory(
                  getTargetDirectory(),
                  getMavenProject().getArtifactId(),
                  getMavenProject().getVersion(),
                  getAOL().toString());
    } else {
      outDir =
          getLayout()
              .getLibDirectory(
                  getTargetDirectory(),
                  getMavenProject().getArtifactId(),
                  getMavenProject().getVersion(),
                  getAOL().toString(),
                  type);
    }
    outDir.mkdirs();

    // outFile
    // FIXME NAR-90 we could get the final name from layout
    final File outFile = new File(outDir, getOutput(getAOL(), type));
    getLog().debug("NAR - output: '" + outFile + "'");
    task.setOutfile(outFile);

    // object directory
    File objDir = new File(getTargetDirectory(), "obj");
    objDir = new File(objDir, getAOL().toString());
    objDir.mkdirs();
    task.setObjdir(objDir);

    // failOnError, libtool
    task.setFailonerror(failOnError(getAOL()));
    task.setLibtool(useLibtool(getAOL()));

    // runtime
    final RuntimeType runtimeType = new RuntimeType();
    runtimeType.setValue(getRuntime(getAOL()));
    task.setRuntime(runtimeType);

    // IDL, MC, RC compilations should probably be 'generate source' type
    // actions, seperate from main build.
    // Needs resolution of handling for generate sources.
    // Order is somewhat important here, IDL and MC generate outputs that are
    // (often) included in the RC compilation
    if (getIdl() != null) {
      final CompilerDef idl = getIdl().getCompiler(Compiler.MAIN, null);
      if (idl != null) {
        task.addConfiguredCompiler(idl);
      }
    }
    if (getMessage() != null) {
      final CompilerDef mc = getMessage().getCompiler(Compiler.MAIN, null);
      if (mc != null) {
        task.addConfiguredCompiler(mc);
      }
    }
    if (getResource() != null) {
      final CompilerDef res = getResource().getCompiler(Compiler.MAIN, null);
      if (res != null) {
        task.addConfiguredCompiler(res);
      }
    }

    // Darren Sargent Feb 11 2010: Use Compiler.MAIN for "type"...appears the
    // wrong "type" variable was being used
    // since getCompiler() expects "main" or "test", whereas the "type" variable
    // here is "executable", "shared" etc.
    // add C++ compiler
    if (getCpp() != null) {
      final CompilerDef cpp = getCpp().getCompiler(Compiler.MAIN, null);
      if (cpp != null) {
        task.addConfiguredCompiler(cpp);
      }
    }

    // add C compiler
    if (getC() != null) {
      final CompilerDef c = getC().getCompiler(Compiler.MAIN, null);
      if (c != null) {
        task.addConfiguredCompiler(c);
      }
    }

    // add Fortran compiler
    if (getFortran() != null) {
      final CompilerDef fortran = getFortran().getCompiler(Compiler.MAIN, null);
      if (fortran != null) {
        task.addConfiguredCompiler(fortran);
      }
    }
    // end Darren

    // add javah include path
    final File jniDirectory = getJavah().getJniDirectory();
    if (jniDirectory.exists()) {
      task.createIncludePath().setPath(jniDirectory.getPath());
    }

    // add java include paths
    getJava().addIncludePaths(task, type);

    final List<NarArtifact> dependencies = getNarArtifacts();
    // add dependency include paths
    for (final Object element : dependencies) {
      // FIXME, handle multiple includes from one NAR
      final NarArtifact narDependency = (NarArtifact) element;
      final String binding = narDependency.getNarInfo().getBinding(getAOL(), Library.STATIC);
      getLog().debug("Looking for " + narDependency + " found binding " + binding);
      if (!binding.equals(Library.JNI)) {
        final File unpackDirectory = getUnpackDirectory();
        final File include =
            getLayout()
                .getIncludeDirectory(
                    unpackDirectory, narDependency.getArtifactId(), narDependency.getBaseVersion());
        getLog().debug("Looking for include directory: " + include);
        if (include.exists()) {
          task.createIncludePath().setPath(include.getPath());
        } else {
          throw new MojoExecutionException("NAR: unable to locate include path: " + include);
        }
      }
    }

    // add linker
    final LinkerDef linkerDefinition =
        getLinker().getLinker(this, antProject, getOS(), getAOL().getKey() + ".linker.", type);
    task.addConfiguredLinker(linkerDefinition);

    // add dependency libraries
    // FIXME: what about PLUGIN and STATIC, depending on STATIC, should we
    // not add all libraries, see NARPLUGIN-96
    if (type.equals(Library.SHARED)
        || type.equals(Library.JNI)
        || type.equals(Library.EXECUTABLE)) {

      final List depLibOrder = getDependencyLibOrder();
      List depLibs = dependencies;

      // reorder the libraries that come from the nar dependencies
      // to comply with the order specified by the user
      if (depLibOrder != null && !depLibOrder.isEmpty()) {
        final List tmp = new LinkedList();

        for (final Iterator i = depLibOrder.iterator(); i.hasNext(); ) {
          final String depToOrderName = (String) i.next();

          for (final Iterator j = depLibs.iterator(); j.hasNext(); ) {
            final NarArtifact dep = (NarArtifact) j.next();
            final String depName = dep.getGroupId() + ":" + dep.getArtifactId();

            if (depName.equals(depToOrderName)) {
              tmp.add(dep);
              j.remove();
            }
          }
        }

        tmp.addAll(depLibs);
        depLibs = tmp;
      }

      for (final Iterator i = depLibs.iterator(); i.hasNext(); ) {
        final NarArtifact dependency = (NarArtifact) i.next();

        // FIXME no handling of "local"

        // FIXME, no way to override this at this stage
        final String binding = dependency.getNarInfo().getBinding(getAOL(), Library.NONE);
        getLog().debug("Using Binding: " + binding);
        AOL aol = getAOL();
        aol = dependency.getNarInfo().getAOL(getAOL());
        getLog().debug("Using Library AOL: " + aol.toString());

        if (!binding.equals(Library.JNI)
            && !binding.equals(Library.NONE)
            && !binding.equals(Library.EXECUTABLE)) {
          final File unpackDirectory = getUnpackDirectory();

          final File dir =
              getLayout()
                  .getLibDirectory(
                      unpackDirectory,
                      dependency.getArtifactId(),
                      dependency.getBaseVersion(),
                      aol.toString(),
                      binding);

          getLog().debug("Looking for Library Directory: " + dir);
          if (dir.exists()) {
            final LibrarySet libSet = new LibrarySet();
            libSet.setProject(antProject);

            // FIXME, no way to override
            final String libs = dependency.getNarInfo().getLibs(getAOL());
            if (libs != null && !libs.equals("")) {
              getLog().debug("Using LIBS = " + libs);
              libSet.setLibs(new CUtil.StringArrayBuilder(libs));
              libSet.setDir(dir);
              task.addLibset(libSet);
            }
          } else {
            getLog().debug("Library Directory " + dir + " does NOT exist.");
          }

          // FIXME, look again at this, for multiple dependencies we may need to
          // remove duplicates
          final String options = dependency.getNarInfo().getOptions(getAOL());
          if (options != null && !options.equals("")) {
            getLog().debug("Using OPTIONS = " + options);
            final LinkerArgument arg = new LinkerArgument();
            arg.setValue(options);
            linkerDefinition.addConfiguredLinkerArg(arg);
          }

          final String sysLibs = dependency.getNarInfo().getSysLibs(getAOL());
          if (sysLibs != null && !sysLibs.equals("")) {
            getLog().debug("Using SYSLIBS = " + sysLibs);
            final SystemLibrarySet sysLibSet = new SystemLibrarySet();
            sysLibSet.setProject(antProject);

            sysLibSet.setLibs(new CUtil.StringArrayBuilder(sysLibs));
            task.addSyslibset(sysLibSet);
          }
        }
      }
    }

    // Add JVM to linker
    getJava().addRuntime(task, getJavaHome(getAOL()), getOS(), getAOL().getKey() + ".java.");

    // execute
    try {
      task.execute();
    } catch (final BuildException e) {
      throw new MojoExecutionException("NAR: Compile failed", e);
    }

    // FIXME, this should be done in CPPTasks at some point
    if (getRuntime(getAOL()).equals("dynamic")
        && getOS().equals(OS.WINDOWS)
        && getLinker().getName(null, null).equals("msvc")
        && !getLinker().getVersion().startsWith("6.")) {
      final String libType = library.getType();
      if (libType.equals(Library.JNI) || libType.equals(Library.SHARED)) {
        final String dll = outFile.getPath() + ".dll";
        final String manifest = dll + ".manifest";
        final int result =
            NarUtil.runCommand(
                "mt.exe",
                new String[] {"/manifest", manifest, "/outputresource:" + dll + ";#2"},
                null,
                null,
                getLog());
        if (result != 0) {
          throw new MojoFailureException("MT.EXE failed with exit code: " + result);
        }
      } else if (libType.equals(Library.EXECUTABLE)) {
        final String exe = outFile.getPath() + ".exe";
        final String manifest = exe + ".manifest";
        final int result =
            NarUtil.runCommand(
                "mt.exe",
                new String[] {"/manifest", manifest, "/outputresource:" + exe + ";#1"},
                null,
                null,
                getLog());
        if (result != 0) {
          throw new MojoFailureException("MT.EXE failed with exit code: " + result);
        }
      }
    }
  }
Esempio n. 4
0
  public final LinkerDef getLinker(
      final AbstractCompileMojo mojo,
      final CCTask task,
      final String os,
      final String prefix,
      final String type)
      throws MojoFailureException, MojoExecutionException {
    Project antProject = task.getProject();
    if (this.name == null) {
      throw new MojoFailureException("NAR: Please specify a <Name> as part of <Linker>");
    }

    final LinkerDef linker = new LinkerDef();
    linker.setProject(antProject);
    final LinkerEnum linkerEnum = new LinkerEnum();
    linkerEnum.setValue(this.name);
    linker.setName(linkerEnum);

    // tool path
    if (this.toolPath != null) {
      linker.setToolPath(this.toolPath);
    } else if ("msvc".equalsIgnoreCase(name)) {
      linker.setToolPath(mojo.getMsvc().getToolPath());
    }

    // incremental, map
    linker.setIncremental(this.incremental);
    linker.setMap(this.map);

    // Add definitions (Window only)
    if (os.equals(OS.WINDOWS)
        && getName(null, null).equals("msvc")
        && (type.equals(Library.SHARED) || type.equals(Library.JNI))) {
      final Set defs = new HashSet();
      try {
        if (mojo.getC() != null) {
          final List cSrcDirs = mojo.getC().getSourceDirectories();
          for (final Iterator i = cSrcDirs.iterator(); i.hasNext(); ) {
            final File dir = (File) i.next();
            if (dir.exists()) {
              defs.addAll(FileUtils.getFiles(dir, "**/*.def", null));
            }
          }
        }
      } catch (final IOException e) {
      }
      try {
        if (mojo.getCpp() != null) {
          final List cppSrcDirs = mojo.getCpp().getSourceDirectories();
          for (final Iterator i = cppSrcDirs.iterator(); i.hasNext(); ) {
            final File dir = (File) i.next();
            if (dir.exists()) {
              defs.addAll(FileUtils.getFiles(dir, "**/*.def", null));
            }
          }
        }
      } catch (final IOException e) {
      }
      try {
        if (mojo.getFortran() != null) {
          final List fortranSrcDirs = mojo.getFortran().getSourceDirectories();
          for (final Iterator i = fortranSrcDirs.iterator(); i.hasNext(); ) {
            final File dir = (File) i.next();
            if (dir.exists()) {
              defs.addAll(FileUtils.getFiles(dir, "**/*.def", null));
            }
          }
        }
      } catch (final IOException e) {
      }

      for (final Iterator i = defs.iterator(); i.hasNext(); ) {
        final LinkerArgument arg = new LinkerArgument();
        arg.setValue("/def:" + i.next());
        linker.addConfiguredLinkerArg(arg);
      }
    }

    // FIXME, this should be done in CPPTasks at some point, and may not be
    // necessary, but was for VS 2010 beta 2
    if (os.equals(OS.WINDOWS)
        && getName(null, null).equals("msvc")
        && !getVersion(mojo).startsWith("6.")
        && (type.equals(Library.SHARED)
            || type.equals(Library.JNI)
            || type.equals(Library.EXECUTABLE))) {
      final LinkerArgument arg = new LinkerArgument();
      if (isGenerateManifest()) arg.setValue("/MANIFEST");
      else arg.setValue("/MANIFEST:NO");
      linker.addConfiguredLinkerArg(arg);

      if (isGenerateManifest()) {
        final LinkerArgument arg2 = new LinkerArgument();
        arg2.setValue("/MANIFESTFILE:" + task.getOutfile() + ".manifest");
        linker.addConfiguredLinkerArg(arg2);
      }
    }

    // Add options to linker
    if (this.options != null) {
      for (final Iterator i = this.options.iterator(); i.hasNext(); ) {
        final LinkerArgument arg = new LinkerArgument();
        arg.setValue((String) i.next());
        linker.addConfiguredLinkerArg(arg);
      }
    }

    if (this.optionSet != null) {

      final String[] opts = this.optionSet.split("\\s");

      for (final String opt : opts) {

        final LinkerArgument arg = new LinkerArgument();

        arg.setValue(opt);
        linker.addConfiguredLinkerArg(arg);
      }
    }

    if (!this.clearDefaultOptions) {
      final String option =
          NarProperties.getInstance(mojo.getMavenProject()).getProperty(prefix + "options");
      if (option != null) {
        final String[] opt = option.split(" ");
        for (final String element : opt) {
          final LinkerArgument arg = new LinkerArgument();
          arg.setValue(element);
          linker.addConfiguredLinkerArg(arg);
        }
      }
    }

    // record the preference for nar dependency library link order
    if (this.narDependencyLibOrder != null) {

      final List libOrder = new LinkedList();

      final String[] lib = this.narDependencyLibOrder.split(",");

      for (final String element : lib) {
        libOrder.add(element.trim());
      }

      mojo.setDependencyLibOrder(libOrder);
    }

    // Add Libraries to linker
    if (this.libs != null || this.libSet != null) {

      if (this.libs != null) {

        for (final Iterator i = this.libs.iterator(); i.hasNext(); ) {

          final Lib lib = (Lib) i.next();
          lib.addLibSet(mojo, linker, antProject);
        }
      }

      if (this.libSet != null) {
        addLibraries(this.libSet, linker, antProject, false);
      }
    } else {

      final String libsList =
          NarProperties.getInstance(mojo.getMavenProject()).getProperty(prefix + "libs");

      addLibraries(libsList, linker, antProject, false);
    }

    // Add System Libraries to linker
    if (this.sysLibs != null || this.sysLibSet != null) {

      if (this.sysLibs != null) {

        for (final Iterator i = this.sysLibs.iterator(); i.hasNext(); ) {

          final SysLib sysLib = (SysLib) i.next();
          linker.addSyslibset(sysLib.getSysLibSet(antProject));
        }
      }

      if (this.sysLibSet != null) {
        addLibraries(this.sysLibSet, linker, antProject, true);
      }
    } else {

      final String sysLibsList =
          NarProperties.getInstance(mojo.getMavenProject()).getProperty(prefix + "sysLibs");

      addLibraries(sysLibsList, linker, antProject, true);
    }

    mojo.getMsvc().configureLinker(linker);

    return linker;
  }