Author Topic: Compile current file on save  (Read 585 times)

asandler

  • Senior Community Member
  • Posts: 303
  • Hero Points: 27
Compile current file on save
« on: July 16, 2018, 01:31:28 am »
I saw "Build on Save", but it is quite crude. We have pretty large C project. Building implies compiling and linking large number of binaries and tests. I would like to compile current file (assuming this is a C file) on save.
I tried something like

Code: [Select]
_command save_and_compile() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
    save();
    activate_build();
    clear_pbuffer();
    _str cmd = "make some_file.o";
    concur_command(cmd, false, true, true, true);
}

It works, but it doesn't interface with Message List and I would really like it. Any ideas how to hook up Message List and point at errors in the code?

asandler

  • Senior Community Member
  • Posts: 303
  • Hero Points: 27
Re: Compile current file on save
« Reply #1 on: July 16, 2018, 12:32:06 pm »
I made a some progress. It looks like I have to call
Code: [Select]
set_error_markers(). The code does not work yet. It seems
Code: [Select]
concur_command() is asynchronous and by the time I run
Code: [Select]
set_error_markers() build buffer is still empty. Anyway, I'll appreciate if C-Slick experts can tell me what I am doing wrong.

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Compile current file on save
« Reply #2 on: July 16, 2018, 01:02:41 pm »
concur_command isn’t the best choice since it runs async and doesn’t support error markers functionality.
Have a look at e.g. https://community.slickedit.com/index.php/topic,988.msg4191.html#msg4191
I think the project_build command also wrapping ‘Compile’ is the way to go and let SE do the invocation and setting the error markers accordingly.
Maybe I’m wrong but isn’t there a SE setting doing ‘Compile’ on save ?
Otherwise you could also make use of the SE callback hook mechanism to hook on the ‘on save’ actions invoking ‘Compile’ perhaps with some filtering to avoid compiling every file you save.
HS2
« Last Edit: July 16, 2018, 01:07:53 pm by hs2 »

Dennis

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2747
  • Hero Points: 420
Re: Compile current file on save
« Reply #3 on: July 16, 2018, 02:18:10 pm »
The project_compile() command will save the current file if you have the "compile" tool configured to do so in your project properties.

asandler

  • Senior Community Member
  • Posts: 303
  • Hero Points: 27
Re: Compile current file on save
« Reply #4 on: July 16, 2018, 08:14:26 pm »
OK. I got it working. I created a new build tool that compiles one file (I could not use stock Compile tool, more on this later). Then I wrote a Slick-C macro that runs it.

I could not actually use Compile tool because for GNU C/C++ projects I cannot make it use my own command. Not sure why.
Another problem has been the fact that in a new build tool I could not express what I want to do in terms of escape sequences for build commands. To compile single file located at <ROOT>/src/foo/bar.c we have to run make out/src/foo/bar.o. It could be possible to do that if there was escape sequence that returns file path relative to project's root directory. So I wrote a small BASH script that constructs and wraps above make command.

Another caveat that I don't quite know how to solve yet is that my new Compile tool is defined for all file extensions. I don't want to run it on anything but C files. I solved this problem by ignoring all non-C files in the wrapper script. However, may be this is the right way to do it because I do want to save the file even if it is not a C file.