All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.mind.compilation.AbstractPreprocessorCommand Maven / Gradle / Ivy

There is a newer version: 2.0
Show newest version
/**
 * Copyright (C) 2009 STMicroelectronics
 *
 * This file is part of "Mind Compiler" is free software: you can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 * Contact: [email protected]
 *
 * Authors: Matthieu Leclercq
 * Contributors: 
 */

package org.ow2.mind.compilation;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public abstract class AbstractPreprocessorCommand
    implements
      PreprocessorCommand {

  protected final Map context;
  protected String                    cmd;
  protected final List        flags             = new ArrayList();
  protected File                      inputFile;
  protected File                      outputFile;
  protected File                      dependencyOutputFile;
  protected Collection          dependencies;
  protected boolean                   dependencyManaged = false;
  protected boolean                   forced            = false;

  private List                  inputFiles;
  private List                  outputFiles;

  protected AbstractPreprocessorCommand(final String cmd,
      final Map context) {
    this.cmd = cmd;
    this.context = context;
  }

  public String getCommand() {
    return cmd;
  }

  public void setCommand(final String command) {
    this.cmd = command;
  }

  public PreprocessorCommand addFlag(final String flag) {
    if (flag != null) flags.add(flag);
    return this;
  }

  public PreprocessorCommand addFlags(final Collection flags) {
    if (flags != null) this.flags.addAll(flags);
    return this;
  }

  public PreprocessorCommand addFlags(final String... flags) {
    if (flags != null) {
      for (final String flag : flags)
        this.flags.add(flag);
    }
    return this;
  }

  public PreprocessorCommand addDefine(final String name) {
    return addDefine(name, null);
  }

  public PreprocessorCommand setInputFile(final File inputFile) {
    this.inputFile = inputFile;
    return this;
  }

  public File getInputFile() {
    return inputFile;
  }

  public PreprocessorCommand setOutputFile(final File outputFile) {
    this.outputFile = outputFile;
    return this;
  }

  public File getOutputFile() {
    return outputFile;
  }

  public PreprocessorCommand addDependency(final File dependency) {
    if (dependencies == null) {
      dependencies = new ArrayList();
    }
    dependencies.add(dependency);
    return this;
  }

  public PreprocessorCommand setDependencyOutputFile(
      final File dependencyOutputFile) {
    this.dependencyOutputFile = dependencyOutputFile;
    return this;
  }

  public PreprocessorCommand setAllDependenciesManaged(
      final boolean dependencyManaged) {
    this.dependencyManaged = dependencyManaged;
    return this;
  }

  public Collection getInputFiles() {
    return inputFiles;
  }

  public Collection getOutputFiles() {
    return outputFiles;
  }

  public boolean forceExec() {
    return forced;
  }

  public void prepare() {
    if (dependencyManaged || dependencyOutputFile != null) {
      forced = false;
    } else {
      forced = true;
    }

    inputFiles = new ArrayList();
    inputFiles.add(inputFile);
    if (dependencies != null) {
      inputFiles.addAll(dependencies);
    }
    if (dependencyOutputFile != null) {
      final Collection deps = readDependencies();
      if (deps != null) {
        inputFiles.addAll(deps);
      } else {
        // Can't read dependencies, force execution.
        forced = true;
      }
    }

    if (dependencyOutputFile != null)
      outputFiles = Arrays.asList(outputFile, dependencyOutputFile);
    else
      outputFiles = Arrays.asList(outputFile);
  }

  protected abstract Collection readDependencies();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy