0

原来的 Makefile 像

hello2.cc: hello.cc
  awk '$1 != "//" { print }' < hello.cc > hello2.cc

我将从官方 gn 示例开始

git clone --depth=1 https://gn.googlesource.com/gn
cp -a gn/tools/gn/example .
cd example
gn gen out
ninja -C out

转换.sh

#!/bin/bash
echo "1=$1"
echo "2=$2"
awk '$1 != "//" { print }' < "$1" > "$2"

我的 BUILD.gn 草案是

action("awk") {
  script = "convert.sh"
  sources = [ "hello.cc" ]
  outputs = [ "$target_out_dir/hello2.cc" ]
  args = rebase_path(sources, root_build_dir)
# +
#    [ rebase_path(target_gen_dir, root_build_dir) ]
}
executable("hello") {
  sources = [
    "hello2.cc",  # I just modify this line
  ]

  deps = [
    ":hello_shared",
    ":hello_static",
    ":awk",
  ]
}

shared_library("hello_shared") {
  sources = [
    "hello_shared.cc",
    "hello_shared.h",
  ]

  defines = [ "HELLO_SHARED_IMPLEMENTATION" ]
}

static_library("hello_static") {
  sources = [
    "hello_static.cc",
    "hello_static.h",
  ]
}

'ninja -C out -v' 的输出,是否意味着脚本只能是 python?

ninja: Entering directory `out'
[1/4] python ../convert.sh ../hello.cc
FAILED: obj/hello2.cc
python ../convert.sh ../hello.cc
  File "../convert.sh", line 2
    echo "1=$1"
              ^
SyntaxError: invalid syntax
ninja: build stopped: subcommand failed.
4

1 回答 1

0

关键是默认脚本是python。这是可行的设置

.gn

buildconfig = "//build/BUILDCONFIG.gn"
script_executable = "/bin/bash"   # ADDED this line

再次运行gn gen out,修改BUILD.gn为

action("awk") {
  script = "convert.sh"
  sources = [ "hello.cc" ]
  outputs = [ "$target_out_dir/hello2.cc" ]
  args = rebase_path(sources, root_build_dir) +
    rebase_path(outputs, root_build_dir)
}
executable("hello") {
  sources = [
    "$target_out_dir/hello2.cc",
  ]
  include_dirs = [ "//" ]  # to support #include in original .cc
  deps = [
    ":hello_shared",
    ":hello_static",
    ":awk",
  ]
}

shared_library("hello_shared") {
  sources = [
    "hello_shared.cc",
    "hello_shared.h",
  ]

  defines = [ "HELLO_SHARED_IMPLEMENTATION" ]
}

static_library("hello_static") {
  sources = [
    "hello_static.cc",
    "hello_static.h",
  ]
}
于 2018-10-04T03:06:22.457 回答