How to deal with failing file(MAKE_DIRECTORY

This is something that’s annoyed me too–I wish file(MAKE_DIRECTORY) had an optional RESULT or such that allowed handling error oneself.

This allows the behavior you want:

cmake_minimum_required(VERSION 3.10)

project(demo LANGUAGES NONE)

function(this_function_fails dir)
  # file(MAKE_DIRECTORY ${dir})
  # message(STATUS "This line will never be reached")

  execute_process(
    COMMAND ${CMAKE_COMMAND} -E make_directory ${dir}
    RESULT_VARIABLE result
  )

  if(NOT result EQUAL 0 OR NOT IS_DIRECTORY ${dir})
    message(FATAL_ERROR "Failed to create directory ${dir}")
  endif()
endfunction()

this_function_fails("/test")

message(STATUS "The next line that will be executed")