buildlib: supply -fPIC or -fpic if supported
[blocksruntime.git] / README.txt
blob7087c046950199397b9b5d22c38ccad57e51ed4d
2 Blocks Runtime
3 ==============
5 This project provides a convenient way to install the BlocksRuntime library
6 from the compiler-rt project (see <http://compiler-rt.llvm.org/>).
8 Several systems (Linux, FreeBSD, MacPorts, etc.) provide the clang compiler
9 either preinstalled or as an available package which has Blocks support
10 (provided the `-fblocks` compiler option is used).
12 Unfortunately, those installer packages do not provide the Blocks runtime
13 library.
15 On the other hand, the compiler-rt library can be downloaded and does contain
16 the Blocks runtime support if properly enabled in the cmake build file.
18 By default, however, the compiler-rt library also builds a compiler runtime
19 support library which is undesirable.
21 This project contains only the BlocksRuntime files (in the `BlocksRuntime`
22 subdirectory) along with tests (in the `BlocksRuntime/tests` subdirectory) and
23 the original `CREDITS.TXT`, `LICENSE.TXT` and `README.txt` from the top-level
24 `compiler-rt` project (which have been placed in the `BlocksRuntime`
25 subdirectory).  Note that in 2014-02 the compiler-rt project moved the
26 BlocksRuntime sources from the `BlocksRuntime` directory to the
27 `lib/BlocksRuntime` directory and moved the tests from the `BlocksRuntime/tests`
28 directory to the `test/BlocksRuntime` directory.  The files themselves, however,
29 remain unchanged and are still the same as they were in 2010-08.
31 This runtime can also be used with the `gcc-apple-4.2` compiler built using the
32 MacPorts.org apple-gcc42 package on Mac OS X.
36 License
37 -------
39 The compiler-rt project (and hence the BlocksRuntime since it's a part of that
40 project) has a very liberal dual-license of either the UIUC or MIT license.
41 The MIT license is fully GPL compatible (and pretty much compatible with just
42 about everything), so there should be no problems linking the
43 `libBlocksRuntime.a` library with your executable.  (Note that on the FSF's site
44 <http://www.gnu.org/licenses/license-list.html>, you find the MIT license under
45 the 'X11 License' section.)  See the `LICENSE.TXT` file in the `BlocksRuntime`
46 subdirectory for all the details.
50 Building
51 --------
53 Since there are only two files to build, a makefile didn't seem warranted.  A
54 special `config.h` file has been created to make the build work.  Build the
55 `libBlocksRuntime.a` library by running:
57         ./buildlib
59 The `gcc` compiler will be used by default, but you can do `CC=clang ./buildlib`
60 for example to use the `clang` compiler instead.  Note that neither `make` nor
61 `cmake` are needed (but `ar` and `ranlib` will be used but they can also be
62 changed with the `AR` and `RANLIB` environment variables similarly to the way
63 the compiler can be changed).
65 **IMPORTANT** Mac OS X Note:  If you are building this library on Mac OS X
66 (presumably to use with a `clang` or `gcc-apple-4.2` built with MacPorts or
67 otherwise obtained), you probably want a fat library with multiple architectures
68 in it.  You can do that with the `CFLAGS` variable like so:
70         CFLAGS='-O2 -arch x86_64 -arch ppc64 -arch i386 -arch ppc' ./buildlib
72 The `buildlib-osx` script will attempt to make an intelligent guess about
73 building an OS X library and then run `buildlib`.  If you're using Mac OS X you
74 can do this to build a FAT OS X library:
76         ./buildlib-osx
78 The `buildlib` (and `buildlib-osx`) script takes a single optional `-shared`
79 argument.  If given it will attempt to also build a shared library instead
80 of just a static one in case you have a policy situation where use of a static
81 library has been forbidden.
85 Testing
86 -------
88 Skip this step at your peril!  It's really quite painless.  You see it's
89 possible that the underlying blocks ABI between the blocks runtime files
90 provided in this project and the blocks ABI used by your version of the clang
91 compiler to implement the `-fblocks` option have diverged in an incompatible
92 way.  If this has happened, at least some of the tests should fail in
93 spectacular ways (i.e. bus faults).  For that reason skipping this step is not
94 recommended.
96 You must have the clang compiler with `-fblocks` support installed for this step
97 (if you don't have a clang compiler with `-fblocks` support available, why
98 bother installing the Blocks runtime in the first place?)  
99 Run the tests like so:
101         ./checktests
103 By default `checktests` expects the `clang` compiler to be available in the
104 `PATH` and named `clang`.  If you are using `gcc-apple-4.2` or your `clang` is
105 named something different (such as `clang-mp-2.9` or `clang-mp-3.0`) run the
106 tests like this instead (replacing `clang-mp-3.0` with your compiler's name):
108         CC=clang-mp-3.0 ./checktests
110 Problems are indicated with a line that starts `not ok`.  You will see a few
111 of these.  The ones that are marked with `# TODO` are expected to fail for the
112 reason shown.  The `copy-block-literal-rdar6439600.c` expected failure is a real
113 failure.  No it's not a bug in the Blocks runtime library, it's actually a bug
114 in the compiler.  You may want to examine the `copy-block-literal-rdar6439600.c`
115 source file to make sure you fully grok the failure so you can avoid getting
116 burned by it in your code.  There may be a fix in the clang project by now (but
117 as of the clang 3.2 release it still seems to fail), however it may be a while
118 until it rolls downhill to your clang package.
120 If you are using `CC=gcc-apple-4.2`, you will probably get two additional expect
121 failure compiler bugs in the `cast.c` and `josh.C` tests.  These extra failures
122 are not failures in the blocks runtime itself, just `gcc` not accepting some
123 source files that `clang` accepts.  You can still use the `libBlocksRuntime.a`
124 library just fine.
126 Note that if you have an earlier version of `clang` (anything before version 2.8
127 see `clang -v`) then `clang++` (C++ support) is either incomplete or missing and
128 the few C++ tests (`.C` extension) will be automatically skipped (if `clang++`
129 is missing) or possibly fail in odd ways (if `clang++` is present but older than
130 version 2.8).
132 Note that the `./checktests` output is TAP (Test Anything Protocol) compliant
133 and can therefore be run with Perl's prove utility like so:
135         prove -v checktests
137 Optionally first setting `CC` like so:
139         CC=gcc-apple-4.2 prove -v checktests
141 Omit the `-v` option for more succinct output.
145 ARM Hard Float Bug
146 ------------------
148 When running on a system that uses the ARM hard float ABI (e.g. RaspberryPi),
149 the clang compiler has a bug.  When passing float arguments to a vararg function
150 they must also be passed on the stack, not just in hardware floating point
151 registers.  The clang compiler does this correctly for normal vararg functions,
152 but fails to do this for block vararg functions.
154 If you really need this, a workaround is to call a normal vararg function that
155 takes a block and `...` arguments.  It can then package up the `...` arguments
156 into a `va_list` and then call the block it was passed as an argument passing
157 the block the `va_list`.  This works fine and avoids the `clang` bug even
158 though it's fugly.
160 The `checktests` script marks this test (`variadic.c`) as expect fail when
161 running the tests on an ARM hard float ABI system if it's able to detect that
162 the ARM hard float ABI is in use.
166 clang -fblocks failure
167 ----------------------
169 If clang is not using the integrated assembler (option `-integrated-as`) then it
170 will incorrectly pass options such as `-fblocks` down to the assembler which
171 will probably not like it.  One example of an error caused by this bug is:
173         gcc: error: unrecognized command line option '-fblocks'
175 In this case clang is not using the integrated assembler (which is not supported
176 on all platforms) and passes the `-fblocks` option down to the gcc assembler
177 which does not like that option at all.
179 The following references talk about this:
181 - <http://thread.gmane.org/gmane.comp.compilers.llvm.devel/56563>
182 - <http://llvm.org/bugs/show_bug.cgi?id=12920>
184 The ugly workaround for this problem is to compile the sources using both the
185 `-S` and `-fblocks` options to produce a `.s` file which can then be compiled
186 into whatever is desired without needing to use the `-fblocks` option.
188 If `checktests` detects this situation it will emit a line similar to this:
190         WARNING: -S required for -fblocks with clang
192 If this is the case, then rules to compile `.c` into `.s` and then compile `.s`
193 into `.o` (or whatever) will be needed instead of the usual compile `.c` into
194 `.o` (or whatever).
196 Note that this workaround is required to use `-fblocks` with the version of
197 clang included with cygwin.
201 Installing
202 ----------
204 Assuming that you have built the library (with `./buildlib`) and are satisfied
205 it works (`./checktests`) then it can be installed with:
207         sudo ./installlib
209 The default installation `prefix` is `/usr/local`, but can be changed to
210 `/myprefix` like so:
212         sudo env prefix=/myprefix ./installlib
214 The include file (`Block.h`) is installed into `$prefix/include` and the library
215 (`libBlocksRuntime.a`) into `$prefix/lib` by default.  (Those can also be
216 changed by setting and exporting `includedir` and/or `libdir` in the same way
217 `prefix` can be changed.)
219 If you want to see what will be installed without actually installing use:
221         ./installlib --dry-run
223 Note that `DESTDIR` is supported by the `installlib` script if that's needed.
224 Just set `DESTDIR` before running `installlib` the same way `prefix` can be set.
226 Note that if the shared library exists it will also be installed.  Add a single
227 optional `-shared` or `-static` option to install only one or the other.
231 Sample Code
232 -----------
234 After you have installed the Blocks runtime header and library, you can check
235 to make sure everything's working by building the `sample.c` file.  The
236 instructions are at the top of the file (use `head sample.c` to see them) or
237 just do this (replace `clang` with the name of the compiler you're using):
239         clang -o sample -fblocks sample.c -lBlocksRuntime && ./sample
241 If the above line outputs `Hello world 2` then your Blocks runtime support is
242 correctly installed and fully usable.  Have fun!
244 Note that if you have the problem described above in the section named
245 "clang -fblocks failure", then you'll need to do this instead:
247         clang -S -o sample.s -fblocks sample.c && \
248         clang -o sample sample.s -lBlocksRuntime && ./sample
250 Note that it's possible to use the Blocks runtime without installing it into
251 the system directories.  You simply need to add an appropriate `-I` option to
252 find the `Block.h` header when you compile your source(s).  And a `-L` option to
253 find the `libBlocksRuntime.a` library when you link your executable.  Since
254 `libBlocksRuntime.a` is a static library no special system support will be
255 needed to run the resulting executable.
259 Glibc Problem
260 -------------
262 The `unistd.h` header from older versions of `glibc` has an incompatibility with
263 the `-fblocks` option.  See <http://mackyle.github.io/blocksruntime/#glibc> for
264 a workaround.
266 This problem was corrected with commit 84ae135d3282dc362bed0a5c9a575319ef336884
267 (<http://repo.or.cz/w/glibc.git/commit/84ae135d>) on 2013-11-21 and first
268 appears in `glibc-2.19` released on 2014-02-07.  Since `ldd` is part of `glibc`
269 you can check to see what version of `glibc` you have with:
271         ldd --version
275 Documentation
276 -------------
278 You can find information on the Blocks language extension at these URLs
280 - <http://clang.llvm.org/docs/LanguageExtensions.html#blocks>
281 - <http://clang.llvm.org/docs/BlockLanguageSpec.html>
282 - <http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Blocks>
283 - <http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Blocks/Blocks.pdf>