Sublime Forum

ST3 - Automatic makefile parsing and targets

#1

Hello,

I am a reasonably long time user of sublime text and have been very very happy with it. Its fast, lightweight and does not distract you from the work at hand. And more often than not, a quick google search will return any plugins you might need.

I have run into a requirement that might be unique to my situation or might be helpful for others like me.
I am working on a C/C++ project that is based on makefiles.

The following is a sample directory structure.

test
├── LICENSE
├── README.md
├── libopencm3
│   ├── COPYING.GPL3
│   ├── COPYING.LGPL3
│   ├── HACKING
│   ├── HACKING_COMMON_DOC
│   ├── Makefile
│   ├── README.md
│   ├── doc
│   ├── include
│   ├── ld
│   ├── lib
│   ├── locm3.sublime-project
│   ├── mk
│   ├── scripts
│   └── tests
├── my-common-code
│   ├── api.c
│   └── api.h
├── rules.mk
├── test1
│   ├── Makefile
│   ├── awesomesauce.elf
│   ├── bin
│   └── my-project.c
├── test2
│   ├── Makefile
│   ├── awesomesauce.elf
│   └── my-project.c
├── untitled.sublime-project
└── untitled.sublime-workspace

The folders test1 and test2, say, contain the application code. They have a makefile in that directory. The local makefiles include a rules.mk file present in the root directory level. The make targets vary between test1 and test2.
Is there a plugin or any way where sublime can parse the makefiles (the local and root directory level) and present the targets to run.

I found one plugin (makecommands)
https://packagecontrol.io/packages/MakeCommands
but I was not able to make it work entirely, I can see the local makefile targets but never the root directory targets. Since the local makefile targets depended on the root level makefile, it did not work.

I would be really thankful if the more experienced folks can guide me how to solve this. Either help me solve how to make the plugin makecommands work or is there an easier way to get ST3 to show the targets based on the makefile.
I am not a hot shot, experienced developer and my experience is limited to C/C++ mostly and that too on the embedded side. I am willing to put in the time to develop something if anyone can guide me how to do so.

Thank you in advance!! :slight_smile:

0 Likes

#2

You can try to add the following line to your Packages/User/Preferences-sublime settings.

"make_commands_makefile_pattern": "/(Makefile|rules\\.mk)$",

This line should cause all Makefile and rules.mk files to be parsed.

Note: If you install PackageDev package you’ll see the available settings via auto-completion while editing your Preferences.

0 Likes

#3

@deathaxe
Thank you!! :slight_smile:
That worked like a charm… I did try the same setting earlier but had the wrong regex.

Instead of
"make_commands_makefile_pattern": "/(Makefile|rules\\.mk)$",

I had this

"make_commands_makefile_pattern": "/Makefile$|\.mk$",
And it threw up all kinds of errors. Clearly I do not understand this well enough.

The plugin shows the correct targets now but it fails with this error. I suspect this might be more of a path error at my end. The linker script exists in the respective folders but is not picked up by the make target…

/Users/srikrishnachaitanyanarumanchi/Workspace/libopencm3/test/rules.mk:119: *** Unable to find specified linker script: .  Stop.
[Finished in 0.0s with exit code 2]
[cmd: ['make', '-f', '/Users/srikrishnachaitanyanarumanchi/Workspace/libopencm3/test/rules.mk', 'all', 'SUBLIME_FILENAME=/Users/srikrishnachaitanyanarumanchi/Workspace/libopencm3/test/test2/my-project.c']]
[dir: /Users/srikrishnachaitanyanarumanchi/Workspace/libopencm3/test]
[path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/opt/X11/bin]  

Again, thank you very much for the help!! :slight_smile:

0 Likes

#4

The package runs the rules.mk as ordinary makefile and thus might miss some definitions from the latter one?

If so this would require changes to the plugin.

0 Likes

#5

It seems that opening sublime from the top level directory “test” causes the make targets to fail.
There the plugin can see the makefile targets on the top level and the directory level.

But since make is effectively running on the top level directory, it fails to see the generated linker script in the sub directories.

Then opening sublime in a particular directory shows the targets only at that level and not its root.
But it is able to run the targets specified in that level Makefile just fine - including targets which are defined in the root or top level.

@deathaxe just out of curiosity, are you the developer of the plugin?
You seem to be pretty knowledgeable about the plugin and sublime.

Thank you for the help. :slight_smile:

0 Likes

#6

Nope , I am not the developer. Just downloaded the package and had a birds eye on the code.

0 Likes