how to build v8 static library

You can generate shared v8 library by following the wiki Building With GYP, but how about static library?

Note: use of static library is discouraged, because it wastes disk space and memory, and is hard to upgrade. Continue reading if you really want this.

In fact, the “.a” files of the default build is not actually ar static library, it’s “thin archive” format which contains reference to other object files  instread of packing object files together in one file. That’s to say, if you copy only the .a files to other people, they will not be able to compile because the referenced object files are missing.

In order to get real static libraries that are easy to distribute, we need to change two gyp files, add the “standalone_static_library” = 1 option,  (the changes made here are based on v8-3.24.5 revision: 18377).

Changes to tools/gyp/v8.gyp:

--- tools/gyp/v8.gyp    (revision 18377)
+++ tools/gyp/v8.gyp    (working copy)
@@ -107,6 +107,7 @@
     {
       'target_name': 'v8_snapshot',
       'type': 'static_library',
+      'standalone_static_library': 1,
       'conditions': [
         ['want_separate_host_toolset==1', {
           'toolsets': ['host', 'target'],
@@ -179,6 +180,7 @@
     {
       'target_name': 'v8_nosnapshot.<(v8_target_arch)',
       'type': 'static_library',
+      'standalone_static_library': 1,
       'dependencies': [
         'v8_base.<(v8_target_arch)',
       ],
@@ -236,6 +238,7 @@
     {
       'target_name': 'v8_base.<(v8_target_arch)',
       'type': 'static_library',
+      'standalone_static_library': 1,
       'variables': {
         'optimize': 'max',
       },

Changes made to third_party/icu/icu.gyp

--- icu.gyp     (revision 239289)
+++ icu.gyp     (working copy)
@@ -53,6 +53,7 @@
         {
           'target_name': 'icudata',
           'type': 'static_library',
+          'standalone_static_library': 1,
           'defines': [
             'U_HIDE_DATA_SYMBOL',
           ],
@@ -123,6 +124,11 @@
         {
           'target_name': 'icui18n',
           'type': '<(component)',
+          'conditions': [
+            [ 'componen!="shared_library"', {
+                'standalone_static_library': 1,
+            }],
+          ],
           'sources': [
             'source/i18n/anytrans.cpp',
             'source/i18n/astro.cpp',
@@ -366,6 +372,11 @@
         {
           'target_name': 'icuuc',
           'type': '<(component)',
+          'conditions': [
+            [ 'component!="shared_library"', {
+                'standalone_static_library': 1,
+            }],
+          ],
           'sources': [
             'source/common/bmpset.cpp',
             'source/common/brkeng.cpp',

make clean and make again, you will get the real static library, the top two large files are:
libv8_base.x64.a(name may differ depends on your build) of 15MB size and libicudata.a of 9M size.

Note: earlier version of gyp didn’t have support for building standalone_static_library, you may need to update it to the latest version first:

cd build/gyp
svn up
cd -
This entry was posted in Programming and tagged , , . Bookmark the permalink.

One Response to how to build v8 static library

  1. Boris van Schooten says:

    Thanks a million, this was a life saver for me! This should be made into a patch for V8. Is that possible, or does this patch negatively affect other functionality?

    Sometimes you just need a static library, such as when you are building an android native-activity without being dependent on the whole v8 build tree. Due to a system limitation/bug, a native activity cannot load local shared libraries (see http://stackoverflow.com/questions/9111107/how-to-use-a-shared-library-in-native-activity).

Leave a Reply