graphitemaster / incbin

Include binary files in C/C++
The Unlicense
967 stars 90 forks source link

ASM template #7

Closed IngwiePhoenix closed 9 years ago

IngwiePhoenix commented 9 years ago

Since MSVC does not support the macro method, I thought about just typing together a little ASM file to pull in the binaries. Thing is, I don't know assembly...

Could you provide me a basic example of an ASM file with which i can include binary files into the same object, just as if i had used the INCBIN macro?

Kind regards, Ingwie

graphitemaster commented 9 years ago

You can use the incbin tool with MSVC to generate a C file containing the data included inline as a raw sequence of bytes. Then use the incbin.h header as you typically would, incbin.cpp just needs a list of files to process (searches for use of the incbin macro)

IngwiePhoenix commented 9 years ago

My original plan was to keep my app so simple, that one would only need to do this:

g++ src/*.cpp -o icetea

to compile it. The IceTea program itself is a build tool, so it shouldnt be a hard task to build a build tool. That is why I thought I could maybe get something like this for Windows:

cl src\*.cpp src\*.asm /Fe:icetea

… but what would the .asm files need to contain to properly export symbols for the included binary data?

I hope you get what I am trying to say ^^; . English is only my secondary language.

Kind regards, Ingwie.

PS: IceTea: https://github.com/IngwiePhoenix/IceTea https://github.com/IngwiePhoenix/IceTea

graphitemaster commented 9 years ago

Hate to burst your bubble but MSVC lacks an assembler for x86_64 and for x86 the assembler it does support does not support a way to include binary data.

graphitemaster commented 9 years ago

If you want you can do g++ incbin.cpp -o incbin && ./incbin src\*.cpp && g++ src\*.cpp incbin.c -o icetea and cl incbin.cpp /FE:incbin && ./incbin.exe src\*.cpp && cl src\*.cpp incbin.c /Fe:icetea

IngwiePhoenix commented 9 years ago

I see. While doing my research, I saw that NASM supports the incbin directive, but NASM is not a directly distributed alongside MSVC. So the only sort-of method, that would work, is to use db statements. But that obviously would be ver cumbersome.

Now, I just tried to use the incbin tool itself, but the output files are always...well, empty.

Ingwie@Ingwies-Macbook-Pro.local ~/W/incbin $ cp ../PhoenixEngine/.IceTea/*.it .
Ingwie@Ingwies-Macbook-Pro.local ~/W/incbin $ ./incbin ./*.it
Ingwie@Ingwies-Macbook-Pro.local ~/W/incbin $ cat incbin.c
/* File automatically generated by incbin */
#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

When using the -o flag, it also tells me that it can not open the specified file. And yes, there are files matching *.it - quite a lot of them too...

I am currently testing on Mac OS X 10.10 and Win 7 in a 32bit VM.

IngwiePhoenix commented 9 years ago

Ohhh nevermind! Now I understand why I got this. It was because I was selecting the target files instead of the source files that I wanted to have this applied to ... oops!

Thanks for answring my question :). I think I have this figured out now and can savely include incbin into my IceTea distribution now.

IngwiePhoenix commented 9 years ago

Turns out I cant compile the incbin tool in my Win7, VS 2010. But seing that it relies on <regex>, I have a doubt that you'd degrade it to use C++03 instead. Any way I could deal with this?

graphitemaster commented 9 years ago

I'll rewrite the tool. There are a few things I wanted to do to improve the performance of it anyways.

graphitemaster commented 9 years ago

The rewrite will likely be in C, so it should compile with the oldest VS imaginable

IngwiePhoenix commented 9 years ago

Awesome! I'm totally looking forward to this :)

graphitemaster commented 9 years ago

Done

IngwiePhoenix commented 9 years ago

Awesome! I went right away to check it out. Result:

Ingwie@Ingwies-Macbook-Pro.local ~/W/incbin $ gcc incbin.c -o incbin
incbin.c:71:43: warning: more '%' conversions than data arguments [-Wformat]
        fprintf(stderr, "failed to open `%s' for output\n");
                                         ~^
incbin.c:83:47: warning: more '%' conversions than data arguments [-Wformat]
            fprintf(stderr, "failed to open `%s' for reading\n");
                                             ~^
2 warnings generated.
IngwiePhoenix commented 9 years ago

Further:

Ingwie@Ingwies-Macbook-Pro.local ~/W/incbin $ ./incbin -o res.c it.cpp 
fish: Job 1, './incbin -o res.c it.cpp ' terminated by signal SIGSEGV (Address boundary error)
Ingwie@Ingwies-Macbook-Pro.local ~/W/incbin $ ./incbin it.cpp -o res.c
failed to open `' for reading
graphitemaster commented 9 years ago

What do you expect for ten minutes of work, try now.

IngwiePhoenix commented 9 years ago

haha, I know. :) I just thought I'd post the issues, since I can't besure the same ones are appearing on your end.

10 minutes? You are damn good o.o I wouldn't be able to do that stuff in such time.

IngwiePhoenix commented 9 years ago

I tested and it works now in the variant of -o res.c being the last part of the argument list - but that is no problem at all. Supplying that portion as the first bit of parameters causes:

Ingwie@Ingwies-Macbook-Pro.local ~/W/incbin $ ./incbin -o res.c it.cpp 
fish: Job 1, './incbin -o res.c it.cpp ' terminated by signal SIGSEGV (Address boundary error)

But as said, no biggie! Thanks for working on this, this is really one very awesome tool!

graphitemaster commented 9 years ago

Fixed

IngwiePhoenix commented 9 years ago

Marvelous. =)

Thanks for supplying the fixes! I am going to have a very fun day using incbin today for my icetea project!