From patchwork Tue Jun 14 15:11:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paulo Neves X-Patchwork-Id: 9208 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7555C433EF for ; Tue, 14 Jun 2022 15:11:25 +0000 (UTC) Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by mx.groups.io with SMTP id smtpd.web09.7360.1655219478122313165 for ; Tue, 14 Jun 2022 08:11:18 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=H9puScvS; spf=pass (domain: gmail.com, ip: 209.85.221.48, mailfrom: ptsneves@gmail.com) Received: by mail-wr1-f48.google.com with SMTP id m24so11677474wrb.10 for ; Tue, 14 Jun 2022 08:11:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jz5NAw+FoFqtqB0JVZHivCln3Thx4vdv26ZsFa2A8oE=; b=H9puScvSU02EyxR37EKUFTAp0TlZb6aKsByX989OKbUnwusl06VrPws3Xbqj6BnCWA RHF2JVh8A3vWTDuzVNxyVazlTKwuDeP3nsbtSTuaU2gal43jreAZ4QFB20tj778HkySD cNfouDpcq0cP7cr5EEXFSKRYuS+mppI1DF2HQF01HBa8sTMwMX7DAhJnEzl/PPByx9Ma iEiuYWTZpAfwkpAYJWiFddUGV8h6MuJhrxLjy4wyLSyelToJVV42F0dBShiIpjoue4QN qy2sD29hVpNl13dlzrrzVgHYw4qSyx2QAs5jTZW1x/6fEuj6w9xwZA++/lERVnnw+FfZ zTww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jz5NAw+FoFqtqB0JVZHivCln3Thx4vdv26ZsFa2A8oE=; b=uaq4sR14lWryDbjc9MWBDMC/JfqI0ZPgqRgSPiXkD60jSad/gN88DdMBuiWuwPPozp RToKlmJbjSwxtz6An3u+0liESfwwntHL28U5KOVRkKJ8/nQXrz8MjLHPbm55CPxnKrep 9Ol2SzY+sGnd/XdrqeJG6NE+PsCWd3910j/vT+vd1ANGmk6djvkRINZvbDtWKaP2Wm4V ScYvncbT6aHXEvSUsMSC/Mt7fPluMOMqtgAkILXvrYEd+x9+kehba5HKHxBy0BzPCR9q 6bVPc1ALzteaRZQZXOd3HxE6M+p8z4x1Y66uTWLle866iXz6VKk5kcyxO0VwsPcARwGv XV1g== X-Gm-Message-State: AJIora/wTqjI/0ROmkV5pno1ULr38r5LB3c+b9B4d4SjmadAD5nLER/c b+0PSKTaxZha98Dc0ez2Yp/6uwMPpw== X-Google-Smtp-Source: AGRyM1u2QK2Q2ySX827Cdf5eW7rakxyCL2fo79XHdjo3xmnK7UEOGJbkc1N58yhIItDjQFeIwa9/IA== X-Received: by 2002:adf:dd0a:0:b0:213:ba65:73fa with SMTP id a10-20020adfdd0a000000b00213ba6573famr5450971wrm.521.1655219476559; Tue, 14 Jun 2022 08:11:16 -0700 (PDT) Received: from localhost.localdomain (031011128028.dynamic-3-poz-k-0-2-0.vectranet.pl. [31.11.128.28]) by smtp.gmail.com with ESMTPSA id h62-20020a1c2141000000b0039c151298b7sm18114522wmh.10.2022.06.14.08.11.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jun 2022 08:11:16 -0700 (PDT) From: Paulo Neves To: openembedded-core@lists.openembedded.org Cc: Paulo Neves Subject: [PATCH v5 6/7] utils: Add cmdline_shebang_wrapper util. Date: Tue, 14 Jun 2022 17:11:04 +0200 Message-Id: <20220614151105.1890454-6-ptsneves@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220614151105.1890454-1-ptsneves@gmail.com> References: <20220606155807.1414519-1-ptsneves@gmail.com> <20220614151105.1890454-1-ptsneves@gmail.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Tue, 14 Jun 2022 15:11:25 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/openembedded-core/message/166900 Useful to work around shebang relocation issues, where shebangs are too long or have arguments in them, thus preventing them from using the /usr/bin/env shebang. --- .../wrapper/cmdline-shebang-wrapper-test.bb | 21 ++++++++++++ .../recipes-test/wrapper/files/test.awk | 2 ++ meta/classes/utils.bbclass | 34 +++++++++++++++++++ meta/lib/oeqa/selftest/cases/wrapper.py | 11 ++++++ 4 files changed, 68 insertions(+) create mode 100644 meta-selftest/recipes-test/wrapper/cmdline-shebang-wrapper-test.bb create mode 100644 meta-selftest/recipes-test/wrapper/files/test.awk create mode 100644 meta/lib/oeqa/selftest/cases/wrapper.py diff --git a/meta-selftest/recipes-test/wrapper/cmdline-shebang-wrapper-test.bb b/meta-selftest/recipes-test/wrapper/cmdline-shebang-wrapper-test.bb new file mode 100644 index 0000000000..c4126a41fc --- /dev/null +++ b/meta-selftest/recipes-test/wrapper/cmdline-shebang-wrapper-test.bb @@ -0,0 +1,21 @@ +SUMMARY = "Check that create_cmdline_shebang works" +LICENSE = "MIT" +LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420" +INHIBIT_DEFAULT_DEPS = "1" + +SRC_URI += "file://test.awk" + +EXCLUDE_FROM_WORLD = "1" +do_install() { + install -d ${D}${bindir} + install -m 0755 ${WORKDIR}/test.awk ${D}${bindir}/test + sed -i -e 's|@AWK_BIN@|${bindir}/awk|g' ${D}${bindir}/test + create_cmdline_shebang_wrapper ${D}${bindir}/test + if [ $(${D}${bindir}/test) != "Don't Panic!" ]; then + bbfatal "Wrapper is broken" + else + bbnote "Wrapper is good" + fi +} + +BBCLASSEXTEND = "native" diff --git a/meta-selftest/recipes-test/wrapper/files/test.awk b/meta-selftest/recipes-test/wrapper/files/test.awk new file mode 100644 index 0000000000..91429197b1 --- /dev/null +++ b/meta-selftest/recipes-test/wrapper/files/test.awk @@ -0,0 +1,2 @@ +#! @AWK_BIN@ -f +BEGIN { print "Don't Panic!" } diff --git a/meta/classes/utils.bbclass b/meta/classes/utils.bbclass index b4eb3d38ab..b617632d9f 100644 --- a/meta/classes/utils.bbclass +++ b/meta/classes/utils.bbclass @@ -184,6 +184,40 @@ END chmod +x $cmd } +create_cmdline_shebang_wrapper () { + # Create a wrapper script where commandline options are needed + # + # These are useful to work around shebang relocation issues, where shebangs are too + # long or have arguments in them, thus preventing them from using the /usr/bin/env + # shebang + # + # Usage: create_cmdline_wrapper FILENAME + + cmd=$1 + shift + + echo "Generating wrapper script for $cmd" + + # Strip #! and get remaining interpreter + arg + argument="$(basename "$(head -n1 $cmd | sed -e 's|#![ ]*||g' )")" + # strip the shebang from the real script as we do not want it to be usable anyway + tail -n +2 $cmd > $cmd.real + cmdname=$(basename $cmd) + dirname=$(dirname $cmd) + cmdoptions=$@ + if [ "${base_prefix}" != "" ]; then + relpath=`python3 -c "import os; print(os.path.relpath('${D}${base_prefix}', '$dirname'))"` + cmdoptions=`echo $@ | sed -e "s:${base_prefix}:\\$realdir/$relpath:g"` + fi + cat <$cmd +#!/usr/bin/env bash +realpath=\`readlink -fn \$0\` +realdir=\`dirname \$realpath\` +exec -a \$realdir/$cmdname $argument \$realdir/$cmdname.real $cmdoptions "\$@" +END + chmod +x $cmd +} + create_wrapper () { # Create a wrapper script where extra environment variables are needed # diff --git a/meta/lib/oeqa/selftest/cases/wrapper.py b/meta/lib/oeqa/selftest/cases/wrapper.py new file mode 100644 index 0000000000..6de63310c0 --- /dev/null +++ b/meta/lib/oeqa/selftest/cases/wrapper.py @@ -0,0 +1,11 @@ +from oeqa.selftest.case import OESelftestTestCase +from oeqa.utils.commands import bitbake + +class WrapperTests(OESelftestTestCase): + def test_shebang_wrapper(self): + """ + Summary: Build a recipe which will fail if the cmdline_shebang_wrapper function is defective. + Expected: Exit status to be 0. + Author: Paulo Neves + """ + res = bitbake("cmdline-shebang-wrapper-test -c install", ignore_status=False)