Skip to content

Unrecognized command line parameters before -t are ignored on linux #2689

@HampusAdolfsson

Description

@HampusAdolfsson

I found a difference in how command line options are parsed on windows and linux, and am wondering if this is intentional.

On windows, any parameter that precedes the -t flag, and is not one of the global flags listed with -h, is passed on to the rest of the program. This means that targets and tool-specific flags can be passed before -t. On linux, any such parameters are ignored and never seen by the rest of the program.

Some examples:

# lists the command for 'foo.o' and 'bar.o' on windows but lists all commands on linux (equivalent to ninja -t commands)
ninja foo.o bar.o -t commands

# cleans generator outputs on windows but not on linux
ninja -g -t clean

The difference comes from here:

ninja/src/ninja.cc

Lines 1703 to 1705 in 931d990

while (!options->tool &&
(opt = getopt_long(*argc, *argv, "d:f:j:k:l:nt:vw:C:h", kLongOptions,
NULL)) != -1) {

Ninja's own implementation of getopt_long appears to eagerly reorder argv, but the glibc(?) implementation appears to only do so when it is done processing the list. Thus if the loop is exited early because a tool was given, argv is left unmodified and optind simply points to the first parameter after the tool name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions