CMake can add interface linking to imported libraries.
For example, a imported library obtained by find_package() or otherwise.
Normally, this would work like the following example to say link “stdc++fs” to example imported library “imported::lib” for GCC older than 9.1.
Function target_link_libraries()
should work,
but does not always work for some projects with a configure time error.
To workaround this issue if it arises, set target property
INTERFACE_LINK_LIBRARIES
directly like:
Many CMake users have muscle memory for the three-stanza configure, build, test with CMake:
cmake -B build
# configure CMake and generate build filescmake --build build
# compile and link binariesctest --test-dir build
# run program self-tests
This can be reduced to a single command for many programs:
ctest -S setup.cmake
where a single file
setup.cmake
is added to the project.
Command-line options like “-Dvar=yes” must be aggregated and passed along to
ctest_configure(OPTIONS)
in setup.cmake.
Capture the number of physical CPU cores available on a computer from Matlab:
functionN =get_cpu_count()%% get apparent number of physical CPU coresN = maxNumCompThreads;
if N < 2% happens on some HPC N = feature('NumCores');
endend
Python can easily be used from CMake, perhaps to simplify test scripts for continuous integration.
Python scripts are managed in CMakeLists.txt.
First, find Python interpreter:
find_package(PythonCOMPONENTSInterpreterREQUIRED)
Then to run a simple Python script in a CMake test:
Octave uses
startup.m
persistent user settings like Matlab.
To keep Matlab compatibility, put Octave-specific startup commands and plotting defaults into
~/.octaverc,
which sets default parameters for all GNU Octave sessions.
eliminate nuisance octave-workspace files that appear when Octave is Ctrl+c
exited or crashes.
page_output_immediately(1)
make Octave print immediately like Matlab.
if exist
use startup.m file like Matlab.
Set plot defaults: useful for HiDPI systems, control Octave default plot text size of axes and titles, useful for HiDPI systems by adding to “~/.octaverc”:
CTest CDash scripts can use the function
ctest_empty_binary_directory
to recursively remove a CMake build directory to avoid hidden state between test runs in an overall build-test cycle.
However, this function will cause the overall CTest run to return a non-zero error code if CMakeCache.txt isn’t present in the build directory.
This is confusing in a CI system particularly.
While we do appreciate this safety feature over simply using
file(REMOVE_RECURSE),
it’s necessary to enclose in if() statements like below to avoid false errors.
CMake build targets are added by “add_[executable,library,custom_target]” commands.
Targets can be dynamically set by arbitrarily complex foreach(), if(), etc. logic.
A list of tests enabled is retrieved by the
BUILDSYSTEM_TARGETS
directory
property.
The variable “target_names” contains all the target names previously added in the CMakeLists.txt in the DIRECTORY scope.
Linux
modules
are often used by HPC systems allow using more current versions of software than are in the default system repositories.
A typical approach is to create an .sh file for a particular job type.
To avoid unexpected conflicts or hidden state, load only the modules necessary for a particular project.
Cray PE
programming environment
allows easy switching of compilers for developing and running applications on Cray supercomputers.
Use the Cray compiler wrappers cc, CC, and ftn instead of directly referencing the compiler backends gcc, g++, and gfortran.
In CMake script, detect if Cray PE is being used, regardless of compiler backend by detecting if environment variable PE_ENV or
CRAYPE_VERSION
is set.
if(DEFINEDENV{CRAYPE_VERSION})message("Cray PE detected")endif()