This project provides a convenient way to install the BlocksRuntime library from the compiler-rt project (seehttp://compiler-rt.llvm.org/).
Several systems (Linux, FreeBSD, MacPorts, etc.) provide the clang compiler
either preinstalled or as an available package which has Blocks support
(provided the-fblocks
compiler option is used).
Unfortunately, those installer packages do not provide the Blocks runtime library.
On the other hand, the compiler-rt library can be downloaded and does contain the Blocks runtime support if properly enabled in the cmake build file.
By default, however, the compiler-rt library also builds a compiler runtime support library which is undesirable.
This project contains only the BlocksRuntime files (in theBlocksRuntime
subdirectory) along with tests (in theBlocksRuntime/tests
subdirectory) and
the originalCREDITS.TXT
,LICENSE.TXT
andREADME.txt
from the top-level
compiler-rt
project (which have been placed in theBlocksRuntime
subdirectory). Note that in 2014-02 the compiler-rt project moved the
BlocksRuntime sources from theBlocksRuntime
directory to the
lib/BlocksRuntime
directory and moved the tests from theBlocksRuntime/tests
directory to thetest/BlocksRuntime
directory. The files themselves, however,
remain unchanged and are still the same as they were in 2010-08.
This runtime can also be used with thegcc-apple-4.2
compiler built using the
MacPorts.org apple-gcc42 package on Mac OS X.
The compiler-rt project (and hence the BlocksRuntime since it's a part of that
project) has a very liberal dual-license of either the UIUC or MIT license.
The MIT license is fully GPL compatible (and pretty much compatible with just
about everything), so there should be no problems linking the
libBlocksRuntime.a
library with your executable. (Note that on the FSF's site
http:// gnu.org/licenses/license-list.html,you find the MIT license under
the 'X11 License' section.) See theLICENSE.TXT
file in theBlocksRuntime
subdirectory for all the details.
Since there are only two files to build, a makefile didn't seem warranted. A
specialconfig.h
file has been created to make the build work. Build the
libBlocksRuntime.a
library by running:
./buildlib
Thegcc
compiler will be used by default, but you can doCC=clang./buildlib
for example to use theclang
compiler instead. Note that neithermake
nor
cmake
are needed (butar
andranlib
will be used but they can also be
changed with theAR
andRANLIB
environment variables similarly to the way
the compiler can be changed).
IMPORTANTMac OS X Note: If you are building this library on Mac OS X
(presumably to use with aclang
orgcc-apple-4.2
built with MacPorts or
otherwise obtained), you probably want a fat library with multiple architectures
in it. You can do that with theCFLAGS
variable like so:
CFLAGS='-O2 -arch x86_64 -arch ppc64 -arch i386 -arch ppc'./buildlib
Thebuildlib-osx
script will attempt to make an intelligent guess about
building an OS X library and then runbuildlib
.If you're using Mac OS X you
can do this to build a FAT OS X library:
./buildlib-osx
Skip this step at your peril! It's really quite painless. You see it's
possible that the underlying blocks ABI between the blocks runtime files
provided in this project and the blocks ABI used by your version of the clang
compiler to implement the-fblocks
option have diverged in an incompatible
way. If this has happened, at least some of the tests should fail in
spectacular ways (i.e. bus faults). For that reason skipping this step is not
recommended.
You must have the clang compiler with-fblocks
support installed for this step
(if you don't have a clang compiler with-fblocks
support available, why
bother installing the Blocks runtime in the first place?)
Run the tests like so:
./checktests
By defaultchecktests
expects theclang
compiler to be available in the
PATH
and namedclang
.If you are usinggcc-apple-4.2
or yourclang
is
named something different (such asclang-mp-2.9
orclang-mp-3.0
) run the
tests like this instead (replacingclang-mp-3.0
with your compiler's name):
CC=clang-mp-3.0./checktests
Problems are indicated with a line that startsnot ok
.You will see a few
of these. The ones that are marked with# TODO
are expected to fail for the
reason shown. Thecopy-block-literal-rdar6439600.c
expected failure is a real
failure. No it's not a bug in the Blocks runtime library, it's actually a bug
in the compiler. You may want to examine thecopy-block-literal-rdar6439600.c
source file to make sure you fully grok the failure so you can avoid getting
burned by it in your code. There may be a fix in the clang project by now (but
as of the clang 3.2 release it still seems to fail), however it may be a while
until it rolls downhill to your clang package.
If you are usingCC=gcc-apple-4.2
,you will probably get two additional expect
failure compiler bugs in thecast.c
andjosh.C
tests. These extra failures
are not failures in the blocks runtime itself, justgcc
not accepting some
source files thatclang
accepts. You can still use thelibBlocksRuntime.a
library just fine.
Note that if you have an earlier version ofclang
(anything before version 2.8
seeclang -v
) thenclang++
(C++ support) is either incomplete or missing and
the few C++ tests (.C
extension) will be automatically skipped (ifclang++
is missing) or possibly fail in odd ways (ifclang++
is present but older than
version 2.8).
Note that the./checktests
output is TAP (Test Anything Protocol) compliant
and can therefore be run with Perl's prove utility like so:
prove -v checktests
Optionally first settingCC
like so:
CC=gcc-apple-4.2 prove -v checktests
Omit the-v
option for more succinct output.
When running on a system that uses the ARM hard float ABI (e.g. RaspberryPi), the clang compiler has a bug. When passing float arguments to a vararg function they must also be passed on the stack, not just in hardware floating point registers. The clang compiler does this correctly for normal vararg functions, but fails to do this for block vararg functions.
If you really need this, a workaround is to call a normal vararg function that
takes a block and...
arguments. It can then package up the...
arguments
into ava_list
and then call the block it was passed as an argument passing
the block theva_list
.This works fine and avoids theclang
bug even
though it's fugly.
Thechecktests
script marks this test (variadic.c
) as expect fail when
running the tests on an ARM hard float ABI system if it's able to detect that
the ARM hard float ABI is in use.
If clang is not using the integrated assembler (option-integrated-as
) then it
will incorrectly pass options such as-fblocks
down to the assembler which
will probably not like it. One example of an error caused by this bug is:
gcc: error: unrecognized command line option '-fblocks'
In this case clang is not using the integrated assembler (which is not supported
on all platforms) and passes the-fblocks
option down to the gcc assembler
which does not like that option at all.
The following references talk about this:
The ugly workaround for this problem is to compile the sources using both the
-S
and-fblocks
options to produce a.s
file which can then be compiled
into whatever is desired without needing to use the-fblocks
option.
Ifchecktests
detects this situation it will emit a line similar to this:
WARNING: -S required for -fblocks with clang
If this is the case, then rules to compile.c
into.s
and then compile.s
into.o
(or whatever) will be needed instead of the usual compile.c
into
.o
(or whatever).
Note that this workaround is required to use-fblocks
with the version of
clang included with cygwin.
Assuming that you have built the library (with./buildlib
) and are satisfied
it works (./checktests
) then it can be installed with:
sudo./installlib
The default installationprefix
is/usr/local
,but can be changed to
/myprefix
like so:
sudo env prefix=/myprefix./installlib
The include file (Block.h
) is installed into$prefix/include
and the library
(libBlocksRuntime.a
) into$prefix/lib
by default. (Those can also be
changed by setting and exportingincludedir
and/orlibdir
in the same way
prefix
can be changed.)
If you want to see what will be installed without actually installing use:
./installlib --dry-run
Note thatDESTDIR
is supported by theinstalllib
script if that's needed.
Just setDESTDIR
before runninginstalllib
the same wayprefix
can be set.
After you have installed the Blocks runtime header and library, you can check
to make sure everything's working by building thesample.c
file. The
instructions are at the top of the file (usehead sample.c
to see them) or
just do this (replaceclang
with the name of the compiler you're using):
clang -o sample -fblocks sample.c -lBlocksRuntime &&./sample
If the above line outputsHello world 2
then your Blocks runtime support is
correctly installed and fully usable. Have fun!
Note that if you have the problem described above in the section named "clang -fblocks failure", then you'll need to do this instead:
clang -S -o sample.s -fblocks sample.c && \
clang -o sample sample.s -lBlocksRuntime &&./sample
Note that it's possible to use the Blocks runtime without installing it into
the system directories. You simply need to add an appropriate-I
option to
find theBlock.h
header when you compile your source(s). And a-L
option to
find thelibBlocksRuntime.a
library when you link your executable. Since
libBlocksRuntime.a
is a static library no special system support will be
needed to run the resulting executable.
Theunistd.h
header from older versions ofglibc
has an incompatibility with
the-fblocks
option. Seehttp://mackyle.github.io/blocksruntime/#glibcfor
a workaround.
This problem was corrected with commit 84ae135d3282dc362bed0a5c9a575319ef336884
(http://repo.or.cz/w/glibc.git/commit/84ae135d) on 2013-11-21 and first
appears inglibc-2.19
released on 2014-02-07. Sinceldd
is part ofglibc
you can check to see what version ofglibc
you have with:
ldd --version
You can find information on the Blocks language extension at these URLs