在 cmake execute_process 中执行 mustache(作为 ruby 脚本)

Execute mustache (as ruby script) in cmake execute_process

我想使用 mustache 作为 cmake 中用于代码生成的简单模板引擎。

我尝试用 execute_process 执行它,如下所示:

execute_process( COMMAND "/path/to/mustache" "<data> <template>" )

但是它说它不是有效的 WIN32 应用程序。事实上,小胡子是一个 ruby 脚本:

#!D:/programs/Ruby23/bin/ruby.exe
#
# This file was generated by RubyGems.
#
# The application 'mustache' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0.a"

if ARGV.first

    ...

所以我尝试了:

execute_process( COMMAMD "/path/to/ruby" "/path/to/mustache --help" )

但是也没用...No such file or directory -- D:/programs/Ruby23/bin/mustache --help (LoadError)

如何在 cmake execute_process 中执行 ruby 脚本?

execute_process(COMMAND < cmd1 > [args1...]] ...)

参数必须作为列表而不是字符串传递。

    # path to executables
    set(RUBY_EXECUTABLE D:/programs/Ruby23/bin/ruby.exe CACHE STRING "ruby executable")
    set(MUSTACHE_SCRIPT D:/programs/Ruby23/bin/mustache CACHE STRING "mustache ruby script")

    # function that call mustache
    function(apply_mustache data template result)
        execute_process(
            COMMAND ${RUBY_EXECUTABLE} -E UTF-8 ${MUSTACHE_SCRIPT} ${data} ${template}
            OUTPUT_VARIABLE result_t
            )
        set(${result} ${result_t} PARENT_SCOPE)
    endfunction()

bonus: -E UTF-8 防止 ruby 乱用 utf-8 字符...