Skip to content

Conversation

413x1nkp
Copy link

@413x1nkp 413x1nkp commented Apr 3, 2025

The goal was to add compile_commands.json generator into nob.
Application: LSP

Windows testing is still required, works well on linux though

Example 1

// nob.c

#define NOB_IMPLEMENTATION
#include "nob.h"

int main(int argc, char** argv) {
    NOB_GO_REBUILD_URSELF(argc, argv);

    Nob_Cmd cmd = {0};
    nob_cmd_append(&cmd, "gcc", "-o", "main", "main.c");
    nob_add_to_compile_database(cmd);

    nob_cmd_run_sync(cmd);

    return 0;
}
// main.c

int main() {
    return 0;
}

produces:

[
{
	"directory": "/kirby/Coding/nobtest",
	"commands": "gcc -o main main.c",
	"file": "main.c"
}
]

Example 2

// nob.c

#define NOB_IMPLEMENTATION
#include "nob.h"

int main(int argc, char** argv) {
    NOB_GO_REBUILD_URSELF(argc, argv);

    Nob_Cmd cmd = {0};
    nob_cmd_append(&cmd, "gcc", "-o", "main", "main.c", "main1.c");
    nob_add_to_compile_database(cmd);

    nob_cmd_run_sync(cmd);

    return 0;
}
// main.c

int main() {
    return 0;
}
// main1.c

int test() {

    return 1;
}

int lulz() {

    return 2;
}

produces:

[
{
	"directory": "/kirby/Coding/nobtest",
	"commands": "gcc -o main main.c main1.c",
	"file": "main.c"
},
{
	"directory": "/kirby/Coding/nobtest",
	"commands": "gcc -o main main.c main1.c",
	"file": "main1.c"
}
]

Example 3

// nob.c

#define NOB_IMPLEMENTATION
#define NOB_COMPILE_COMMANDS_PATH "build/compile_commands.json"
#include "nob.h"

int main(int argc, char** argv) {
    NOB_GO_REBUILD_URSELF(argc, argv);

    Nob_Cmd cmd = {0};
    nob_cmd_append(&cmd, "gcc", "-DSOME_VALUE=\"SOMEVALUE\"", "-o", "main", "main.c", "main1.c");
    nob_cmd_run_sync(cmd);
    nob_add_to_compile_database(cmd);

    return 0;
}
// main.c

int main() {
    return 0;
}
// main1.c

int test() {

    return 1;
}

int lulz() {

    return 2;
}

produces build/compile_commands.json:

[
{
	"directory": "/kirby/Coding/nobtest",
	"command": "gcc -DSOME_VALUE=\"LOLO\" -o main main.c main1.c",
	"file": "main.c"
},
{
	"directory": "/kirby/Coding/nobtest",
	"command": "gcc -DSOME_VALUE=\"LOLO\" -o main main.c main1.c",
	"file": "main1.c"
}
]

@413x1nkp 413x1nkp changed the title compile_commands.json generator feat: on-demand compile_commands.json generation Apr 4, 2025
@413x1nkp 413x1nkp marked this pull request as ready for review April 4, 2025 07:11
@413x1nkp
Copy link
Author

Clang is stingy about including POSIX extensions implicitly, so -std=c99 to it means "NO POSIX ALLOWED"
GCC is more lenient about this on the other hand, it'll use POSIX if available and if required.
Functions ftruncate and readlink are therefore invisible, but only to Clang.
Solution is to either change workflow file to have -std=gnu99 instead of -std=c99, to define test macros required, to not rely on either (pretty hard), or to declare ftruncate and readlink explicitly.

I couldn't figure out what the best option is, so i settled for declaring both explicitly. Let me know if this causes issues!

@rhmoller
Copy link

rhmoller commented May 9, 2025

Alternatively, you can use Bear together with nob for generating compile_commands.json

@finnkauski
Copy link

+1 for Bear. No issues here with LSP and using what it generates.

bear -- ./nob

lol

@413x1nkp
Copy link
Author

Alternatively, you can use Bear together with nob for generating compile_commands.json

Yeah, bear works, but having explicit control on what ends up in compile_commands.json sounds like a good idea.
My approach is self-contained and doesn't rely on other tools, which might be a good or a bad thing depending on how you look at it. In my opinion, less dependencies to worry about - the better.

@rexim
Copy link
Member

rexim commented Aug 11, 2025

This feature looks interesting, but I feel like it is too much outside of the scope of nob.h is trying to be, sorry.

@rexim rexim closed this Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants