Yocto Recipe中通过postinstall解决交叉编译ARM二进制执行兼容问题
Let’s walk through each of your questions with clear, actionable solutions tailored to your Yocto and Makefile setup:
1. Modified Makefile (Removing the json Target)
The json target relies on your ARM-built analyzer binary, which can’t run on an x86 host—so we’ll strip out all related code. Here’s the cleaned-up Makefile:
TARGETDIR=../rel/$(PLATFORM) ANALYZER=$(TARGETDIR)/analyzer TARGETS=$(ANALYZER) all: $(TARGETS) $(ANALYZER): analyzer.c pgn.c analyzer.h pgn.h ../common/common.c ../common/common.h Makefile $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $(ANALYZER) -I../common pgn.c analyzer.c ../common/common.c $(LDLIBS$(LDLIBS-$(@)))
We removed:
- The
JSONFILEandXMLFILEvariables (only used by thejsontarget) - The entire
jsontarget rule
2. Do I need to add DEPENDS = "libxslt libxml2"?
No, you don’t need this in DEPENDS—here’s why:
DEPENDSspecifies build-time dependencies (libraries/tools needed to compile your code). Youranalyzerbinary doesn’t link againstlibxml2orlibxslt, so these aren’t required for compilation.- Instead, you need to add
libxslttoRDEPENDS_${PN}(runtime dependencies), since the target device will needxsltproc(provided bylibxslt) to convert the XML to JSON during installation.
3. Do I need to add RDEPENDS_${PN} = "bash"?
Only if your post-install script uses bash-specific syntax.
Yocto’s default shell on target devices is a POSIX-compliant /bin/sh (usually busybox sh or dash). If your script only uses standard shell features (simple commands, pipes, redirection), bash isn’t needed. Reserve this dependency only if you use bash-exclusive tools like arrays, [[ ]] conditionals, or shopt settings.
4. Writing the Target Device Post-Install Script
We’ll use Yocto’s pkg_postinst_${PN} hook, which runs on the target device when the package is installed. First, update your Recipe to install the XSLT script and define the post-install logic:
SUMMARY = "CANBOAT" SECTION = "base" LICENSE = "GPLv3" LIC_FILES_CHKSUM = "file://GPL;md5=05507c6da2404b0e88fe5a152fd12540" S = "${WORKDIR}/git" SRC_URI = "git://github.com/canboat/canboat.git;branch=${SRCBRANCH}" SRCBRANCH = "master" SRCREV = "93b2ebfb334d7a9750b6947d3a4af9b091be2432" EXTRA_OEMAKE = "'CC=${CC}' 'AR=${AR}'" # Runtime dependency: target needs xsltproc for XML->JSON conversion RDEPENDS_${PN} += "libxslt" do_compile() { oe_runmake } do_install() { oe_runmake install # Install the XSLT script to a shared directory on the target install -d ${D}${datadir}/canboat/ install -m 0644 ${S}/pgns2json.xslt ${D}${datadir}/canboat/ } # This script executes on the target device during package installation pkg_postinst_${PN}() { #!/bin/sh set -e # Define paths on the target (adjust analyzer path if your make install uses a different location) ANALYZER="/usr/bin/analyzer" XSLT_SCRIPT="/usr/share/canboat/pgns2json.xslt" TEMP_XML="/tmp/pgns.xml" FINAL_JSON="/usr/share/canboat/pgns.json" # Generate XML, convert to JSON, clean up ${ANALYZER} -explain-xml > ${TEMP_XML} xsltproc ${XSLT_SCRIPT} ${TEMP_XML} > ${FINAL_JSON} rm -f ${TEMP_XML} # Set correct permissions for the JSON file chmod 0644 ${FINAL_JSON} }
Key details:
do_installadds thepgns2json.xsltscript to the target’s shared directory so the post-install script can access it.pkg_postinst_${PN}runs on the target, using the native ARManalyzerbinary to generate XML, thenxsltprocto convert it to JSON. We use a temporary XML file to avoid clutter.- Double-check the
ANALYZERpath: if yourmake installputs the binary somewhere other than/usr/bin/, update this accordingly.
内容的提问来源于stack exchange,提问作者user4851126

