graphitemaster / incbin

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

Does not work for iOS #2

Closed mika314 closed 9 years ago

graphitemaster commented 9 years ago

Interesting. Which compiler does iOS use again? Presumably the one that ships with XCode which should be clang. Can I have some information on which version of clang, this may be a incompatibility with clang. I need more information though. The output of the compiler would help as well.

mika314 commented 9 years ago

For folowing code snippet:

#include "incbin.hpp"

INCBIN(Test, "Icon.png");

Smething like:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch armv7 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=c++1y -Wno-trigraphs -fpascal-strings -Os -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -g -fvisibility=hidden -fvisibility-inlines-hidden -Wno-sign-conversion -miphoneos-version-min=8.1 -iquote /Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/peatree-generated-files.hmap -I/Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/peatree-own-target-headers.hmap -I/Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/peatree-all-target-headers.hmap -iquote /Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/peatree-project-headers.hmap -I/Users/antonte/prj/mobilegames/game2/build/Release-iphoneos/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/DerivedSources/armv7 -I/Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/DerivedSources -F/Users/antonte/prj/mobilegames/game2/build/Release-iphoneos -MMD -MT dependencies -MF /Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/Objects-normal/armv7/sound.d --serialize-diagnostics /Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/Objects-normal/armv7/sound.dia -c /Users/antonte/prj/mobilegames/game2/sound.cpp -o /Users/antonte/prj/mobilegames/game2/build/game2.build/Release-iphoneos/peatree.build/Objects-normal/armv7/sound.o
<inline asm>:1:17: error: unexpected token in '.section' directive
.section .rodata
                ^
<inline asm>:3:1: error: unknown directive
.type gTest, @object
^
<inline asm>:8:1: error: unknown directive
.type gTestSize, @object
^
3 errors generated.
mika314 commented 9 years ago
$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang --version
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
graphitemaster commented 9 years ago

I see. XCode's inline assembler is drastically different than GNU. More information on the directives can be found here https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html#//apple_ref/doc/uid/TP30000823-TPXREF101

I sense removing .type and .section directives and replacing them with .const_data would be sufficient.

Try the following

#define INCBIN(NAME, FILENAME) \
    __asm__(".const_data\n" \
            ".global g" #NAME "\n" \
            ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) " \n" \
            "g" #NAME ":\n" \
            "  .incbin \"" FILENAME "\"\n" \
            ".global g" #NAME "Size\n" \
            ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) " \n" \
            "g" #NAME "Size:\n" \
            "  .int g" #NAME "Size - g" #NAME "\n"); \
    INCBIN_EXTERN(NAME)

If that works I'll add the appropriate fix in incbin.

mika314 commented 9 years ago

It's compiling. Thanks. I'll test it more at evening (don't have phisical access to my mac).

mika314 commented 9 years ago

I was able to make it work with:

#define INCBIN(NAME, FILENAME) \
    __asm__(".const_data\n" \
            ".global _g" #NAME "\n" \
            ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) " \n" \
            "_g" #NAME ":\n" \
            "  .incbin \"" FILENAME "\"\n" \
            ".global _g" #NAME "Size\n" \
            ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) " \n" \
            "_g" #NAME "Size:\n" \
            "  .int _g" #NAME "Size - _g" #NAME "\n"); \
    INCBIN_EXTERN(NAME)

With _ before g. Something messed up with name mangling? I don't know.

mika314 commented 9 years ago

Full listing:

/**
 * @file incbin.h
 * @author Dale Weiler
 * @brief Utility for including binary files
 *
 * Facilities for including binary files into the current translation unit and
 * making use from them externally in other translation units.
 */
#ifndef INCBIN_HDR
#define INCBIN_HDR
#include <limits.h>

#if defined(__SSE__) || defined(__neon__)
# define INCBIN_ALIGNMENT 16
#else
# if ULONG_MAX == 0xffffffffu
#  define INCBIN_ALIGNMENT 4
# else
#  define INCBIN_ALIGNMENT 8
# endif
#endif

#define INCBIN_ALIGN __attribute__((aligned(INCBIN_ALIGNMENT)))

#ifdef __cplusplus
#  define INCBIN_EXTERNAL extern "C"
#else
#  define INCBIN_EXTERNAL extern
#endif

#define INCBIN_STR(X) #X
#define INCBIN_STRINGIZE(X) INCBIN_STR(X)

/**
 * @brief Externally reference binary data included in another translation unit.
 *
 * Produces two external symbols that reference the binary data included in
 * another translation unit.
 *
 * The symbol names are a concatenation of "g" before *NAME*; with "Data", as well
 * as "Size" after. An example is provided below.
 *
 * @param NAME The name given for the binary data
 *
 * @code
 * INCBIN_EXTERN(Foo);
 *
 * // Now you have the following symbols:
 * // extern unsigned char gFooData[];
 * // extern unsigned int gFooSize;
 * @endcode
 */
#define INCBIN_EXTERN(NAME) \
    INCBIN_EXTERNAL const INCBIN_ALIGN unsigned char g ## NAME ## Data[]; \
    INCBIN_EXTERNAL const unsigned int g ## NAME ## Size

/**
 * @brief Include a binary file into the current translation unit.
 *
 * Includes a binary file into the current translation unit, producing two symbols
 * for objects that encode the data and size respectively.
 *
 * The symbol names are a concatenation of "g" before *NAME*; with "Data", as well
 * as "Size" after. An example is provided below.
 *
 * @param NAME The name to associate with this binary data (as an identifier.)
 * @param FILENAME The file to include (as a string literal.)
 *
 * @code
 * INCBIN(Icon, "icon.png");
 *
 * // Now you have the following symbols:
 * // unsigned char gIconData[];
 * // unsigned int gIconSize;
 * @endcode
 *
 * @warning This must be used in global scope
 *
 * To externally reference the data included by this in another translation unit
 * please @see INCBIN_EXTERN.
 */
#ifndef __APPLE__
#define INCBIN(NAME, FILENAME) \
    __asm__(".section .rodata\n" \
            ".global g" #NAME "Data\n" \
            ".type g" #NAME "Data, @object\n" \
            ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n" \
            "g" #NAME "Data:\n" \
                ".incbin \"" FILENAME "\"\n" \
            ".global g" #NAME "Size\n" \
            ".type g" #NAME "Size, @object\n" \
            ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n" \
            "g" #NAME "Size:\n" \
                ".int g" #NAME "Size - g" #NAME "Data\n" \
    ); \
    INCBIN_EXTERN(NAME)
#else
#define INCBIN(NAME, FILENAME) \
    __asm__(".const_data\n" \
            ".globl _g" #NAME "Data\n"      \
            ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n" \
            "_g" #NAME "Data:\n" \
                ".incbin \"" FILENAME "\"\n" \
            ".globl _g" #NAME "Size\n"      \
            ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n" \
            "_g" #NAME "Size:\n" \
            ".long _g" #NAME "Size - _g" #NAME "Data\n" \
    ); \
    INCBIN_EXTERN(NAME)
#endif
#endif
graphitemaster commented 9 years ago

Try using the current branch. I pushed some changes, let me know if I still need the underscore for name mangling.

mika314 commented 9 years ago

I tried "Fix for XCode" it has problem with underscore.

graphitemaster commented 9 years ago

If you add the underscore does it work?

mika314 commented 9 years ago

Yes

On Saturday, February 7, 2015, Dale Weiler notifications@github.com wrote:

If you add the underscore does it work?

— Reply to this email directly or view it on GitHub https://github.com/graphitemaster/incbin/issues/2#issuecomment-73397604.

graphitemaster commented 9 years ago

Okay, going to commit a fix and mark this as closed