diff options
author | Peter Wu <peter@lekensteyn.nl> | 2018-03-27 03:30:01 +0200 |
---|---|---|
committer | Gerald Combs <gerald@wireshark.org> | 2018-03-27 21:11:37 +0000 |
commit | fdef4057824ff3cfc0cd4e79b393106b73296179 (patch) | |
tree | bf82c3d3407bc8a6b009ae00e0a0daa3b9de8e83 /cmake | |
parent | 036c5a4e6ab2242614aee1cd2f31caf321a360cf (diff) |
cmake: fix "cmake -E env" compatibility with older CMake
"cmake -E env" was added in CMake 3.1, but we currently support 2.8.12
at minimum. Add a best-effort replacement for older versions. There are
some limitations from CMake (see comments), but these should not affect
the current user (FindAsciidoctor.cmake).
Change-Id: I56c92aa9ad42fb3950dbdfd955d4ff902111e0d7
Fixes: v2.5.1rc0-76-g94a0f7c641 ("Switch from AsciiDoc to Asciidoctor.")
Reviewed-on: https://code.wireshark.org/review/26658
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/env.cmake | 76 | ||||
-rw-r--r-- | cmake/modules/FindAsciidoctor.cmake | 10 |
2 files changed, 84 insertions, 2 deletions
diff --git a/cmake/env.cmake b/cmake/env.cmake new file mode 100644 index 0000000000..677c94c4f9 --- /dev/null +++ b/cmake/env.cmake @@ -0,0 +1,76 @@ +# +# Set environment variables and execute a program. +# Attempts to emulate "cmake -E env" which is only available since CMake 3.1. +# +# Copyright 2018 Peter Wu <peter@lekensteyn.nl> +# SPDX-License-Identifier: MIT +# +# Usage: +# +# cmake -P env.cmake [NAME=VALUE]... [COMMAND [ARG]...] +# +# Limitations due to CMake: +# +# - The command and arguments cannot be keywords for the "execute_process" +# command ("COMMAND", "ENCODING", "TIMEOUT", "WORKING_DIRECTORY", etc.). +# - Empty arguments are ignored, arguments with a trailing backslash ("\") will +# have the slash replaced by a forward slash ("/"). +# - If a program fails, a message will be printed and exit code 1 is returned. + +# Choose between two evils for the command line limitations: +# (1) Hard-coded number of maximum arguments and repetitive lines. +# (2) Limitations on the arguments (due to use of lists). +# (3) A combination of both. +# For simplicity, (2) is chosen here. +set(command) + +math(EXPR argsCount "${CMAKE_ARGC} - 1") +set(skip_args ${argsCount}) +set(maybe_env TRUE) + +foreach(argNumber RANGE ${argsCount}) + set(arg "${CMAKE_ARGV${argNumber}}") + + if(skip_args EQUAL 0) + # Escape ";" (list separator) to avoid splitting arguments. + string(REPLACE ";" "\\;" argForList "${arg}") + + # Prevent a trailing backslash from escaping the next list separator. + # Hopefully it is just a path separator, otherwise there will be problems. + if(argForList MATCHES "(.*)\\\\$") + message(WARNING "Trailing backslash is converted to forward slash in: ${arg}") + set(argForList "${CMAKE_MATCH_1}/") + endif() + + if(argForList STREQUAL "") + message(WARNING "Empty arguments are currently not supported and ignored") + endif() + + if(maybe_env) + # Try to parse NAME=VALUE + if(arg MATCHES "^([^=]+)=(.*)$") + set("ENV{${CMAKE_MATCH_1}}" "${CMAKE_MATCH_2}") + else() + set(maybe_env FALSE) + list(APPEND command "${argForList}") + endif() + else() + # Definitely no more env vars. + list(APPEND command "${argForList}") + endif() + else() + # Skip arguments until "-P env.cmake" is found. + if(arg STREQUAL "-P") + # just skip "env.cmake" from now on + set(skip_args 1) + else() + math(EXPR skip_args "${skip_args} - 1") + endif() + endif() +endforeach() + +execute_process(COMMAND ${command} RESULT_VARIABLE exitCode) + +if(NOT exitCode EQUAL 0) + message(FATAL_ERROR "Process exited with ${exitCode}") +endif() diff --git a/cmake/modules/FindAsciidoctor.cmake b/cmake/modules/FindAsciidoctor.cmake index 1050d16f43..409e48b865 100644 --- a/cmake/modules/FindAsciidoctor.cmake +++ b/cmake/modules/FindAsciidoctor.cmake @@ -41,8 +41,14 @@ if(ASCIIDOCTOR_EXECUTABLE) --require ${CMAKE_CURRENT_SOURCE_DIR}/asciidoctor-macros/ws_salink-inline-macro.rb ) - set(_asciidoctor_common_command ${CMAKE_COMMAND} -E - env TZ=UTC ASCIIDOCTORJ_OPTS="${_asciidoctorj_opts}" + if(CMAKE_VERSION VERSION_LESS 3.1) + set(_env_command ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/cmake/env.cmake) + else() + set(_env_command ${CMAKE_COMMAND} -E env) + endif() + + set(_asciidoctor_common_command ${_env_command} + TZ=UTC ASCIIDOCTORJ_OPTS="${_asciidoctorj_opts}" ${ASCIIDOCTOR_EXECUTABLE} ${_asciidoctor_common_args} ) |