]>
Commit | Line | Data |
---|---|---|
1 | .. _static-linking: | |
2 | ||
3 | Static Linking | |
4 | ============== | |
5 | ||
6 | This document describes how to build FRR without hard dependencies on shared | |
7 | libraries. Note that it's not possible to build FRR *completely* statically. | |
8 | This document just covers how to statically link the dependencies that aren't | |
9 | likely to be present on a given platform - libfrr and libyang. The resultant | |
10 | binaries should still be fairly portable. For example, here is the DSO | |
11 | dependency list for `bgpd` after using these steps: | |
12 | ||
13 | .. code-block:: shell | |
14 | ||
15 | $ ldd bgpd | |
16 | linux-vdso.so.1 (0x00007ffe3a989000) | |
17 | libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f9dc10c0000) | |
18 | libcap.so.2 => /lib/x86_64-linux-gnu/libcap.so.2 (0x00007f9dc0eba000) | |
19 | libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9dc0b1c000) | |
20 | libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9dc0918000) | |
21 | libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f9dc06e0000) | |
22 | libjson-c.so.3 => /lib/x86_64-linux-gnu/libjson-c.so.3 (0x00007f9dc04d5000) | |
23 | librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f9dc02cd000) | |
24 | libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9dc00ae000) | |
25 | libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f9dbfe96000) | |
26 | libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9dbfaa5000) | |
27 | /lib64/ld-linux-x86-64.so.2 (0x00007f9dc1449000) | |
28 | ||
29 | Procedure | |
30 | --------- | |
31 | Note that these steps have only been tested with LLVM 9 / clang. | |
32 | ||
33 | Today, libfrr can already be statically linked by passing these configure | |
34 | options:: | |
35 | ||
36 | --enable-static --enable-static-bin --enable-shared | |
37 | ||
38 | libyang is more complicated. You must build and install libyang as a static | |
39 | library. To do this, follow the usual libyang build procedure as listed in the | |
40 | FRR developer docs, but set the ``ENABLE_STATIC`` option in your cmake | |
41 | invocation. You also need to build with PIC enabled, which today is disabled | |
42 | when building libyang statically. | |
43 | ||
44 | The resultant cmake command is:: | |
45 | ||
46 | cmake -DENABLE_STATIC=ON -DENABLE_LYD_PRIV=ON \ | |
47 | -DCMAKE_INSTALL_PREFIX:PATH=/usr \ | |
48 | -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE \ | |
49 | -DCMAKE_BUILD_TYPE:String="Release" .. | |
50 | ||
51 | This produces a bunch of ``.a`` static archives that need to ultimately be linked | |
52 | into FRR. However, not only is it 6 archives rather than the usual ``libyang.so``, | |
53 | you will now also need to link FRR with ``libpcre.a``. Ubuntu's ``libpcre3-dev`` | |
54 | package provides this, but it hasn't been built with PIC enabled, so it's not | |
55 | usable for our purposes. So download ``libpcre`` from | |
56 | `SourceForge <https://sourceforge.net/projects/pcre/>`_, and build it | |
57 | like this: | |
58 | ||
59 | .. code-block:: shell | |
60 | ||
61 | ./configure --with-pic | |
62 | make | |
63 | ||
64 | Hopefully you get a nice, usable, PIC ``libpcre.a``. | |
65 | ||
66 | So now we have to link all these static libraries into FRR. Rather than modify | |
67 | FRR to accomodate this, the best option is to create an archive with all of | |
68 | libyang's dependencies. Then to avoid making any changes to FRR build foo, | |
69 | rename this ``libyang.a`` and copy it over the usual static library location. | |
70 | Ugly but it works. To do this, go into your libyang build directory, which | |
71 | should have a bunch of ``.a`` files. Copy ``libpcre.a`` into this directory. | |
72 | Write the following into a shell script and run it: | |
73 | ||
74 | .. code-block:: shell | |
75 | ||
76 | #!/bin/bash | |
77 | ar -M <<EOM | |
78 | CREATE libyang_fat.a | |
79 | ADDLIB libyang.a | |
80 | ADDLIB libyangdata.a | |
81 | ADDLIB libmetadata.a | |
82 | ADDLIB libnacm.a | |
83 | ADDLIB libuser_inet_types.a | |
84 | ADDLIB libuser_yang_types.a | |
85 | ADDLIB libpcre.a | |
86 | SAVE | |
87 | END | |
88 | EOM | |
89 | ranlib libyang_fat.a | |
90 | ||
91 | ``libyang_fat.a`` is your archive. Now copy this over your install | |
92 | ``libyang.a``, which on my machine is located at | |
93 | ``/usr/lib/x86_64-linux-gnu/libyang.a`` (try ``locate libyang.a`` if not). | |
94 | ||
95 | Now when you build FRR with the static options enabled as above, clang should | |
96 | pick up the static libyang and link it, leaving you with FRR binaries that have | |
97 | no hard DSO dependencies beyond common system libraries. To verify, run ``ldd`` | |
98 | over the resultant binaries. |