xuhdev / SingleCompile

A Vim plugin making it more convenient to compile or run a single source file.
https://singlecompile.topbug.net
GNU General Public License v3.0
123 stars 20 forks source link

"modeline" style SCCompileAF. #12

Closed johntyree closed 10 years ago

johntyree commented 12 years ago

It would be really helpful if there were a way to specify file-specific compiler flags. We already kind of do this with the auto detection for -lm, but it's a common case that I need to link against some random library, -lgsl -lgslcblas or -lcusparse etc and it's not worth making an entirely new compiler setting with special parameters for this one file. It also gets a bit annoying to remap keys :map <F12> <ESC>:SCCompileRunAF -lwhatever <CR> every time I open vim for a quick test of something.

What if we use a magic comment or something like vim's modelines to acheive this?

/*  SCCompileAF: -lwhatever */

Then we can just grep the file and the settings are carried between sessions automagically?

xuhdev commented 12 years ago

I'll think about how to deal with this. Thank you for your reminding.

xuhdev commented 12 years ago

This method is a bit limited, since only some compilers can have -lxxx parameter. Maybe extend the math.h feature, or think another way?

johntyree commented 12 years ago

The comment could contain anything that would normally be passed to SCCompileAF, not just a linker flag. Any time you'd like to use AF for a particular file, you use this feature.

johntyree commented 12 years ago

/* SCCompileAF: --make */ would be a useful example with GHC (Haskell) for example.

Or /* SCCompileAF: -std=c++11 */ with GCC.

xuhdev commented 12 years ago

I mean, you may have multiple compilers working with this file. If you are using Microsoft Visual C++, -std=c++11 does not take any effect. Either does -lxxx. So in fact this mode line does not take the variation of compilers into account.

johntyree commented 12 years ago

But the same argument applies to the :SCCompileAF command in the first place. The idea is not to make a portable way for everyone in the world to compile this file with the push of a button. It's just for you, as a developer, to not to have to remap your vim keys every time you want to work on this file.

For example, for the last week I've been working on a project that uses the GNU Scientific Library. When I want to test something quickly I open my "scratch" program with some #includes and boiler plate in it already for the GSL, then I write a quick C function and hit <F12> for :SCCompileRun. Then it inevitably fails because I forgot to add linker flags again. So I have to :map <F12> <ESC>:SCCompileRunAF -lgsl -lgslcblas<CR><CR><CR> again. Then maybe I have to do it again for just :SCCompileAF if I'm not interested in the output, just the compiler errors. Then of course I realize that I forgot to add buflocal so now my python scripts won't run properly, etc.

No one else uses these little files and you really don't care if they aren't portable to MSVCC or TurboC or whatever. It's just a way of quickly maintaining state in an obvious and modifiable way. If this file were to be included in production for whatever reason, it would be part of the build system and SingleCompile would be mostly unnecessary.

johntyree commented 12 years ago

Additional but related question, Is it possible to see what the current SCCompile command actually is? You can see the selected compiler quite easily, but I don't see a way to find out the default flags for that compiler without digging through the SingleCompile source code yourself.

xuhdev commented 12 years ago

There is a workaround:

autocmd Filetype c nmap <F9> :SCCompileAF yourflags<cr>

Do you think this is enough? If you think this is enough, I'll write a wiki. If you still think, a modeline is better, I'll try to implement it soon.

When you run :SCCompile, the command will show.

johntyree commented 12 years ago

I think that's a poor solution because it doesn't allow for different flags in different files, ever, which was the whole idea. Some files need extra options to build which are specific to the file and can change easily. For example if I'm playing around with ideas and I think "I should time these and compare...". Now I need to link against the realtime libs with -lrt, at least until I'm satisfied with this mini benchmark. I don't want every C file to do that though, just this one. You see what I mean? It's not really about the language, it's about the context around the file and it should be easily changeable between builds/runs.

xuhdev commented 12 years ago

OK. Please hold on. I'll try to implement this soon. Currently there is another issue earlier than this one, so please be patient.

johntyree commented 12 years ago

No need to rush. This isn't critical by any means. If you're busy I can try as well, but it will be ugly :) VimL is pretty much the worst language I've ever seen in a large project.

Just a note, what I'm imagining in bizarro-world pseudo-code is that it would be something like

l:extraFlags = `sed -n 's/SCCompile:(.*)/\1/p <THISFILE>`
:call SingleCompileAF l:extraFlags

Granted this is a really basic implementation and doesn't do any testing or deal with things like /* foo */ comments that need to be trimmed, or have the user do /* SCCompileAF: feaijfoeij */ instead.

xuhdev commented 12 years ago

No need to rush. This isn't critical by any means. If you're busy I can try as well, but it will be ugly :) VimL is pretty much the worst language I've ever seen in a large project.

I agree.

At least, for the Vim modeline, a colon must be appended in this case, or you still need to write

/* vim: blabla
*/

To make it simpler, we can support the two-line style, since it's no big deal at all.

johntyree commented 12 years ago

How's this going. Is it still viable?

xuhdev commented 12 years ago

Yeah it's viable, but it's a matter of time.

zhou13 commented 11 years ago

Have you started this feature? Maybe I also can finish it.

xuhdev commented 11 years ago

@zhou13 Thank you. I am quite busy recently, if you would like to, feel free to do so. Nice to hear this!

xuhdev commented 10 years ago

Hi @johntyree and @zhou13: Although I didn't solve this issue internally, I've solved it from outside. See the new wiki page. Please close this issue if you find this works, or make a comment if something doesn't work. I'm now personally using this method and it works pretty cool.

johntyree commented 10 years ago

:+1:

Looks good to me. Thanks :)

2013/10/27 Hong Xu notifications@github.com

Hi @johntyree https://github.com/johntyree and @zhou13https://github.com/zhou13: Although I didn't solve this issue internally, I've solved it from outside. See the new wiki pagehttps://github.com/xuhdev/SingleCompile/wiki/Specify-Compilation-Flags-for-Each-Individual-Source-File. Please close this issue if you find this works, or make a comment if something doesn't work. I'm now personally using this method and it works pretty cool.

— Reply to this email directly or view it on GitHubhttps://github.com/xuhdev/SingleCompile/issues/12#issuecomment-27178189 .