How to convert a Makefile awk to BUILD.gn

858 views Asked by At

The original Makefile like

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

I'll started from official gn example

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=$1"
echo "2=$2"
awk '$1 != "//" { print }' < "$1" > "$2"

My draft BUILD.gn is

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",
  ]
}

The output of 'ninja -C out -v', does that means script should be python only?

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.
1

There are 1 answers

1
Daniel YC Lin On BEST ANSWER

The key point is default script is python. Here are workable settings

.gn

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

Run gn gen out again, modify BUILD.gn as

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",
  ]
}