Skip to content

mcmay/blocksruntime

Repository files navigation

Blocks Runtime

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-fblockscompiler 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/testssubdirectory) and the originalCREDITS.TXT,LICENSE.TXTandREADME.txtfrom the top-level compiler-rtproject (which have been placed in theBlocksRuntime subdirectory). Note that in 2014-02 the compiler-rt project moved the BlocksRuntime sources from theBlocksRuntimedirectory to the lib/BlocksRuntimedirectory and moved the tests from theBlocksRuntime/tests directory to thetest/BlocksRuntimedirectory. 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.2compiler built using the MacPorts.org apple-gcc42 package on Mac OS X.

License

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.alibrary 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.TXTfile in theBlocksRuntime subdirectory for all the details.

Building

Since there are only two files to build, a makefile didn't seem warranted. A specialconfig.hfile has been created to make the build work. Build the libBlocksRuntime.alibrary by running:

./buildlib

Thegcccompiler will be used by default, but you can doCC=clang./buildlib for example to use theclangcompiler instead. Note that neithermakenor cmakeare needed (butarandranlibwill be used but they can also be changed with theARandRANLIBenvironment 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 aclangorgcc-apple-4.2built with MacPorts or otherwise obtained), you probably want a fat library with multiple architectures in it. You can do that with theCFLAGSvariable like so:

CFLAGS='-O2 -arch x86_64 -arch ppc64 -arch i386 -arch ppc'./buildlib

Thebuildlib-osxscript 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

Testing

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-fblocksoption 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-fblockssupport installed for this step (if you don't have a clang compiler with-fblockssupport available, why bother installing the Blocks runtime in the first place?)
Run the tests like so:

./checktests

By defaultchecktestsexpects theclangcompiler to be available in the PATHand namedclang.If you are usinggcc-apple-4.2or yourclangis named something different (such asclang-mp-2.9orclang-mp-3.0) run the tests like this instead (replacingclang-mp-3.0with 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# TODOare expected to fail for the reason shown. Thecopy-block-literal-rdar6439600.cexpected 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.candjosh.Ctests. These extra failures are not failures in the blocks runtime itself, justgccnot accepting some source files thatclangaccepts. 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 (.Cextension) 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./checktestsoutput is TAP (Test Anything Protocol) compliant and can therefore be run with Perl's prove utility like so:

prove -v checktests

Optionally first settingCClike so:

CC=gcc-apple-4.2 prove -v checktests

Omit the-voption for more succinct output.

ARM Hard Float Bug

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_listand then call the block it was passed as an argument passing the block theva_list.This works fine and avoids theclangbug even though it's fugly.

Thechecktestsscript 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.

clang -fblocks failure

If clang is not using the integrated assembler (option-integrated-as) then it will incorrectly pass options such as-fblocksdown 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-fblocksoption 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 -Sand-fblocksoptions to produce a.sfile which can then be compiled into whatever is desired without needing to use the-fblocksoption.

Ifchecktestsdetects 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.cinto.sand then compile.s into.o(or whatever) will be needed instead of the usual compile.cinto .o(or whatever).

Note that this workaround is required to use-fblockswith the version of clang included with cygwin.

Installing

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 installationprefixis/usr/local,but can be changed to /myprefixlike so:

sudo env prefix=/myprefix./installlib

The include file (Block.h) is installed into$prefix/includeand the library (libBlocksRuntime.a) into$prefix/libby default. (Those can also be changed by setting and exportingincludedirand/orlibdirin the same way prefixcan be changed.)

If you want to see what will be installed without actually installing use:

./installlib --dry-run

Note thatDESTDIRis supported by theinstalllibscript if that's needed. Just setDESTDIRbefore runninginstalllibthe same wayprefixcan be set.

Sample Code

After you have installed the Blocks runtime header and library, you can check to make sure everything's working by building thesample.cfile. The instructions are at the top of the file (usehead sample.cto see them) or just do this (replaceclangwith the name of the compiler you're using):

clang -o sample -fblocks sample.c -lBlocksRuntime &&./sample

If the above line outputsHello world 2then 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-Ioption to find theBlock.hheader when you compile your source(s). And a-Loption to find thelibBlocksRuntime.alibrary when you link your executable. Since libBlocksRuntime.ais a static library no special system support will be needed to run the resulting executable.

Glibc Problem

Theunistd.hheader from older versions ofglibchas an incompatibility with the-fblocksoption. 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.19released on 2014-02-07. Sincelddis part ofglibc you can check to see what version ofglibcyou have with:

ldd --version

Documentation

You can find information on the Blocks language extension at these URLs

About

Blocks runtime library (libBlocksRuntime.a)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 66.1%
  • Objective-C 22.3%
  • Shell 9.7%
  • Makefile 1.9%