Commit e5e39b2f authored by Lionel Seinturier's avatar Lionel Seinturier
Browse files

Merge JuliacConfig into Juliac.

parent 81a7fd27
......@@ -31,7 +31,6 @@ import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.objectweb.fractal.juliac.conf.JuliacConfig;
import org.objectweb.fractal.juliac.module.CompileSupportItf;
/**
......@@ -41,7 +40,7 @@ import org.objectweb.fractal.juliac.module.CompileSupportItf;
*/
public class CompilationRound {
public CompilationRound( JuliacConfig jconf ) {
public CompilationRound( Juliac jconf ) {
this.jconf = jconf;
}
......@@ -123,7 +122,7 @@ public class CompilationRound {
// Data
// --------------------------------------------------------------------
private JuliacConfig jconf;
private Juliac jconf;
/** The source files which are given as input to Juliac. */
private List<SourceFile> inputFiles = new ArrayList<>();
......
......@@ -35,7 +35,6 @@ import java.util.logging.Logger;
import org.objectweb.fractal.api.factory.InstantiationException;
import org.objectweb.fractal.juliac.commons.util.CmdLineArgs;
import org.objectweb.fractal.juliac.conf.JDKLevel;
import org.objectweb.fractal.juliac.conf.JuliacConfig;
import org.objectweb.fractal.juliac.module.JuliacModuleItf;
/**
......@@ -70,9 +69,7 @@ public class JuliacCmdLine {
cla.registerFlags(CmdLineFlags.values());
Juliac jc = new Juliac();
JuliacConfig jconf = new JuliacConfig();
Logger logger = jconf.getLogger();
jc.setJuliacConfig(jconf);
Logger logger = jc.getLogger();
/*
* Analyze the command line arguments.
......@@ -85,7 +82,7 @@ public class JuliacCmdLine {
String baseDir = cla.getOptionValue(CmdLineOptions.BASEDIR);
if( baseDir != null ) {
File f = new File(baseDir);
jconf.setBaseDir(f);
jc.setBaseDir(f);
}
/*
......@@ -94,56 +91,56 @@ public class JuliacCmdLine {
*/
String s = cla.getOptionValue(CmdLineOptions.SRCS);
String[] srcs = s==null ? new String[0] : s.split(File.pathSeparator);
jconf.addSrcs(srcs);
jc.addSrcs(srcs);
s = cla.getOptionValue(CmdLineOptions.MIXINS);
String[] srclibs = s==null ? new String[0] : s.split(File.pathSeparator);
jconf.addSrclibs(srclibs);
jc.addSrclibs(srclibs);
String pkgRoot = cla.getOptionValue(CmdLineOptions.PKGROOT);
if( pkgRoot != null ) {
jconf.setPkgRoot(pkgRoot);
jc.setPkgRoot(pkgRoot);
}
String genDirName = cla.getOptionValue(CmdLineOptions.GENSRC);
if( genDirName != null ) {
jconf.setGenDirName(genDirName);
jc.setGenDirName(genDirName);
}
String classDirName = cla.getOptionValue(CmdLineOptions.GENCLASS);
if( classDirName != null ) {
jconf.setClassDirName(classDirName);
jc.setClassDirName(classDirName);
}
String source = cla.getOptionValue(CmdLineOptions.SOURCE);
if( source != null ) {
JDKLevel level = JDKLevel.valueOf(source);
jconf.setSourceLevel(level);
jc.setSourceLevel(level);
}
String target = cla.getOptionValue(CmdLineOptions.TARGET);
if( target != null ) {
JDKLevel level = JDKLevel.valueOf(target);
jconf.setTargetLevel(level);
jc.setTargetLevel(level);
}
String sourceEncoding = cla.getOptionValue(CmdLineOptions.SOURCEENCODING);
if ( sourceEncoding != null ) {
jconf.setSourceEncoding(sourceEncoding);
jc.setSourceEncoding(sourceEncoding);
}
boolean debug = cla.isFlagSet(CmdLineFlags.DEBUG);
if(debug) {
JuliacConfig.setLevel(logger,Level.FINE);
Juliac.setLevel(logger,Level.FINE);
}
boolean compilationWarnings =
cla.isFlagSet(CmdLineFlags.COMPILATIONWARNINGS);
jconf.setCompilationWarnings(compilationWarnings);
jc.setCompilationWarnings(compilationWarnings);
String juliaCfgFiles = cla.getOptionValue(CmdLineOptions.JULIACFGFILES);
if( juliaCfgFiles != null ) {
jconf.setJuliaCfgFiles(juliaCfgFiles);
jc.setJuliaCfgFiles(juliaCfgFiles);
}
/*
......@@ -231,9 +228,9 @@ public class JuliacCmdLine {
* If requested, compile the generated code.
*/
if( cla.isFlagSet(CmdLineFlags.COMPILEGENERATED) ) {
jconf.setCompilationWarnings(false);
jc.setCompilationWarnings(false);
jc.build();
jconf.setCompilationWarnings(compilationWarnings);
jc.setCompilationWarnings(compilationWarnings);
}
/*
......@@ -248,8 +245,8 @@ public class JuliacCmdLine {
*/
private static void usage() {
JuliacConfig jconf = new JuliacConfig();
Logger logger = jconf.getLogger();
Juliac jc = new Juliac();
Logger logger = jc.getLogger();
logger.info("java "+JuliacCmdLine.class.getName()+" [options] types");
logger.info("Generate the Java source code associated with the specified ADL, membrane or component descriptors");
......@@ -257,17 +254,17 @@ public class JuliacCmdLine {
logger.info("Options:");
logger.info("");
logger.info(" --gensrc <dir> : directory for generated source code (default: "+JuliacConfig.DEFAULT_GEN_DIR_NAME+")");
logger.info(" --genclass <dir> : directory for compiled code (default: "+JuliacConfig.DEFAULT_CLASS_DIR_NAME+")");
logger.info(" --gensrc <dir> : directory for generated source code (default: "+Juliac.DEFAULT_GEN_DIR_NAME+")");
logger.info(" --genclass <dir> : directory for compiled code (default: "+Juliac.DEFAULT_CLASS_DIR_NAME+")");
logger.info(" --iclassgs <generators> : interceptor class generators");
logger.info(" --icodegs <generators> : interceptor source code generators");
logger.info(" --juliaCfgFiles <files> : comma-separated list of Julia configuration files");
logger.info(" --mixins <dirs> : mixin layers");
logger.info(" --modules <modules> : modules");
logger.info(" --pkgRoot <name> : root package for generated code");
logger.info(" --source <version> : the Java source code compatibility level (default: "+JDKLevel.getDefaultSourceLevel(jconf.getLogger())+")");
logger.info(" --source <version> : the Java source code compatibility level (default: "+JDKLevel.getDefaultSourceLevel(jc.getLogger())+")");
logger.info(" --srcs <dirs> : source files (default: src/main/java)");
logger.info(" --target <version> : the Java bytecode compatibility level (default: "+JDKLevel.getDefaultTargetLevel(jconf.getLogger())+")");
logger.info(" --target <version> : the Java bytecode compatibility level (default: "+JDKLevel.getDefaultTargetLevel(jc.getLogger())+")");
logger.info(" --sourceEncoding <encoding> : the Java source encoding");
logger.info("");
logger.info(" --compilationWarnings : report compilation warnings");
......
......@@ -83,8 +83,7 @@ public class CfgLoader implements CfgLoaderItf {
// -----------------------------------------------------------------------
public void init( Juliac jc ) throws IOException {
JuliacConfig jconf = jc.getJuliacConfig();
classloader = jconf.getClassLoader();
classloader = jc.getClassLoader();
// Register the service
jc.register(CfgLoaderItf.class,this);
......
/***
* Juliac
* Copyright (C) 2008-2017 Inria, Univ. Lille 1
*
* This library 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 2 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: fractal@ow2.org
*
* Author: Lionel Seinturier
*/
package org.objectweb.fractal.juliac.conf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.processing.ProcessingEnvironment;
import org.objectweb.fractal.juliac.CompilationRound;
import org.objectweb.fractal.juliac.SourceFile;
import org.objectweb.fractal.juliac.commons.io.FileHelper;
/**
* This class manages the Juliac configuration parameters.
*
* @author Lionel Seinturier <Lionel.Seinturier@univ-lille1.fr>
* @since 2.1.4
*/
public class JuliacConfig {
// ----------------------------------------------------------------------
// Juliac properties
// ----------------------------------------------------------------------
/**
* The base directory for locations specified in the parameters.
*/
private File baseDir = new File(".");
/**
* The list of locations containing source files. A location denotes either
* an absolute path or a path relative to {@link #baseDir}, for a directory
* or a jar file.
*/
private List<String> srcs = new ArrayList<>();
/**
* The list of locations containing source files for librairies. A location
* denotes either an absolute path or a path relative to {@link #baseDir},
* for a directory or a jar file.
*/
private List<String> srclibs = new ArrayList<>();
/**
* The root package for generated code.
*/
private String pkgRoot = "";
/**
* The name of the directory where generated files are stored. The name
* denotes either an absolute path or a path relative to {@link #baseDir}.
* The resulting {@link File} is stored in {@link #genDirName}.
*/
private String genDirName = DEFAULT_GEN_DIR_NAME;
/**
* Default directory name for generated files.
* @since 2.7
*/
final public static String DEFAULT_GEN_DIR_NAME =
"target/generated-sources/juliac";
/**
* The directory where generated files are stored.
*/
private File genDir;
/**
* The name of the directory where generated files are compiled. The name
* denotes either an absolute path or a path relative to {@link #baseDir}.
* The resulting {@link File} is stored in {@link #classDir}.
*/
private String classDirName = DEFAULT_CLASS_DIR_NAME;
/**
* Default directory name for compiled generated files.
* @since 2.7
*/
final public static String DEFAULT_CLASS_DIR_NAME = "target/classes";
/**
* The directory where generated files are compiled.
*/
private File classDir;
/**
* JDK level for source code.
* @since 2.2.2
*/
private JDKLevel sourceLevel = JDKLevel.getDefaultSourceLevel(getLogger());
/**
* JDK level for class files.
* @since 2.2.2
*/
private JDKLevel targetLevel = JDKLevel.getDefaultTargetLevel(getLogger());
/**
* Encoding for source files.
* @since 2.6
*/
private String sourceEncoding = DEFAULT_SOURCE_ENCODING;
/**
* Default encoding for source files.
* @since 2.6
*/
final public static String DEFAULT_SOURCE_ENCODING =
Charset.defaultCharset().displayName();
private ClassLoader classLoader;
private CompilationRound round;
/**
* The default logger.
* @since 2.2.4
*/
private Logger logger = null;
/**
* The name of the default logger.
* @since 2.2.4
*/
private final static String LOGGER_NAME = "org.objectweb.fractal.juliac";
/**
* The logger for the compiler plugin.
* @since 2.2.4
*/
private Logger compilationLogger = null;
/**
* The name of the logger for the compiler plugin.
* @since 2.2.4
*/
private final static String COMPILATION_LOGGER_NAME =
"org.objectweb.fractal.juliac.plugin.compiler";
/**
* <code>true</code> if compilation warnings should be reported.
* @since 2.2.4
*/
private boolean compilationWarnings = false;
/**
* Reference to the processing environment of the JDK annotation processing
* tool for the Adlet extension.
*
* @since 2.7
*/
private ProcessingEnvironment processingEnvironment = null;
/**
* The comma-separated list of Julia configuration files.
* @since 2.7
*/
private String juliaCfgFiles = "";
// ----------------------------------------------------------------------
// Setter/getter methods
// ----------------------------------------------------------------------
public void setProcessingEnvironment( ProcessingEnvironment pe ) {
this.processingEnvironment = pe;
}
public ProcessingEnvironment getProcessingEnvironment() {
return processingEnvironment;
}
public void setJuliaCfgFiles( String juliaConfig ) {
this.juliaCfgFiles = juliaConfig;
}
public String getJuliaCfgFiles() {
return juliaCfgFiles;
}
// ----------------------------------------------------------------------
// Setter/getter methods for Juliac file and directory related properties
// ----------------------------------------------------------------------
public void setPkgRoot( final String pkgRoot ) {
this.pkgRoot = pkgRoot;
if( pkgRoot!=null && pkgRoot.length()!=0 ) {
this.pkgRoot += '.';
}
}
public String getPkgRoot() { return pkgRoot; }
public void setBaseDir( File baseDir ) { this.baseDir = baseDir; }
public File getBaseDir() { return baseDir; }
public String[] getSrcs() {
/*
* Return a copy in order to prevent unwanted adding in the list.
* Elements must be added in the list by invoking either {@link
* #addSrc(String)} or {@link #addSrcs(String[])}.
*/
return srcs.toArray(new String[srcs.size()]);
}
/**
* Add the specified location to the list of locations. A location denotes
* either an absolute path or a path relative to {@link #baseDir}, for a
* directory or a jar file. All source files contained in the specified
* location are registered with the CompilationRound API for being
* compiled when requested (see {@link #build()}) or processed by the
* juliac-spoon module (see SpoonSupportImpl.)
*
* @param src the location to add
* @throws IOException
* if the location does not exist or an error occurred while fetching
* the source files contained in the location
*/
public void addSrc( String src ) throws IOException {
/*
* Retrieve all source files contained in src.
*/
File baseDir = getBaseDir();
List<SourceFile> sfs = new ArrayList<>();
SourceFile.addAllJavaFiles(baseDir,src,sfs);
/*
* Register the source files with the current compilation round.
*/
CompilationRound round = getCompilationRound();
round.addInput(sfs);
srcs.add(src);
}
/**
* Invoke {@link #addSrc(String)} for each element contained in the
* specified array of locations.
*/
public void addSrcs( String[] srcs ) throws IOException {
for (String src : srcs) {
addSrc(src);
}
}
/**
* Add the specified resource file to the class directory.
*
* @param filename the name of the resource file
* @param sb the content of the resource file
* @source 2.7
*/
public void addResource( String filename, StringBuffer sb )
throws IOException {
// Copy the resource to the class directory
File genDir = getClassDir();
File file = new File(genDir,filename);
file.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte[] content = sb.toString().getBytes();
fos.write(content);
fos.close();
logger.info(filename+" copied to "+file.getAbsolutePath());
}
public String[] getSrclibs() {
/*
* Return a copy in order to prevent unwanted adding in the list.
* Elements must be added in the list by invoking either {@link
* #addSrclib(String)} or {@link #addSrclibs(String[])}.
*/
return srclibs.toArray(new String[srclibs.size()]);
}
/**
* Add the specified location to the list of library locations. A location
* denotes either an absolute path or a path relative to {@link #baseDir},
* for a directory or a jar file.
*
* @param src the location to add
*/
public void addSrclib( String srclib ) {
srclibs.add(srclib);
}
/**
* Invoke {@link #addSrclibs(String)} for each element contained in the
* specified array of locations.
*/
public void addSrclibs( String[] srclibs ) {
for (String srclib : srclibs) {
addSrclib(srclib);
}
}
public File getGenDir() throws IOException {
if( genDir == null ) {
File baseDir = getBaseDir();
String genDirName = getGenDirName();
genDir = initDir(baseDir,genDirName);
}
return genDir;
}
public String getGenDirName() { return genDirName; }
public void setGenDirName( String genDirName ) throws IOException {
File baseDir = getBaseDir();
genDir = initDir(baseDir,genDirName);
}
public File getClassDir() throws IOException {
if( classDir == null ) {
File baseDir = getBaseDir();
String classDirName = getClassDirName();
classDir = initDir(baseDir,classDirName);
}
return classDir;
}
public String getClassDirName() { return classDirName; }
public void setClassDirName( String classDirName ) throws IOException {
File baseDir = getBaseDir();
classDir = initDir(baseDir,classDirName);
}
// ----------------------------------------------------------------------
// Setter/getter methods for Java compilation related properties
// ----------------------------------------------------------------------
public JDKLevel getSourceLevel() { return sourceLevel; }
public void setSourceLevel( JDKLevel sourceLevel ) {
this.sourceLevel = sourceLevel;
}
public JDKLevel getTargetLevel() { return targetLevel; }
public void setTargetLevel( JDKLevel targetLevel ) {
this.targetLevel = targetLevel;
}
/** @since 2.6 */
public String getSourceEncoding() { return sourceEncoding; }
/** @since 2.6 */
public void setSourceEncoding( String encoding ) {
this.sourceEncoding = encoding;
}
// ----------------------------------------------------------------------
// Setter/getter methods for loaders and compilation rounds
// ----------------------------------------------------------------------
public CompilationRound getCompilationRound() {
if( round == null ) {
round = new CompilationRound(this);
}
return round;
}
public ClassLoader getClassLoader() {
if( classLoader == null ) {
classLoader = getClass().getClassLoader();
}
return classLoader;
}
public void setClassLoader( ClassLoader classLoader ) {
this.classLoader = classLoader;
}
// ----------------------------------------------------------------------
// Logging facilities
// ----------------------------------------------------------------------