Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active May 15, 2026 20:11
Show Gist options
  • Select an option

  • Save scivision/de1cd4b1bbdb475dbbcaa34ab3195249 to your computer and use it in GitHub Desktop.

Select an option

Save scivision/de1cd4b1bbdb475dbbcaa34ab3195249 to your computer and use it in GitHub Desktop.
CMake toast notifications

Toast Notification from CMake

This is a simple CMake module to send toast notifications to the user across operating systems. This is useful for long-running scripts to signal completion. It has a fallback to Terminal bell.

We didn't use "wall" or for-loop tty because those are annoying / brittle.

Requirements

  • Linux: libnotify-bin apt install libnotify-bin (for notify-send)
  • macOS: none (uses AppleScript)
  • Windows: Install-Module -Name BurntToast (BurntToast module)

Example

include(ToastNotify.cmake)

toast_nofify("Build complete ${CMAKE_CURRENT_BINARY_DIR}")

Or more typically using this CMakeLists.txt:

cmake -B build

cmake --build build
cmake_minimum_required(VERSION 3.15)
project(DemoToast LANGUAGES NONE)
add_custom_target(long_task ALL
COMMAND ${CMAKE_COMMAND} -E echo "Simulating one second duration task..."
COMMAND ${CMAKE_COMMAND} -E sleep 1)
add_custom_command(TARGET long_task POST_BUILD
COMMAND ${CMAKE_COMMAND} "-Dmsg=Build complete ${CMAKE_CURRENT_BINARY_DIR}"
-P ${CMAKE_CURRENT_LIST_DIR}/ToastNotify.cmake)
include(ToastNotify.cmake)
toast_nofify("Build complete ${CMAKE_CURRENT_BINARY_DIR}")
# send toast notification to user from CMake across operating systems.
# This is useful for long-running scripts to signal completion.
# Fallback to Terminal "bell".
function(terminal_bell)
# Makes a brief default Terminal sound and/or flashes the terminal window,
# if not disabled by the kernel or terminal.
if(CMAKE_VERSION VERSION_LESS 4.0)
set(absorb_error RESULT_VARIABLE _ret)
else()
set(absorb_error COMMAND_ERROR_IS_FATAL NONE)
endif()
if(UNIX)
execute_process(COMMAND printf \\007 ${absorb_error})
else()
execute_process(COMMAND pwsh -c "Write-Host `a" ${absorb_error})
endif()
endfunction()
function(toast_nofify msg)
set(_ret -1)
if(APPLE)
# Escape message content for AppleScript string literal.
string(REPLACE "\\" "\\\\" _msg_escaped "${msg}")
string(REPLACE "\"" "\\\"" _msg_escaped "${_msg_escaped}")
execute_process(COMMAND osascript -e "display notification \"${_msg_escaped}\" with title \"CMake\""
RESULT_VARIABLE _ret)
elseif(UNIX)
execute_process(COMMAND notify-send "CMake" "${msg}" RESULT_VARIABLE _ret)
elseif(WIN32)
execute_process(COMMAND powershell -c "New-BurntToastNotification -Text '${msg}'"
RESULT_VARIABLE _ret)
endif()
if(NOT _ret EQUAL 0)
terminal_bell()
endif()
endfunction()
# run if in script mode
if(CMAKE_SCRIPT_MODE_FILE)
toast_nofify("${msg}")
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment