From 269dc1f70360c3382e9f38f05d7337a81ce13b07 Mon Sep 17 00:00:00 2001
From: Holger Freyther <ich@tamarin.(none)>
Date: Tue, 19 May 2009 07:44:55 +0200
Subject: [PATCH] [lib] Create a shared library, install it, header files and pkg-config file

Create a shared library using libtool (I'm deeply sorry for this) and
only export certain symbols. For testing purpose export the timer functions
from the previous commit. We will use -fvisibility=hidden by default
and export API using the BSC_API construct. This way we are in full
control over what we want to export.
---
 Makefile.am                      |    3 +++
 configure.in                     |   18 +++++++++++++++++-
 include/openbsc/Makefile.am      |    5 ++++-
 include/openbsc/openbscdefines.h |   35 +++++++++++++++++++++++++++++++++++
 include/openbsc/timer.h          |    9 +++++----
 openbsc.pc.in                    |   11 +++++++++++
 src/Makefile.am                  |   18 +++++++++++++-----
 tests/db/Makefile.am             |    2 +-
 tests/gsm0408/Makefile.am        |    2 +-
 tests/sms/Makefile.am            |    2 +-
 10 files changed, 91 insertions(+), 14 deletions(-)
 create mode 100644 include/openbsc/openbscdefines.h
 create mode 100644 openbsc.pc.in

diff --git a/Makefile.am b/Makefile.am
index 1ba0604..deaba0f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,5 +3,8 @@ AUTOMAKE_OPTIONS = foreign dist-bzip2 1.6
 INCLUDES = $(all_includes) -I$(top_srcdir)/include
 SUBDIRS = include src tests
 
+pkgconfigdir = $(libdir)/pkgconfig
+pkgconfig_DATA = openbsc.pc
+
 #dist-hook:
 #	rm -rf `find $(distdir) -name .svn`
diff --git a/configure.in b/configure.in
index 9b38d41..6099f56 100644
--- a/configure.in
+++ b/configure.in
@@ -7,7 +7,7 @@ dnl checks for programs
 AC_PROG_MAKE_SET
 AC_PROG_CC
 AC_PROG_INSTALL
-AC_PROG_RANLIB
+AC_PROG_LIBTOOL
 
 dnl checks for libraries
 
@@ -16,7 +16,23 @@ AC_HEADER_STDC
 
 dnl Checks for typedefs, structures and compiler characteristics
 
+# The following test is taken from WebKit's webkit.m4
+saved_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS -fvisibility=hidden "
+AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])
+AC_COMPILE_IFELSE([char foo;],
+      [ AC_MSG_RESULT([yes])
+        SYMBOL_VISIBILITY="-fvisibility=hidden"],
+        AC_MSG_RESULT([no]))
+CFLAGS="$saved_CFLAGS"
+AC_SUBST(SYMBOL_VISIBILITY)
+
+
+dnl Generate the output
+AM_CONFIG_HEADER(bscconfig.h)
+
 AC_OUTPUT(
+    openbsc.pc
     include/openbsc/Makefile
     include/vty/Makefile
     include/Makefile
diff --git a/include/openbsc/Makefile.am b/include/openbsc/Makefile.am
index 27adb25..d0b1509 100644
--- a/include/openbsc/Makefile.am
+++ b/include/openbsc/Makefile.am
@@ -1,5 +1,8 @@
 noinst_HEADERS = abis_nm.h abis_rsl.h debug.h db.h gsm_04_08.h gsm_data.h \
 		 gsm_subscriber.h linuxlist.h msgb.h select.h tlv.h gsm_04_11.h \
-		 timer.h misdn.h chan_alloc.h telnet_interface.h paging.h \
+		 misdn.h chan_alloc.h telnet_interface.h paging.h \
 		 subchan_demux.h trau_frame.h e1_input.h trau_mux.h signal.h \
 		 gsm_utils.h ipaccess.h rs232.h
+
+openbsc_includedir=$(includedir)/openbsc
+openbsc_include_HEADERS = openbscdefines.h timer.h
diff --git a/include/openbsc/openbscdefines.h b/include/openbsc/openbscdefines.h
new file mode 100644
index 0000000..082e592
--- /dev/null
+++ b/include/openbsc/openbscdefines.h
@@ -0,0 +1,35 @@
+/* 
+ * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef OPENBSCDEFINES_H
+#define OPENBSCDEFINES_H
+
+#ifdef BUILDING_ON_WINDOWS
+    #ifdef BUILDING_OPENBSC
+        #define BSC_API __declspec(dllexport)
+    #else
+        #define BSC_API __declspec(dllimport)
+    #endif
+#else
+    #define BSC_API __attribute__((visibility("default")))
+#endif
+
+#endif
diff --git a/include/openbsc/timer.h b/include/openbsc/timer.h
index cc32f71..3148a4c 100644
--- a/include/openbsc/timer.h
+++ b/include/openbsc/timer.h
@@ -24,6 +24,7 @@
 #include <sys/time.h>
 
 #include "linuxlist.h"
+#include "openbscdefines.h"
 
 /**
  * Timer management:
@@ -55,10 +56,10 @@ struct timer_list {
 /**
  * timer management
  */
-void bsc_add_timer(struct timer_list *timer);
-void bsc_schedule_timer(struct timer_list *timer, int seconds, int microseconds);
-void bsc_del_timer(struct timer_list *timer);
-int bsc_timer_pending(struct timer_list *timer);
+BSC_API void bsc_add_timer(struct timer_list *timer);
+BSC_API void bsc_schedule_timer(struct timer_list *timer, int seconds, int microseconds);
+BSC_API void bsc_del_timer(struct timer_list *timer);
+BSC_API int bsc_timer_pending(struct timer_list *timer);
 
 
 /**
diff --git a/openbsc.pc.in b/openbsc.pc.in
new file mode 100644
index 0000000..5feee49
--- /dev/null
+++ b/openbsc.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: OpenBSC
+Description: OpenBaseStationController project
+Requires:
+Version: @VERSION@
+Libs: -L${libdir} -lopenbsc
+Cflags: -I${includedir}
diff --git a/src/Makefile.am b/src/Makefile.am
index bda3fd1..b086be0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,19 +2,27 @@ INCLUDES = $(all_includes) -I$(top_srcdir)/include
 AM_CFLAGS=-Wall
 
 sbin_PROGRAMS = bsc_hack bs11_config ipaccess-find ipaccess-config
-noinst_LIBRARIES = libbsc.a libvty.a
+noinst_LTLIBRARIES = libbsc.la libvty.la
 noinst_HEADERS = vty/cardshell.h
 
-libbsc_a_SOURCES = abis_rsl.c abis_nm.c gsm_04_08.c gsm_data.c \
+lib_LTLIBRARIES = libopenbsc.la
+
+libbsc_la_CFLAGS = $(SYMBOL_VISIBILITY)
+libbsc_la_CPPFLAGS = -DBUILDING_OPENBSC=1
+libbsc_la_SOURCES = abis_rsl.c abis_nm.c gsm_04_08.c gsm_data.c \
 		gsm_subscriber.c msgb.c select.c chan_alloc.c timer.c debug.c db.c \
 		gsm_04_11.c telnet_interface.c subchan_demux.c \
 		trau_frame.c trau_mux.c paging.c e1_config.c e1_input.c tlv_parser.c \
 		input/misdn.c input/ipaccess.c signal.c gsm_utils.c
 
-libvty_a_SOURCES = vty/buffer.c vty/command.c vty/vector.c vty/vty.c
+libvty_la_CFLAGS = $(SYMBOL_VISIBILITY)
+libvty_la_SOURCES = vty/buffer.c vty/command.c vty/vector.c vty/vty.c
 
 bsc_hack_SOURCES = bsc_hack.c vty_interface.c
-bsc_hack_LDADD = libbsc.a libvty.a -ldl -ldbi -lcrypt
+bsc_hack_LDADD = libbsc.la libvty.la -ldl -ldbi -lcrypt
+
+libopenbsc_la_SOURCES =
+libopenbsc_la_LIBADD = libbsc.la libvty.la
 
 bs11_config_SOURCES = bs11_config.c abis_nm.c gsm_data.c msgb.c debug.c \
 		      select.c timer.c rs232.c tlv_parser.c signal.c
@@ -22,4 +30,4 @@ bs11_config_SOURCES = bs11_config.c abis_nm.c gsm_data.c msgb.c debug.c \
 ipaccess_find_SOURCES = ipaccess-find.c select.c timer.c
 
 ipaccess_config_SOURCES = ipaccess-config.c
-ipaccess_config_LDADD = libbsc.a libvty.a -ldl -ldbi -lcrypt
+ipaccess_config_LDADD = libbsc.la libvty.la -ldl -ldbi -lcrypt
diff --git a/tests/db/Makefile.am b/tests/db/Makefile.am
index 3d9722c..a993d6c 100644
--- a/tests/db/Makefile.am
+++ b/tests/db/Makefile.am
@@ -4,5 +4,5 @@ AM_CFLAGS=-Wall -ggdb3
 noinst_PROGRAMS = db_test
 
 db_test_SOURCES = db_test.c
-db_test_LDADD = $(top_builddir)/src/libbsc.a -ldl -ldbi
+db_test_LDADD = $(top_builddir)/src/libbsc.la -ldl -ldbi
 
diff --git a/tests/gsm0408/Makefile.am b/tests/gsm0408/Makefile.am
index 51463dc..f9c700a 100644
--- a/tests/gsm0408/Makefile.am
+++ b/tests/gsm0408/Makefile.am
@@ -2,4 +2,4 @@ INCLUDES = $(all_includes) -I$(top_srcdir)/include
 noinst_PROGRAMS = gsm0408_test
 
 gsm0408_test_SOURCES = gsm0408_test.c
-gsm0408_test_LDADD = $(top_builddir)/src/libbsc.a -ldbi
+gsm0408_test_LDADD = $(top_builddir)/src/libbsc.la -ldbi
diff --git a/tests/sms/Makefile.am b/tests/sms/Makefile.am
index 23df871..b463198 100644
--- a/tests/sms/Makefile.am
+++ b/tests/sms/Makefile.am
@@ -2,4 +2,4 @@ INCLUDES = $(all_includes) -I$(top_srcdir)/include
 noinst_PROGRAMS = sms_test
 
 sms_test_SOURCES = sms_test.c
-sms_test_LDADD = $(top_builddir)/src/libbsc.a -ldl -ldbi
+sms_test_LDADD = $(top_builddir)/src/libbsc.la -ldl -ldbi
-- 
1.6.0.4

