Package org.bytedeco.javacpp.tools
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 forProcessBuilder
to execute for the build, instead of JavaCPP itself.(package private) ClassScanner
(package private) boolean
If true, deletes all files fromoutputDirectory
before writing anything in it.(package private) CommandExecutor
An alternative CommandExecutor to use to execute commands.(package private) boolean
If 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) File
The name of the directory where to output config files for GraalVM native-image, if notnull
.(package private) boolean
If true, also copies to the output directory dependent shared libraries (link and preload).(package private) boolean
If true, also copies to the output directory resources listed in properties.(package private) boolean
If true, preserves the generated C++ JNI files after compilation.(package private) String
The 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) boolean
If true, attempts to generate C++ JNI files, but if false, only attempts to parse header files.(package private) boolean
If true, also generates C++ header files containing declarations of callback functions.(package private) String
The name of the JAR file to create, if notnull
.(package private) final Logger
Logger where to send debug, info, warning, and error messages.(package private) File
The directory where the generated files and compiled shared libraries get written to.(package private) String
The name of the output generated source file or shared library.(package private) Properties
Accumulates the various properties loaded from resources, files, command line options, etc.(package private) File
User 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 ofFile
produced.buildCommand
(String[] buildCommand) Sets thebuildCommand
field to the argument.classesOrPackages
(String... classesOrPackages) Requests theclassScanner
to add a class or all classes from a package.classPaths
(String classPaths) Splits argument withFile.pathSeparator
and appends result to paths of theclassScanner
.classPaths
(String... classPaths) Appends argument to the paths of theclassScanner
.clean
(boolean clean) Sets theclean
field to the argument.(package private) void
DeletesoutputDirectory
ifclean
is true.commandExecutor
(CommandExecutor commandExecutor) Sets thecommandExecutor
field to the argument.compile
(boolean compile) Sets thecompile
field to the argument.(package private) int
compile
(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 thecompilerOptions
field.configDirectory
(File configDirectory) Sets theconfigDirectory
field to the argument.configDirectory
(String configDirectory) Sets theconfigDirectory
field to the argument.copyLibs
(boolean copyLibs) Sets thecopyLibs
field to the argument.copyResources
(boolean copyResources) Sets thecopyResources
field to the argument.(package private) void
Stores all the files in the given JAR file.deleteJniFiles
(boolean deleteJniFiles) Sets thedeleteJniFiles
field to the argument.Sets theencoding
field to the argument.environmentVariables
(Map<String, String> environmentVariables) Sets theenvironmentVariables
field to the argument.generate
(boolean generate) Sets thegenerate
field 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) File
getOutputPath
(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 theheader
field to the argument.(package private) void
includeJavaPaths
(ClassProperties properties, boolean header) Tries to find automatically include paths forjni.h
andjni_md.h
, as well as the link and library paths for thejvm
library.Sets thejarPrefix
field to the argument.static void
The terminal shell interface to the Builder.outputDirectory
(File outputDirectory) Sets theoutputDirectory
field to the argument.outputDirectory
(String outputDirectory) Sets theoutputDirectory
field to the argument.outputName
(String outputName) Sets theoutputName
field to the argument.(package private) File[]
CallsParser.parse(File, String[], Class)
after creating an instance of the Class.static void
Simply prints out to the display the command line usage.properties
(String platform) Sets theproperties
field to the ones loaded from resources for the specified platform.properties
(Properties properties) Adds all the properties of the argument to theproperties
field.Sets a property of theproperties
field, in either "key=value" or "key:value" format.Sets a key/value pair property of theproperties
field.propertyFile
(File propertyFile) Sets theproperties
field to the ones loaded from the specified file.propertyFile
(String filename) Sets theproperties
field to the ones loaded from the specified file.workingDirectory
(File workingDirectory) Sets theworkingDirectory
field to the argument.workingDirectory
(String workingDirectory) Sets theworkingDirectory
field 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.class
file. -
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 fromoutputDirectory
before 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 forProcessBuilder
to execute for the build, instead of JavaCPP itself. -
workingDirectory
File workingDirectoryUser specified working directory to execute build subprocesses under. -
environmentVariables
User specified environment variables to pass to the native compiler. -
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
DeletesoutputDirectory
ifclean
is 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 withProperties
and 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.h
andjni_md.h
, as well as the link and library paths for thejvm
library.- 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:
IOException
InterruptedException
-
getOutputPath
Creates and returns the directory where output files should be placed. UsesoutputDirectory
as 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:
IOException
InterruptedException
-
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.pathSeparator
and appends result to paths of theclassScanner
. -
classPaths
Appends argument to the paths of theclassScanner
. -
encoding
Sets theencoding
field to the argument. -
outputDirectory
Sets theoutputDirectory
field to the argument. -
outputDirectory
Sets theoutputDirectory
field to the argument. -
clean
Sets theclean
field to the argument. -
generate
Sets thegenerate
field to the argument. -
compile
Sets thecompile
field to the argument. -
deleteJniFiles
Sets thedeleteJniFiles
field to the argument. -
header
Sets theheader
field to the argument. -
copyLibs
Sets thecopyLibs
field to the argument. -
copyResources
Sets thecopyResources
field to the argument. -
outputName
Sets theoutputName
field to the argument. -
configDirectory
Sets theconfigDirectory
field to the argument. -
configDirectory
Sets theconfigDirectory
field to the argument. -
jarPrefix
Sets thejarPrefix
field to the argument. -
properties
Sets theproperties
field to the ones loaded from resources for the specified platform. -
properties
Adds all the properties of the argument to theproperties
field. -
propertyFile
Sets theproperties
field to the ones loaded from the specified file.- Throws:
IOException
-
propertyFile
Sets theproperties
field to the ones loaded from the specified file.- Throws:
IOException
-
property
Sets a property of theproperties
field, in either "key=value" or "key:value" format. -
property
Sets a key/value pair property of theproperties
field. -
getProperties
Returnsproperties
. -
getProperty
Returnsproperties.getProperty(key)
. -
addProperty
Adds values to a given property key, seperating them with "platform.path.separator". -
classesOrPackages
public Builder classesOrPackages(String... classesOrPackages) throws IOException, ClassNotFoundException, NoClassDefFoundError Requests theclassScanner
to add a class or all classes from a package. Anull
argument indicates the unnamed package. -
buildCommand
Sets thebuildCommand
field to the argument. -
workingDirectory
Sets theworkingDirectory
field to the argument. -
workingDirectory
Sets theworkingDirectory
field to the argument. -
environmentVariables
Sets theenvironmentVariables
field to the argument. -
compilerOptions
Appends arguments to thecompilerOptions
field. -
commandExecutor
Sets thecommandExecutor
field to the argument. -
build
Starts the build process and returns an array ofFile
produced.- Returns:
- the array of File produced
- Throws:
IOException
InterruptedException
ParserException
-
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
-