bash 来自 Android.mk 的 Shell 脚本调用、标准输出和缺少分隔符错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9869349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:51:29  来源:igfitidea点击:

Shell script call from Android.mk, standard output and missing separator error

bashshellandroid-ndkstdout

提问by Sogartar

I have a simple Android.mk file:

我有一个简单的 Android.mk 文件:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

$(shell ($(LOCAL_PATH)/echo_test.sh))

LOCAL_MODULE := libecho_test
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)

The interesting thing that it does is to call the 'echo_test.sh' bash script. In the case when the contents of the script are

它所做的有趣的事情是调用'echo_test.sh' bash 脚本。如果脚本的内容是

#!/bin/bash
echo 'echo is working' >&2

or

或者

#!/bin/bash
echo 'echo is working' >/dev/null

everything is OK.

一切都好。

Things go wrong when the bash script is

当 bash 脚本出现问题时

#!/bin/bash
echo 'echo is working'

or

或者

#!/bin/bash
echo 'echo is working' >&1

Then the returned error is

然后返回的错误是

Android.mk:4: *** missing separator.  Stop. 

This happens both with Android NDK 7 and when you include this module during the build of Android Ice Cream Sandwich 4.0.3.

在 Android NDK 7 以及在 Android Ice Cream Sandwich 4.0.3 的构建过程中包含此模块时,都会发生这种情况。

I really can't understand what's the deal with the standard output and the Android build system. Does anyone have an explanation?

我真的无法理解标准输出和 Android 构建系统有什么关系。有人有解释吗?

回答by richq

The Android NDK build system is actually GNU Make. All of the code in the Android.mk file has to be valid make.

Android NDK 构建系统实际上是GNU Make。Android.mk 文件中的所有代码都必须有效make

When you run $(shell) and don't store the value in a variable, then it is as if you copied the standard output of the script into your Android.mk file. i.e. it is as if your file contained the following:

当您运行 $(shell) 并且不将值存储在变量中时,就好像您将脚本的标准输出复制到您的 Android.mk 文件中一样。即就好像您的文件包含以下内容:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

echo is working

LOCAL_MODULE := libecho_test
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)

.. which is not valid make syntax. Redirecting to >&2 in your script works because the output goes to the error output and is then shown on the console.

.. 这是无效的 make 语法。在脚本中重定向到 >&2 有效,因为输出会转到错误输出,然后显示在控制台上。

As Vishrut mentions, use $(info) or $(warning) to print messages. Or if you really want to run a script during the build, store its output in a variable:

正如 Vishrut 提到的,使用 $(info) 或 $(warning) 打印消息。或者,如果您真的想在构建期间运行脚本,请将其输出存储在一个变量中:

ECHO_RESULT := $(shell ($(LOCAL_PATH)/echo_test.sh))

Here you won't see the echo output of the script, it goes into the variable.

在这里你不会看到脚本的 echo 输出,它进入了变量。

回答by Vishrut

Try $(info $(shell ($(LOCAL_PATH)/echo_test.sh))), it works.

尝试$(info $(shell ($(LOCAL_PATH)/echo_test.sh))),它有效。

回答by Eun

Since richq's Answer doesn't work for me I use this:

由于richq的答案对我不起作用,我使用这个:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libecho_test
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)

all:
    echo hello