Create userdata without user values in Lua 5.4
[liba.git] / cmake / FindLuaFormat.cmake
blobc9bf12b2e41d03de3a54f335641e1b8ef96fe44d
1 #.rst:
2 # FindLuaFormat
3 # -------------
5 # Find lua-format executable.
7 # Result Variables
8 # ^^^^^^^^^^^^^^^^
10 # This module defines the following variables:
12 # ``LUA_FORMAT_FOUND``, ``LuaFormat_FOUND``
14 # ``LUA_FORMAT_EXECUTABLE``
16 # Functions
17 # ^^^^^^^^^
19 # .. command:: add_lua_format
21 #   ::
23 #     add_lua_format(target [VERBOSE]
24 #         [WORKING_DIRECTORY dir] [COMMENT comment] [CONFIG cfg]
25 #         [OPTIONS opt ...] [SOURCES src ...] [src ...]
26 #     )
28 include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
29 find_program(LUA_FORMAT_EXECUTABLE NAMES lua-format)
30 mark_as_advanced(LUA_FORMAT_EXECUTABLE)
32 if(EXISTS "${LUA_FORMAT_EXECUTABLE}")
33   execute_process(COMMAND ${LUA_FORMAT_EXECUTABLE} --version
34     OUTPUT_VARIABLE LUA_FORMAT_VERSION ERROR_VARIABLE LUA_FORMAT_VERSION
35   )
36   string(REGEX REPLACE "[^0-9]+([^\n ]*).*" "\\1" LUA_FORMAT_VERSION "${LUA_FORMAT_VERSION}")
37 endif()
39 find_package_handle_standard_args(LuaFormat
40   FOUND_VAR
41     LuaFormat_FOUND
42   REQUIRED_VARS
43     LUA_FORMAT_EXECUTABLE
44   VERSION_VAR
45     LUA_FORMAT_VERSION
48 if(LuaFormat_FOUND)
49   set(LUA_FORMAT_FOUND 1)
50   function(add_lua_format target)
51     cmake_parse_arguments(LUA_FORMAT "VERBOSE" "WORKING_DIRECTORY;COMMENT;CONFIG" "OPTIONS;SOURCES" ${ARGN})
52     list(APPEND LUA_FORMAT_SOURCES ${LUA_FORMAT_UNPARSED_ARGUMENTS})
53     list(APPEND LUA_FORMAT_OPTIONS --in-place)
54     if(LUA_FORMAT_VERBOSE)
55       list(INSERT LUA_FORMAT_OPTIONS 0 --verbose)
56     endif()
57     if(EXISTS "${LUA_FORMAT_CONFIG}")
58       list(APPEND LUA_FORMAT_OPTIONS --config=${LUA_FORMAT_CONFIG})
59     endif()
60     if(NOT LUA_FORMAT_WORKING_DIRECTORY)
61       set(LUA_FORMAT_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
62     endif()
63     if(NOT LUA_FORMAT_COMMENT)
64       set(LUA_FORMAT_COMMENT "Formatting ${target} using lua-format")
65     endif()
66     add_custom_target(${target} # https://github.com/Koihik/LuaFormatter
67       ${LUA_FORMAT_EXECUTABLE} ${LUA_FORMAT_OPTIONS} -- ${LUA_FORMAT_SOURCES}
68       WORKING_DIRECTORY ${LUA_FORMAT_WORKING_DIRECTORY}
69       COMMENT "${LUA_FORMAT_COMMENT}"
70     )
71   endfunction()
72 endif()