lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 7/15/18, Sarah <morningcrafter0603@gmail.com> wrote:
> I am writing a build script in Lua for my language and I need to find
> out if the user has either Clang or GCC installed. My first idea was
> to run os.execute("clang") and check the return code, but clang just
> errors straight to the console.


You can run "clang -v" (or "clang --help") instead.

Here's a function, try_program(), for doing this. It was taken from
[1]. It claims to be compatible with various Lua versions:

    ---
    -- Figures out if a program is installed (by
    -- trying to run it).
    --
    -- Argument 'cmd' is often just the program name.
    -- Sometimes (as in git) the program would expect
    -- *some* argument on the command line (or else
    -- report error). You can get away with this, in git's
    -- case for example, by passing "git --version". Each
    -- program has its own rules.
    --
    -- STDIN, STDOUT, and STDERR are all redirected
    -- to /dev/null.
    --
    function try_program(cmd)
      local full = cmd .. " < /dev/null > /dev/null 2>&1"
      local res = os.execute(full)
      -- "== true" is for Lua 5.3
      -- "== 0" is for olders.
      return (res == 0) or (res == true)
    end


[1] https://github.com/mooffie/mc/blob/luatip/src/lua/library/modules/samples/libs/os.lua