Sublime Forum

When to use an array for "cmd [[String]]" in custom sublime-build

#1

Hello,

I was trying to create a custom sublime-build for clang-omp on Mac OS X.

It seems that the custom build works if I use a single string for parameter cmd, such as:

{
    "cmd"       : ["clang-omp++ -fopenmp ${file_name} -o ${file_base_name} && ${file_path}/${file_base_name}"],
    "shell"     : true,
    "selector"  : "source.c++",
    "path"      : "/usr/local/bin/:$PATH",
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)"
}

but fails when I separate this string into an array of strings, such as:

{
    "cmd"       : ["clang-omp++", "-fopenmp", "${file_name}", "-o ${file_base_name}", "&&", "${file_path}/${file_base_name}"],
    "shell"     : true,
    "selector"  : "source.c++",
    "path"      : "/usr/local/bin/:$PATH",
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)"
}

I have used “control + `” to call-up console, and both methods looks the same in console:

Running clang-omp++ -fopenmp HelloThreads.cpp -o HelloThreads && /<path-to-src-folder>/HelloThreads Running clang-omp++ -fopenmp HelloThreads.cpp -o HelloThreads && /<path-to-src-folder>/HelloThreads

Are there any differences between using a single string and an array of strings for parameter cmd?

Thanks,

0 Likes

#2
  1. If you are not using ST3, use it. The following points assume that.
  2. cmd is for running a single program (read: executable) with parameters only. It is usually specified as an array and has the advantage of automatically wrapping and escaping arguments that contain spaces (or quotation marks).
  3. shell_cmd, which is the ST3-equivalent of cmd with shell: true, is instead supposed to be a string and will be run … on the shell, thus allowing constructs like your && or other intersting things, although that is the most common.

Thus, your build should look like this:

{
    "shell_cmd" : "clang-omp++ -fopenmp \"${file_name}\" -o \"${file_base_name}\" && \"${file_path}/${file_base_name}\"",
    "selector"  : "source.c++",
    "path"      : "/usr/local/bin/:$PATH",
    "file_regex": "^(.*?):([0-9]*):?([0-9]*)"
}
2 Likes

#3

Thanks enormously @FichteFoll!!

I was using ST3, but I wasn’t aware of the differences between shell_cmd and cmd plus shell: true. The error message I had got, clang-3.5: error: no input files is reflecting exactly what you mentioned in bullet point 2: cmd does not take input file.

Just two more questions regarding to this topic:

  1. Where could I find more details such as "cmd is for running a single program with parameters only"? I used to refer to Sublime Text Unofficial Documentation, but descriptions there for cmd with shell : true and shell_cmd almost sounds identical.

  2. I thought python ... [ -c command | script | - ] [ arguments ] is actually taking script as an input file name, just like input-filenames in clang ... input-filenames, right? If so, perhaps the following example in Sublime Text Unofficial Documentation (v:sublime-text-3) build-system basics might be a bit confusing?

    {
        "cmd": ["python", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python"
    }
    

Thanks again for your time!

0 Likes

#4

Previously, ST2 supported both a string and an array for cmd. The shell option was just a mirror of the underlying subprocess.Popen Python API that was used to run the external command (I think).

This was rather implicit and also created several situations where the exact behavior would be rather unpredictable, especially on UNIX systems, which is why ST3 introduced the new shell_cmd key that would always run on the shell and has to be a string, because that’s what the shell gets handed anyway.

The unofficial docs are a bit misleading on that, I agree. It’s on our todo list and I just added a reference to this thread so that we can use that as a souce later when we revisit the topic eventually.

0 Likes

#5

Thanks @FichteFoll !!

I feel that the [quote=“FichteFoll, post:4, topic:17981”]
todo list
[/quote]
could be a great self-helping material – despite of its briefness, it brings the frontier news – I will add it into my self-helping searching “$PATH” :laughing:

Thank you,

0 Likes