Class Builder
java.lang.Object
org.bytedeco.javacpp.tools.Builder
The Builder is responsible for coordinating efforts between the Parser, the
Generator, and the native compiler. It contains the main() method, and basically
takes care of the tasks one would expect from a command line build tool, but
can also be used programmatically by setting its properties and calling build().
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) String[]A system command forProcessBuilderto execute for the build, instead of JavaCPP itself.(package private) ClassScanner(package private) booleanIf true, deletes all files fromoutputDirectorybefore writing anything in it.(package private) CommandExecutorAn alternative CommandExecutor to use to execute commands.(package private) booleanIf true, compiles the generated source file to a shared library and deletes source.(package private) Collection<String> Contains additional command line options from the user for the native compiler.(package private) FileThe name of the directory where to output config files for GraalVM native-image, if notnull.(package private) booleanIf true, also copies to the output directory dependent shared libraries (link and preload).(package private) booleanIf true, also copies to the output directory resources listed in properties.(package private) booleanIf true, preserves the generated C++ JNI files after compilation.(package private) StringThe name of the character encoding used for input files as well as output files.User specified environment variables to pass to the native compiler.(package private) booleanIf true, attempts to generate C++ JNI files, but if false, only attempts to parse header files.(package private) booleanIf true, also generates C++ header files containing declarations of callback functions.(package private) StringThe name of the JAR file to create, if notnull.(package private) final LoggerLogger where to send debug, info, warning, and error messages.(package private) FileThe directory where the generated files and compiled shared libraries get written to.(package private) StringThe name of the output generated source file or shared library.(package private) PropertiesAccumulates the various properties loaded from resources, files, command line options, etc.(package private) FileUser specified working directory to execute build subprocesses under. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddProperty(String key, String... values) Adds values to a given property key, seperating them with "platform.path.separator".File[]build()Starts the build process and returns an array ofFileproduced.buildCommand(String[] buildCommand) Sets thebuildCommandfield to the argument.classesOrPackages(String... classesOrPackages) Requests theclassScannerto add a class or all classes from a package.classPaths(String classPaths) Splits argument withFile.pathSeparatorand appends result to paths of theclassScanner.classPaths(String... classPaths) Appends argument to the paths of theclassScanner.clean(boolean clean) Sets thecleanfield to the argument.(package private) voidDeletesoutputDirectoryifcleanis true.commandExecutor(CommandExecutor commandExecutor) Sets thecommandExecutorfield to the argument.compile(boolean compile) Sets thecompilefield to the argument.(package private) intcompile(String[] sourceFilenames, String outputFilename, ClassProperties properties, File workingDirectory) Launches and waits for the native compiler to produce a native shared library.compilerOptions(String... options) Appends arguments to thecompilerOptionsfield.configDirectory(File configDirectory) Sets theconfigDirectoryfield to the argument.configDirectory(String configDirectory) Sets theconfigDirectoryfield to the argument.copyLibs(boolean copyLibs) Sets thecopyLibsfield to the argument.copyResources(boolean copyResources) Sets thecopyResourcesfield to the argument.(package private) voidStores all the files in the given JAR file.deleteJniFiles(boolean deleteJniFiles) Sets thedeleteJniFilesfield to the argument.Sets theencodingfield to the argument.environmentVariables(Map<String, String> environmentVariables) Sets theenvironmentVariablesfield to the argument.generate(boolean generate) Sets thegeneratefield to the argument.(package private) File[]generateAndCompile(Class[] classes, String outputName, boolean first, boolean last) Generates C++ source files for classes, and compiles everything in one shared library whencompile == true.(package private) FilegetOutputPath(Class[] classes, String[] sourcePrefixes) Creates and returns the directory where output files should be placed.Returnsproperties.getProperty(String key) Returnsproperties.getProperty(key).header(boolean header) Sets theheaderfield to the argument.(package private) voidincludeJavaPaths(ClassProperties properties, boolean header) Tries to find automatically include paths forjni.handjni_md.h, as well as the link and library paths for thejvmlibrary.Sets thejarPrefixfield to the argument.static voidThe terminal shell interface to the Builder.outputDirectory(File outputDirectory) Sets theoutputDirectoryfield to the argument.outputDirectory(String outputDirectory) Sets theoutputDirectoryfield to the argument.outputName(String outputName) Sets theoutputNamefield to the argument.(package private) File[]CallsParser.parse(File, String[], Class)after creating an instance of the Class.static voidSimply prints out to the display the command line usage.properties(String platform) Sets thepropertiesfield to the ones loaded from resources for the specified platform.properties(Properties properties) Adds all the properties of the argument to thepropertiesfield.Sets a property of thepropertiesfield, in either "key=value" or "key:value" format.Sets a key/value pair property of thepropertiesfield.propertyFile(File propertyFile) Sets thepropertiesfield to the ones loaded from the specified file.propertyFile(String filename) Sets thepropertiesfield to the ones loaded from the specified file.workingDirectory(File workingDirectory) Sets theworkingDirectoryfield to the argument.workingDirectory(String workingDirectory) Sets theworkingDirectoryfield to the argument.
-
Field Details
-
logger
Logger where to send debug, info, warning, and error messages. -
encoding
String encodingThe name of the character encoding used for input files as well as output files. -
outputDirectory
File outputDirectoryThe directory where the generated files and compiled shared libraries get written to. By default they are placed in the same directory as the.classfile. -
outputName
String outputNameThe name of the output generated source file or shared library. This enables single- file output mode. By default, the top-level enclosing classes get one file each. -
configDirectory
File configDirectoryThe name of the directory where to output config files for GraalVM native-image, if notnull. -
jarPrefix
String jarPrefixThe name of the JAR file to create, if notnull. -
clean
boolean cleanIf true, deletes all files fromoutputDirectorybefore writing anything in it. -
generate
boolean generateIf true, attempts to generate C++ JNI files, but if false, only attempts to parse header files. -
compile
boolean compileIf true, compiles the generated source file to a shared library and deletes source. -
deleteJniFiles
boolean deleteJniFilesIf true, preserves the generated C++ JNI files after compilation. -
header
boolean headerIf true, also generates C++ header files containing declarations of callback functions. -
copyLibs
boolean copyLibsIf true, also copies to the output directory dependent shared libraries (link and preload). -
copyResources
boolean copyResourcesIf true, also copies to the output directory resources listed in properties. -
properties
Properties propertiesAccumulates the various properties loaded from resources, files, command line options, etc. -
classScanner
ClassScanner classScanner -
buildCommand
String[] buildCommandA system command forProcessBuilderto execute for the build, instead of JavaCPP itself. -
workingDirectory
File workingDirectoryUser specified working directory to execute build subprocesses under. -
environmentVariables
-
compilerOptions
Collection<String> compilerOptionsContains additional command line options from the user for the native compiler. -
commandExecutor
CommandExecutor commandExecutorAn alternative CommandExecutor to use to execute commands.
-
-
Constructor Details
-
Builder
public Builder()Default constructor that simply initializes everything. -
Builder
Constructor that simply initializes everything.- Parameters:
logger- where to send messages
-
-
Method Details
-
cleanOutputDirectory
DeletesoutputDirectoryifcleanis true.- Throws:
IOException
-
parse
CallsParser.parse(File, String[], Class)after creating an instance of the Class.- Parameters:
classPath- an array of paths to try to load header files fromcls- The class annotated withPropertiesand implementingInfoMapper- Returns:
- the target File produced
- Throws:
IOException- on Java target file writing errorParserException- on C/C++ header file parsing error
-
includeJavaPaths
Tries to find automatically include paths forjni.handjni_md.h, as well as the link and library paths for thejvmlibrary.- Parameters:
properties- the Properties containing the paths to updateheader- to request support for exporting callbacks via generated header file
-
compile
int compile(String[] sourceFilenames, String outputFilename, ClassProperties properties, File workingDirectory) throws IOException, InterruptedException Launches and waits for the native compiler to produce a native shared library.- Parameters:
sourceFilenames- the C++ source filenamesoutputFilename- the output filename of the shared libraryproperties- the Properties detailing the compiler options to use- Returns:
- the result of
Process.waitFor() - Throws:
IOExceptionInterruptedException
-
getOutputPath
Creates and returns the directory where output files should be placed. UsesoutputDirectoryas is when available, but falls back on the longest common path to the classes as well as the platform specific library path when available, or the platform name itself and the user provided extension when not.- Parameters:
classes- from which to derive the output pathsourcePrefixes- returned, 2 strings without platform names, one from our class loader, the other from the common path of the classes- Returns:
- directory where binary files should be written to
- Throws:
IOException
-
generateAndCompile
File[] generateAndCompile(Class[] classes, String outputName, boolean first, boolean last) throws IOException, InterruptedException Generates C++ source files for classes, and compiles everything in one shared library whencompile == true.- Parameters:
classes- the Class objects as input to GeneratoroutputName- the output name of the shared libraryfirst- of the batch, so generate jnijavacpp.cpplast- of the batch, so delete jnijavacpp.cpp- Returns:
- the actual File generated, either the compiled library or its source
- Throws:
IOExceptionInterruptedException
-
createJar
Stores all the files in the given JAR file. Also attempts to root the paths of the filenames to each element of a list of classpaths.- Parameters:
jarFile- the JAR file to createclassPath- an array of paths to try to use as root for classesfiles- a list of files to store in the JAR file- Throws:
IOException
-
classPaths
Splits argument withFile.pathSeparatorand appends result to paths of theclassScanner. -
classPaths
Appends argument to the paths of theclassScanner. -
encoding
-
outputDirectory
Sets theoutputDirectoryfield to the argument. -
outputDirectory
Sets theoutputDirectoryfield to the argument. -
clean
-
generate
-
compile
-
deleteJniFiles
Sets thedeleteJniFilesfield to the argument. -
header
-
copyLibs
-
copyResources
Sets thecopyResourcesfield to the argument. -
outputName
Sets theoutputNamefield to the argument. -
configDirectory
Sets theconfigDirectoryfield to the argument. -
configDirectory
Sets theconfigDirectoryfield to the argument. -
jarPrefix
-
properties
Sets thepropertiesfield to the ones loaded from resources for the specified platform. -
properties
Adds all the properties of the argument to thepropertiesfield. -
propertyFile
Sets thepropertiesfield to the ones loaded from the specified file.- Throws:
IOException
-
propertyFile
Sets thepropertiesfield to the ones loaded from the specified file.- Throws:
IOException
-
property
Sets a property of thepropertiesfield, in either "key=value" or "key:value" format. -
property
Sets a key/value pair property of thepropertiesfield. -
getProperties
Returnsproperties. -
getProperty
-
addProperty
-
classesOrPackages
public Builder classesOrPackages(String... classesOrPackages) throws IOException, ClassNotFoundException, NoClassDefFoundError Requests theclassScannerto add a class or all classes from a package. Anullargument indicates the unnamed package. -
buildCommand
Sets thebuildCommandfield to the argument. -
workingDirectory
Sets theworkingDirectoryfield to the argument. -
workingDirectory
Sets theworkingDirectoryfield to the argument. -
environmentVariables
Sets theenvironmentVariablesfield to the argument. -
compilerOptions
Appends arguments to thecompilerOptionsfield. -
commandExecutor
Sets thecommandExecutorfield to the argument. -
build
Starts the build process and returns an array ofFileproduced.- Returns:
- the array of File produced
- Throws:
IOExceptionInterruptedExceptionParserException
-
printHelp
public static void printHelp()Simply prints out to the display the command line usage. -
main
The terminal shell interface to the Builder.- Parameters:
args- an array of arguments as described byprintHelp()- Throws:
Exception
-