Skip to content

SR-2309: embed BlocksRuntime in libdispatch to eliminate external dep… #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions dispatch/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Dispatch {
requires blocks
export *
link "dispatch"
link "BlocksRuntime"
}

module DispatchIntrospection [system] [extern_c] {
Expand All @@ -16,5 +15,4 @@ module CDispatch [system] [extern_c] {
export *
requires blocks
link "dispatch"
link "BlocksRuntime"
}
104 changes: 62 additions & 42 deletions m4/blocks.m4
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ AC_ARG_WITH([blocks-runtime],
LIBS="$LIBS -L$blocks_runtime"]
)

#
# Configure argument to enable/disable using an embedded blocks runtime
#
AC_ARG_ENABLE([embedded_blocks_runtime],
[AS_HELP_STRING([--enable_embedded_blocks_runtime],
[Enable usage of blocks runtime embedded in libdispatch])],,
[case $target_os in
linux*)
enable_embedded_blocks_runtime=yes
;;
*)
enable_embedded_blocks_runtime=no
esac]
)

#
# Detect compiler support for Blocks; perhaps someday -fblocks won't be
# required, in which case we'll need to change this.
Expand All @@ -29,30 +44,32 @@ AC_CACHE_CHECK([for C Blocks support], [dispatch_cv_cblocks], [
AS_IF([test "x$dispatch_cv_cblocks" != "xno"], [
CBLOCKS_FLAGS="$dispatch_cv_cblocks"

#
# It may be necessary to directly link the Blocks runtime on some
# systems, so give it a try if we can't link a C program that uses
# Blocks. We will want to remove this at somepoint, as really -fblocks
# should force that linkage already.
#
saveCFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -fblocks -O0"
AC_MSG_CHECKING([whether additional libraries are required for the Blocks runtime])
AC_TRY_LINK([], [
^{ int j; j=0; }();
], [
AC_MSG_RESULT([no]);
], [
saveLIBS="$LIBS"
LIBS="$LIBS -lBlocksRuntime"
AC_TRY_LINK([], [
^{ int k; k=0; }();
], [
AC_MSG_RESULT([-lBlocksRuntime])
], [
AC_MSG_ERROR([can't find Blocks runtime])
])
])
AS_IF([test "x$enable_embedded_blocks_runtime" != "xyes"], [
#
# It may be necessary to directly link the Blocks runtime on some
# systems, so give it a try if we can't link a C program that uses
# Blocks. We will want to remove this at somepoint, as really -fblocks
# should force that linkage already.
#
saveCFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -fblocks -O0"
AC_MSG_CHECKING([whether additional libraries are required for the Blocks runtime])
AC_TRY_LINK([], [
^{ int j; j=0; }();
], [
AC_MSG_RESULT([no]);
], [
saveLIBS="$LIBS"
LIBS="$LIBS -lBlocksRuntime"
AC_TRY_LINK([], [
^{ int k; k=0; }();
], [
AC_MSG_RESULT([-lBlocksRuntime])
], [
AC_MSG_ERROR([can't find Blocks runtime])
])
])
])
CFLAGS="$saveCFLAGS"
have_cblocks=true
], [
Expand All @@ -61,6 +78,7 @@ AS_IF([test "x$dispatch_cv_cblocks" != "xno"], [
])
AM_CONDITIONAL(HAVE_CBLOCKS, $have_cblocks)
AC_SUBST([CBLOCKS_FLAGS])
AM_CONDITIONAL([BUILD_OWN_BLOCKS_RUNTIME], [test "x$enable_embedded_blocks_runtime" = "xyes"])

#
# Because a different C++ compiler may be specified than C compiler, we have
Expand All @@ -82,24 +100,26 @@ AC_CACHE_CHECK([for C++ Blocks support], [dispatch_cv_cxxblocks], [
AS_IF([test "x$dispatch_cv_cxxblocks" != "xno"], [
CXXBLOCKS_FLAGS="$dispatch_cv_cxxblocks"

saveCXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS -fblocks -O0"
AC_MSG_CHECKING([whether additional libraries are required for the Blocks runtime])
AC_TRY_LINK([], [
^{ int j; j=0; }();
], [
AC_MSG_RESULT([no]);
], [
saveLIBS="$LIBS"
LIBS="$LIBS -lBlocksRuntime"
AC_TRY_LINK([], [
^{ int k; k=0; }();
], [
AC_MSG_RESULT([-lBlocksRuntime])
], [
AC_MSG_ERROR([can't find Blocks runtime])
])
])
AS_IF([test "x$enable_embedded_blocks_runtime" != "xyes"], [
saveCXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS -fblocks -O0"
AC_MSG_CHECKING([whether additional libraries are required for the Blocks runtime])
AC_TRY_LINK([], [
^{ int j; j=0; }();
], [
AC_MSG_RESULT([no]);
], [
saveLIBS="$LIBS"
LIBS="$LIBS -lBlocksRuntime"
AC_TRY_LINK([], [
^{ int k; k=0; }();
], [
AC_MSG_RESULT([-lBlocksRuntime])
], [
AC_MSG_ERROR([can't find Blocks runtime])
])
])
])
CXXFLAGS="$saveCXXFLAGS"
have_cxxblocks=true
], [
Expand Down
54 changes: 54 additions & 0 deletions src/BlocksRuntime/Block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//


#ifndef _Block_H_
#define _Block_H_

#if !defined(BLOCK_EXPORT)
# if defined(__cplusplus)
# define BLOCK_EXPORT extern "C" __attribute__((visibility("default")))
# else
# define BLOCK_EXPORT extern __attribute__((visibility("default")))
# endif
#endif

#if __cplusplus
extern "C" {
#endif

// Create a heap based copy of a Block or simply add a reference to an existing one.
// This must be paired with Block_release to recover memory, even when running
// under Objective-C Garbage Collection.
BLOCK_EXPORT void *_Block_copy(const void *aBlock);

// Lose the reference, and if heap based and last reference, recover the memory
BLOCK_EXPORT void _Block_release(const void *aBlock);

// Used by the compiler. Do not call this function yourself.
BLOCK_EXPORT void _Block_object_assign(void *, const void *, const int);

// Used by the compiler. Do not call this function yourself.
BLOCK_EXPORT void _Block_object_dispose(const void *, const int);

// Used by the compiler. Do not use these variables yourself.
BLOCK_EXPORT void * _NSConcreteGlobalBlock[32];
BLOCK_EXPORT void * _NSConcreteStackBlock[32];

#if __cplusplus
}
#endif

// Type correct macros

#define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__)))
#define Block_release(...) _Block_release((const void *)(__VA_ARGS__))


#endif
Loading