如何将 Makefile awk 转换为 BUILD.gn

How to convert a Makefile awk to BUILD.gn

原来的Makefile喜欢

hello2.cc: hello.cc
  awk ' != "//" { 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

convert.sh

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

我的草稿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="
              ^
SyntaxError: invalid syntax
ninja: build stopped: subcommand failed.

重点是默认脚本是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",
  ]
}