| 1 |
# runandsort.cmake: Run a program and sort its output |
| 2 |
# For use in editorconfig; see |
| 3 |
# https://github.com/editorconfig/editorconfig/issues/375 . |
| 4 |
|
| 5 |
# BSD-2-Clause |
| 6 |
# Copyright 2018 Christopher White (cxw42 at GitHub; http://devwrench.com) |
| 7 |
# All rights reserved |
| 8 |
# |
| 9 |
# Redistribution and use in source and binary forms, with or without |
| 10 |
# modification, are permitted provided that the following conditions are met: |
| 11 |
# |
| 12 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer. |
| 14 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 |
# this list of conditions and the following disclaimer in the documentation |
| 16 |
# and/or other materials provided with the distribution. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
# POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
cmake_minimum_required(VERSION 3.16.3) |
| 31 |
|
| 32 |
# Conventions: |
| 33 |
# - "P_*" are parameters parsed using cmake_parse_arguments |
| 34 |
# - Any parameter called "RETVAL", "RETVAL_*", or "*_NAME" should be given |
| 35 |
# the name of a variable in the caller's scope into which to |
| 36 |
# store results. |
| 37 |
|
| 38 |
# Run a program with the given arguments, and return its sorted output as |
| 39 |
# a string. |
| 40 |
# CAUTION 1: May not produce correct output if any output line includes |
| 41 |
# a semicolon, because that is a list separator in CMake. |
| 42 |
# CAUTION 2: Depends on sort order of CMake; see discussion at |
| 43 |
# https://gitlab.kitware.com/cmake/cmake/issues/18551 |
| 44 |
# |
| 45 |
# Limitations: |
| 46 |
# Any \x01 in the string will be corrupted - this routine uses those to work |
| 47 |
# around CMake limitations. |
| 48 |
|
| 49 |
# Arguments: |
| 50 |
# RETVAL . . . . . . . The name of the variable to store the result in |
| 51 |
# CMDLINE . . . . . . . The program to run, and any arguments |
| 52 |
# RETVAL_FAILURE . . . . If present, a variable that will be set to TRUE |
| 53 |
# if PGM returns a non-zero exit code. |
| 54 |
# CAPTURE_STDERR . . . . If present, capture stderr instead of stdout |
| 55 |
# TRIM_INITIAL_LEADING_SPACE If present, remove initial spaces from the |
| 56 |
# first line. This is to work around a hack |
| 57 |
# in almostcat.cmake. |
| 58 |
# |
| 59 |
# Returns: |
| 60 |
# The sorted stdout of PGM, or the FAILURE string if PGM failed. |
| 61 |
# PGM's stderr is ignored. |
| 62 |
|
| 63 |
function(run_and_sort) |
| 64 |
# Argument parsing |
| 65 |
set(option_keywords CAPTURE_STDERR TRIM_INITIAL_LEADING_SPACE) |
| 66 |
set(one_value_keywords RETVAL RETVAL_FAILURE) |
| 67 |
set(multi_value_keywords CMDLINE ARGS) |
| 68 |
cmake_parse_arguments(P "${option_keywords}" "${one_value_keywords}" |
| 69 |
"${multi_value_keywords}" ${ARGN}) |
| 70 |
|
| 71 |
#message(STATUS "Running ${P_CMDLINE}") # DEBUG |
| 72 |
execute_process(COMMAND ${P_CMDLINE} |
| 73 |
RESULT_VARIABLE ep_retval |
| 74 |
OUTPUT_VARIABLE ep_stdout |
| 75 |
ERROR_VARIABLE ep_stderr |
| 76 |
) |
| 77 |
|
| 78 |
# Which one are we processing? |
| 79 |
if(${P_CAPTURE_STDERR}) |
| 80 |
set(ep_out "${ep_stderr}") |
| 81 |
else() |
| 82 |
set(ep_out "${ep_stdout}") |
| 83 |
endif() |
| 84 |
|
| 85 |
#message(STATUS "Got retval =${ep_retval}=") # DEBUG |
| 86 |
#message(STATUS "Got stdout =${ep_stdout}=") # DEBUG |
| 87 |
#message(STATUS "Got stderr =${ep_stderr}=") # DEBUG |
| 88 |
|
| 89 |
# Early bail on failure |
| 90 |
if(NOT("${ep_retval}" EQUAL "0")) |
| 91 |
set(${P_RETVAL} "" PARENT_SCOPE) |
| 92 |
if("${P_RETVAL_FAILURE}" MATCHES ".") # if we got a name |
| 93 |
set(${P_RETVAL_FAILURE} TRUE PARENT_SCOPE) |
| 94 |
endif() |
| 95 |
return() |
| 96 |
endif() |
| 97 |
|
| 98 |
# Trim hack |
| 99 |
if(${P_TRIM_INITIAL_LEADING_SPACE}) |
| 100 |
string(REGEX REPLACE "^[ ]+" "" ep_out "${ep_out}") |
| 101 |
endif() |
| 102 |
|
| 103 |
# Change all the semicolons in the output to \x01 |
| 104 |
string(ASCII 1 ONE) |
| 105 |
string(REPLACE ";" "${ONE}" ep_out "${ep_out}") |
| 106 |
#message(STATUS "After escaping =${ep_out}=") # DEBUG |
| 107 |
|
| 108 |
# Normalize line endings, just in case |
| 109 |
string(REGEX REPLACE "\r|\n|\r\n" "\n" ep_out "${ep_out}") |
| 110 |
#message(STATUS "After line-endings =${ep_out}=") # DEBUG |
| 111 |
|
| 112 |
# Turn the string into a list |
| 113 |
string(REPLACE "\n" ";" ep_out "${ep_out}") |
| 114 |
#message(STATUS "After listifying =${ep_out}=") # DEBUG |
| 115 |
|
| 116 |
# Sort the list |
| 117 |
list(SORT ep_out) |
| 118 |
|
| 119 |
# Back to individual lines |
| 120 |
string(REPLACE ";" "\n" ep_out "${ep_out}") |
| 121 |
#message(STATUS "After back to lines =${ep_out}=") # DEBUG |
| 122 |
|
| 123 |
# And back to semicolons. Note: I am not trying to reverse line endings. |
| 124 |
string(REPLACE "${ONE}" ";" ep_out "${ep_out}") |
| 125 |
#message(STATUS "After unescaping =${ep_out}=") # DEBUG |
| 126 |
|
| 127 |
# Out to the caller |
| 128 |
set(${P_RETVAL} "${ep_out}" PARENT_SCOPE) |
| 129 |
#message(STATUS "Returned =${ep_out}=") # DEBUG |
| 130 |
|
| 131 |
endfunction(run_and_sort) |
| 132 |
|