代码之家  ›  专栏  ›  技术社区  ›  Daniel YC Lin

如何将makefile awk转换为build.gn

gn
  •  0
  • Daniel YC Lin  · 技术社区  · 6 年前

    原始的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
    

    转换开关

    #!/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.
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Daniel YC Lin    6 年前

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

    GN

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

    gn gen out 再次修改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",
      ]
    }
    
    推荐文章