From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 9B31523B3EF; Mon, 9 Jan 2023 18:43:12 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9B31523B3EF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1673278992; bh=TuUxxdESvuT3s2/xrBmxzcTs42J+9B0Aj4yEEVQeZ+8=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=PGPjeJY11ziYPEtSORdbHTXNR4zYkX2IlKl0MKhUK4UQSuEYZ3TQVeA1kxLHtwdlw 9+koTGDj7l6bizi5nD2HKvNR3382HlmDq9Bdv7z4BLtFiBBaZyho7ToLqlXgXqK5gI oxvcQq2KrXll3NSrhUS/BQR9ckl/jqp/3x779sB4= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 31CD179901 for ; Mon, 9 Jan 2023 18:43:11 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 31CD179901 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1pEuIn-0005PW-Su; Mon, 09 Jan 2023 18:43:10 +0300 To: Sergey Kaplun , Maxim Kokryashkin Date: Mon, 9 Jan 2023 18:28:21 +0300 Message-Id: X-Mailer: git-send-email 2.34.0 In-Reply-To: <20221221143317.477323-1-m.kokryashkin@tarantool.org> References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD90D1502B3BE350FE4B0ECD008C8B7787646428637BB112E1300894C459B0CD1B9B1EEFCD1A5E8966E7DA2DBA963D2AF008184FA132C01006D3E4D764DDA6573C6 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76AB1B6FB25ACEDC9EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637EA9DEEAA3ECF8E948638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D84269E5C028C5EE0E98B1C202B497F1F7117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC5FC25ED3FCEC3375A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735200AC5B80A05675ACD618001F51B5FD3F9D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6AAFF6397ACA71750089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A56592242643106F7395BBD8C1FBB3DD275FE52440848A21D14EAF44D9B582CE87C8A4C02DF684249CC203C45FEA855C8F X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3407FE5477D6A8AF084959D07B77DAC186ED1DBB7490229D0992DBA93551E1934269C6C2676612FBB41D7E09C32AA3244CBB17BC45129267AD1E8BE7DB7B2E92278A6D4CC6FBFAC251927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojqOyU7017cIvGuQQ7LJV/fA== X-DA7885C5: 652FD82A9F174615126A15F154E6D09372E9561CAC4A7FBCB21CB6CE2CFC7D21262E2D401490A4A0DB037EFA58388B346E8BC1A9835FDE71 X-Mailru-Sender: 689FA8AB762F73933AF1F914F131DBF5E6B65D236C76B9185152FCB20A1FDFF5A7C8D0F45F857DBFE9F1EFEE2F478337FB559BB5D741EB964C8C2C849690F8E70A04DAD6CC59E3365FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH] cmake: introduce CheckUnwindTables helper X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Igor Munkin via Tarantool-patches Reply-To: Igor Munkin Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" After struggling with black voodoo magic oneliner provided by Mike Pall in scope of e131936133c58de4426c595db2341caf5a1665b5 ("Cleanup and enable external unwinding for more platforms.") to check whether the target toolchain always generates unwind tables, it was decided to implement it as a CMake function to encapsulate and comment this damn "grep" spell. Signed-off-by: Igor Munkin --- Branch: https://github.com/tarantool/luajit/compare/imun/fix-build-for-non-bash CI is green(*): https://github.com/tarantool/luajit/commit/e717eaa (*) Max, could you please check this patch on the Alpine? cmake/CheckUnwindTables.cmake | 114 ++++++++++++++++++++++++++++++++++ cmake/SetTargetFlags.cmake | 18 ++---- 2 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 cmake/CheckUnwindTables.cmake diff --git a/cmake/CheckUnwindTables.cmake b/cmake/CheckUnwindTables.cmake new file mode 100644 index 00000000..903977ad --- /dev/null +++ b/cmake/CheckUnwindTables.cmake @@ -0,0 +1,114 @@ +# CheckUnwindTables provides a convenient way to define whether +# the target toolchain generates unwind tables. +# +# This function implements black voodoo magic provided by Mike +# Pall in scope of e131936133c58de4426c595db2341caf5a1665b5 +# ("Cleanup and enable external unwinding for more platforms.") +# via CMake. +# +# Example usage: +# +# # Find out whether the target toolchain always generates +# # unwind tables. +# CheckUnwindTables(HAVE_UNWIND_TABLES) +# if(HAVE_UNWIND_TABLES) +# AppendFlags(TARGET_C_FLAGS -DLUAJIT_UNWIND_EXTERNAL) +# endif() + +function(CheckUnwindTables status) + set(_CHECK_UNWIND_TABLES_RESULT FALSE) + + # 1. Build the command compiling the simple object file using + # the target toolchain. + # XXX: CMake helper can't be used, since there is + # no executable file as a result, but only an object file (in + # other words, there is no
function in C source file). + set(_TEST_UNWIND_OBJECT "${CMAKE_BINARY_DIR}/test-unwind-tables.o") + string(CONCAT _TEST_UNWIND_SOURCE + "extern void b(void);" + "int a(void) {" + "b();" + "return 0;" + "}" + ) + string(CONCAT _TEST_UNWIND_COMPILE + "echo '${_TEST_UNWIND_SOURCE}'" + "|" + "${CMAKE_C_COMPILER} -c -x c - -o ${_TEST_UNWIND_OBJECT}" + ) + + # 2. Use to find either .eh_frame (for ELF) or + # __unwind_info (for Mach-O) entries in the compiled object + # file. After the several attempts in scope of the following + # commits: + # * e131936133c58de4426c595db2341caf5a1665b5 ("Cleanup and + # enable external unwinding for more platforms.") + # * d4a554d6ee1507f7313641b26ed09bf1b518fa1f ("OSX: Fix build + # by hardcoding external frame unwinding.") + # * b9d523965b3f55b19345a1ed1ebc92e431747ce1 ("BSD: Fix build + # with BSD grep.") + # * 66563bdab0c7acf3cd61dc6cfcca36275951d084 ("Fix build with + # busybox grep.") + # Mike came up with the solution to use both alternatives: + # `grep -qa' as the major one and `grep -qU' as a fallback for + # BSD platforms. + set(_TEST_GREP_GNU "grep -qa") + set(_TEST_GREP_BSD "grep -qU") + set(_TEST_GREP_PATTERN "-e eh_frame -e __unwind_info") + string(CONCAT _TEST_UNWIND_CHECK + # XXX: Mind the space after the opening brace. The space is + # vital since { is a *reserved word* (i.e. the command built + # into the shell). For more info see the following link: + # https://www.gnu.org/software/bash/manual/html_node/Reserved-Words.html. + "{ " + "${_TEST_GREP_GNU} ${_TEST_GREP_PATTERN} ${_TEST_UNWIND_OBJECT}" + "||" + "${_TEST_GREP_BSD} ${_TEST_GREP_PATTERN} ${_TEST_UNWIND_OBJECT}" + # XXX: Mind the semicolon prior to the closing brace. The + # semicolon is vital due to we are executing the list of shell + # commands that has to be terminated by one of ';', '&', or a + # newline. Considering it will be executed synchronously via + # , only the first option fits here. For more + # info see the following link: + # https://www.gnu.org/software/bash/manual/html_node/Lists.html + # XXX: Considering the preceding semicolon, there is no need to + # separate } command with the additional whitespace. + ";}" + ) + + # 3. Use step 1 and step 2 to check whether target toolchain + # always generates unwind tables. + # XXX: There is no need in "echo E" command at the end, since + # we can check $? of the command by value. + # Fun fact: there is $(.SHELLSTATUS) variable in GNU Make, but + # it can't be used in / conditions, so we can't get + # rid of "echo E" in the original Makefile machinery. + execute_process( + COMMAND /bin/sh -c "${_TEST_UNWIND_COMPILE} && ${_TEST_UNWIND_CHECK}" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + RESULT_VARIABLE _TEST_UNWIND_RC + ERROR_QUIET + OUTPUT_QUIET + ) + + if(_TEST_UNWIND_RC EQUAL 0) + set(_CHECK_UNWIND_TABLES_RESULT TRUE) + endif() + + # Remove generated object file. + file(REMOVE ${_TEST_UNWIND_OBJECT}) + + set(${status} ${_CHECK_UNWIND_TABLES_RESULT} PARENT_SCOPE) + # XXX: Unset the internal variable to not spoil CMake cache. + # Study the case in CheckIPOSupported.cmake, that affected this + # module: https://gitlab.kitware.com/cmake/cmake/-/commit/4b82977 + unset(_CHECK_UNWIND_TABLES_RESULT) + unset(_TEST_UNWIND_RC) + unset(_TEST_UNWIND_CHECK) + unset(_TEST_GREP_PATTERN) + unset(_TEST_GREP_BSD) + unset(_TEST_GREP_GNU) + unset(_TEST_UNWIND_COMPILE) + unset(_TEST_UNWIND_SOURCE) + unset(_TEST_UNWIND_OBJECT) +endfunction() diff --git a/cmake/SetTargetFlags.cmake b/cmake/SetTargetFlags.cmake index 1ca438f1..3b9e481d 100644 --- a/cmake/SetTargetFlags.cmake +++ b/cmake/SetTargetFlags.cmake @@ -6,6 +6,8 @@ # * TARGET_SHARED_FLAGS # * TARGET_LIBS +include(CheckUnwindTables) + if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") set(BUILDVM_MODE machasm) else() # Linux and FreeBSD. @@ -21,18 +23,10 @@ else() string(FIND ${TARGET_C_FLAGS} "LJ_NO_UNWIND 1" UNWIND_POS) if(UNWIND_POS EQUAL -1) # Find out whether the target toolchain always generates - # unwindtables. - execute_process( - COMMAND bash -c "exec 2>/dev/null; echo 'extern void b(void);int a(void){b();return 0;}' | ${CMAKE_C_COMPILER} -c -x c - -o tmpunwind.o && { grep -qa -e eh_frame -e __unwind_info tmpunwind.o || grep -qU -e eh_frame -e __unwind_info tmpunwind.o; } && echo E; rm -f tmpunwind.o" - WORKING_DIRECTORY ${LUAJIT_SOURCE_DIR} - OUTPUT_VARIABLE TESTUNWIND - RESULT_VARIABLE TESTUNWIND_RC - ) - if(TESTUNWIND_RC EQUAL 0) - string(FIND "${TESTUNWIND}" "E" UNW_TEST_POS) - if(NOT UNW_TEST_POS EQUAL -1) - AppendFlags(TARGET_C_FLAGS -DLUAJIT_UNWIND_EXTERNAL) - endif() + # unwind tables. + CheckUnwindTables(HAVE_UNWIND_TABLES) + if(HAVE_UNWIND_TABLES) + AppendFlags(TARGET_C_FLAGS -DLUAJIT_UNWIND_EXTERNAL) endif() endif() endif() -- 2.34.0