Compiled Components
Introduction
Compiled components represent collections of compiled functionality. This includes declarations that can be used in compiled code and installed functions that can be used in top-level code. Compiled components provide a framework for packaging and distributing this functionality.
Creating a Compiler-Based Paclet
Integration with the paclet system makes it possible to distribute a compiled component and its associated builds beyond a single kernel session. In this example, a compiled component is defined that exposes a few installed functions and that depends on an external library.
Defining the Paclet
PacletObject[
<|
"Name" -> "ComponentPaclet",
"Version" -> "0.0.1",
"WolframVersion" -> "13.2+",
"Extensions" -> {
{"Kernel", "Root" -> "Kernel", "Context" -> "ComponentPaclet`"},
{"LibraryResources"}
}
|>
]
BeginPackage["ComponentPaclet`"]
AddOne
RaiseToPower
Begin["`Private`"]
(* Declare compiler declarations *)
DeclareCompiledComponent["ExampleComponent", {
LibraryFunctionDeclaration["addone", "compilerDemoBase", {"CInt"}->"CInt"],
FunctionDeclaration[AddOne,
Typed[{"CInt"} -> "CInt"]@
Function[arg, LibraryFunction["addone"][arg]]
],
FunctionDeclaration[RaiseToPower,
Typed[{"MachineInteger","MachineInteger"} -> "MachineInteger"]@
Function[{x,y}, x^y]
]
}];
(* Declare installed functions *)
DeclareCompiledComponent["ExampleComponent", "InstalledFunctions" -> {
AddOne,
RaiseToPower
}];
(* Declare library functions *)
DeclareCompiledComponent["ExampleComponent", "LibraryFunctions" -> <|
"sqrt" -> Function[Typed[arg,"Real64"], Sqrt[arg]]
|>];
(* Declare external library dependencies *)
DeclareCompiledComponent["ExampleComponent", "ExternalLibraries" -> {
"compilerDemoBase"
}];
End[] (* End `Private` *)
EndPackage[]
Loading the Paclet
Building the Component
Compiled components must be built in order to access compiled library functions and installed functions.
Loading the Component
Now that the component has been built, installed functions can be used.
This uses FindLibrary to locate the component build, which will be found in the paclet because it was given the "LibraryResources" extension in the PacletInfo.wl file.
Now that it has been built, the "ComponentPaclet" paclet could be immediately used in a fresh kernel session without having to recompile its source. It could also be distributed to other machines, though the build will only be compatible with the platform on which it was built. To build a cross-platform component library, BuildCompiledComponent will need to be run on every target platform.