FindMathematica manual ====================== FindMathematica is a CMake module that tries to find a Wolfram Language installation and provides CMake functions for its C/C++ interface. Installation ------------ Copy the directory `CMake/Mathematica` to the root directory of your CMake project. In the top-level `CMakeList.txt` file add the module directory to the CMake module search path: set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake/Mathematica" ${CMAKE_MODULE_PATH}) Optional CMake MUnit testing support requires the installation of the Wolfram MUnit package. MUnit is built into *Mathematica* 10 or later. For earlier versions of *Mathematica*, a compatible MUnit package ships with Wolfram Workbench 2.0. The JAR file `com.wolfram.eclipse.testing_2.0.126.jar` in the `plugins` subdirectory of the Workbench installation folder contains different MUnit package versions for *Mathematica* versions 5.2 to 9.0. To install MUnit, extract the MUnit package version appropriate for your installed *Mathematica* version from the JAR file to a directory on the *Mathematica* `$Path` (e.g., `$BaseDirectory/Applications` or `$UserBaseDirectory/Applications`). Alternatively you can copy the MUnit package to the FindMathematica module directory which is automatically prepended to `$Path` when the Wolfram Language kernel is launched through the FindMathematica module. If you plan on generating Wolfram Language documentation with CMake, the installation of two Wolfram documentation build packages, which also ship with Wolfram Workbench 2.0, is required. The JAR file `com.wolfram.eclipse.paclet.develop_2.0.138.jar` in the `plugins` subdirectory of the Workbench installation folder contains the package folders `DocumentationBuild` and `Transmogrify`. These must be copied to a directory on the Wolfram Language `$Path`. The `DocumentationBuild` package also requires the installation of [Apache Ant][aant]. In order for Apache Ant to be found by CMake, the environment variable `ANT_HOME` needs to point to Apache Ant's installation directory. Usage ----- If you are new to CMake, check out the [CMake tutorial][cmtut] first. To find the newest Wolfram Language installation in a CMake list file, run the `find_package` command: find_package(Mathematica) The FindMathematica module will look for a Wolfram Language installation in the default installation location of the used platform. Under Windows, it will also use installation locations from the Windows Registry. Under OS X it will also query the Launch Services database. By default, FindMathematica will return the newest Wolfram Language installation it can find. To find a minimum version of Wolfram Language, run the `find_package` command with a version argument: find_package(Mathematica 10.0) To find a specific version of Wolfram Language, run the `find_package` command with a version argument and the parameter `EXACT`: find_package(Mathematica 10.0.2 EXACT) Depending on the version of Wolfram Language, the FindMathematica module will try to find the components `MathLink`, `WolframLibrary`, `WSTP`, `JLink` and `MUnit`. To explicitly specify the components to be found, use: find_package(Mathematica COMPONENTS MathLink WolframLibrary) The package ships as a fully functional CMake project that demonstrates how to use functionality provided by the FindMathematica module: * `MathematicaExamples` show how to run plain Wolfram Language code as part of the build process. * `MathLinkExamples` build all standard MathLink example files. * `WSTPExamples` build all standard WSTP example files. * `LibraryLinkExamples` build all standard LibraryLink example files. * `CodeGenerationExamples` builds binaries from Wolfram Language compiled functions exported to C. * `MUnitExamples` demonstrate how to run Wolfram Language MUnit test files as CMake tests. * `DocumentationExamples` demonstrate how to build Wolfram Language documentation. * `JLinkExamples` builds JAR archives from J/Link example source files. ### Basic Windows Usage #### Visual Studio To build the FindMathematica project with Visual Studio C++ 2010 for 32-bit Windows, open a Visual Studio command prompt, change directory to the `FindMathematica` root directory and run the following commands: D:\FindMathematica>mkdir build D:\FindMathematica>cd build D:\FindMathematica\build>cmake -G "Visual Studio 10" .. Then open the generated Visual Studio solution file with Visual Studio C++ 2010: D:\FindMathematica\build>start Mathematica-project.sln Alternatively, you can build and test the project's "Debug" configuration from the command prompt: D:\FindMathematica\build>cmake --build . --config Debug D:\FindMathematica\build>ctest --build-config Debug To build the "Release" configuration and install the built files to your user base directory, run: D:\FindMathematica\build>cmake --build . --target install --config Release To build the Wolfram Language documentation in notebook format, run: D:\FindMathematica\build>cmake --build . --config Debug --target documentation To build the FindMathematica project with Visual Studio C++ 2010 for 64-bit Windows, open a Visual Studio x64 cross tools command prompt in the `FindMathematica` build directory: D:\FindMathematica\build>cmake -G "Visual Studio 10 Win64" .. FindMathematica supports building 32-bit and 64-bit MathLink executables and LibraryLink dynamic libraries using the appropriate link libraries that ship with the Windows version. If you are using a 32-bit version of Windows, you can run the Wolfram Language kernel only in 32-bit mode, though. If you are using a 64-bit version of Windows, you can run the Wolfram Language kernel both as a 64-bit native executable or as a 32-bit executable under WoW64. #### MinGW To build the FindMathematica project with [MinGW][mingw] run the following commands from a command prompt: D:\FindMathematica\build>cmake -G "MinGW Makefiles" .. D:\FindMathematica\build>mingw32-make #### Cygwin Under [Cygwin][cgwn] the FindMathematica module requires the Cygwin version of CMake, which is different to the regular Windows CMake version. To build the FindMathematica project open a Cygwin shell prompt and run the following commands in the `FindMathematica` root directory: $ mkdir build $ cd build $ cmake .. $ make The Wolfram Language kernel cannot load a Cygwin generated LibraryLink DLL that has been linked with the Cygwin runtime library. As a work-around, the FindMathematica module suppresses linking with the Cygwin runtime library by adding the `-mno-cygwin` flag when a LibraryLink target is added. This flag is supported by Cygwin GCC version 3.x, but not by the default Cygwin GCC version 4.x. To force the use of GCC version 3.x under Cygwin, run: $ cmake -DCMAKE_CXX_COMPILER=/usr/bin/g++-3.exe -DCMAKE_C_COMPILER=/usr/bin/gcc-3.exe .. ### Basic Linux Usage To build the FindMathematica project with the CMake makefile generator, open a shell session in the `FindMathematica` root directory and enter the following commands: $ mkdir build $ cd build $ cmake .. $ make Optionally, you can run all tests and install the built files to your user base directory: $ make test $ make install To build the Wolfram Language documentation in notebook format, run: $ make documentation FindMathematica supports building 32-bit and 64-bit MathLink executables and LibraryLink shared libraries using the appropriate link libraries that ship with the Linux version. If you are using a 64-bit version of Linux, you can run the Wolfram Language kernel both as a 64-bit executable or as a 32-bit executable. To cross-compile to 32-bit Linux under 64-bit Linux, the packages `ia32-libs` and `libc6-dev-i386` need to be installed. To force a 32-bit build then, run: $ cmake -DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32 .. ### Basic OS X Usage To build the FindMathematica project with the CMake makefile generator, open Terminal.app, change to the `FindMathematica` root directory and enter the following commands: $ mkdir build $ cd build $ cmake .. $ make Optionally, you can run all tests and install the built files to your user base directory: $ make test $ make install To build the Wolfram Language documentation in notebook format, run: $ make documentation To build the FindMathematica project with the Xcode project generator, run CMake with the following arguments: $ cmake -G "Xcode" .. Then open the generated Xcode project file `Mathematica-project.xcodeproj` with Xcode.app. FindMathematica supports building MathLink executables and LibraryLink shared libraries for any OS X architecture type supported by the installed OS X version (`x86_64` and `i386` as of *Mathematica* 8.0). To select the build target architecture types, set the CMake `CMAKE_OSX_ARCHITECTURES` variable. E.g., to build a 32/64-bit Intel universal binary use the following setting: $ cmake "-DCMAKE_OSX_ARCHITECTURES=i386;x86_64" .. If you are running *Mathematica* 5.2 on a 64-bit capable Intel Mac, note that 5.2 does not support `x86_64`. To get a workable project set CMake's target architecture to 32-bit by running: $ cmake "-DCMAKE_OSX_ARCHITECTURES=i386" .. Used Variables -------------- The module uses the following variables upon the invocation of `find_package`: * `Mathematica_FIND_VERSION` - full requested Wolfram Language version string * `Mathematica_FIND_VERSION_EXACT` - `TRUE` if `EXACT` option was given upon `find_package` * `Mathematica_FIND_QUIETLY` - `TRUE` if `QUIET` option was given upon `find_package` * `Mathematica_FIND_REQUIRED` - `TRUE` if `REQUIRED` option was given upon `find_package` * `Mathematica_FIND_COMPONENTS` - Set of Wolfram Language components requested upon `find_package` * `Mathematica_FIND_REQUIRED_MathLink` - `TRUE` if `REQUIRED` option was given for component MathLink * `Mathematica_FIND_REQUIRED_WSTP` - `TRUE` if `REQUIRED` option was given for component MathLink * `Mathematica_FIND_REQUIRED_WolframLibrary` - `TRUE` if `REQUIRED` option was given for component WolframLibrary * `Mathematica_FIND_REQUIRED_JLink` - `TRUE` if `REQUIRED` option was given for component JLink * `Mathematica_FIND_REQUIRED_MUnit` - `TRUE` if `REQUIRED` option was given for component MUnit * `Mathematica_USE_STATIC_LIBRARIES` - if `TRUE`, prefer static libraries to dynamic libraries (defaults to `FALSE`) * `Mathematica_USE_MINIMAL_LIBRARIES` - if `TRUE`, prefer minimal libraries to full libraries (defaults to `FALSE`) * `Mathematica_USE_LIBCXX_LIBRARIES` - if `TRUE`, prefer libc++ linked libraries to libstdc++ linked libraries (defaults to `FALSE`, only applies to OS X) * `Mathematica_MathLink_FIND_VERSION_MAJOR` - requested MathLink interface version (e.g., `"3"`) * `Mathematica_MathLink_FIND_VERSION_MINOR` - requested MathLink revision number (e.g., `"16"`) * `Mathematica_WSTP_FIND_VERSION_MAJOR` - requested WSTP interface version (e.g., `"4"`) * `Mathematica_WSTP_FIND_VERSION_MINOR` - requested WSTP revision version (e.g., `"25"`) * `Mathematica_DEBUG` - if `TRUE`, enable debugging output (defaults to `FALSE`) Defined Variables ----------------- The module defines the following variables: * `Mathematica_CMAKE_MODULE_DIR` - directory containing the FindMathematica module (always prepended to `$Path`) * `Mathematica_CMAKE_MODULE_VERSION` - FindMathematica module version * `Mathematica_FOUND` - True if Wolfram Language installation and all required components are found * `Mathematica_SYSTEM_ID` - default build target platform System ID (e.g., `"Windows"` or `"Linux"`) * `Mathematica_SYSTEM_IDS` - list of supported build target platform System IDs (e.g., `"MacOSX"`, `"MacOSX-x86"`, `"MacOSX-x86-64"`) * `Mathematica_HOST_SYSTEM_ID` - default host platform System ID (e.g., `"Windows-x86-64"` or `"MacOSX-x86-64"`) * `Mathematica_HOST_SYSTEM_IDS` - list of System IDs available with the host installation * `Mathematica_ROOT_DIR` - Wolfram Language installation directory valid for build target platform * `Mathematica_HOST_ROOT_DIR` - Wolfram Language installation directory valid for host platform (corresponds to `$InstallationDirectory`) * `Mathematica_KERNEL_EXECUTABLE` - path to host Wolfram Language kernel executable * `Mathematica_FRONTEND_EXECUTABLE` - path to host Wolfram Language frontend executable * `Mathematica_BASE_DIR` - directory for system wide files (corresponds to `$BaseDirectory`) * `Mathematica_USERBASE_DIR` - directory for user-specific files (corresponds to `$UserBaseDirectory`) * `Mathematica_INCLUDE_DIR` - header file `mdefs.h` include directory * `Mathematica_INCLUDE_DIRS` - list of include directories for all components * `Mathematica_LIBRARIES` - list of libraries to link against for all components * `Mathematica_LIBRARY_DIRS` - list of Wolfram Language library directories for all components * `Mathematica_RUNTIME_LIBRARY_DIRS` - list of Wolfram Language library directories required at runtime * `Mathematica_RUNTIME_LIBRARY_DIRS_DEBUG` - list of debug Wolfram Language library directories required at runtime * `Mathematica_VERSION` - Wolfram Language version number given as "major.minor.patch" * `Mathematica_VERSION_MAJOR` - Wolfram Language major version number * `Mathematica_VERSION_MINOR` - Wolfram Language minor version number * `Mathematica_VERSION_PATCH` - Wolfram Language patch version number * `Mathematica_VERSION_STRING` - Wolfram Language version string given as "major.minor.patch" * `Mathematica_VERSION_COUNT` - Wolfram Language version component count (usually 3) * `Mathematica_CREATION_ID` - Wolfram Language installation creation ID number The module defines the following variables for component `WolframLibrary`: * `Mathematica_WolframLibrary_FOUND` - True if the Wolfram Language installation has WolframLibrary * `Mathematica_WolframLibrary_INCLUDE_DIR` - WolframLibrary include directory (contains header files `WolframLibrary.h` and `WolframRTL.h`) * `Mathematica_WolframLibrary_LIBRARY` - path to WolframRTL library for default target platform (e.g., `WolframRTL_Minimal.lib`) * `Mathematica_WolframLibrary_LIBRARIES` - WolframRTL libraries for all target platforms and required system libraries * `Mathematica_WolframLibrary_VERSION` - WolframLibrary version number given as "version" * `Mathematica_WolframLibrary_VERSION_MAJOR` - WolframLibrary version number * `Mathematica_WolframLibrary_VERSION_STRING` - WolframLibrary version number given as "version" * `Mathematica_WolframLibrary_VERSION_COUNT` - WolframLibrary number of version components (usually 1) * `Mathematica_LibraryLink_PACKAGE_FILE` - LibraryLink package file * `Mathematica_LibraryLink_PACKAGE_DIR` - LibraryLink package root directory * `Mathematica_WolframLibrary_RUNTIME_LIBRARY_DIRS` - list of WolframLibrary library directories required at runtime * `Mathematica_WolframLibrary_RUNTIME_LIBRARY_DIRS_DEBUG` - list of debug WolframLibrary library directories required at runtime The module defines the following variables for component `MathLink`: * `Mathematica_MathLink_FOUND` - True if the Wolfram Language installation has MathLink SDK * `Mathematica_MathLink_ROOT_DIR` - MathLink C SDK root directory for default target platform * `Mathematica_MathLink_HOST_ROOT_DIR` - MathLink C SDK root directory for host platform * `Mathematica_MathLink_INCLUDE_DIR` - header file `mathlink.h` include directory for default target platform * `Mathematica_MathLink_LIBRARY` - path to MathLink library for default target platform * `Mathematica_MathLink_LIBRARIES` - MathLink library for all target platforms and required system libraries * `Mathematica_MathLink_MPREP_EXECUTABLE` - path to host `mprep` executable (MathLink template file preprocessor) * `Mathematica_MathLink_HOST_INCLUDE_DIR` - header file `mathlink.h` include directory for host platform * `Mathematica_MathLink_DEFINITIONS` - MathLink compile definitions, e.g., `-DMLINTERFACE=3` * `Mathematica_MathLink_LINKER_FLAGS` - MathLink linker flags * `Mathematica_MathLink_VERSION` - MathLink version number given as "interface.revision" * `Mathematica_MathLink_VERSION_MAJOR` - MathLink interface number * `Mathematica_MathLink_VERSION_MINOR` - MathLink revision number * `Mathematica_MathLink_VERSION_STRING` - MathLink version string given as "interface.revision" * `Mathematica_MathLink_VERSION_COUNT` - MathLink version component count (usually 2) * `Mathematica_MathLink_RUNTIME_LIBRARY_DIRS` - list of MathLink library directories required at runtime * `Mathematica_MathLink_RUNTIME_LIBRARY_DIRS_DEBUG` - list of debug MathLink library directories required at runtime The module defines the following variables for component `WSTP`: * `Mathematica_WSTP_FOUND` - True if the Wolfram Language installation has WSTP SDK * `Mathematica_WSTP_ROOT_DIR` - WSTP C SDK root directory for default target platform * `Mathematica_WSTP_HOST_ROOT_DIR` - WSTP C SDK root directory for host platform * `Mathematica_WSTP_INCLUDE_DIR` - header file `wstp.h` include directory for default target platform * `Mathematica_WSTP_LIBRARY` - path to WSTP library for default target platform * `Mathematica_WSTP_LIBRARIES` - WSTP library for all target platforms and required system libraries * `Mathematica_WSTP_WSPREP_EXECUTABLE` - path to host `wsprep` executable (WSTP template file preprocessor) * `Mathematica_WSTP_HOST_INCLUDE_DIR` - header file wstp.h include directory for host platform * `Mathematica_WSTP_DEFINITIONS` - WSTP compile definitions, e.g., `-DWSINTERFACE=4` * `Mathematica_WSTP_LINKER_FLAGS` - WSTP linker flags * `Mathematica_WSTP_VERSION` - WSTP version number given as "interface.revision" * `Mathematica_WSTP_VERSION_MAJOR` - WSTP interface number * `Mathematica_WSTP_VERSION_MINOR` - WSTP revision number * `Mathematica_WSTP_VERSION_STRING` - WSTP version string given as "interface.revision" * `Mathematica_WSTP_VERSION_COUNT` - WSTP version component count (usually 2) * `Mathematica_WSTP_RUNTIME_LIBRARY_DIRS` - list of WSTP library directories required at runtime * `Mathematica_WSTP_RUNTIME_LIBRARY_DIRS_DEBUG` - list of debug WSTP library directories required at runtime The module defines the following variables for component `JLink`: * `Mathematica_JLink_FOUND` - True if the Wolfram Language installation has J/Link SDK * `Mathematica_JLink_PACKAGE_DIR` - J/Link package root directory * `Mathematica_JLink_JAR_FILE` - Full path to J/Link JAR file * `Mathematica_JLink_JAVA_HOME` - Full path to Java SDK bundled with *Mathematica* * `Mathematica_JLink_JAVA_EXECUTABLE` - path to the host Java runtime executable used by J/Link * `Mathematica_JLink_RUNTIME_LIBRARY` - Full path to JLinkNativeLibrary * `Mathematica_JLink_VERSION` - J/Link version number given as "major.minor.patch" * `Mathematica_JLink_VERSION_MAJOR` - J/Link major version number * `Mathematica_JLink_VERSION_MINOR` - J/Link minor version number * `Mathematica_JLink_VERSION_PATCH` - J/Link patch version number * `Mathematica_JLink_VERSION_STRING` - J/Link version string given as "major.minor.patch" * `Mathematica_JLink_VERSION_COUNT` - JLink version component count (usually 3) The module defines the following variables for component `MUnit`: * `Mathematica_MUnit_FOUND` - True if the Wolfram Language installation has the Wolfram MUnit package * `Mathematica_MUnit_PACKAGE_FILE` - MUnit package file * `Mathematica_MUnit_PACKAGE_DIR` - MUnit package root directory * `Mathematica_MUnit_VERSION` - MUnit version number given as "major.minor.patch" * `Mathematica_MUnit_VERSION_MAJOR` - MUnit major version number * `Mathematica_MUnit_VERSION_MINOR` - MUnit minor version number * `Mathematica_MUnit_VERSION_PATCH` - MUnit patch version number * `Mathematica_MUnit_VERSION_STRING` - MUnit version string given as "major.minor.patch" * `Mathematica_MUnit_VERSION_COUNT` - MUnit version component count (usually 3) Defined Functions ----------------- Depending on the Wolfram Language version and components found, the module defines following functions: Mathematica_TO_NATIVE_STRING(string result) Converts a CMake string to a Wolfram Language `InputForm` string usable verbatim in Wolfram Language code. Mathematica_TO_NATIVE_LIST(result [element ...]) Converts a CMake list to a Wolfram Language `InputForm` list usable verbatim in Wolfram Language code. Mathematica_TO_NATIVE_PATH(path result) Converts a CMake file path to a Wolfram Language `InputForm` file path for the native platform usable verbatim in Wolfram Language code. The input can be a single CMake path or a CMake path list. If multiple consecutive paths in the CMake path list share the same directory portion, the function produces a compact Wolfram Language code representation using `Map` and `ToFileName`. Mathematica_EXECUTE( [ CODE [ stmnt ...] ] [ SCRIPT